This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
OT2.py
56 lines (55 loc) · 1.5 KB
/
OT2.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import socket
from utils import *
BigPrime = 4776913109852041418248056622882488319
def OT_Encrypt(g,p,pk,s):
r=randint(1,p-1)
tmp= n2b(pow(g,r,p))
tmp2 = bytes_to_long(hash( n2b( pow(pk,r,p) ) )) ^ bytes_to_long(s)
return tmp+b'-'+n2b(tmp2)
def OT_Receiver(choice,server):
m= server.recv(8)
if(m==b'n132-OT2'):
GC = CyclicGroup(BigPrime)
g = GC.generator
data = n2b(BigPrime)+b"-"+n2b(g)
server.send(data)
c = int(server.recv(1024))
k = GC.rand_int()
pks=[0,0]
b = choice
pks[b] = GC.pow(g,k)
pks[b-1] = GC.div(c,pks[b])
server.send(pack(pks))
data = server.recv(1024).split(b"-")
b = b*2
u = data[b]
v = data[b+1]
u = int(u)
v = int(v)
res=long_to_bytes( bytes_to_long(hash(n2b(GC.pow(u,k)))) ^ v )
return res
else:
return -1
def OT_Sender(message,client):
try:
client.send(b"n132-OT2")
data= client.recv(1024)
p, g=data.split(b"-")
p = int(p)
g = int(g)
c = randint(1,p-1)
client.send(n2b(c))
data = client.recv(1024)
pks = data.split(b"-")
pks[0] = int(pks[0])
pks[1] = int(pks[1])
assert(c== (pks[0] * pks[1])% p)
sec = message
data=b""
data+= OT_Encrypt(g,p,pks[0],sec[0])
data+=b'-'
data+= OT_Encrypt(g,p,pks[1],sec[1])
client.send(data)
return 1
except:
return 0