Class: Secp256k1Zkp::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/secp256k1zkp/context.rb,
ext/secp256k1zkp/secp256k1zkp.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags) ⇒ Context

Secp256k1Zkp::Context.new(flags)

创建Secp256k1上下文对象。

Parameters:

  • flags (Fixnum)

    上下文标记。



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'ext/secp256k1zkp/secp256k1zkp.c', line 165

static VALUE dm_context_initialize(VALUE self, VALUE flags)
{
  rb_struct_context *context;
  int i_flags;

  Check_Type(flags, T_FIXNUM);
  i_flags = FIX2INT(flags);

  TypedData_Get_Struct(self, rb_struct_context, &rb_type_context, context);

  context->ctx = secp256k1_context_create(i_flags);
  context->flags = i_flags;

  return self;
}

Class Method Details

.defaultObject



9
10
11
# File 'lib/secp256k1zkp/context.rb', line 9

def self.default
  return new(SECP256K1_CONTEXT_ALL)
end

Instance Method Details

#cloneContext

Secp256k1Zkp::Context#clone #dup

克隆Secp256k1上下文对象。

Returns:



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'ext/secp256k1zkp/secp256k1zkp.c', line 232

static VALUE dm_context_clone(VALUE self)
{
  rb_struct_context *context;
  rb_struct_context *context_new;
  VALUE new_instance;

  TypedData_Get_Struct(self, rb_struct_context, &rb_type_context, context);

  new_instance = dm_context_alloc(rb_cSecp256k1Context);
  TypedData_Get_Struct(new_instance, rb_struct_context, &rb_type_context, context_new);
  context_new->ctx = secp256k1_context_clone(context->ctx);
  context_new->flags = context->flags;

  return new_instance;
}

#dupContext

Secp256k1Zkp::Context#clone #dup

克隆Secp256k1上下文对象。

Returns:



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'ext/secp256k1zkp/secp256k1zkp.c', line 232

static VALUE dm_context_clone(VALUE self)
{
  rb_struct_context *context;
  rb_struct_context *context_new;
  VALUE new_instance;

  TypedData_Get_Struct(self, rb_struct_context, &rb_type_context, context);

  new_instance = dm_context_alloc(rb_cSecp256k1Context);
  TypedData_Get_Struct(new_instance, rb_struct_context, &rb_type_context, context_new);
  context_new->ctx = secp256k1_context_clone(context->ctx);
  context_new->flags = context->flags;

  return new_instance;
}

#is_valid_private_keydata?(private_keydata) ⇒ Boolean

Secp256k1Zkp::Context#is_valid_private_keydata?(private_keydata)

是否是有效的私钥判断。

Parameters:

  • private_keydata (String)

    私钥字符串。

Returns:

  • (Boolean)


210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'ext/secp256k1zkp/secp256k1zkp.c', line 210

static VALUE dm_context_verify_private_keydata(VALUE self, VALUE private_keydata)
{
  rb_struct_context *context;
  TypedData_Get_Struct(self, rb_struct_context, &rb_type_context, context);

  if (RB_TYPE_P(private_keydata, T_STRING) &&
      RSTRING_LEN(private_keydata) == kByteSizePrivateKeyData &&
      secp256k1_ec_seckey_verify(context->ctx, (const unsigned char *)RSTRING_PTR(private_keydata)))
  {
    return Qtrue;
  }

  return Qfalse;
}

#is_valid_public_keydata?(public_keydata) ⇒ Boolean

Secp256k1Zkp::Context#is_valid_public_keydata?(public_keydata)

是否是有效的公钥判断。

Parameters:

  • public_keydata (String)

    压缩公钥字符串。

Returns:

  • (Boolean)


189
190
191
192
193
194
195
196
197
198
199
200
# File 'ext/secp256k1zkp/secp256k1zkp.c', line 189

static VALUE dm_context_verify_public_keydata(VALUE self, VALUE public_keydata)
{
  rb_struct_context *context;
  TypedData_Get_Struct(self, rb_struct_context, &rb_type_context, context);

  if (RB_TYPE_P(public_keydata, T_STRING) && secp256k1_ec_pubkey_verify(context->ctx, (const unsigned char *)RSTRING_PTR(public_keydata), (int)RSTRING_LEN(public_keydata)))
  {
    return Qtrue;
  }

  return Qfalse;
}

