-
Notifications
You must be signed in to change notification settings - Fork 0
/
Value.py
128 lines (107 loc) · 3.23 KB
/
Value.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
Value.py
Wrapper for data type and operations
"""
import os
import gmpy2
from gmpy2 import mpz
from Cryptodome.Util.number import bytes_to_long
# normal = mpz((2**127)-1)
lzkp = 61
field_bits = lzkp
field = mpz(2**(lzkp))
r_state = gmpy2.random_state(bytes_to_long(os.urandom(16)))
def getfield():
global field
return field
def get_bits():
global field_bits
return field_bits
class Value:
def __init__(self, value=None, p=field):
if value == None:
self.value = None
else:
self.value = gmpy2.f_mod(mpz(value), p)
"""
overload + and sum with ^, * with & for boolean circuit
overload arithmetic operation with gmpy2 library
"""
def __add__(self, other, p = field):
if type(other) == int:
return Value(self.add(other, p))
else:
return Value(self.add(other.value, p))
def __radd__(self, other, p = field):
if type(other) == int:
return Value(self.add(other, p))
else:
return Value(self.add(other, p))
def __sub__(self, other, p = field):
if type(other) == int:
return Value(self.sub(other, p))
else:
return Value(self.sub(other.value, p))
def __mul__(self, other, p = field):
if type(other) == int:
return Value(self.mul(other, p))
else:
return Value(self.mul(other.value, p))
"""
overload equality check
"""
def __eq__(self, other, p = field):
if self.value == None or other.value == None:
return self.value == other.value
else:
left = gmpy2.f_mod(self.value, p)
right = gmpy2.f_mod(other.value, p)
return left == right
def __ne__(self, other, p = field):
left = gmpy2.f_mod(self.value, p)
right = gmpy2.f_mod(other.value, p)
return left != right
'''
overload print
'''
def __repr__(self):
return str(self.value)
"""
support arithmetic operations
"""
def add(self, num, p = field):
ret = gmpy2.add(self.value, num)
return gmpy2.f_mod(ret, p)
def sub(self, num, p = field):
ret = gmpy2.sub(self.value, num)
return gmpy2.f_mod(ret, p)
def mul(self, num, p = field):
ret = gmpy2.mul(self.value, num)
return gmpy2.f_mod(ret, p)
"""
generate a random number from defined field
"""
def getRand(self, state = r_state):
if (not self.value):
# set randomness with seed
if (not state):
state = gmpy2.random_state(bytes_to_long(os.urandom(16)))
self.value = gmpy2.mpz_random(state, field)
def getBinRand(self, state = r_state):
if (not self.value):
# set randomness with seed
if (not state):
state = gmpy2.random_state(bytes_to_long(os.urandom(16)))
self.value = gmpy2.mpz_random(state, 2)
"""
split val into n random shares
"""
def splitVal(self, n):
ret = []
for i in range(n-1):
temp = Value()
temp.getRand()
ret.append(temp)
last = self - sum(ret)
ret.append(last)
return ret