summaryrefslogtreecommitdiff
path: root/bip-schnorr
diff options
context:
space:
mode:
Diffstat (limited to 'bip-schnorr')
-rw-r--r--bip-schnorr/reference.py136
-rw-r--r--bip-schnorr/speedup-batch.pngbin0 -> 11914 bytes
-rw-r--r--bip-schnorr/test-vectors.csv17
3 files changed, 153 insertions, 0 deletions
diff --git a/bip-schnorr/reference.py b/bip-schnorr/reference.py
new file mode 100644
index 0000000..48a36a6
--- /dev/null
+++ b/bip-schnorr/reference.py
@@ -0,0 +1,136 @@
+import hashlib
+import binascii
+
+p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
+n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
+G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)
+
+def point_add(P1, P2):
+ if (P1 is None):
+ return P2
+ if (P2 is None):
+ return P1
+ if (P1[0] == P2[0] and P1[1] != P2[1]):
+ return None
+ if (P1 == P2):
+ lam = (3 * P1[0] * P1[0] * pow(2 * P1[1], p - 2, p)) % p
+ else:
+ lam = ((P2[1] - P1[1]) * pow(P2[0] - P1[0], p - 2, p)) % p
+ x3 = (lam * lam - P1[0] - P2[0]) % p
+ return (x3, (lam * (P1[0] - x3) - P1[1]) % p)
+
+def point_mul(P, n):
+ R = None
+ for i in range(256):
+ if ((n >> i) & 1):
+ R = point_add(R, P)
+ P = point_add(P, P)
+ return R
+
+def bytes_from_int(x):
+ return x.to_bytes(32, byteorder="big")
+
+def bytes_from_point(P):
+ return (b'\x03' if P[1] & 1 else b'\x02') + bytes_from_int(P[0])
+
+def point_from_bytes(b):
+ if b[0] in [b'\x02', b'\x03']:
+ odd = b[0] - 0x02
+ else:
+ return None
+ x = int_from_bytes(b[1:33])
+ y_sq = (pow(x, 3, p) + 7) % p
+ y0 = pow(y_sq, (p + 1) // 4, p)
+ if pow(y0, 2, p) != y_sq:
+ return None
+ y = p - y0 if y0 & 1 != odd else y0
+ return [x, y]
+
+def int_from_bytes(b):
+ return int.from_bytes(b, byteorder="big")
+
+def hash_sha256(b):
+ return hashlib.sha256(b).digest()
+
+def jacobi(x):
+ return pow(x, (p - 1) // 2, p)
+
+def schnorr_sign(msg, seckey):
+ if len(msg) != 32:
+ raise ValueError('The message must be a 32-byte array.')
+ if not (1 <= seckey <= n - 1):
+ raise ValueError('The secret key must be an integer in the range 1..n-1.')
+ k0 = int_from_bytes(hash_sha256(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 (jacobi(R[1]) != 1) else k0
+ e = int_from_bytes(hash_sha256(bytes_from_int(R[0]) + bytes_from_point(point_mul(G, seckey)) + msg)) % n
+ return bytes_from_int(R[0]) + bytes_from_int((k + e * seckey) % n)
+
+def schnorr_verify(msg, pubkey, sig):
+ if len(msg) != 32:
+ raise ValueError('The message must be a 32-byte array.')
+ if len(pubkey) != 33:
+ raise ValueError('The public key must be a 33-byte array.')
+ if len(sig) != 64:
+ raise ValueError('The signature must be a 64-byte array.')
+ P = point_from_bytes(pubkey)
+ if (P is None):
+ return False
+ r = int_from_bytes(sig[0:32])
+ s = int_from_bytes(sig[32:64])
+ if (r >= p or s >= n):
+ return False
+ e = int_from_bytes(hash_sha256(sig[0:32] + bytes_from_point(P) + msg)) % n
+ R = point_add(point_mul(G, s), point_mul(P, n - e))
+ if R is None or jacobi(R[1]) != 1 or R[0] != r:
+ return False
+ return True
+
+#
+# The following code is only used to verify the test vectors.
+#
+import csv
+
+def test_vectors():
+ all_passed = True
+ with open('test-vectors.csv', newline='') as csvfile:
+ reader = csv.reader(csvfile)
+ reader.__next__()
+ for row in reader:
+ (index, seckey, pubkey, msg, sig, result, comment) = row
+ pubkey = bytes.fromhex(pubkey)
+ msg = bytes.fromhex(msg)
+ sig = bytes.fromhex(sig)
+ result = result == 'TRUE'
+ print('\nTest vector #%-3i: ' % int(index))
+ if seckey != '':
+ seckey = int(seckey, 16)
+ sig_actual = schnorr_sign(msg, seckey)
+ if sig == sig_actual:
+ print(' * Passed signing test.')
+ else:
+ print(' * Failed signing test.')
+ print(' Excepted signature:', sig.hex())
+ print(' Actual signature:', sig_actual.hex())
+ all_passed = False
+ result_actual = schnorr_verify(msg, pubkey, sig)
+ if result == result_actual:
+ print(' * Passed verification test.')
+ else:
+ print(' * Failed verification test.')
+ print(' Excepted verification result:', result)
+ print(' Actual verification result:', result_actual)
+ if comment:
+ print(' Comment:', comment)
+ all_passed = False
+ print()
+ if all_passed:
+ print('All test vectors passed.')
+ else:
+ print('Some test vectors failed.')
+ return all_passed
+
+if __name__ == '__main__':
+ test_vectors()
diff --git a/bip-schnorr/speedup-batch.png b/bip-schnorr/speedup-batch.png
new file mode 100644
index 0000000..fe672d4
--- /dev/null
+++ b/bip-schnorr/speedup-batch.png
Binary files differ
diff --git a/bip-schnorr/test-vectors.csv b/bip-schnorr/test-vectors.csv
new file mode 100644
index 0000000..9b89a7b
--- /dev/null
+++ b/bip-schnorr/test-vectors.csv
@@ -0,0 +1,17 @@
+index,secret key,public key,message,signature,verification result,comment
+1,0000000000000000000000000000000000000000000000000000000000000001,0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,0000000000000000000000000000000000000000000000000000000000000000,787A848E71043D280C50470E8E1532B2DD5D20EE912A45DBDD2BD1DFBF187EF67031A98831859DC34DFFEEDDA86831842CCD0079E1F92AF177F7F22CC1DCED05,TRUE,
+2,B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF,02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD,TRUE,
+3,C90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B14E5C7,03FAC2114C2FBB091527EB7C64ECB11F8021CB45E8E7809D3C0938E4B8C0E5F84B,5E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C,00DA9B08172A9B6F0466A2DEFD817F2D7AB437E0D253CB5395A963866B3574BE00880371D01766935B92D2AB4CD5C8A2A5837EC57FED7660773A05F0DE142380,TRUE,
+4,,03DEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34,4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703,00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6302A8DC32E64E86A333F20EF56EAC9BA30B7246D6D25E22ADB8C6BE1AEB08D49D,TRUE,
+5,,031B84C5567B126440995D3ED5AABA0565D71E1834604819FF9C17F5E9D5DD078F,0000000000000000000000000000000000000000000000000000000000000000,52818579ACA59767E3291D91B76B637BEF062083284992F2D95F564CA6CB4E3530B1DA849C8E8304ADC0CFE870660334B3CFC18E825EF1DB34CFAE3DFC5D8187,TRUE,"test fails if jacobi symbol of x(R) instead of y(R) is used"
+6,,03FAC2114C2FBB091527EB7C64ECB11F8021CB45E8E7809D3C0938E4B8C0E5F84B,FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,570DD4CA83D4E6317B8EE6BAE83467A1BF419D0767122DE409394414B05080DCE9EE5F237CBD108EABAE1E37759AE47F8E4203DA3532EB28DB860F33D62D49BD,TRUE,"test fails if msg is reduced"
+7,,03EEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34,4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703,00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6302A8DC32E64E86A333F20EF56EAC9BA30B7246D6D25E22ADB8C6BE1AEB08D49D,FALSE,"public key not on the curve"
+8,,02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1DFA16AEE06609280A19B67A24E1977E4697712B5FD2943914ECD5F730901B4AB7,FALSE,"incorrect R residuosity"
+9,,03FAC2114C2FBB091527EB7C64ECB11F8021CB45E8E7809D3C0938E4B8C0E5F84B,5E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C, 00DA9B08172A9B6F0466A2DEFD817F2D7AB437E0D253CB5395A963866B3574BED092F9D860F1776A1F7412AD8A1EB50DACCC222BC8C0E26B2056DF2F273EFDEC,FALSE,"negated message hash"
+10,,0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,0000000000000000000000000000000000000000000000000000000000000000,787A848E71043D280C50470E8E1532B2DD5D20EE912A45DBDD2BD1DFBF187EF68FCE5677CE7A623CB20011225797CE7A8DE1DC6CCD4F754A47DA6C600E59543C,FALSE,"negated s value"
+11,,03DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD,FALSE,"negated public key"
+12,,02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,00000000000000000000000000000000000000000000000000000000000000009E9D01AF988B5CEDCE47221BFA9B222721F3FA408915444A4B489021DB55775F,FALSE,"sG - eP is infinite. Test fails in single verification if jacobi(y(inf)) is defined as 1 and x(inf) as 0"
+13,,02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,0000000000000000000000000000000000000000000000000000000000000001D37DDF0254351836D84B1BD6A795FD5D523048F298C4214D187FE4892947F728,FALSE,"sG - eP is infinite. Test fails in single verification if jacobi(y(inf)) is defined as 1 and x(inf) as 1"
+14,,02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,4A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD,FALSE,"sig[0:32] is not an X coordinate on the curve"
+15,,02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC2F1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD,FALSE,"sig[0:32] is equal to field size"
+16,,02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1DFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141,FALSE,"sig[32:64] is equal to curve order"