#pedersen_blind_sum(blinds_in, non_neg) ⇒ String

Secp256k1Zkp::Context#pedersen_blind_sum(blinds_in, non_neg)

盲化因子求和。

Parameters:

  • blinds_in (Array)

    盲化因子数组。

  • non_neg (Integer)

    正因子数量。

Returns:

  • (String)


384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'ext/secp256k1zkp/secp256k1zkp.c', line 384

static VALUE dm_context_pedersen_blind_sum(VALUE self, VALUE blinds_in, VALUE non_neg)
{
  rb_struct_context *context;
  long blinds_in_size;
  long i;
  rb_struct_blind_factor_type result = {
      0,
  };

  Check_Type(blinds_in, T_ARRAY);

  TypedData_Get_Struct(self, rb_struct_context, &rb_type_context, context);

  blinds_in_size = RARRAY_LEN(blinds_in);
  if (blinds_in_size <= 0)
  {
    return Qnil;
  }
  const unsigned char *blinds[blinds_in_size];
  for (i = 0; i < blinds_in_size; ++i)
  {
    VALUE blind_factor = rb_ary_entry(blinds_in, i);
    SafeStringValue(blind_factor);
    blinds[i] = (const unsigned char *)RSTRING_PTR(blind_factor);
  }

  if (!secp256k1_pedersen_blind_sum(context->ctx, result.data, blinds, (int)blinds_in_size, (uint32_t)NUM2UINT(non_neg)))
  {
    return Qnil;
  }

  return rb_str_new((const char *)&result.data[0], sizeof(result.data));
}

#pedersen_commit(blind_factor, value) ⇒ String

Secp256k1Zkp::Context#pedersen_commit(blind_factor, value)

生成佩德森承诺。

Parameters:

  • blind_factor (String)

    盲化因子。

  • value (Integer)

    承诺的数值。

Returns:

  • (String)


342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'ext/secp256k1zkp/secp256k1zkp.c', line 342

static VALUE dm_context_pedersen_commit(VALUE self, VALUE blind_factor, VALUE value)
{
  rb_struct_context *context;
  rb_struct_commitment_type commitment = {
      0,
  };

  SafeStringValue(blind_factor);
  if (RSTRING_LEN(blind_factor) != kByteSizeBlindFactor)
  {
    rb_raise(rb_eRuntimeError, "invalid blind_factor.");
  }

  TypedData_Get_Struct(self, rb_struct_context, &rb_type_context, context);

  //  check flags
  if (!(context->flags & SECP256K1_CONTEXT_SIGN))
  {
    rb_raise(rb_eRuntimeError, "invalid secp256k1 context, missing `SECP256K1_CONTEXT_SIGN` flag.");
  }
  if (!(context->flags & SECP256K1_CONTEXT_COMMIT))
  {
    rb_raise(rb_eRuntimeError, "invalid secp256k1 context, missing `SECP256K1_CONTEXT_COMMIT` flag.");
  }

  if (!secp256k1_pedersen_commit(context->ctx, commitment.data, (unsigned char *)RSTRING_PTR(blind_factor), (uint64_t)NUM2ULL(value)))
  {
    return Qnil;
  }

  return rb_str_new((const char *)&commitment.data[0], sizeof(commitment.data));
}

#range_proof_sign(min_value, commit, commit_blind, nonce, base10_exp, min_bits, actual_value) ⇒ String

Secp256k1Zkp::Context#range_proof_sign(min_value, commit, commit_blind, nonce, base10_exp, min_bits, actual_value)

生成范围证明。

Parameters:

  • *

    参考 secp256k1_rangeproof_sign 参数。

Returns:

  • (String)


426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'ext/secp256k1zkp/secp256k1zkp.c', line 426

