Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# ephemeral key k must be kept secret and never reused !!!!!
# good choice: k = sha256(msg, p)
# different for each msg, private because of p
temp = msg1+hex(p)
k1 = int(sha256(temp.encode()).hexdigest(), 16) % order
# 0 < k1 < order
assert 0 < k1
assert k1 < order
K1 = pointMultiply(k1, G)
r = K1[0] % order
# if r == 0 (extremely unlikely for large order) go back to a different ephemeral key
assert r != 0
s1 = ((h1 + r*p)*modInv(k1, order)) % order
# if s1 == 0 (extremely unlikely for large order) go back to a different ephemeral key
assert s1 != 0
print(" r:", hex(r))
print(" s1:", hex(s1))
print("*** Signature Verification")
w = modInv(s1, order)
u = (h1*w) %order
v = (r*w) %order
assert u != 0
assert v != 0
U = pointMultiply(u, G)
V = pointMultiply(v, P)
x, y = pointAdd(U, V)
print(r == x %order)
assert h2 != 0
print(" h2:", hex(h2))
print("\n*** Signature")
k2 = k1 #very bad! Never reuse the same ephemeral key!!!
# 0 < k2 < order
assert 0 < k2
assert k2 < order
K2 = pointMultiply(k2, G)
r = K2[0] % order
# if r == 0 (extremely unlikely for large order) go back to a different ephemeral key
assert r != 0
s2 = ((h2 + r*p)*modInv(k2, order)) %order
# if s2 == 0 (extremely unlikely for large order) go back to a different ephemeral key
assert s2 != 0
print(" r:", hex(r))
print(" s2:", hex(s2))
print("*** Signature Verification")
w = modInv(s2, order)
u = (h2*w) %order
v = (r*w) %order
assert u != 0
assert v != 0
U = pointMultiply(u, G)
V = pointMultiply(v, P)
x, y = pointAdd(U, V)
print(r == x %order)
K1 = pointMultiply(k1, G)
r = K1[0] % order
# if r == 0 (extremely unlikely for large order) go back to a different ephemeral key
assert r != 0
s1 = ((h1 + r*p)*modInv(k1, order)) % order
# if s1 == 0 (extremely unlikely for large order) go back to a different ephemeral key
assert s1 != 0
print(" r:", hex(r))
print(" s1:", hex(s1))
print("*** Signature Verification")
w = modInv(s1, order)
u = (h1*w) %order
v = (r*w) %order
assert u != 0
assert v != 0
U = pointMultiply(u, G)
V = pointMultiply(v, P)
x, y = pointAdd(U, V)
print(r == x %order)
print("\n*** Malleated Signature")
s1m = order - s1
print(" r:", hex(r))
print("*s1:", hex(s1m))
print("*** Malleated Signature Verification")
w = modInv(s1m, order)
u = (h1*w) %order
v = (r*w) %order
assert u != 0
assert v != 0
U = pointMultiply(u, G)
V = pointMultiply(v, P)
x, y = pointAdd(U, V)
print(r == x %order)
print("\n*** Malleated Signature")
s1m = order - s1
print(" r:", hex(r))
print("*s1:", hex(s1m))
print("*** Malleated Signature Verification")
w = modInv(s1m, order)
u = (h1*w) %order
v = (r*w) %order
assert u != 0
assert v != 0
U = pointMultiply(u, G)
V = pointMultiply(v, P)
x, y = pointAdd(U, V)
print(r == x %order)
print("\n*** Another message")
msg2 = "and Paolo is right to be afraid"
print(msg2)
print("*** The hash of the message")
hstring2 = sha256(msg2.encode()).hexdigest()
# Hash(msg) must be transformed into an integer modulo order:
K2 = pointMultiply(k2, G)
r = K2[0] % order
# if r == 0 (extremely unlikely for large order) go back to a different ephemeral key
assert r != 0
s2 = ((h2 + r*p)*modInv(k2, order)) %order
# if s2 == 0 (extremely unlikely for large order) go back to a different ephemeral key
assert s2 != 0
print(" r:", hex(r))
print(" s2:", hex(s2))
print("*** Signature Verification")
w = modInv(s2, order)
u = (h2*w) %order
v = (r*w) %order
assert u != 0
assert v != 0
U = pointMultiply(u, G)
V = pointMultiply(v, P)
x, y = pointAdd(U, V)
print(r == x %order)