schnorr_sig 1.0.0.2 → 1.0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d07355d7c8e4e0ce43bd586ba7cea831b72d2fd40b6c2efa1d49c46c536ab3ab
4
- data.tar.gz: 346f2e23f326259d0e86f0837377e03cef56518b31828b22d3f7fa7ddca8b76b
3
+ metadata.gz: 583aef17bbda178fd790a7cfddb29bdd1d38ee1d44092d3cb6afc2517a564a09
4
+ data.tar.gz: 299a66f0e042c200b81f902e23ab2adb712a6260532fb28a6e23555cfc414db6
5
5
  SHA512:
6
- metadata.gz: f91e124755a889779f7b5bda0991f9cd082078c855a5f8bcf2fe30886b779d5033d712801d776558bc454a5e5ba1a219fbd05f66b77dfd53098551dc7d6462e1
7
- data.tar.gz: 7709c7e780d437dce791ac7f959350b71622c05c9838cdd6f5173fef3f232ace1a2e4b258ba56f697c4025266fab6e7b8ab0e0f6a77358d07880412963ae717f
6
+ metadata.gz: bb3a7ade41fcdd713ae0b9da0d8c0cf8c390f7c3da232e19e4be146f66bf45311aa19ab02025ff3b55e5dc11a000094817751aaf4dd2c4a9d7e9f93b455ff866
7
+ data.tar.gz: 0dd87d6af1595d3a51b4d1788bb3b24f3c1a11a0997ceb3fc0543e422b4dde75fa04e2b3f1ca4ec5cdafe3fbd9f23091c23cd9608e9ea338e7a21f5b515a6dcf
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0.2
1
+ 1.0.1.1
@@ -34,11 +34,10 @@ module SchnorrSig
34
34
  # Output
35
35
  # Secp256k1::KeyPair
36
36
  def keypair_obj(sk = nil)
37
- if sk
38
- binary!(sk, KEY)
39
- CONTEXT.key_pair_from_private_key(sk)
40
- else
37
+ if sk.nil?
41
38
  CONTEXT.generate_key_pair
39
+ else
40
+ CONTEXT.key_pair_from_private_key(binary!(sk, KEY))
42
41
  end
43
42
  end
44
43
 
@@ -69,8 +68,7 @@ module SchnorrSig
69
68
  # Output
70
69
  # Secp256k1::SchnorrSignature
71
70
  def signature(str)
72
- binary!(str, SIG)
73
- Secp256k1::SchnorrSignature.from_data(str)
71
+ Secp256k1::SchnorrSignature.from_data(binary!(str, SIG))
74
72
  end
75
73
 
76
74
  # Input
@@ -78,10 +76,8 @@ module SchnorrSig
78
76
  # The message, m: 32 byte hash value
79
77
  # Output
80
78
  # 64 bytes binary
81
- def sign(sk, m)
82
- binary!(sk, KEY) and binary!(m, 32)
83
- CONTEXT.sign_schnorr(keypair_obj(sk), m).serialized
84
- end
79
+ def sign(sk, m) = CONTEXT.sign_schnorr(keypair_obj(sk),
80
+ binary!(m, 32)).serialized
85
81
 
86
82
  # Input
87
83
  # The public key, pk: 32 bytes binary
@@ -112,10 +108,7 @@ module SchnorrSig
112
108
  # msg: UTF-8 / binary / agnostic
113
109
  # Output
114
110
  # 32 bytes binary
115
- def tagged_hash(tag, msg)
116
- check!(tag, String) and check!(msg, String)
117
- CONTEXT.tagged_sha256(tag, msg)
118
- end
111
+ def tagged_hash(tag, msg) = CONTEXT.tagged_sha256(str!(tag), str!(msg))
119
112
  end
120
113
 
121
114
  Fast.include Utils
@@ -87,15 +87,12 @@ module SchnorrSig
87
87
  # Output
88
88
  # 32 bytes binary
89
89
  def tagged_hash(tag, msg)
90
- check!(tag, String) and check!(msg, String)
91
- warn("tag expected to be UTF-8") unless tag.encoding == Encoding::UTF_8
92
-
93
90
  # BIP340: The function hash[name](x) where x is a byte array
94
91
  # returns the 32-byte hash
95
92
  # SHA256(SHA256(tag) || SHA256(tag) || x)
96
93
  # where tag is the UTF-8 encoding of name.
97
- tag_hash = Digest::SHA256.digest(tag)
98
- Digest::SHA256.digest(tag_hash + tag_hash + msg)
94
+ tag_hash = Digest::SHA256.digest tag
95
+ Digest::SHA256.digest(tag_hash + tag_hash + str!(msg).b)
99
96
  end
100
97
 
101
98
  #
@@ -135,7 +132,7 @@ module SchnorrSig
135
132
  # The signature, sig: 64 bytes binary
136
133
  def sign(sk, m, auxrand: nil)
137
134
  a = auxrand.nil? ? random_bytes(B) : auxrand
138
- binary!(sk, KEY) and check!(m, String) and binary!(a, B)
135
+ binary!(sk, KEY) and str!(m) and binary!(a, B)
139
136
 
140
137
  # BIP340: Let d' = int(sk)
141
138
  # BIP340: Fail if d' = 0 or d' >= n
@@ -186,7 +183,7 @@ module SchnorrSig
186
183
  # Output
187
184
  # Boolean
188
185
  def verify?(pk, m, sig)
189
- binary!(pk, KEY) and check!(m, String) and binary!(sig, SIG)
186
+ binary!(pk, KEY) and str!(m) and binary!(sig, SIG)
190
187
 
191
188
  # BIP340: Let P = lift_x(int(pk))
192
189
  p = lift_x(int(pk))
@@ -11,13 +11,19 @@ module SchnorrSig
11
11
  val.is_a?(cls) ? val : raise(SpecError, "#{cls}: #{val.inspect}")
12
12
  end
13
13
 
14
+ # raise SpecError or return str
15
+ def str!(str, length = nil)
16
+ if check!(str, String) and !length.nil? and length != str.length
17
+ raise(SpecError, "Length #{str.length} should be #{length}")
18
+ end
19
+ str
20
+ end
21
+
14
22
  # raise SpecError or return str
15
23
  def binary!(str, length)
16
- check!(str, String)
17
- if str.encoding != Encoding::BINARY
24
+ if str!(str, length).encoding != Encoding::BINARY
18
25
  raise(SpecError, "Encoding: #{str.encoding}")
19
26
  end
20
- raise(SpecError, "Length: #{str.length}") if str.length != length
21
27
  str
22
28
  end
23
29
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schnorr_sig
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.2
4
+ version: 1.0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Hull