bip-schnorr 0.3.0 → 0.3.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/lib/schnorr.rb +6 -7
- data/lib/schnorr/ec_point_ext.rb +15 -12
- data/lib/schnorr/signature.rb +2 -2
- data/lib/schnorr/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8680b3084a188b36328a6614f92235ae532fd0126c7e447ac49095a9e8b8a84
|
4
|
+
data.tar.gz: d71f6677bed86a690f70069de25b356cdef1e1f7fa36c58e6e19fdaa284d7197
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: facac2fcd1579f7e3bacdbc1d6616311ee731d1c9971d68eacec956eb9683b9e6f89a470a8c0652160da7d91e09085bdf97ec14783356461e7c363a04be0d538
|
7
|
+
data.tar.gz: 788aa518466375b03e0ce63df8dd293c59ff82464a0ed2b6b2e46037a29cb0cbed6393b91af04494248e0436ac16422e25c487e32d46c7876977d1dc71cc4e21
|
data/lib/schnorr.rb
CHANGED
@@ -4,7 +4,6 @@ require_relative 'schnorr/ec_point_ext'
|
|
4
4
|
require_relative 'schnorr/signature'
|
5
5
|
|
6
6
|
module Schnorr
|
7
|
-
|
8
7
|
module_function
|
9
8
|
|
10
9
|
GROUP = ECDSA::Group::Secp256k1
|
@@ -17,14 +16,15 @@ module Schnorr
|
|
17
16
|
# @return (Schnorr::Signature)
|
18
17
|
def sign(message, private_key, aux_rand = SecureRandom.bytes(32))
|
19
18
|
raise 'The message must be a 32-byte array.' unless message.bytesize == 32
|
20
|
-
|
19
|
+
|
20
|
+
d0 = private_key.unpack1('H*').to_i(16)
|
21
21
|
raise 'private_key must be an integer in the range 1..n-1.' unless 0 < d0 && d0 <= (GROUP.order - 1)
|
22
22
|
raise 'aux_rand must be 32 bytes.' unless aux_rand.bytesize == 32
|
23
23
|
|
24
24
|
p = GROUP.new_point(d0)
|
25
25
|
d = p.has_even_y? ? d0 : GROUP.order - d0
|
26
26
|
|
27
|
-
t = d ^ tagged_hash('BIP0340/aux', aux_rand).
|
27
|
+
t = d ^ tagged_hash('BIP0340/aux', aux_rand).unpack1('H*').to_i(16)
|
28
28
|
t = ECDSA::Format::IntegerOctetString.encode(t, GROUP.byte_length)
|
29
29
|
|
30
30
|
k0 = ECDSA::Format::IntegerOctetString.decode(tagged_hash('BIP0340/nonce', t + p.encode(true) + message)) % GROUP.order
|
@@ -36,6 +36,7 @@ module Schnorr
|
|
36
36
|
|
37
37
|
sig = Schnorr::Signature.new(r.x, (k + e * d) % GROUP.order)
|
38
38
|
raise 'The created signature does not pass verification.' unless valid_sig?(message, p.encode(true), sig.encode)
|
39
|
+
|
39
40
|
sig
|
40
41
|
end
|
41
42
|
|
@@ -98,7 +99,6 @@ module Schnorr
|
|
98
99
|
end
|
99
100
|
|
100
101
|
class ::Integer
|
101
|
-
|
102
102
|
def to_hex
|
103
103
|
hex = to_s(16)
|
104
104
|
hex.rjust((hex.length / 2.0).ceil * 2, '0')
|
@@ -111,7 +111,8 @@ module Schnorr
|
|
111
111
|
|
112
112
|
# alternative implementation of Integer#pow for ruby 2.4 and earlier.
|
113
113
|
def mod_pow(x, y)
|
114
|
-
return self
|
114
|
+
return self**x unless y
|
115
|
+
|
115
116
|
b = self
|
116
117
|
result = 1
|
117
118
|
while x > 0
|
@@ -121,7 +122,5 @@ module Schnorr
|
|
121
122
|
end
|
122
123
|
result
|
123
124
|
end
|
124
|
-
|
125
125
|
end
|
126
|
-
|
127
126
|
end
|
data/lib/schnorr/ec_point_ext.rb
CHANGED
@@ -29,19 +29,22 @@ module ECDSA
|
|
29
29
|
|
30
30
|
raise DecodeError, 'Point octet string is empty.' if string.empty?
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
check_length string, 1
|
35
|
-
return group.infinity
|
36
|
-
when 2
|
37
|
-
decode_compressed string, group, 0
|
38
|
-
when 3
|
39
|
-
decode_compressed string, group, 1
|
40
|
-
when 4
|
41
|
-
decode_uncompressed string, group
|
32
|
+
if string.bytesize == 32
|
33
|
+
decode_from_x(string, group)
|
42
34
|
else
|
43
|
-
|
44
|
-
|
35
|
+
case string[0].ord
|
36
|
+
when 0
|
37
|
+
check_length string, 1
|
38
|
+
return group.infinity
|
39
|
+
when 2
|
40
|
+
decode_compressed string, group, 0
|
41
|
+
when 3
|
42
|
+
decode_compressed string, group, 1
|
43
|
+
when 4
|
44
|
+
decode_uncompressed string, group
|
45
|
+
else
|
46
|
+
raise DecodeError, 'Unrecognized start byte for point octet string: 0x%x' % string[0].ord
|
47
|
+
end
|
45
48
|
end
|
46
49
|
end
|
47
50
|
|
data/lib/schnorr/signature.rb
CHANGED
@@ -22,8 +22,8 @@ module Schnorr
|
|
22
22
|
# @return (Signature) signature instance.
|
23
23
|
def self.decode(string)
|
24
24
|
raise InvalidSignatureError, 'Invalid schnorr signature length.' unless string.bytesize == 64
|
25
|
-
r = string[0...32].
|
26
|
-
s = string[32..-1].
|
25
|
+
r = string[0...32].unpack1('H*').to_i(16)
|
26
|
+
s = string[32..-1].unpack1('H*').to_i(16)
|
27
27
|
new(r, s)
|
28
28
|
end
|
29
29
|
|
data/lib/schnorr/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bip-schnorr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- azuchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ecdsa
|
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
requirements: []
|
112
|
-
rubygems_version: 3.
|
112
|
+
rubygems_version: 3.0.8
|
113
113
|
signing_key:
|
114
114
|
specification_version: 4
|
115
115
|
summary: The ruby implementation of bip-schnorr.
|