static VALUE dm_context_range_proof_sign(VALUE self, VALUE min_value, VALUE commit, VALUE commit_blind, VALUE nonce, VALUE base10_exp, VALUE min_bits, VALUE actual_value)
{
  rb_struct_context *context;
  unsigned char proof[5134];
  int proof_len;

  SafeStringValue(commit);
  SafeStringValue(commit_blind);
  SafeStringValue(nonce);

  TypedData_Get_Struct(self, rb_struct_context, &rb_type_context, context);

  //  check flags
  if (!(context->flags & SECP256K1_CONTEXT_SIGN))
  {
    rb_raise(rb_eRuntimeError, "invalid secp256k1 context, missing `SECP256K1_CONTEXT_SIGN` flag.");
  }
  if (!(context->flags & SECP256K1_CONTEXT_COMMIT))
  {
    rb_raise(rb_eRuntimeError, "invalid secp256k1 context, missing `SECP256K1_CONTEXT_COMMIT` flag.");
  }
  if (!(context->flags & SECP256K1_CONTEXT_RANGEPROOF))
  {
    rb_raise(rb_eRuntimeError, "invalid secp256k1 context, missing `SECP256K1_CONTEXT_RANGEPROOF` flag.");
  }

  proof_len = sizeof(proof);

  if (!secp256k1_rangeproof_sign(context->ctx, proof, &proof_len, (uint64_t)NUM2ULL(min_value),
                                 (const unsigned char *)RSTRING_PTR(commit), (const unsigned char *)RSTRING_PTR(commit_blind), (const unsigned char *)RSTRING_PTR(nonce),
                                 (int8_t)FIX2INT(base10_exp), (uint8_t)FIX2INT(min_bits), (uint64_t)NUM2ULL(actual_value)))
  {
    return Qnil;
  }

  return rb_str_new((const char *)proof, proof_len);
}

#sign_compact(*args) ⇒ String

Secp256k1Zkp::Context#sign_compact(message_digest, private_key[, require_canonical = true])

生成紧凑格式的 ECDSA 签名(recId + 64字节)。

Parameters:

  • message_digest (String)

    待签名的消息摘要(SHA256摘要、32字节)。

  • private_key (Secp256k1Zkp::PrivateKey)

    私钥。

Returns:

  • (String)


276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'ext/secp256k1zkp/secp256k1zkp.c', line 276

static VALUE dm_context_sign_compact(int argc, VALUE *argv, VALUE self)
{
  VALUE message_digest, private_key, v_require_canonical;
  rb_struct_context *context;
  rb_struct_private_key *sp_private_key;
  const unsigned char *digest32;
  int require_canonical;
  int recid;
  unsigned int counter;
  rb_struct_compact_signature signature = {
      0,
  };

  if (2 == rb_scan_args(argc, argv, "21", &message_digest, &private_key, &v_require_canonical))
  {
    v_require_canonical = Qtrue;
  }

  SafeStringValue(message_digest);
  if (RSTRING_LEN(message_digest) != kByteSizeSha256)
  {
    rb_raise(rb_eRuntimeError, "invalid message digest32.");
  }
  Check_AnyType(private_key, rb_cSecp256k1PrivateKey);

  TypedData_Get_Struct(self, rb_struct_context, &rb_type_context, context);

  //  check flags
  if (!(context->flags & SECP256K1_CONTEXT_SIGN))
  {
    rb_raise(rb_eRuntimeError, "invalid secp256k1 context, missing `SECP256K1_CONTEXT_SIGN` flag.");
  }

  TypedData_Get_Struct(private_key, rb_struct_private_key, &rb_type_private_key, sp_private_key);

  //  初始化部分参数
  digest32 = (const unsigned char *)RSTRING_PTR(message_digest);
  require_canonical = RTEST(v_require_canonical) ? 1 : 0;
  counter = 0;
  
  //  循环计算签名,直到找到合适的 canonical 签名。
  do
  {
    if (!secp256k1_ecdsa_sign_compact(context->ctx, digest32,
                                      &signature.data[1],
                                      sp_private_key->data,
                                      extended_nonce_function,
                                      &counter, &recid))
    {
      return Qnil;
    }
  } while (require_canonical && !is_canonical(&signature));
  signature.data[0] = 27 + 4 + recid;

  return rb_str_new((const char *)&signature.data[0], sizeof(signature.data));
}