schnorr_sig 1.0.0.2 → 1.0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/schnorr_sig/fast.rb +7 -14
- data/lib/schnorr_sig/pure.rb +4 -7
- data/lib/schnorr_sig/utils.rb +9 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 583aef17bbda178fd790a7cfddb29bdd1d38ee1d44092d3cb6afc2517a564a09
|
4
|
+
data.tar.gz: 299a66f0e042c200b81f902e23ab2adb712a6260532fb28a6e23555cfc414db6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb3a7ade41fcdd713ae0b9da0d8c0cf8c390f7c3da232e19e4be146f66bf45311aa19ab02025ff3b55e5dc11a000094817751aaf4dd2c4a9d7e9f93b455ff866
|
7
|
+
data.tar.gz: 0dd87d6af1595d3a51b4d1788bb3b24f3c1a11a0997ceb3fc0543e422b4dde75fa04e2b3f1ca4ec5cdafe3fbd9f23091c23cd9608e9ea338e7a21f5b515a6dcf
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1.1
|
data/lib/schnorr_sig/fast.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/schnorr_sig/pure.rb
CHANGED
@@ -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
|
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
|
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
|
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))
|
data/lib/schnorr_sig/utils.rb
CHANGED
@@ -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
|
-
|
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
|
|