Class: Secp256k1::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
ext/rbsecp256k1/rbsecp256k1.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_data(in_public_key_data) ⇒ Secp256k1::PublicKey

Loads a public key from compressed or uncompressed binary data.

Parameters:

  • in_public_key_data (String)

    binary string with compressed or uncompressed public key data.

Returns:

Raises:



480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 480

static VALUE
PublicKey_from_data(VALUE klass, VALUE in_public_key_data)
{
  unsigned char *public_key_data;

  Check_Type(in_public_key_data, T_STRING);

  public_key_data = (unsigned char*)StringValuePtr(in_public_key_data);
  return PublicKey_create_from_data(
    public_key_data,
    (int)RSTRING_LEN(in_public_key_data)
  );
}

Instance Method Details

#==(other) ⇒ Boolean

Compares two public keys.

Public keys are considered equal if their compressed representations match.

Parameters:

Returns:

  • (Boolean)

    true if the public keys are identical, false otherwise.



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 548

static VALUE
PublicKey_equals(VALUE self, VALUE other)
{
  PublicKey *lhs;
  PublicKey *rhs;
  unsigned char lhs_compressed[33];
  unsigned char rhs_compressed[33];
  size_t lhs_len;
  size_t rhs_len;

  lhs_len = 33;
  rhs_len = 33;

  TypedData_Get_Struct(self, PublicKey, &PublicKey_DataType, lhs);
  TypedData_Get_Struct(other, PublicKey, &PublicKey_DataType, rhs);

  secp256k1_ec_pubkey_serialize(
    secp256k1_context_no_precomp,
    lhs_compressed,
    &lhs_len,
    &(lhs->pubkey),
    SECP256K1_EC_COMPRESSED
  );
  secp256k1_ec_pubkey_serialize(
    secp256k1_context_no_precomp,
    rhs_compressed,
    &rhs_len,
    &(rhs->pubkey),
    SECP256K1_EC_COMPRESSED
  );

  if (lhs_len == rhs_len &&
      memcmp(lhs_compressed, rhs_compressed, lhs_len) == 0)
  {
    return Qtrue;
  }

  return Qfalse;
}

#compressedString

Returns binary string containing the compressed representation of this public key.

Returns:

  • (String)

    binary string containing the compressed representation of this public key.



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 521

static VALUE
PublicKey_compressed(VALUE self)
{
  // TODO: Cache value after first computation
  PublicKey *public_key;
  size_t serialized_pubkey_len = COMPRESSED_PUBKEY_SIZE_BYTES;
  unsigned char serialized_pubkey[COMPRESSED_PUBKEY_SIZE_BYTES];

  TypedData_Get_Struct(self, PublicKey, &PublicKey_DataType, public_key);

  secp256k1_ec_pubkey_serialize(secp256k1_context_no_precomp,
                                serialized_pubkey,
                                &serialized_pubkey_len,
                                &(public_key->pubkey),
                                SECP256K1_EC_COMPRESSED);

  return rb_str_new((char*)serialized_pubkey, serialized_pubkey_len);
}

#uncompressedString

Returns binary string containing the uncompressed representation of this public key.

Returns:

  • (String)

    binary string containing the uncompressed representation of this public key.



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 498

static VALUE
PublicKey_uncompressed(VALUE self)
{
  // TODO: Cache value after first computation
  PublicKey *public_key;
  size_t serialized_pubkey_len = UNCOMPRESSED_PUBKEY_SIZE_BYTES;
  unsigned char serialized_pubkey[UNCOMPRESSED_PUBKEY_SIZE_BYTES];

  TypedData_Get_Struct(self, PublicKey, &PublicKey_DataType, public_key);

  secp256k1_ec_pubkey_serialize(secp256k1_context_no_precomp,
                                serialized_pubkey,
                                &serialized_pubkey_len,
                                &(public_key->pubkey),
                                SECP256K1_EC_UNCOMPRESSED);

  return rb_str_new((char*)serialized_pubkey, serialized_pubkey_len);
}