-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbidder-client.py
More file actions
109 lines (89 loc) · 2.84 KB
/
bidder-client.py
File metadata and controls
109 lines (89 loc) · 2.84 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from __future__ import print_function
import socket
import json
import threading
from util import *
import random
# variables
MAX_DATA_RECV = 999999
# asynchronous threads are needed, hence no thread locks required
"""
VPOT Protocol
"""
def vpot_client():
try:
# TODO - check for 0/1 only
# b = int(raw_input("Enter your bit(0/1): "))
# TODO : Check for correct input length
# TODO : allow bid to be entered as int
b = raw_input("Enter bid in binary: ")
except ValueError:
print("ValueError - Please enter a bit (0/1)")
exit(0)
# 3. Chooser receives N,C from proxy and splits b into b = bs ^ bp
# Select x in Zn*. If bp=0 x0=x^3 else x0=x^3/C. Also compute v=E[bs]
data = client.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
pub_key,C = data.get("pub_key"),data.get("C")
N = pub_key[1]
print("Received pub_key: {} , C: {} from proxy".format(pub_key,C))
print("---------------------------------------------")
bid = []
bid_s = []
bid_p = []
for i in b:
bs = random.SystemRandom().getrandbits(1)
bp = int(i)^bs
bid.append(int(i))
bid_s.append(bs)
bid_p.append(bp)
print("bs:{}, bp:{}, b:{} ".format(bid_s,bid_p,bid))
# ip_len = length of input
ip_len = len(bid)
X = []
X0 = []
V = []
for i in range(ip_len):
while True:
x = random.SystemRandom().randint(1,N-1)
if util.gcd(x,N)==1:
break
if bid_p[i] == 0:
# TODO: Change x0 = pow(x,3,N)
x0 = (x*x*x)%N
else:
x0 = util.modular_div_util(pow(x,3),C[i],N)
v = GM.GM_encrypt(str(bid_s[i]),pub_key)
# 4. Chooser sends (x0,v,x) to proxy. Proxy sends to sender: (x0,v)
v_str = ''.join(str(s) for s in v)
X.append(x)
X0.append(x0)
V.append(v_str)
data = json.dumps({"x0":X0, "v":V, "x":X, "bp":bid_p})
print("Sent x0: {},v: {},x: {}, bp: {} to proxy".format(X0,V,X,bid_p))
client.send(data.encode())
# Receive tag_int from proxy
data = client.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
tag_int = data.get("tag_int")
print("Received tag_int: {} from proxy".format(tag_int))
for t in tag_int:
tag = gc_util.decode_str(t)
print("Decoded tag: ",tag)
"""
end of VPOT
"""
if __name__ == "__main__":
try:
SERVER = '127.0.0.1'
PORT = int(raw_input("Enter proxy-server port number: "))
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client.connect((SERVER,PORT))
vpot_client()
except socket.error as e:
print("An error occurred : ",e)
exit(0)
except(KeyboardInterrupt):
print("\nShutting down...")
exit(0)