-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe_dirichlet_sieve.c
287 lines (227 loc) · 8.46 KB
/
safe_dirichlet_sieve.c
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <string.h>
#include "primes.h"
#include "safe_dirichlet_sieve.h"
/*
function: implements the safe Dirichlet sieve, where if it=0 outputs n = z*mr + a where n in [2^(k-1) + 2^(k-2), 2^k - 1], else outputs n = n+mr (n+n0)
The safe implementation differs in the fact that z must be even. Additionally 'a' must be in [2,mr-1], odd and coprime to mr. (a-1)/2 must be corpime to mr.
n = z*mr+a in [2^(k-1) + 2^(k-2), 2^k -1], z = 2*u
n = u*mr in [(2^(k-1)+ 2^(k-2) -a)/2, (2^k -(a+1))/2] where all interval values are divisible by mr
We therefore generate a number in the above interval and check for divisibility by mr. If it isn't divisible, shift to the next divisible number by mr.
As bn_rand_range only generates numbers in [0, x-1], we must apply some transformations to return numbers from our desired interval.
arguments: sieve = not used, sieve_sz = not used, n {returned if success} = z*mr+1 or n=n+mr (when it!=0) , n0 = mr (product of first r odd primes), r = number of primes to do trial division with, it = iterator variable, k = bitsize
returns: 1 if successful, 0 if failure, -1 if error
*/
int safe_dirichlet_sieve(unsigned short *sieve, int sieve_sz, BIGNUM *n, BIGNUM *n0, int r, unsigned long *it, int k){
int ret = 0;
if(*it == 0){
// create buffer for internal computations
BN_CTX *ctx;
ctx = BN_CTX_new();
// initialize constant '1'
BIGNUM *bn_one;
bn_one = BN_new();
// initialize constant '2'
BIGNUM *bn_two;
bn_two = BN_new();
// initialize a
BIGNUM *bn_a;
bn_a = BN_new();
// holds gcd value
BIGNUM *bn_gcd;
bn_gcd = BN_new();
// initialize lower interval bound = 2^(k-1) + 1, will first be initialized as 2^(k-1)
BIGNUM *bn_lw;
bn_lw = BN_new();
// initialize upper interval bound = 2^k
BIGNUM *bn_up;
bn_up = BN_new();
// initialize value for shifting rng interval = 2^(k-1) - a
BIGNUM *bn_shift_interval;
bn_shift_interval = BN_new();
// initialize bn holding remainder
BIGNUM *rem;
rem = BN_new();
// holds remainder of gcd(mr, (a-1)/2)
BIGNUM *rem_sub;
rem_sub = BN_new();
if(!BN_set_word(bn_one, (BN_ULONG) 1)){ // = 1
ret = -1;
goto free_bn;
}
if(!BN_set_word(bn_two, (BN_ULONG) 2)){ // = 2
ret = -1;
goto free_bn;
}
// this do-while loop generates odd 'a' in [2,mr-1] relatively prime to mr (gcd(mr,a) = 1) where additionally (a-1)/2 coprime to mr
do{
if(!BN_sub(bn_lw, n0, bn_two)){ // use bn_lw to hold mr-2
ret = -1;
goto free_bn;
}
do{
if(!BN_rand_range(bn_a, bn_lw)){ // generate number in [0, mr-3]
ret = -1;
goto free_bn;
}
if(!BN_add(bn_a, bn_a, bn_two)){ // bn_a in [2, mr-1]
ret = -1;
goto free_bn;
}
}while(!BN_is_odd(bn_a));
// postcondition: 'a' odd in [2, mr-1]
// if a is odd then right shifting by 1 is equivalent to subtracting 1 and right shifting by 1
if(!BN_rshift1(rem_sub, bn_a)){
ret = -1;
goto free_bn;
}
if(!BN_gcd(bn_gcd, n0, bn_a, ctx)){ // compute gcd(n0, bn_a)
ret = -1;
goto free_bn;
}
if(!BN_gcd(rem_sub, n0, rem_sub, ctx)){ // compute gcd(n0, rem_sub)
ret = -1;
goto free_bn;
}
}while(!BN_is_one(bn_gcd) || !BN_is_one(rem_sub)); // check gcd values
//post-cond: 2 <= a < mr and (a-1)/2 is relatively prime to mr, 'a' odd
// set the values
if(!BN_set_word(bn_lw, (BN_ULONG) 0)){ // reset bn_lw to = 0
ret = -1;
goto free_bn;
}
if(!BN_set_word(bn_up, (BN_ULONG) 0)){ // = 0
ret = -1;
goto free_bn;
}
if(!BN_set_bit(bn_lw, (k-1))){ // = 2^(k-1)
ret = -1;
goto free_bn;
}
if(!BN_set_bit(bn_up, (k-2))){ // use bn_up to temporarily hold 2^(k-2)
ret = -1;
goto free_bn;
}
if(!BN_add(bn_lw, bn_lw, bn_up)){ // = 2^(k-1) + 2^(k-2)
ret = -1;
goto free_bn;
}
if(!BN_sub(bn_shift_interval, bn_lw, bn_a)){ // = 2^(k-1) + 2^(k-2) - a, note: this number is always odd
ret = -1;
goto free_bn;
}
if(!BN_rshift1(bn_shift_interval, bn_shift_interval)){ // = (2^(k-1) + 2^(k-2) - a)/2
ret = -1;
goto free_bn;
}
if(!BN_add(bn_lw, bn_lw, bn_one)){ // = 2^(k-1) + 2^(k-2) + 1
ret = -1;
goto free_bn;
}
if(!BN_set_word(bn_up, (BN_ULONG) 0)){ // = 0
ret = -1;
goto free_bn;
}
if(!BN_set_bit(bn_up, k)){ // = 2^k
ret = -1;
goto free_bn;
}
if(!BN_sub(bn_up, bn_up, bn_lw)){ // = 2^k - (2^(k-1) + 2^(k-2) + 1)
ret = -1;
goto free_bn;
}
if(!BN_rshift1(bn_up, bn_up)){ // = (2^k - (2^(k-1) + 2^(k-2) + 1)) / 2
ret = -1;
goto free_bn;
}
if(!BN_rand_range(n, bn_up)){ // generate number in [0, (2^k - (2^(k-1) + 2^(k-2) + 1)) / 2]
ret = -1;
goto free_bn;
}
// precondition: n is out of interval [0, (2^k - (2^(k-1) + 2^(k-2) + 1)) / 2]
if(!BN_add(n, n, bn_shift_interval)){ // shift the interval from [0, (2^k - (2^(k-1) + 2^(k-2) + 1)) / 2] to [(2^(k-1) + 2^(k-2) - a) / 2, (2^k - 1 - a) / 2]
ret = -1;
goto free_bn;
}
if(!BN_mod(rem, n, n0, ctx)){ // get remainder of n/mr, s.t. we have a u for which holds: n = u*mr
ret = -1;
goto free_bn;
}
if(!BN_sub(n, n, rem)){ // subtract the remainder, s.t. n divisible by mr
ret = -1;
goto free_bn;
}
//edge case: If n < (2^(k-1)-a)/2 because of the subtraction of the remainder just above, then add mr back
if(BN_cmp(bn_shift_interval, n) == 1){
if(!BN_add(n, n, n0)){
ret = -1;
goto free_bn;
}
}
// postcondition: n is out of interval [(2^(k-1) + 2^(k-2) - a) / 2, (2^k - 1 - a) / 2] AND n is divisible by mr which implies n = u*mr for some u
// we now multiply by 2 to retrieve: n = 2*u*mr = z*mr in [(2^(k-1) + 2^(k-2) - a), (2^k - 1 - a)]
if(!BN_lshift1(n, n)){
ret = -1;
goto free_bn;
}
if(!BN_add(n, n, bn_a)){ // construct n = z*mr + a, with z = 2*u
ret = -1;
goto free_bn;
}
*it = 1; // set it=1 to signal that next iteration should just add mr
ret = 1;
// free all bignums used
free_bn:
BN_free(bn_one);
BN_free(bn_two);
BN_free(bn_a);
BN_free(bn_gcd);
BN_free(bn_lw);
BN_free(bn_up);
BN_free(bn_shift_interval);
BN_free(rem);
BN_free(rem_sub);
BN_CTX_free(ctx);
}else{
ret = 1;
if(!BN_add(n, n, n0)){ // n = n+mr
ret -1;
}
}
return ret;
}
/*
function: calculates mr for usage in the dirichlet sieve
arguments: sieve = not used, sieve_sz = not used, n0 = mr if successful, r = number of primes to do trial division with
returns: 1 if successful, 0 if failure, -1 if error
*/
int safe_dirichlet_generate_sieve(unsigned short *sieve, int sieve_sz, BIGNUM *n0, int r){
int ret = 0;
// create buffer for internal computations
BN_CTX *ctx;
ctx = BN_CTX_new();
// init n0 to 1
BIGNUM *bn_prime;
bn_prime = BN_new();
if(!BN_set_word(n0, (BN_ULONG) 1)){
ret = -1;
goto free_bn;
}
// compute product of first r-1 odd primes
for(int i=1; i<r; i++){
if(!BN_set_word(bn_prime, (BN_ULONG) primes[i])){
ret = -1;
goto free_bn;
}
if(!BN_mul(n0, n0, bn_prime, ctx)){
ret = -1;
goto free_bn;
}
}
ret = 1;
free_bn:
BN_CTX_free(ctx);
BN_free(bn_prime);
return ret;
}