Module: Bitcoin::Secp256k1::Native
- Extended by:
- Native
- Includes:
- FFI::Library
- Included in:
- Native
- Defined in:
- lib/bitcoin/secp256k1/native.rb
Overview
binding for secp256k1 (github.com/bitcoin-core/secp256k1/) commit: efad3506a8937162e8010f5839fdf3771dfcf516 this is not included by default, to enable set shared object path to ENV for linux, ENV = ‘/usr/local/lib/libsecp256k1.so’ for mac,
Constant Summary collapse
- SECP256K1_FLAGS_TYPE_MASK =
((1 << 8) - 1)
- SECP256K1_FLAGS_TYPE_CONTEXT =
(1 << 0)
- SECP256K1_FLAGS_TYPE_COMPRESSION =
(1 << 1)
- SECP256K1_FLAGS_BIT_CONTEXT_VERIFY =
(1 << 8)
- SECP256K1_FLAGS_BIT_CONTEXT_SIGN =
(1 << 9)
- SECP256K1_FLAGS_BIT_COMPRESSION =
(1 << 8)
- SECP256K1_CONTEXT_VERIFY =
Flags to pass to secp256k1_context_create.
(SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
- SECP256K1_CONTEXT_SIGN =
(SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
- SECP256K1_EC_COMPRESSED =
Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export.
(SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
- SECP256K1_EC_UNCOMPRESSED =
(SECP256K1_FLAGS_TYPE_COMPRESSION)
Class Method Summary collapse
-
.create_keypair(priv_key) ⇒ String
Create key pair data from private key.
-
.generate_key(compressed: true) ⇒ Object
generate bitcoin key object.
-
.generate_key_pair(compressed: true) ⇒ Object
generate ec private key and public key.
- .generate_pubkey(priv_key, compressed: true) ⇒ Object
- .init ⇒ Object
- .load_functions ⇒ Object
-
.parse_ec_pubkey?(pub_key, allow_hybrid = false) ⇒ Boolean
# validate whether this is a valid public key (more expensive than IsValid()).
-
.recover_compact(data, signature, rec, compressed) ⇒ Bitcoin::Key
Recover public key from compact signature.
-
.sign_compact(data, privkey) ⇒ Array[signature, recovery id]
Sign data with compact format.
-
.sign_data(data, privkey, extra_entropy = nil, algo: :ecdsa) ⇒ String
sign data.
-
.valid_xonly_pubkey?(pub_key) ⇒ Boolean
Check whether valid x-only public key or not.
-
.verify_sig(data, sig, pubkey, algo: :ecdsa) ⇒ Boolean
verify signature # @param [Symbol] algo signature algorithm.
- .with_context(flags: (SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN)) ⇒ Object
Class Method Details
.create_keypair(priv_key) ⇒ String
Create key pair data from private key.
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/bitcoin/secp256k1/native.rb', line 204 def create_keypair(priv_key) with_context do |context| priv_key = priv_key.htb secret = FFI::MemoryPointer.new(:uchar, priv_key.bytesize).put_bytes(0, priv_key) raise 'priv_key is invalid.' unless secp256k1_ec_seckey_verify(context, secret) keypair = FFI::MemoryPointer.new(:uchar, 96) raise 'priv_key is invalid.' unless secp256k1_keypair_create(context, keypair, secret) == 1 keypair.read_string(96).bth end end |
.generate_key(compressed: true) ⇒ Object
generate bitcoin key object
95 96 97 98 |
# File 'lib/bitcoin/secp256k1/native.rb', line 95 def generate_key(compressed: true) privkey, pubkey = generate_key_pair(compressed: compressed) Bitcoin::Key.new(priv_key: privkey, pubkey: pubkey, compressed: compressed) end |
.generate_key_pair(compressed: true) ⇒ Object
generate ec private key and public key
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bitcoin/secp256k1/native.rb', line 80 def generate_key_pair(compressed: true) with_context do |context| ret, tries, max = 0, 0, 20 while ret != 1 raise 'secp256k1_ec_seckey_verify in generate_key_pair failed.' if tries >= max tries += 1 priv_key = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, SecureRandom.random_bytes(32)) ret = secp256k1_ec_seckey_verify(context, priv_key) end private_key = priv_key.read_string(32).bth [private_key , generate_pubkey_in_context(context, private_key, compressed: compressed) ] end end |
.generate_pubkey(priv_key, compressed: true) ⇒ Object
100 101 102 103 104 |
# File 'lib/bitcoin/secp256k1/native.rb', line 100 def generate_pubkey(priv_key, compressed: true) with_context do |context| generate_pubkey_in_context(context, priv_key, compressed: compressed) end end |
.init ⇒ Object
34 35 36 37 38 |
# File 'lib/bitcoin/secp256k1/native.rb', line 34 def init raise 'secp256k1 library dose not found.' unless File.exist?(ENV['SECP256K1_LIB_PATH']) ffi_lib(ENV['SECP256K1_LIB_PATH']) load_functions end |
.load_functions ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bitcoin/secp256k1/native.rb', line 40 def load_functions attach_function(:secp256k1_context_create, [:uint], :pointer) attach_function(:secp256k1_context_destroy, [:pointer], :void) attach_function(:secp256k1_context_randomize, [:pointer, :pointer], :int) attach_function(:secp256k1_ec_pubkey_create, [:pointer, :pointer, :pointer], :int) attach_function(:secp256k1_ec_seckey_verify, [:pointer, :pointer], :int) attach_function(:secp256k1_ecdsa_sign, [:pointer, :pointer, :pointer, :pointer, :pointer, :pointer], :int) attach_function(:secp256k1_ec_pubkey_serialize, [:pointer, :pointer, :pointer, :pointer, :uint], :int) attach_function(:secp256k1_ecdsa_signature_serialize_der, [:pointer, :pointer, :pointer, :pointer], :int) attach_function(:secp256k1_ec_pubkey_parse, [:pointer, :pointer, :pointer, :size_t], :int) attach_function(:secp256k1_ecdsa_signature_parse_der, [:pointer, :pointer, :pointer, :size_t], :int) attach_function(:secp256k1_ecdsa_signature_normalize, [:pointer, :pointer, :pointer], :int) attach_function(:secp256k1_ecdsa_verify, [:pointer, :pointer, :pointer, :pointer], :int) attach_function(:secp256k1_schnorrsig_sign, [:pointer, :pointer, :pointer, :pointer, :pointer, :pointer], :int) attach_function(:secp256k1_schnorrsig_verify, [:pointer, :pointer, :pointer, :pointer], :int) attach_function(:secp256k1_keypair_create, [:pointer, :pointer, :pointer], :int) attach_function(:secp256k1_xonly_pubkey_parse, [:pointer, :pointer, :pointer], :int) attach_function(:secp256k1_ecdsa_sign_recoverable, [:pointer, :pointer, :pointer, :pointer, :pointer, :pointer], :int) attach_function(:secp256k1_ecdsa_recoverable_signature_serialize_compact, [:pointer, :pointer, :pointer, :pointer], :int) attach_function(:secp256k1_ecdsa_recover, [:pointer, :pointer, :pointer, :pointer], :int) attach_function(:secp256k1_ecdsa_recoverable_signature_parse_compact, [:pointer, :pointer, :pointer, :int], :int) end |
.parse_ec_pubkey?(pub_key, allow_hybrid = false) ⇒ Boolean
# validate whether this is a valid public key (more expensive than IsValid())
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/bitcoin/secp256k1/native.rb', line 190 def parse_ec_pubkey?(pub_key, allow_hybrid = false) pub_key = pub_key.htb return false if !allow_hybrid && ![0x02, 0x03, 0x04].include?(pub_key[0].ord) with_context do |context| pubkey = FFI::MemoryPointer.new(:uchar, pub_key.bytesize).put_bytes(0, pub_key) internal_pubkey = FFI::MemoryPointer.new(:uchar, 64) result = secp256k1_ec_pubkey_parse(context, internal_pubkey, pubkey, pub_key.bytesize) result == 1 end end |
.recover_compact(data, signature, rec, compressed) ⇒ Bitcoin::Key
Recover public key from compact signature.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/bitcoin/secp256k1/native.rb', line 152 def recover_compact(data, signature, rec, compressed) with_context do |context| sig = FFI::MemoryPointer.new(:uchar, 65) input = FFI::MemoryPointer.new(:uchar, 64).put_bytes(0, signature[1..-1]) result = secp256k1_ecdsa_recoverable_signature_parse_compact(context, sig, input, rec) raise 'secp256k1_ecdsa_recoverable_signature_parse_compact failed.' unless result == 1 pubkey = FFI::MemoryPointer.new(:uchar, 64) msg = FFI::MemoryPointer.new(:uchar, data.bytesize).put_bytes(0, data) result = secp256k1_ecdsa_recover(context, pubkey, sig, msg) raise 'secp256k1_ecdsa_recover failed.' unless result == 1 pubkey = serialize_pubkey_internal(context, pubkey.read_string(64), compressed) Bitcoin::Key.new(pubkey: pubkey, compressed: compressed) end end |
.sign_compact(data, privkey) ⇒ Array[signature, recovery id]
Sign data with compact format.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/bitcoin/secp256k1/native.rb', line 127 def sign_compact(data, privkey) with_context do |context| sig = FFI::MemoryPointer.new(:uchar, 65) hash =FFI::MemoryPointer.new(:uchar, data.bytesize).put_bytes(0, data) priv_key = privkey.htb sec_key = FFI::MemoryPointer.new(:uchar, priv_key.bytesize).put_bytes(0, priv_key) result = secp256k1_ecdsa_sign_recoverable(context, sig, hash, sec_key, nil, nil) raise 'secp256k1_ecdsa_sign_recoverable failed.' if result == 0 output = FFI::MemoryPointer.new(:uchar, 64) rec = FFI::MemoryPointer.new(:uint64) result = secp256k1_ecdsa_recoverable_signature_serialize_compact(context, output, rec, sig) raise 'secp256k1_ecdsa_recoverable_signature_serialize_compact failed.' unless result == 1 raw_sig = output.read_string(64) [ECDSA::Signature.new(raw_sig[0...32].bti, raw_sig[32..-1].bti), rec.read(:int)] end end |
.sign_data(data, privkey, extra_entropy = nil, algo: :ecdsa) ⇒ String
sign data.
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/bitcoin/secp256k1/native.rb', line 112 def sign_data(data, privkey, extra_entropy = nil, algo: :ecdsa) case algo when :ecdsa sign_ecdsa(data, privkey, extra_entropy) when :schnorr sign_schnorr(data, privkey, extra_entropy) else nil end end |
.valid_xonly_pubkey?(pub_key) ⇒ Boolean
Check whether valid x-only public key or not.
218 219 220 221 222 223 224 225 |
# File 'lib/bitcoin/secp256k1/native.rb', line 218 def valid_xonly_pubkey?(pub_key) begin full_pubkey_from_xonly_pubkey(pub_key) rescue Exception return false end true end |
.verify_sig(data, sig, pubkey, algo: :ecdsa) ⇒ Boolean
verify signature # @param [Symbol] algo signature algorithm. ecdsa(default) or schnorr.
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/bitcoin/secp256k1/native.rb', line 175 def verify_sig(data, sig, pubkey, algo: :ecdsa) case algo when :ecdsa verify_ecdsa(data, sig, pubkey) when :schnorr verify_schnorr(data, sig, pubkey) else false end end |
.with_context(flags: (SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN)) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/bitcoin/secp256k1/native.rb', line 63 def with_context(flags: (SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN)) init begin context = secp256k1_context_create(flags) ret, tries, max = 0, 0, 20 while ret != 1 raise 'secp256k1_context_randomize failed.' if tries >= max tries += 1 ret = secp256k1_context_randomize(context, FFI::MemoryPointer.from_string(SecureRandom.random_bytes(32))) end yield(context) if block_given? ensure secp256k1_context_destroy(context) end end |