-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.py
229 lines (187 loc) · 5.75 KB
/
helpers.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import random
import gmpy2
from gmpy2 import mpz
import numpy as np
import cPickle
import math
import copy
import sys
########################
# General Math Helpers
########################
'''
Returns a random prime number in the range [start, end)
'''
primes = cPickle.load(open("picklePrimesSmall.pkl","r"))
'''
Returns a random number within a start and end
'''
def get_random_prime(start,end):
i = random.randint(start,end) # better random nunber generator
while not gmpy2.is_prime(i):
i +=1
return i
'''
Returns a safe prime within a start and end.
Safe prime is p = 2q+1 where q is prime
'''
def get_random_safe_prime(start,end):
i = random.randint(start,end) # better random nunber generator
while not (gmpy2.is_prime(i) and gmpy2.is_prime(gmpy2.t_div((i-1),2))):
i +=1
return i
'''
Returns all primes in (a,b]
only works up to 1,000,000,000
'''
def get_primes_in_range(a,b):
start = np.searchsorted(primes, a)+1
end = np.searchsorted(primes, b)+1
return primes[start:end].tolist()
'''
Returns a number between 1 and n that is relatively prime to n
'''
def get_relatively_prime_int(n):
guess = get_random_int(n)
while GCD(guess,n)!=1:
guess = get_random_int(n)
return guess
'''
Returns a number between 1 and sqrt(n) that is relatively prime to n
'''
def get_relatively_prime_int_small(n):
n_prime = gmpy2.isqrt(n)
guess = get_random_int(n_prime)
while GCD(guess, n) != 1:
guess = get_random_int(n_prime)
return guess
'''
Returns the GCD of two numbers
'''
def GCD(a, b):
return gmpy2.gcd(gmpy2.mpz(a), gmpy2.mpz(b))
'''
Breaks up p into n pieces that adds up to p mod M
'''
def getShares(p,n,M):
base = p/n
shares = [0]*n
for i in range(n-1):
shares[i]= random.randint(3,2**1023)
rest = sum(shares)%M
shares[-1] = p-rest
return shares
'''
Returns a random integer between 0 and n-1.
'''
def get_random_int(n):
i = random.randint(0,2**30) # better random nunber generator
return gmpy2.mpz_random(gmpy2.random_state(i), n)
'''
Returns (x^y) mod m
'''
def powmod(x, y, m):
return gmpy2.powmod(gmpy2.mpz(x), gmpy2.mpz(y), gmpy2.mpz(m))
'''
Returns (x*y) mod m
'''
def mulmod(x, y, m):
return mod(multiply(x, y), m)
'''
Multiply x * y
'''
def multiply(x, y):
return gmpy2.mul(gmpy2.mpz(x), gmpy2.mpz(y))
'''
Add x + y
'''
def add(x, y):
return gmpy2.add(gmpy2.mpz(x), gmpy2.mpz(y))
'''
Divide x / y
'''
def divide(x, y):
return gmpy2.t_div(gmpy2.mpz(x), gmpy2.mpz(y))
def floor_divide(x, y):
return gmpy2.f_div(gmpy2.mpz(x), gmpy2.mpz(y))
'''
Subtract x - y
'''
def subtract(x, y):
return gmpy2.sub(gmpy2.mpz(x), gmpy2.mpz(y))
'''
Calcaulte x mod m
'''
def mod(x, m):
remainder = gmpy2.t_mod(gmpy2.mpz(x), gmpy2.mpz(m))
# gmpy2.t_mod can return negative values, but we want positive ones.
if remainder < 0:
remainder = gmpy2.add(remainder, m)
return remainder
'''
[d_1, d_2...d_n] such that sum [] = d mod N
'''
def sum_genereator(d, n, N):
d_i = []
#choose random values for first n-1
for i in range(n-1):
d_i.append(get_random_int(N))
#last one must make sum to d
d_i.append(mod(subtract(d,1*reduce(add,d_i)), N))
assert mod(reduce(add, d_i), N) == mod(d,N)
return d_i
#########################################
# Subset Presigning Algorithm Helpers
#########################################
'''
Helper class (basically a struct) that stores the data that
a computer needs for the subset presigning algorithm.
'''
class PresigningData:
def __init__(self):
# All variables are named as they are in the paper pages 26 - 27.
self.lamdba_t_i = None
self.s_t_i = None
self.h_t_i = None
self.received_h_t_i = {} # maps id -> h_t_i for all k computers
self.sigma_I_t_i = None # signature on the dummy message
self.x_I = None
self.received_x_I = [] # contains tuples with (id, x_I) computed by other parties, length of array = k-1
self.D_I = None # will contain tuples of the form (x_I, [(id, h_t_i, c_prime_t_i)])
self.S_I_t_i = None # will contain simply s_t_i
#########################################
# Helpers for Generating N
#########################################
'''
Helper class for BGW Protocol data.
'''
class BGWData:
def __init__(self, M, p_i, q_i, l):
# All variables are as described in Section 4.3, pg 14-15
self.M = M
self.l = l
self.p_i = p_i
self.q_i = q_i
self.l = l # l = floor((n-1)/2) where n is the number of computers
self.a = [] # array of l random coefficients
self.b = [] # array of l random coefficients
self.c = [] # array of 2l random coefficients
self.f_i_j = {} # maps j -> f_i(j) where i is this computer
self.g_i_j = {}
self.h_i_j = {}
self.received_fgh = [] # array that stores the (f_i(j), g_i(j), h_i(j)) from every computer i, where j is this computer
self.n_j = None # the final share calculated
'''
Helper class (basically a struct) that stores the data that
a computer needs for Distributed Sieving section 5.2.1 in the paper.
'''
class PQData:
def __init__(self, _round, M, l):
# All variables are named as they are in the paper pages 17-18
self.round = _round # the round we are on, should start at 1
self.M = M # M = product of all prime numbers between n and B1
self.l = l # l = floor((n-1)/2)
self.a_i = None # random secret integer relatively prime to self.M
self.u = [] # 2d array where u[i][j] is u_{i,j} in the notation of the paper
self.v = [] # 2d array where v[i][j] is v_{i,j} in the notation of the paper
self.a = None # cummulative product of the a_i as we iterate through the protocol