Class: Secp256k1::PublicKey
- Defined in:
- lib/secp256k1/key.rb
Constant Summary
Constants included from ECDSA
ECDSA::SIZE_COMPACT, ECDSA::SIZE_SERIALIZED
Instance Attribute Summary collapse
-
#public_key ⇒ Object
Returns the value of attribute public_key.
Instance Method Summary collapse
-
#combine(pubkeys) ⇒ Object
Add a number of public keys together.
- #deserialize(pubkey_ser) ⇒ Object
- #ecdh(scalar) ⇒ Object
- #ecdsa_verify(msg, raw_sig, raw: false, digest: Digest::SHA256) ⇒ Object
-
#initialize(pubkey: nil, raw: false, flags: FLAG_VERIFY, ctx: nil) ⇒ PublicKey
constructor
A new instance of PublicKey.
- #serialize(compressed: true) ⇒ Object
-
#tweak_add(scalar) ⇒ Object
Tweak the current public key by adding a 32 byte scalar times the generator to it and return a new PublicKey instance.
-
#tweak_mul(scalar) ⇒ Object
Tweak the current public key by multiplying it by a 32 byte scalar and return a new PublicKey instance.
Methods included from ECDSA
#ecdsa_deserialize, #ecdsa_deserialize_compact, #ecdsa_recover, #ecdsa_recoverable_convert, #ecdsa_recoverable_deserialize, #ecdsa_recoverable_serialize, #ecdsa_serialize, #ecdsa_serialize_compact, #ecdsa_signature_normalize
Methods included from Utils
#decode_hex, #encode_hex, #hash32
Constructor Details
#initialize(pubkey: nil, raw: false, flags: FLAG_VERIFY, ctx: nil) ⇒ PublicKey
Returns a new instance of PublicKey.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/secp256k1/key.rb', line 27 def initialize(pubkey: nil, raw: false, flags: FLAG_VERIFY, ctx: nil) super(ctx, flags) if pubkey if raw raise ArgumentError, 'raw pubkey must be bytes' unless pubkey.instance_of?(String) @public_key = deserialize pubkey else #raise ArgumentError, 'pubkey must be an internal object' unless pubkey.instance_of?(String) @public_key = pubkey end else @public_key = nil end end |
Instance Attribute Details
#public_key ⇒ Object
Returns the value of attribute public_key.
25 26 27 |
# File 'lib/secp256k1/key.rb', line 25 def public_key @public_key end |
Instance Method Details
#combine(pubkeys) ⇒ Object
Add a number of public keys together.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/secp256k1/key.rb', line 72 def combine(pubkeys) raise ArgumentError, 'must give at least 1 pubkey' if pubkeys.empty? outpub = FFI::Pubkey.new.pointer #pubkeys.each {|item| } res = C.secp256k1_ec_pubkey_combine(@ctx, outpub, pubkeys, pubkeys.size) raise AssertError, 'failed to combine public keys' unless res == 1 @public_key = outpub outpub end |
#deserialize(pubkey_ser) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/secp256k1/key.rb', line 57 def deserialize(pubkey_ser) raise ArgumentError, 'unknown public key size (expected 33 or 65)' unless [33,65].include?(pubkey_ser.size) pubkey = C::Pubkey.new.pointer res = C.secp256k1_ec_pubkey_parse(@ctx, pubkey, pubkey_ser, pubkey_ser.size) raise AssertError, 'invalid public key' unless res == 1 @public_key = pubkey pubkey end |
#ecdh(scalar) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/secp256k1/key.rb', line 110 def ecdh(scalar) raise AssertError, 'No public key defined' unless @public_key raise ArgumentError, 'scalar must be composed of 32 bytes' unless scalar.instance_of?(String) && scalar.size == 32 result = FFI::MemoryPointer.new :char, 32 res = C.secp256k1_ecdh @ctx, result, @public_key, scalar raise AssertError, "invalid scalar (#{scalar})" unless res == 1 result.read_bytes(32) end |
#ecdsa_verify(msg, raw_sig, raw: false, digest: Digest::SHA256) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/secp256k1/key.rb', line 101 def ecdsa_verify(msg, raw_sig, raw: false, digest: Digest::SHA256) raise AssertError, 'No public key defined' unless @public_key raise AssertError, 'instance not configured for sig verification' if (@flags & FLAG_VERIFY) != FLAG_VERIFY msg32 = hash32 msg, raw, digest C.secp256k1_ecdsa_verify(@ctx, raw_sig, msg32, @public_key) == 1 end |
#serialize(compressed: true) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/secp256k1/key.rb', line 43 def serialize(compressed: true) raise AssertError, 'No public key defined' unless @public_key len_compressed = compressed ? 33 : 65 res_compressed = FFI::MemoryPointer.new :char, len_compressed outlen = FFI::MemoryPointer.new(:size_t).write_uint(len_compressed) compflag = compressed ? EC_COMPRESSED : EC_UNCOMPRESSED res = C.secp256k1_ec_pubkey_serialize(@ctx, res_compressed, outlen, @public_key, compflag) raise AssertError, 'pubkey serialization failed' unless res == 1 res_compressed.read_bytes(len_compressed) end |
#tweak_add(scalar) ⇒ Object
Tweak the current public key by adding a 32 byte scalar times the generator to it and return a new PublicKey instance.
89 90 91 |
# File 'lib/secp256k1/key.rb', line 89 def tweak_add(scalar) tweak_public :secp256k1_ec_pubkey_tweak_add, scalar end |
#tweak_mul(scalar) ⇒ Object
Tweak the current public key by multiplying it by a 32 byte scalar and return a new PublicKey instance.
97 98 99 |
# File 'lib/secp256k1/key.rb', line 97 def tweak_mul(scalar) tweak_public :secp256k1_ec_pubkey_tweak_mul, scalar end |