bip-schnorr 0.2.0 → 0.3.0
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/README.md +4 -3
- data/lib/schnorr.rb +5 -6
- data/lib/schnorr/ec_point_ext.rb +0 -13
- data/lib/schnorr/version.rb +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: 46a74af7fff8c807780488e505f4065e7ec3f7ad63a8e5b9ca52f4304f19c9b8
|
4
|
+
data.tar.gz: f68b54e7158a474a821bac1d62ea1f9cfade33fc81f58a65bc4f5a885f674a61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0a90d133c6afb2015c19e5c93c054bfc744d8fc4d6f9a768fa6c087e37545bb4248e09bd8785ba24915169ca27afadcd0134ce49d59d3c1fda668efd20c89e3
|
7
|
+
data.tar.gz: 0c22a9a02be20cb9386bab115770e95db0ab9513f2c3bf168439ee22f864a5e50610ed4d8a02f289a7cdec0bde63c9cff01e7f9b713b6f4c796aab750f8b1566
|
data/README.md
CHANGED
@@ -34,6 +34,9 @@ message = ['5E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C'].p
|
|
34
34
|
|
35
35
|
# create signature
|
36
36
|
signature = Schnorr.sign(message, private_key)
|
37
|
+
# if use auxiliary random data, specify it to the 3rd arguments.
|
38
|
+
aux_rand = SecureRandom.bytes(32) # aux_rand must be a 32-byte binary.
|
39
|
+
signature = Schnorr.sign(message, private_key, aux_rand)
|
37
40
|
|
38
41
|
# signature r value
|
39
42
|
signature.r
|
@@ -55,7 +58,7 @@ require 'schnorr'
|
|
55
58
|
# public key does not start with 02 or 03.
|
56
59
|
public_key = ['DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659'].pack('H*')
|
57
60
|
|
58
|
-
signature = ['
|
61
|
+
signature = ['6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A'].pack('H*')
|
59
62
|
|
60
63
|
message = ['243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89'].pack('H*')
|
61
64
|
|
@@ -71,8 +74,6 @@ sig = Schnorr::Signature.decode(signature)
|
|
71
74
|
This library changes the following functions of `ecdsa` gem in `lib/schnorr/ec_point_ext.rb`.
|
72
75
|
|
73
76
|
* `ECDSA::Point` class has following two instance methods.
|
74
|
-
* `#has_square_y?` check this point does not infinity and square?(y coordinate)
|
75
|
-
* `#square?(x)` check whether `x` is a quadratic residue modulo p.
|
76
77
|
* `#has_even_y?` check the y-coordinate of this point is an even.
|
77
78
|
* `#encode(only_x = false)` encode this point into a binary string.
|
78
79
|
* `ECDSA::Format::PointOctetString#decode` supports decoding only from x coordinate.
|
data/lib/schnorr.rb
CHANGED
@@ -24,14 +24,14 @@ module Schnorr
|
|
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('
|
27
|
+
t = d ^ tagged_hash('BIP0340/aux', aux_rand).unpack('H*').first.to_i(16)
|
28
28
|
t = ECDSA::Format::IntegerOctetString.encode(t, GROUP.byte_length)
|
29
29
|
|
30
|
-
k0 = ECDSA::Format::IntegerOctetString.decode(tagged_hash('
|
30
|
+
k0 = ECDSA::Format::IntegerOctetString.decode(tagged_hash('BIP0340/nonce', t + p.encode(true) + message)) % GROUP.order
|
31
31
|
raise 'Creation of signature failed. k is zero' if k0.zero?
|
32
32
|
|
33
33
|
r = GROUP.new_point(k0)
|
34
|
-
k = r.
|
34
|
+
k = r.has_even_y? ? k0 : GROUP.order - k0
|
35
35
|
e = create_challenge(r.x, p, message)
|
36
36
|
|
37
37
|
sig = Schnorr::Signature.new(r.x, (k + e * d) % GROUP.order)
|
@@ -59,7 +59,6 @@ module Schnorr
|
|
59
59
|
raise InvalidSignatureError, 'The message must be a 32-byte array.' unless message.bytesize == 32
|
60
60
|
raise InvalidSignatureError, 'The public key must be a 32-byte array.' unless public_key.bytesize == 32
|
61
61
|
|
62
|
-
|
63
62
|
sig = Schnorr::Signature.decode(signature)
|
64
63
|
pubkey = ECDSA::Format::PointOctetString.decode(public_key, GROUP)
|
65
64
|
field = GROUP.field
|
@@ -73,7 +72,7 @@ module Schnorr
|
|
73
72
|
|
74
73
|
r = GROUP.new_point(sig.s) + pubkey.multiply_by_scalar(GROUP.order - e)
|
75
74
|
|
76
|
-
if r.infinity? || !r.
|
75
|
+
if r.infinity? || !r.has_even_y? || r.x != sig.r
|
77
76
|
raise Schnorr::InvalidSignatureError, 'signature verification failed.'
|
78
77
|
end
|
79
78
|
|
@@ -86,7 +85,7 @@ module Schnorr
|
|
86
85
|
# @return (Integer) digest e.
|
87
86
|
def create_challenge(x, p, message)
|
88
87
|
r_x = ECDSA::Format::IntegerOctetString.encode(x, GROUP.byte_length)
|
89
|
-
(ECDSA.normalize_digest(tagged_hash('
|
88
|
+
(ECDSA.normalize_digest(tagged_hash('BIP0340/challenge', r_x + p.encode(true) + message), GROUP.bit_length)) % GROUP.order
|
90
89
|
end
|
91
90
|
|
92
91
|
# Generate tagged hash value.
|
data/lib/schnorr/ec_point_ext.rb
CHANGED
@@ -2,25 +2,12 @@
|
|
2
2
|
module ECDSA
|
3
3
|
class Point
|
4
4
|
|
5
|
-
# Check this point does not infinity and square?(y coordinate)
|
6
|
-
# @return (Boolean)
|
7
|
-
def has_square_y?
|
8
|
-
!infinity? && square?(y)
|
9
|
-
end
|
10
|
-
|
11
5
|
# Check the y-coordinate of this point is an even.
|
12
6
|
# @return (Boolean) if even, return true.
|
13
7
|
def has_even_y?
|
14
8
|
y.even?
|
15
9
|
end
|
16
10
|
|
17
|
-
# Check whether +x+ is a quadratic residue modulo p.
|
18
|
-
# @param x (Integer)
|
19
|
-
# @return (Boolean)
|
20
|
-
def square?(x)
|
21
|
-
x.pow((group.field.prime - 1) / 2, group.field.prime) == 1
|
22
|
-
end
|
23
|
-
|
24
11
|
# Encode this point into a binary string.
|
25
12
|
# @param (Boolean) only_x whether or not to encode only X-coordinate. default is false.
|
26
13
|
def encode(only_x = false)
|
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.
|
4
|
+
version: 0.3.0
|
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-08-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ecdsa
|