summaryrefslogtreecommitdiff
path: root/bip-schnorr/reference.py
diff options
context:
space:
mode:
Diffstat (limited to 'bip-schnorr/reference.py')
-rw-r--r--bip-schnorr/reference.py17
1 files changed, 10 insertions, 7 deletions
diff --git a/bip-schnorr/reference.py b/bip-schnorr/reference.py
index e5d9557..7de37c2 100644
--- a/bip-schnorr/reference.py
+++ b/bip-schnorr/reference.py
@@ -11,6 +11,9 @@ def tagged_hash(tag, msg):
tag_hash = hashlib.sha256(tag.encode()).digest()
return hashlib.sha256(tag_hash + tag_hash + msg).digest()
+def is_infinity(P):
+ return P is None
+
def x(P):
return P[0]
@@ -59,11 +62,11 @@ def int_from_bytes(b):
def hash_sha256(b):
return hashlib.sha256(b).digest()
-def jacobi(x):
- return pow(x, (p - 1) // 2, p)
+def is_square(x):
+ return pow(x, (p - 1) // 2, p) == 1
-def is_quad(x):
- return jacobi(x) == 1
+def has_square_y(P):
+ return not is_infinity(P) and is_square(y(P))
def pubkey_gen(seckey):
x = int_from_bytes(seckey)
@@ -79,12 +82,12 @@ def schnorr_sign(msg, seckey0):
if not (1 <= seckey0 <= n - 1):
raise ValueError('The secret key must be an integer in the range 1..n-1.')
P = point_mul(G, seckey0)
- seckey = seckey0 if is_quad(y(P)) else n - seckey0
+ seckey = seckey0 if has_square_y(P) else n - seckey0
k0 = int_from_bytes(tagged_hash("BIPSchnorrDerive", bytes_from_int(seckey) + msg)) % n
if k0 == 0:
raise RuntimeError('Failure. This happens only with negligible probability.')
R = point_mul(G, k0)
- k = n - k0 if not is_quad(y(R)) else k0
+ k = n - k0 if not has_square_y(R) else k0
e = int_from_bytes(tagged_hash("BIPSchnorr", bytes_from_point(R) + bytes_from_point(P) + msg)) % n
return bytes_from_point(R) + bytes_from_int((k + e * seckey) % n)
@@ -104,7 +107,7 @@ def schnorr_verify(msg, pubkey, sig):
return False
e = int_from_bytes(tagged_hash("BIPSchnorr", sig[0:32] + pubkey + msg)) % n
R = point_add(point_mul(G, s), point_mul(P, n - e))
- if R is None or not is_quad(y(R)) or x(R) != r:
+ if R is None or not has_square_y(R) or x(R) != r:
return False
return True