forked from jamespayor/vector-homomorphic-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhevector.py
59 lines (46 loc) · 1.33 KB
/
hevector.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
def tupleToVec(t):
if type(t) in (int, long): return str(t).strip('L')
if t and type(t[0]) is tuple:
return 'matrix [%s]' % ' '.join('[%s]' % ' '.join(map(str, x)) for x in t)
return 'vector [%s]' % ' '.join(map(tupleToVec,t))
def vecToTuple(v):
v = v[1:-1]
return tuple(map(int, v.split()))
def send(ops):
return '\n'.join(v if type(v) is str else tupleToVec(v) for v in ops)
def recv(output):
out = map(str.strip, output.splitlines())[::-1]
res = []
while out:
x = out.pop()
if x[:2] == '[[':
x = x[1:]
m = []
while x != ']':
m.append(vecToTuple(x))
x = out.pop()
res.append(tuple(m))
else:
res.append(vecToTuple(x))
return tuple(res)
def evaluate(operations, DEBUG=False):
from subprocess import Popen, PIPE
if DEBUG:
print
print operations
print
inp = send(operations)
if DEBUG:
print inp
print
with open('vhe.in', 'w') as f:
f.write(inp)
output, error = Popen(['./vhe'], stdin=PIPE, stdout=PIPE, shell=True).communicate('')
if DEBUG:
print output
print
if error:
from sys import stderr
stderr.write(error + '\n')
stderr.flush()
return recv(output)