secp256k1rb 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +5 -2
- data/lib/secp256k1/c.rb +5 -1
- data/lib/secp256k1/ellswift.rb +1 -1
- data/lib/secp256k1/musig/key_agg.rb +1 -1
- data/lib/secp256k1/musig/session.rb +1 -5
- data/lib/secp256k1/musig.rb +7 -6
- data/lib/secp256k1/recovery.rb +6 -11
- data/lib/secp256k1/schnorrsig.rb +14 -15
- data/lib/secp256k1/version.rb +1 -1
- data/lib/secp256k1.rb +53 -45
- data/secp256k1rb.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d1626509b5565bd7eb99f4099c97beedf44bf2a080e983aefef55dd615bd4edb
|
|
4
|
+
data.tar.gz: 3b0df7520dde0973d3ee747ef1e40ca6ecb61777431d0e76ccb9a2a302322ac5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3b9bc340ac3a1c81397a053362e7a55311ab182a2208ee7bf3c2e4483df7950485518aaef676aeb96dc16258d4f386455860e3667e10d88072c61b537acdbc3c
|
|
7
|
+
data.tar.gz: 68fc807c8b8a5610a90d92e74151f739631c28ffc6a4691c1d6ef7fdd7f074adbeee5305b4dce0680ad51cbbf0519fe1c278181cbb2ab150c312baccdb07eb18
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.8.1] - 2026-11-01
|
|
4
|
+
|
|
5
|
+
- Install a non-aborting illegal-argument callback on every secp256k1 context.
|
|
6
|
+
Previously any `ARG_CHECK` failure inside libsecp256k1 (e.g. `generate_pubkey` with an invalid
|
|
7
|
+
private key, `verify_ecdsa` with an unparsable public key, `aggregate_pubkey([])`,
|
|
8
|
+
`aggregate_musig_nonce([])`) called `abort()` and killed the Ruby process. These cases now
|
|
9
|
+
raise `Secp256k1::Error`/`ArgumentError` or return `false`.
|
|
10
|
+
- Fix C return values being treated as Ruby truthy: `0` (failure) was never detected in
|
|
11
|
+
`sign_ecdsa`, `verify_ecdsa`, `create_keypair`, `generate_pubkey` and `serialize_pubkey_internal`.
|
|
12
|
+
- Fix heap buffer overflow in `MuSig::Session#partial_sign` (key pair buffer was 86 bytes instead of 96).
|
|
13
|
+
- Fix `create_keypair` raising `NoMethodError` instead of `Secp256k1::Error` for an invalid private key.
|
|
14
|
+
- Fix `generate_musig_nonce` raising `NoMethodError` when `key_agg_ctx` is omitted.
|
|
15
|
+
- Fix `sign_schnorr` (`aux_rand`) and `sign_ecdsa` (`extra_entropy`) rejecting hex-encoded input.
|
|
16
|
+
- Fix `verify_schnorr_custom` always returning `false` for the empty message.
|
|
17
|
+
- Fix `parse_ec_pubkey?('')` raising `NoMethodError`.
|
|
18
|
+
- `valid_xonly_pubkey?` no longer rescues `Exception`.
|
|
19
|
+
- `verify_schnorr`/`verify_schnorr_custom` now return `false` instead of raising `ArgumentError`
|
|
20
|
+
when the public key is not a valid point on the curve, consistent with `verify_ecdsa`.
|
|
21
|
+
- Use `size_t`/`int` sized buffers for length and recovery id out-parameters.
|
|
22
|
+
- Raise a descriptive `LoadError` when `SECP256K1_LIB_PATH` is not set.
|
|
23
|
+
|
|
3
24
|
## [0.8.0]
|
|
4
25
|
|
|
5
26
|
- Change versioning policy: the gem's major and minor version now match the
|
data/README.md
CHANGED
|
@@ -23,8 +23,11 @@ Or install it yourself as:
|
|
|
23
23
|
To use this library, you need to specify the path of the secp256k1 shared library in environment variable
|
|
24
24
|
`SECP256K1_LIB_PATH`, e.g: `$ export SECP256K1_LIB_PATH=/var/local/lib/libsecp256k1.so`.
|
|
25
25
|
|
|
26
|
-
Note: This library
|
|
27
|
-
|
|
26
|
+
Note: This library binds the `recovery`, `ecdh`, `extrakeys`, `schnorrsig`, `musig` and `ellswift` modules,
|
|
27
|
+
so the secp256k1 library must have been built with these modules enabled
|
|
28
|
+
(e.g. `--enable-module-recovery --enable-module-musig --enable-module-ellswift`; the others are enabled by default).
|
|
29
|
+
Loading fails with `FFI::NotFoundError` if one of them is missing.
|
|
30
|
+
The `silentpayments` module (`--enable-module-silentpayments`, experimental) is optional.
|
|
28
31
|
|
|
29
32
|
By including the Secp256k1 module, you can use the features provided by the `libsepc256k1` library. For example:
|
|
30
33
|
|
data/lib/secp256k1/c.rb
CHANGED
|
@@ -6,7 +6,11 @@ module Secp256k1
|
|
|
6
6
|
module C
|
|
7
7
|
extend FFI::Library
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
lib_path = ENV['SECP256K1_LIB_PATH']
|
|
10
|
+
if lib_path.nil? || lib_path.empty?
|
|
11
|
+
raise LoadError, 'The environment variable SECP256K1_LIB_PATH must specify the path to the libsecp256k1 shared library.'
|
|
12
|
+
end
|
|
13
|
+
ffi_lib(lib_path)
|
|
10
14
|
|
|
11
15
|
attach_function(:secp256k1_context_create, [:uint], :pointer)
|
|
12
16
|
attach_function(:secp256k1_context_destroy, [:pointer], :void)
|
data/lib/secp256k1/ellswift.rb
CHANGED
|
@@ -60,7 +60,7 @@ module Secp256k1
|
|
|
60
60
|
# @param [String] their_ell_pubkey Their EllSwift public key.
|
|
61
61
|
# @param [String] our_ell_pubkey Our EllSwift public key.
|
|
62
62
|
# @param [String] private_key private key with hex format.
|
|
63
|
-
# @param [Boolean] initiating Whether
|
|
63
|
+
# @param [Boolean] initiating Whether we are the initiator of the connection (BIP-324).
|
|
64
64
|
# @return [String] x coordinate with hex format.
|
|
65
65
|
# @raise [Secp256k1::Error] If secret is invalid or hashfp return 0.
|
|
66
66
|
def ellswift_ecdh_xonly(their_ell_pubkey, our_ell_pubkey, private_key, initiating)
|
|
@@ -16,7 +16,7 @@ module Secp256k1
|
|
|
16
16
|
# @param [Secp256k1::MuSig::KeyAggCache] key_agg_cache Key aggregation cache.
|
|
17
17
|
# @raise [ArgumentError] If invalid arguments specified.
|
|
18
18
|
def initialize(key_agg_cache)
|
|
19
|
-
raise ArgumentError, "key_agg_cache must be Secp256k1::KeyAggCache." unless key_agg_cache.is_a?(
|
|
19
|
+
raise ArgumentError, "key_agg_cache must be Secp256k1::MuSig::KeyAggCache." unless key_agg_cache.is_a?(KeyAggCache)
|
|
20
20
|
@cache = key_agg_cache
|
|
21
21
|
end
|
|
22
22
|
|
|
@@ -49,11 +49,7 @@ module Secp256k1
|
|
|
49
49
|
with_context do |context|
|
|
50
50
|
partial_sig = FFI::MemoryPointer.new(:uchar, 36)
|
|
51
51
|
sec_nonce_ptr = FFI::MemoryPointer.new(:uchar, 132).put_bytes(0, hex2bin(sec_nonce))
|
|
52
|
-
|
|
53
|
-
key_pair = FFI::MemoryPointer.new(:uchar, 86)
|
|
54
|
-
if secp256k1_keypair_create(context, key_pair, private_key_ptr) == 0
|
|
55
|
-
raise Error, "secp256k1_keypair_create invalid private_key."
|
|
56
|
-
end
|
|
52
|
+
key_pair = create_keypair_internal(context, hex2bin(private_key))
|
|
57
53
|
if secp256k1_musig_partial_sign(
|
|
58
54
|
context, partial_sig, sec_nonce_ptr, key_pair, key_agg_ctx.cache.pointer, session) == 0
|
|
59
55
|
raise Error, "secp256k1_musig_partial_sign arguments invalid or sec_nonce has already been used for signing."
|
data/lib/secp256k1/musig.rb
CHANGED
|
@@ -26,7 +26,7 @@ module Secp256k1
|
|
|
26
26
|
#
|
|
27
27
|
# # Nonce generation
|
|
28
28
|
# session_id1 = generate_musig_session_id
|
|
29
|
-
# secnonce1, pubnonce1 =
|
|
29
|
+
# secnonce1, pubnonce1 = generate_musig_nonce(
|
|
30
30
|
# session_id1,
|
|
31
31
|
# pk1,
|
|
32
32
|
# sk: sk1,
|
|
@@ -35,7 +35,7 @@ module Secp256k1
|
|
|
35
35
|
# )
|
|
36
36
|
#
|
|
37
37
|
# session_id2 = generate_musig_session_id
|
|
38
|
-
# secnonce2, pubnonce2 =
|
|
38
|
+
# secnonce2, pubnonce2 = generate_musig_nonce(
|
|
39
39
|
# session_id2,
|
|
40
40
|
# pk2,
|
|
41
41
|
# sk: sk2,
|
|
@@ -60,10 +60,11 @@ module Secp256k1
|
|
|
60
60
|
|
|
61
61
|
# Aggregate public keys.
|
|
62
62
|
# @param [Array] pubkeys An array of public keys.
|
|
63
|
-
# @return [
|
|
63
|
+
# @return [Secp256k1::MuSig::KeyAggContext]
|
|
64
64
|
# @raise [Secp256k1::Error]
|
|
65
65
|
def aggregate_pubkey(pubkeys)
|
|
66
66
|
raise ArgumentError, "pubkeys must be an array." unless pubkeys.is_a?(Array)
|
|
67
|
+
raise ArgumentError, "pubkeys must not be empty." if pubkeys.empty?
|
|
67
68
|
with_context do |context|
|
|
68
69
|
pubkeys_ptrs = pubkeys.map do |pubkey|
|
|
69
70
|
pubkey = hex2bin(pubkey)
|
|
@@ -124,7 +125,7 @@ module Secp256k1
|
|
|
124
125
|
session_secrand32 = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, hex2bin(session_id))
|
|
125
126
|
|
|
126
127
|
raise Error, "arguments is invalid." unless secp256k1_musig_nonce_gen(
|
|
127
|
-
context, secnonce, pubnonce, session_secrand32, seckey, pubkey, msg32, key_agg_ctx
|
|
128
|
+
context, secnonce, pubnonce, session_secrand32, seckey, pubkey, msg32, key_agg_ctx&.pointer, extra_input32) == 1
|
|
128
129
|
|
|
129
130
|
pub66 = FFI::MemoryPointer.new(:uchar, 66)
|
|
130
131
|
secp256k1_musig_pubnonce_serialize(context, pub66, pubnonce)
|
|
@@ -153,8 +154,7 @@ module Secp256k1
|
|
|
153
154
|
end
|
|
154
155
|
|
|
155
156
|
with_context do |context|
|
|
156
|
-
|
|
157
|
-
keypair_ptr = FFI::MemoryPointer.new(:uchar, 96).put_bytes(0, keypair)
|
|
157
|
+
keypair_ptr = create_keypair_internal(context, hex2bin(private_key))
|
|
158
158
|
pubnonce = FFI::MemoryPointer.new(:uchar, 132)
|
|
159
159
|
secnonce = FFI::MemoryPointer.new(:uchar, 132)
|
|
160
160
|
msg32 = msg ? FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, hex2bin(msg)) : nil
|
|
@@ -177,6 +177,7 @@ module Secp256k1
|
|
|
177
177
|
# @raise [ArgumentError] If invalid arguments specified.
|
|
178
178
|
def aggregate_musig_nonce(pub_nonces)
|
|
179
179
|
raise ArgumentError, "pub_nonces must be Array." unless pub_nonces.is_a?(Array)
|
|
180
|
+
raise ArgumentError, "pub_nonces must not be empty." if pub_nonces.empty?
|
|
180
181
|
|
|
181
182
|
with_context do |context|
|
|
182
183
|
nonce_ptrs = pub_nonces.map do |pub_nonce|
|
data/lib/secp256k1/recovery.rb
CHANGED
|
@@ -8,7 +8,7 @@ module Secp256k1
|
|
|
8
8
|
# sig, rec = sign_recoverable(msg, sk)
|
|
9
9
|
# full_sig = [rec + 0x1b + 4].pack('C') + [sig].pack('H*')
|
|
10
10
|
# compressed = true
|
|
11
|
-
# recover_pubkey =
|
|
11
|
+
# recover_pubkey = recover(msg, full_sig, compressed)
|
|
12
12
|
#
|
|
13
13
|
module Recover
|
|
14
14
|
# Sign data with compact format.
|
|
@@ -24,18 +24,18 @@ module Secp256k1
|
|
|
24
24
|
data = hex2bin(data)
|
|
25
25
|
with_context do |context|
|
|
26
26
|
sig = FFI::MemoryPointer.new(:uchar, 65)
|
|
27
|
-
hash =FFI::MemoryPointer.new(:uchar, data.bytesize).put_bytes(0, data)
|
|
27
|
+
hash = FFI::MemoryPointer.new(:uchar, data.bytesize).put_bytes(0, data)
|
|
28
28
|
sec_key = FFI::MemoryPointer.new(:uchar, private_key.bytesize).put_bytes(0, private_key)
|
|
29
29
|
result = secp256k1_ecdsa_sign_recoverable(context, sig, hash, sec_key, nil, nil)
|
|
30
30
|
raise Error, 'secp256k1_ecdsa_sign_recoverable failed.' if result == 0
|
|
31
31
|
|
|
32
32
|
output = FFI::MemoryPointer.new(:uchar, 64)
|
|
33
|
-
rec = FFI::MemoryPointer.new(:
|
|
33
|
+
rec = FFI::MemoryPointer.new(:int)
|
|
34
34
|
result = secp256k1_ecdsa_recoverable_signature_serialize_compact(context, output, rec, sig)
|
|
35
35
|
raise Error, 'secp256k1_ecdsa_recoverable_signature_serialize_compact failed.' unless result == 1
|
|
36
36
|
|
|
37
37
|
sig = output.read_string(64).unpack1('H*')
|
|
38
|
-
[sig, rec.
|
|
38
|
+
[sig, rec.read_int]
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
|
@@ -52,7 +52,6 @@ module Secp256k1
|
|
|
52
52
|
signature = hex2bin(signature)
|
|
53
53
|
data = hex2bin(data)
|
|
54
54
|
rec = (signature[0].ord - 0x1b) & 3
|
|
55
|
-
raise ArgumentError, "rec must be between 0 and 3." if rec < 0 || rec > 3
|
|
56
55
|
|
|
57
56
|
with_context do |context|
|
|
58
57
|
sig = FFI::MemoryPointer.new(:uchar, 65)
|
|
@@ -65,7 +64,7 @@ module Secp256k1
|
|
|
65
64
|
result = secp256k1_ecdsa_recover(context, pubkey, sig, msg)
|
|
66
65
|
raise Error, 'secp256k1_ecdsa_recover failed.' unless result == 1
|
|
67
66
|
|
|
68
|
-
serialize_pubkey_internal(context, pubkey
|
|
67
|
+
serialize_pubkey_internal(context, pubkey, compressed)
|
|
69
68
|
end
|
|
70
69
|
end
|
|
71
70
|
|
|
@@ -78,17 +77,13 @@ module Secp256k1
|
|
|
78
77
|
validate_string!("signature", signature, 65)
|
|
79
78
|
signature = hex2bin(signature)
|
|
80
79
|
rec = (signature[0].ord - 0x1b) & 3
|
|
81
|
-
raise ArgumentError, "rec must be between 0 and 3." if rec < 0 || rec > 3
|
|
82
80
|
with_context do |context|
|
|
83
81
|
recoverable = FFI::MemoryPointer.new(:uchar, 65)
|
|
84
82
|
input = FFI::MemoryPointer.new(:uchar, 64).put_bytes(0, signature[1..-1])
|
|
85
83
|
raise Error, 'secp256k1_ecdsa_recoverable_signature_parse_compact failed.' unless secp256k1_ecdsa_recoverable_signature_parse_compact(context, recoverable, input, rec) == 1
|
|
86
84
|
internal_signature = FFI::MemoryPointer.new(:uchar, 64)
|
|
87
85
|
raise Error, 'secp256k1_ecdsa_recoverable_signature_convert failed.' unless secp256k1_ecdsa_recoverable_signature_convert(context, internal_signature, recoverable) == 1
|
|
88
|
-
|
|
89
|
-
output_len = FFI::MemoryPointer.new(:uint64).put_uint64(0, 72)
|
|
90
|
-
raise Error, 'secp256k1_ecdsa_signature_serialize_der failed.' unless secp256k1_ecdsa_signature_serialize_der(context, output, output_len, internal_signature) == 1
|
|
91
|
-
output.read_string(output_len.read_uint64)
|
|
86
|
+
serialize_ecdsa_signature_der(context, internal_signature)
|
|
92
87
|
end
|
|
93
88
|
end
|
|
94
89
|
end
|
data/lib/secp256k1/schnorrsig.rb
CHANGED
|
@@ -14,20 +14,20 @@ module Secp256k1
|
|
|
14
14
|
# Sign to data using schnorr.
|
|
15
15
|
# @param [String] data The 32-byte message hash being signed with binary format.
|
|
16
16
|
# @param [String] private_key a private key with hex format using sign.
|
|
17
|
-
# @param [String] aux_rand The 32-byte extra entropy.
|
|
18
|
-
# @return [String] signature data with binary format
|
|
17
|
+
# @param [String] aux_rand (Optional)The 32-byte extra entropy with hex or binary format.
|
|
18
|
+
# @return [String] signature data with binary format(64 bytes).
|
|
19
|
+
# @raise [Secp256k1::Error] If signing failed.
|
|
19
20
|
# @raise [ArgumentError] If invalid arguments specified.
|
|
20
21
|
def sign_schnorr(data, private_key, aux_rand = nil)
|
|
21
22
|
validate_string!("data", data, 32)
|
|
22
23
|
validate_string!("private_key", private_key, 32)
|
|
23
24
|
validate_string!("aux_rand", aux_rand, 32) if aux_rand
|
|
24
|
-
raise ArgumentError, "aux_rand must be String." if !aux_rand.nil? && !aux_rand.is_a?(String)
|
|
25
25
|
private_key = hex2bin(private_key)
|
|
26
26
|
data = hex2bin(data)
|
|
27
|
+
aux_rand = hex2bin(aux_rand) if aux_rand
|
|
27
28
|
|
|
28
29
|
with_context do |context|
|
|
29
|
-
keypair =
|
|
30
|
-
keypair = FFI::MemoryPointer.new(:uchar, 96).put_bytes(0, keypair)
|
|
30
|
+
keypair = create_keypair_internal(context, private_key)
|
|
31
31
|
signature = FFI::MemoryPointer.new(:uchar, 64)
|
|
32
32
|
msg32 = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, data)
|
|
33
33
|
aux_rand = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, aux_rand) if aux_rand
|
|
@@ -55,8 +55,7 @@ module Secp256k1
|
|
|
55
55
|
aux_rand = hex2bin(aux_rand) if aux_rand
|
|
56
56
|
|
|
57
57
|
with_context do |context|
|
|
58
|
-
keypair =
|
|
59
|
-
keypair = FFI::MemoryPointer.new(:uchar, 96).put_bytes(0, keypair)
|
|
58
|
+
keypair = create_keypair_internal(context, private_key)
|
|
60
59
|
signature = FFI::MemoryPointer.new(:uchar, 64)
|
|
61
60
|
msg = FFI::MemoryPointer.new(:uchar, [data.bytesize, 1].max).put_bytes(0, data)
|
|
62
61
|
|
|
@@ -77,7 +76,7 @@ module Secp256k1
|
|
|
77
76
|
# @param [String] data The 32-byte message hash assumed to be signed.
|
|
78
77
|
# @param [String] signature signature data with binary format
|
|
79
78
|
# @param [String] pubkey a public key with hex format using verify.
|
|
80
|
-
# @return [Boolean] verification result.
|
|
79
|
+
# @return [Boolean] verification result. Returns false if +pubkey+ is not a valid x-only public key.
|
|
81
80
|
# @raise [ArgumentError] If invalid arguments specified.
|
|
82
81
|
def verify_schnorr(data, signature, pubkey)
|
|
83
82
|
validate_string!("data", data, 32)
|
|
@@ -88,7 +87,7 @@ module Secp256k1
|
|
|
88
87
|
# @param [String] data The message assumed to be signed. The length is arbitrary.
|
|
89
88
|
# @param [String] signature signature data with binary format(64 bytes).
|
|
90
89
|
# @param [String] pubkey an x-only public key with hex format(32 bytes) using verify.
|
|
91
|
-
# @return [Boolean] verification result.
|
|
90
|
+
# @return [Boolean] verification result. Returns false if +pubkey+ is not a valid x-only public key.
|
|
92
91
|
# @raise [ArgumentError] If invalid arguments specified.
|
|
93
92
|
def verify_schnorr_custom(data, signature, pubkey)
|
|
94
93
|
raise ArgumentError, "data must be String." unless data.is_a?(String)
|
|
@@ -104,13 +103,13 @@ module Secp256k1
|
|
|
104
103
|
pubkey = hex2bin(pubkey)
|
|
105
104
|
signature = hex2bin(signature)
|
|
106
105
|
with_context do |context|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
106
|
+
xonly_input = FFI::MemoryPointer.new(:uchar, X_ONLY_PUBKEY_SIZE).put_bytes(0, pubkey)
|
|
107
|
+
xonly_pubkey = FFI::MemoryPointer.new(:uchar, 64)
|
|
108
|
+
# A public key that is not a valid point on the curve cannot have signed anything.
|
|
109
|
+
return false unless secp256k1_xonly_pubkey_parse(context, xonly_pubkey, xonly_input) == 1
|
|
110
|
+
sig_ptr = FFI::MemoryPointer.new(:uchar, 64).put_bytes(0, signature)
|
|
111
111
|
msg = FFI::MemoryPointer.new(:uchar, [data.bytesize, 1].max).put_bytes(0, data)
|
|
112
|
-
|
|
113
|
-
result == 1
|
|
112
|
+
secp256k1_schnorrsig_verify(context, sig_ptr, msg, data.bytesize, xonly_pubkey) == 1
|
|
114
113
|
end
|
|
115
114
|
end
|
|
116
115
|
end
|
data/lib/secp256k1/version.rb
CHANGED
data/lib/secp256k1.rb
CHANGED
|
@@ -58,13 +58,21 @@ module Secp256k1
|
|
|
58
58
|
X_ONLY_PUBKEY_SIZE = 32
|
|
59
59
|
ELL_SWIFT_KEY_SIZE = 64
|
|
60
60
|
|
|
61
|
+
# Illegal-argument callback installed on every context created by {#with_context}.
|
|
62
|
+
# libsecp256k1's default callback calls abort(), which kills the whole Ruby process whenever
|
|
63
|
+
# an ARG_CHECK fails inside the library. With this no-op callback installed the C function
|
|
64
|
+
# simply returns 0 and the Ruby wrapper raises a proper exception instead.
|
|
65
|
+
ILLEGAL_CALLBACK = FFI::Function.new(:void, [:string, :pointer]) { |_message, _data| }
|
|
66
|
+
|
|
61
67
|
# Creates a secp256k1 context object, performs the operations passed in the block,
|
|
62
68
|
# and then ensures that the secp256k1 context object is destroyed at the end.
|
|
63
69
|
# @param [Integer] flags The flag to use when performing the operation.
|
|
64
70
|
# @raise [Secp256k1::Error] If secp256k1_context_randomize failed.
|
|
65
71
|
def with_context(flags: (CONTEXT_VERIFY | CONTEXT_SIGN))
|
|
72
|
+
context = nil
|
|
66
73
|
begin
|
|
67
74
|
context = secp256k1_context_create(flags)
|
|
75
|
+
secp256k1_context_set_illegal_callback(context, ILLEGAL_CALLBACK, nil)
|
|
68
76
|
ret, tries, max = 0, 0, 20
|
|
69
77
|
while ret != 1
|
|
70
78
|
raise Error, 'secp256k1_context_randomize failed.' if tries >= max
|
|
@@ -116,6 +124,7 @@ module Secp256k1
|
|
|
116
124
|
def parse_ec_pubkey?(pubkey, allow_hybrid = false)
|
|
117
125
|
raise ArgumentError, "pubkey must be String." unless pubkey.is_a?(String)
|
|
118
126
|
pubkey = hex2bin(pubkey)
|
|
127
|
+
return false if pubkey.empty?
|
|
119
128
|
return false if !allow_hybrid && ![0x02, 0x03, 0x04].include?(pubkey[0].ord)
|
|
120
129
|
with_context do |context|
|
|
121
130
|
pubkey_size = pubkey.bytesize
|
|
@@ -135,11 +144,7 @@ module Secp256k1
|
|
|
135
144
|
validate_string!("private_key", private_key, 32)
|
|
136
145
|
private_key = hex2bin(private_key)
|
|
137
146
|
with_context do |context|
|
|
138
|
-
|
|
139
|
-
raise Error, 'private_key is invalid.' unless secp256k1_ec_seckey_verify(context, secret)
|
|
140
|
-
keypair = FFI::MemoryPointer.new(:uchar, 96)
|
|
141
|
-
raise Error 'private_key is invalid.' unless secp256k1_keypair_create(context, keypair, secret) == 1
|
|
142
|
-
keypair.read_string(96).unpack1('H*')
|
|
147
|
+
create_keypair_internal(context, private_key).read_string(96).unpack1('H*')
|
|
143
148
|
end
|
|
144
149
|
end
|
|
145
150
|
|
|
@@ -150,7 +155,7 @@ module Secp256k1
|
|
|
150
155
|
return false unless pubkey.is_a?(String)
|
|
151
156
|
begin
|
|
152
157
|
full_pubkey_from_xonly_pubkey(hex2bin(pubkey))
|
|
153
|
-
rescue
|
|
158
|
+
rescue ArgumentError, Error
|
|
154
159
|
return false
|
|
155
160
|
end
|
|
156
161
|
true
|
|
@@ -159,8 +164,9 @@ module Secp256k1
|
|
|
159
164
|
# Sign to data using ecdsa.
|
|
160
165
|
# @param [String] data The 32-byte message hash being signed with binary format.
|
|
161
166
|
# @param [String] private_key a private key with hex format using sign.
|
|
162
|
-
# @param [String] extra_entropy (Optional)
|
|
163
|
-
# @return [String] signature data with binary format.
|
|
167
|
+
# @param [String] extra_entropy (Optional)A 32-byte extra entropy with hex or binary format for rfc6979.
|
|
168
|
+
# @return [String] DER-encoded signature data with binary format.
|
|
169
|
+
# @raise [Secp256k1::Error] If private_key is invalid or signing failed.
|
|
164
170
|
# @raise [ArgumentError] If invalid arguments specified.
|
|
165
171
|
def sign_ecdsa(data, private_key, extra_entropy = nil)
|
|
166
172
|
validate_string!("private_key", private_key, 32)
|
|
@@ -168,29 +174,20 @@ module Secp256k1
|
|
|
168
174
|
validate_string!("extra_entropy", extra_entropy, 32) if extra_entropy
|
|
169
175
|
private_key = hex2bin(private_key)
|
|
170
176
|
data = hex2bin(data)
|
|
177
|
+
extra_entropy = hex2bin(extra_entropy) if extra_entropy
|
|
171
178
|
|
|
172
179
|
with_context do |context|
|
|
173
|
-
secret = FFI::MemoryPointer.new(:uchar,
|
|
174
|
-
raise Error, 'private_key is invalid' unless secp256k1_ec_seckey_verify(context, secret)
|
|
180
|
+
secret = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, private_key)
|
|
181
|
+
raise Error, 'private_key is invalid.' unless secp256k1_ec_seckey_verify(context, secret) == 1
|
|
175
182
|
|
|
176
183
|
internal_signature = FFI::MemoryPointer.new(:uchar, 64)
|
|
177
184
|
msg32 = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, data)
|
|
178
185
|
entropy = extra_entropy ? FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, extra_entropy) : nil
|
|
179
186
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
while ret != 1
|
|
183
|
-
raise Error, 'secp256k1_ecdsa_sign failed.' if tries >= max
|
|
184
|
-
tries += 1
|
|
185
|
-
ret = secp256k1_ecdsa_sign(context, internal_signature, msg32, secret, nil, entropy)
|
|
186
|
-
end
|
|
187
|
-
|
|
188
|
-
signature = FFI::MemoryPointer.new(:uchar, 72)
|
|
189
|
-
signature_len = FFI::MemoryPointer.new(:uint64).put_uint64(0, 72)
|
|
190
|
-
result = secp256k1_ecdsa_signature_serialize_der(context, signature, signature_len, internal_signature)
|
|
191
|
-
raise Error, 'secp256k1_ecdsa_signature_serialize_der failed' unless result
|
|
187
|
+
# The nonce is derived deterministically (RFC6979), so retrying with the same inputs cannot help.
|
|
188
|
+
raise Error, 'secp256k1_ecdsa_sign failed.' unless secp256k1_ecdsa_sign(context, internal_signature, msg32, secret, nil, entropy) == 1
|
|
192
189
|
|
|
193
|
-
|
|
190
|
+
serialize_ecdsa_signature_der(context, internal_signature)
|
|
194
191
|
end
|
|
195
192
|
end
|
|
196
193
|
|
|
@@ -207,17 +204,15 @@ module Secp256k1
|
|
|
207
204
|
data = hex2bin(data)
|
|
208
205
|
pubkey = hex2bin(pubkey)
|
|
209
206
|
signature = hex2bin(signature)
|
|
207
|
+
return false if pubkey.empty? || signature.empty?
|
|
210
208
|
with_context do |context|
|
|
211
|
-
|
|
212
|
-
pubkey = FFI::MemoryPointer.new(:uchar, pubkey.bytesize).put_bytes(0, pubkey)
|
|
209
|
+
pubkey_ptr = FFI::MemoryPointer.new(:uchar, pubkey.bytesize).put_bytes(0, pubkey)
|
|
213
210
|
internal_pubkey = FFI::MemoryPointer.new(:uchar, 64)
|
|
214
|
-
|
|
215
|
-
return false unless result
|
|
211
|
+
return false unless secp256k1_ec_pubkey_parse(context, internal_pubkey, pubkey_ptr, pubkey.bytesize) == 1
|
|
216
212
|
|
|
217
|
-
|
|
213
|
+
sig_ptr = FFI::MemoryPointer.new(:uchar, signature.bytesize).put_bytes(0, signature)
|
|
218
214
|
internal_signature = FFI::MemoryPointer.new(:uchar, 64)
|
|
219
|
-
|
|
220
|
-
return false unless result
|
|
215
|
+
return false unless secp256k1_ecdsa_signature_parse_der(context, internal_signature, sig_ptr, signature.bytesize) == 1
|
|
221
216
|
|
|
222
217
|
# libsecp256k1's ECDSA verification requires lower-S signatures, which have not historically been enforced in Bitcoin, so normalize them first.
|
|
223
218
|
secp256k1_ecdsa_signature_normalize(context, internal_signature, internal_signature)
|
|
@@ -259,10 +254,7 @@ module Secp256k1
|
|
|
259
254
|
sig_ptr = FFI::MemoryPointer.new(:uchar, 64).put_bytes(0, signature)
|
|
260
255
|
internal_signature = FFI::MemoryPointer.new(:uchar, 64)
|
|
261
256
|
raise Error, 'secp256k1_ecdsa_signature_parse_compact failed.' unless secp256k1_ecdsa_signature_parse_compact(context, internal_signature, sig_ptr) == 1
|
|
262
|
-
|
|
263
|
-
output_len = FFI::MemoryPointer.new(:uint64).put_uint64(0, 72)
|
|
264
|
-
raise Error, 'secp256k1_ecdsa_signature_serialize_der failed.' unless secp256k1_ecdsa_signature_serialize_der(context, output, output_len, internal_signature) == 1
|
|
265
|
-
output.read_string(output_len.read_uint64)
|
|
257
|
+
serialize_ecdsa_signature_der(context, internal_signature)
|
|
266
258
|
end
|
|
267
259
|
end
|
|
268
260
|
|
|
@@ -332,23 +324,39 @@ module Secp256k1
|
|
|
332
324
|
internal_pubkey = FFI::MemoryPointer.new(:uchar, 64)
|
|
333
325
|
priv_ptr = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, private_key)
|
|
334
326
|
result = secp256k1_ec_pubkey_create(context, internal_pubkey, priv_ptr)
|
|
335
|
-
raise '
|
|
327
|
+
raise Error, 'private_key is invalid.' unless result == 1
|
|
336
328
|
serialize_pubkey_internal(context, internal_pubkey, compressed)
|
|
337
329
|
end
|
|
338
330
|
|
|
339
331
|
# Serialize public key.
|
|
340
332
|
def serialize_pubkey_internal(context, pubkey_input, compressed)
|
|
341
333
|
pubkey = FFI::MemoryPointer.new(:uchar, 65)
|
|
342
|
-
pubkey_len = FFI::MemoryPointer.new(:
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
334
|
+
pubkey_len = FFI::MemoryPointer.new(:size_t)
|
|
335
|
+
pubkey_len.write(:size_t, compressed ? 33 : 65)
|
|
336
|
+
flags = compressed ? EC_COMPRESSED : EC_UNCOMPRESSED
|
|
337
|
+
raise Error, 'error serialize pubkey' unless secp256k1_ec_pubkey_serialize(context, pubkey, pubkey_len, pubkey_input, flags) == 1
|
|
338
|
+
pubkey.read_string(pubkey_len.read(:size_t)).unpack1('H*')
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
# Create an internal key pair(96 bytes) from a 32-byte binary private key using given +context+.
|
|
342
|
+
# @return [FFI::MemoryPointer] key pair pointer(96 bytes).
|
|
343
|
+
# @raise [Secp256k1::Error] If private_key is invalid.
|
|
344
|
+
def create_keypair_internal(context, private_key)
|
|
345
|
+
secret = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, private_key)
|
|
346
|
+
keypair = FFI::MemoryPointer.new(:uchar, 96)
|
|
347
|
+
raise Error, 'private_key is invalid.' unless secp256k1_keypair_create(context, keypair, secret) == 1
|
|
348
|
+
keypair
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
# Serialize an internal ECDSA signature(64 bytes) into DER format.
|
|
352
|
+
# @return [String] DER-encoded signature with binary format.
|
|
353
|
+
# @raise [Secp256k1::Error] If serialization failed.
|
|
354
|
+
def serialize_ecdsa_signature_der(context, internal_signature)
|
|
355
|
+
output = FFI::MemoryPointer.new(:uchar, 72)
|
|
356
|
+
output_len = FFI::MemoryPointer.new(:size_t)
|
|
357
|
+
output_len.write(:size_t, 72)
|
|
358
|
+
raise Error, 'secp256k1_ecdsa_signature_serialize_der failed.' unless secp256k1_ecdsa_signature_serialize_der(context, output, output_len, internal_signature) == 1
|
|
359
|
+
output.read_string(output_len.read(:size_t))
|
|
352
360
|
end
|
|
353
361
|
|
|
354
362
|
def hex_string?(str)
|
data/secp256k1rb.gemspec
CHANGED
|
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
|
|
|
16
16
|
|
|
17
17
|
spec.metadata["homepage_uri"] = spec.homepage
|
|
18
18
|
spec.metadata["source_code_uri"] = spec.homepage
|
|
19
|
-
spec.metadata["changelog_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md"
|
|
20
20
|
|
|
21
21
|
# Specify which files should be added to the gem when it is released.
|
|
22
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: secp256k1rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- azuchi
|
|
@@ -61,7 +61,7 @@ licenses:
|
|
|
61
61
|
metadata:
|
|
62
62
|
homepage_uri: https://github.com/azuchi/secp256k1rb
|
|
63
63
|
source_code_uri: https://github.com/azuchi/secp256k1rb
|
|
64
|
-
changelog_uri: https://github.com/azuchi/secp256k1rb
|
|
64
|
+
changelog_uri: https://github.com/azuchi/secp256k1rb/blob/master/CHANGELOG.md
|
|
65
65
|
rdoc_options: []
|
|
66
66
|
require_paths:
|
|
67
67
|
- lib
|