-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRlweSample.java
42 lines (32 loc) · 1.09 KB
/
RlweSample.java
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
package rlwe;
/*************************************************************************************************
*
* Implements sampling for use with the RLWE Key Exchange.
*
*************************************************************************************************/
import java.math.BigInteger;
import java.util.Random;
class Sample {
static final int BINOMIAL_ITERATIONS = 16;
public static RingElt getSample () {
int i, j, b0, b1, offset, m = RingElt.getLength();
long[] s = new long[m];
Random rand = new Random ();
int numbits = m * BINOMIAL_ITERATIONS;
BigInteger randbits0 = new BigInteger (numbits, rand);
BigInteger randbits1 = new BigInteger (numbits, rand);
for (i = 0; i < m; i++) {
offset = i * BINOMIAL_ITERATIONS;
for (j = 0; j < BINOMIAL_ITERATIONS; j++) {
b0 = boolToInt (randbits0.testBit (offset + j));
b1 = boolToInt (randbits1.testBit (offset + j));
s[i] += (long) b1 - b0;
}
}
return new RingElt (s);
}
private static int boolToInt (boolean b) {
if (b) return 1;
return 0;
}
}