bip-schnorr 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e68ad5d5ccf6213171279052ddef947229a8eab7597cc93ca2b8cc312d0e7254
4
- data.tar.gz: b77fed1e8cc5dba386dcf6ce81bfe3d36c20985bd246b5c5d5e6f9ce45db6177
3
+ metadata.gz: 46a74af7fff8c807780488e505f4065e7ec3f7ad63a8e5b9ca52f4304f19c9b8
4
+ data.tar.gz: f68b54e7158a474a821bac1d62ea1f9cfade33fc81f58a65bc4f5a885f674a61
5
5
  SHA512:
6
- metadata.gz: 0d2a1ffd174074375af26fe2e4dfae18d6a72e9e44e76861d09e3724d3d13c4b90e182b2dc7533cefb93fbc44317cd92f4941716ae3e0fe549dc4e950e09b710
7
- data.tar.gz: ebeba83783283f328b321ab19a5c3cebf4f74aa0d56c9f491283a502367a4baf29ea78f905b335f547f1cd8b6215395260c72981776d9e2743d3b4d03d408d7f
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 = ['0E12B8C520948A776753A96F21ABD7FDC2D7D0C0DDC90851BE17B04E75EF86A47EF0DA46C4DC4D0D1BCB8668C2CE16C54C7C23A6716EDE303AF86774917CF928'].pack('H*')
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.
@@ -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('BIP340/aux', aux_rand).unpack('H*').first.to_i(16)
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('BIP340/nonce', t + p.encode(true) + message)) % GROUP.order
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.has_square_y? ? k0 : GROUP.order - k0
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.has_square_y? || r.x != sig.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('BIP340/challenge', r_x + p.encode(true) + message), GROUP.bit_length)) % GROUP.order
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.
@@ -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)
@@ -1,3 +1,3 @@
1
1
  module Schnorr
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.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-05-08 00:00:00.000000000 Z
11
+ date: 2020-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ecdsa