forked from asanso/CryptoWithSageMath
-
Notifications
You must be signed in to change notification settings - Fork 1
/
two-party-ecdsa.sage
84 lines (61 loc) · 1.89 KB
/
two-party-ecdsa.sage
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# two party generation of ecdsa signature
# based on paillier encryption
# use `sage <filename.sage>` to execute the sage script
load("prime192v1.sage")
load("digest.sage")
load("eckeygen.sage")
load("ecdsa.sage")
load("paillier.sage")
# Alice keygen
(QA, dA) = ec_keygen()
print " Alice's ECDSA keypair : ", (QA, dA)
# Bob keygen
(QB, dB) = ec_keygen()
print " Bob's ECDSA keypair : ", (QB, dB)
# Joint public key can be generate with ECDH
# but neither Alice nor Bob has the joint private key `d`
d = (dA * dB) % n
Q = dA * QB
print " Joint ECDSA keypair : ", (Q, d)
#print " Joint ECDSA keypair :", (d*P, d)
# start of the two-party siganture generation
# Alice receives the message to be signed and will
# return the final signature
m = "message to be signed"
e = digest(m)
# Alice generate temp keypair (TA, kA)
(TA, kA) = ec_keygen()
# Bob generate temp keypair (TB, kB)
(TB, kB) = ec_keygen()
# Alice and Bob exchange temp public key and use ECDH to
# generate `r`
k = (kA * kB) % n
T = kA * TB
(x, y) = T.xy()
r = Fn(x)
s = (Fn(k)^-1) * (e + d * r)
#print " Joint ECDSA Signature : ", (r, s)
# Alice generate her Paillier keypair
# it will be very slow, you can change bits to 512 to make it faster
bits = 512
sk = paillier_keygen_simple(bits)
pk = sk[0]
print " Alice's Paillier keypair : ", sk
# Alice's secets are kA and dA
# Alice convert secret to kA^-1, kA^-1 * dA
uA = Fn(kA)^(-1)
vA = uA * dA
euA = paillier_encrypt(uA, pk)
evA = paillier_encrypt(vA, pk)
# Alice send euA, evA to Bob
# Bob generate the signature ciphertext
a1 = Fn(kB)^(-1) * e
a2 = Fn(kB)^(-1) * dB * r
es = paillier_ciphertext_linear(a1, euA, a2, evA, pk)
# Bob send encrypted sigature esig to Alice
# Alice decrytp it with her paillier private key `sk`
s = paillier_decrypt(es, sk)
s = Fn(s)
print " Joint ECDSA Signature : ", (r, s)
ret = ecdsa_verify(Q, m, r, s)
print " Verification Result : ", ret