-
Notifications
You must be signed in to change notification settings - Fork 25
/
weak_signature_generator.py
38 lines (27 loc) · 1.01 KB
/
weak_signature_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import ecdsa
import random
import sys
secret = int(sys.argv[1], 16)
bits = int(sys.argv[2])
BITS = 1 << bits
n = int(sys.argv[3])
mode = "MSB"
gen = ecdsa.SECP256k1.generator
order = gen.order()
# secret = random.randrange(1,order)
pub_key = ecdsa.ecdsa.Public_key(gen, gen * secret)
priv_key = ecdsa.ecdsa.Private_key(pub_key, secret)
fixed_bits = random.randrange(BITS, order)
if mode == "MSB":
# generate n most significant bits, nonce must be less than order
nonces = [fixed_bits + random.randrange(1, BITS) for _ in range(n)]
else:
# generate n least significant bits, nonce must be less than order
nonces = [random.randrange(BITS, order) + fixed_bits for _ in range(n)]
msgs = [random.randrange(1, order) for _ in range(n)]
sigs = [priv_key.sign(msgs[i], nonces[i]) for i in range(n)]
def inttohex(i):
tmpstr = hex(i)
return tmpstr.replace("0x", "").replace("L", "").zfill(64)
for i in range(0, len(msgs)):
print(f"1111,{inttohex(sigs[i].r)},{inttohex(sigs[i].s)},{inttohex(msgs[i])},0000")