lib/tapyrus/secp256k1/native.rb in tapyrus-0.2.7 vs lib/tapyrus/secp256k1/native.rb in tapyrus-0.2.8
- old
+ new
@@ -1,11 +1,10 @@
# Porting part of the code from bitcoin-ruby. see the license.
# https://github.com/lian/bitcoin-ruby/blob/master/COPYING
module Tapyrus
module Secp256k1
-
# binding for secp256k1 (https://github.com/chaintope/tapyrus-core/tree/v0.4.0/src/secp256k1)
# tag: v0.4.0
# this is not included by default, to enable set shared object path to ENV['SECP256K1_LIB_PATH']
# for linux, ENV['SECP256K1_LIB_PATH'] = '/usr/local/lib/libsecp256k1.so'
# for mac,
@@ -78,25 +77,23 @@
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) ]
+ private_key = priv_key.read_string(32).bth
+ [private_key, generate_pubkey_in_context(context, private_key, compressed: compressed)]
end
end
# generate tapyrus key object
def generate_key(compressed: true)
privkey, pubkey = generate_key_pair(compressed: compressed)
Tapyrus::Key.new(priv_key: privkey, pubkey: pubkey, compressed: compressed)
end
def generate_pubkey(priv_key, compressed: true)
- with_context do |context|
- generate_pubkey_in_context(context, priv_key, compressed: compressed)
- end
+ with_context { |context| generate_pubkey_in_context(context, priv_key, compressed: compressed) }
end
# sign data.
# @param [String] data a data to be signed with binary format
# @param [String] privkey a private key using sign
@@ -150,17 +147,18 @@
result = secp256k1_ec_pubkey_create(context, internal_pubkey, privkey.htb)
raise 'error creating pubkey' unless result
pubkey = FFI::MemoryPointer.new(:uchar, 65)
pubkey_len = FFI::MemoryPointer.new(:uint64)
- result = if compressed
- pubkey_len.put_uint64(0, 33)
- secp256k1_ec_pubkey_serialize(context, pubkey, pubkey_len, internal_pubkey, SECP256K1_EC_COMPRESSED)
- else
- pubkey_len.put_uint64(0, 65)
- secp256k1_ec_pubkey_serialize(context, pubkey, pubkey_len, internal_pubkey, SECP256K1_EC_UNCOMPRESSED)
- end
+ result =
+ if compressed
+ pubkey_len.put_uint64(0, 33)
+ secp256k1_ec_pubkey_serialize(context, pubkey, pubkey_len, internal_pubkey, SECP256K1_EC_COMPRESSED)
+ else
+ pubkey_len.put_uint64(0, 65)
+ secp256k1_ec_pubkey_serialize(context, pubkey, pubkey_len, internal_pubkey, SECP256K1_EC_UNCOMPRESSED)
+ end
raise 'error serialize pubkey' unless result || pubkey_len.read_uint64 > 0
pubkey.read_string(pubkey_len.read_uint64).bth
end
def sign_ecdsa(data, privkey, extra_entropy)
@@ -194,11 +192,13 @@
secret = FFI::MemoryPointer.new(:uchar, privkey.htb.bytesize).put_bytes(0, privkey.htb)
raise 'priv_key invalid' unless secp256k1_ec_seckey_verify(context, secret)
signature = FFI::MemoryPointer.new(:uchar, 64)
msg32 = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, data)
- raise 'Failed to generate schnorr signature.' unless secp256k1_schnorr_sign(context, signature, msg32, secret, nil, nil) == 1
+ unless secp256k1_schnorr_sign(context, signature, msg32, secret, nil, nil) == 1
+ raise 'Failed to generate schnorr signature.'
+ end
signature.read_string(64)
end
end
def verify_ecdsa(data, sig, pub_key)
@@ -239,9 +239,8 @@
result = secp256k1_schnorr_verify(context, signature, msg32, internal_pubkey)
result == 1
end
end
-
end
end
end