Skip to content

Commit 15719fb

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents c9e0970 + df9de32 commit 15719fb

18 files changed

Lines changed: 257 additions & 235 deletions

File tree

core/src/main/java/org/bouncycastle/crypto/generators/Argon2BytesGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ private void xorWith(Block b1, Block b2)
675675
}
676676
}
677677

678-
public Block clear()
678+
private Block clear()
679679
{
680680
Arrays.fill(v, 0);
681681
return this;

core/src/main/java/org/bouncycastle/pqc/crypto/hqc/FastFourierTransform.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void fastFourierTransform(int[] output, int[] elements, int noCoefs, int
2929
for (int i = 0; i < m - 1; i++)
3030
{
3131
int beta_i = betas[i];
32-
deltas[i] = GFCalculator.sqr(beta_i) ^ beta_i;
32+
deltas[i] = GF.sqr(beta_i) ^ beta_i;
3333
}
3434

3535
// Step 5:
@@ -47,7 +47,7 @@ static void fastFourierTransform(int[] output, int[] elements, int noCoefs, int
4747

4848
for (int i = 1; i < k; i++)
4949
{
50-
int ti = u[i] ^ GFCalculator.mul(betaSum[i], v[i]);
50+
int ti = u[i] ^ GF.mul(betaSum[i], v[i]);
5151
output[i] = ti;
5252
output[k + i] ^= ti;
5353
}
@@ -178,7 +178,7 @@ static void computeFFTRec(int[] output, int[] func, int noCoeffs, int noOfBetas,
178178
{
179179
for (int i = 0; i < noOfBetas; i++)
180180
{
181-
tempSet[i] = GFCalculator.mul(betaSet[i], func[1]);
181+
tempSet[i] = GF.mul(betaSet[i], func[1]);
182182
}
183183

184184
output[0] = func[0];
@@ -200,18 +200,18 @@ static void computeFFTRec(int[] output, int[] func, int noCoeffs, int noOfBetas,
200200
int x = 1 << noCoeffsPlus;
201201
for (int i = 1; i < x; i++)
202202
{
203-
betaMPow = GFCalculator.mul(betaMPow, betaSet[noOfBetas - 1]);
204-
func[i] = GFCalculator.mul(betaMPow, func[i]);
203+
betaMPow = GF.mul(betaMPow, betaSet[noOfBetas - 1]);
204+
func[i] = GF.mul(betaMPow, func[i]);
205205
}
206206
}
207207

208208
computeRadix(fx0, fx1, func, noCoeffsPlus, fft);
209209

210210
for (int i = 0; i < noOfBetas - 1; i++)
211211
{
212-
int gamma_i = GFCalculator.div(betaSet[i], betaSet[noOfBetas - 1]);
212+
int gamma_i = GF.div(betaSet[i], betaSet[noOfBetas - 1]);
213213
gammaSet[i] = gamma_i;
214-
deltaSet[i] = GFCalculator.sqr(gamma_i) ^ gamma_i;
214+
deltaSet[i] = GF.sqr(gamma_i) ^ gamma_i;
215215
}
216216

217217
computeSubsetSum(gammaSumSet, gammaSet, noOfBetas - 1);
@@ -225,7 +225,7 @@ static void computeFFTRec(int[] output, int[] func, int noCoeffs, int noOfBetas,
225225
output[k] = uSet[0] ^ fx1[0];
226226
for (int i = 1; i < k; i++)
227227
{
228-
int ti = uSet[i] ^ GFCalculator.mul(gammaSumSet[i], fx1[0]);
228+
int ti = uSet[i] ^ GF.mul(gammaSumSet[i], fx1[0]);
229229
output[i] = ti;
230230
output[k + i] = ti ^ fx1[0];
231231
}
@@ -240,7 +240,7 @@ static void computeFFTRec(int[] output, int[] func, int noCoeffs, int noOfBetas,
240240
output[k] ^= uSet[0];
241241
for (int i = 1; i < k; i++)
242242
{
243-
int ti = uSet[i] ^ GFCalculator.mul(gammaSumSet[i], vSet[i]);
243+
int ti = uSet[i] ^ GF.mul(gammaSumSet[i], vSet[i]);
244244
output[i] = ti;
245245
output[k + i] ^= ti;
246246
}

core/src/main/java/org/bouncycastle/pqc/crypto/hqc/GFCalculator.java renamed to core/src/main/java/org/bouncycastle/pqc/crypto/hqc/GF.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,52 @@
11
package org.bouncycastle.pqc.crypto.hqc;
22

3-
class GFCalculator
3+
class GF
44
{
55
// NB: _LOG[0] and _EXP[255] are both dummy values that map to each other for consistency
66
private static final int[] _EXP = new int[]{ 1, 2, 4, 8, 16, 32, 64, 128, 29, 58, 116, 232, 205, 135, 19, 38, 76, 152, 45, 90, 180, 117, 234, 201, 143, 3, 6, 12, 24, 48, 96, 192, 157, 39, 78, 156, 37, 74, 148, 53, 106, 212, 181, 119, 238, 193, 159, 35, 70, 140, 5, 10, 20, 40, 80, 160, 93, 186, 105, 210, 185, 111, 222, 161, 95, 190, 97, 194, 153, 47, 94, 188, 101, 202, 137, 15, 30, 60, 120, 240, 253, 231, 211, 187, 107, 214, 177, 127, 254, 225, 223, 163, 91, 182, 113, 226, 217, 175, 67, 134, 17, 34, 68, 136, 13, 26, 52, 104, 208, 189, 103, 206, 129, 31, 62, 124, 248, 237, 199, 147, 59, 118, 236, 197, 151, 51, 102, 204, 133, 23, 46, 92, 184, 109, 218, 169, 79, 158, 33, 66, 132, 21, 42, 84, 168, 77, 154, 41, 82, 164, 85, 170, 73, 146, 57, 114, 228, 213, 183, 115, 230, 209, 191, 99, 198, 145, 63, 126, 252, 229, 215, 179, 123, 246, 241, 255, 227, 219, 171, 75, 150, 49, 98, 196, 149, 55, 110, 220, 165, 87, 174, 65, 130, 25, 50, 100, 200, 141, 7, 14, 28, 56, 112, 224, 221, 167, 83, 166, 81, 162, 89, 178, 121, 242, 249, 239, 195, 155, 43, 86, 172, 69, 138, 9, 18, 36, 72, 144, 61, 122, 244, 245, 247, 243, 251, 235, 203, 139, 11, 22, 44, 88, 176, 125, 250, 233, 207, 131, 27, 54, 108, 216, 173, 71, 142, 0 };
7-
private static final int[] _INV = new int[]{ 0, 0, 142, 244, 71, 167, 122, 186, 173, 157, 221, 152, 61, 170, 93, 150, 216, 114, 192, 88, 224, 62, 76, 102, 144, 222, 85, 128, 160, 131, 75, 42, 108, 237, 57, 81, 96, 86, 44, 138, 112, 208, 31, 74, 38, 139, 51, 110, 72, 137, 111, 46, 164, 195, 64, 94, 80, 34, 207, 169, 171, 12, 21, 225, 54, 95, 248, 213, 146, 78, 166, 4, 48, 136, 43, 30, 22, 103, 69, 147, 56, 35, 104, 140, 129, 26, 37, 97, 19, 193, 203, 99, 151, 14, 55, 65, 36, 87, 202, 91, 185, 196, 23, 77, 82, 141, 239, 179, 32, 236, 47, 50, 40, 209, 17, 217, 233, 251, 218, 121, 219, 119, 6, 187, 132, 205, 254, 252, 27, 84, 161, 29, 124, 204, 228, 176, 73, 49, 39, 45, 83, 105, 2, 245, 24, 223, 68, 79, 155, 188, 15, 92, 11, 220, 189, 148, 172, 9, 199, 162, 28, 130, 159, 198, 52, 194, 70, 5, 206, 59, 13, 60, 156, 8, 190, 183, 135, 229, 238, 107, 235, 242, 191, 175, 197, 100, 7, 123, 149, 154, 174, 182, 18, 89, 165, 53, 101, 184, 163, 158, 210, 247, 98, 90, 133, 125, 168, 58, 41, 113, 200, 246, 249, 67, 215, 214, 16, 115, 118, 120, 153, 10, 25, 145, 20, 63, 230, 240, 134, 177, 226, 241, 250, 116, 243, 180, 109, 33, 178, 106, 227, 231, 181, 234, 3, 143, 211, 201, 66, 212, 232, 117, 127, 255, 126, 253 };
7+
private static final int[] _INV = new int[]{ 0, 1, 142, 244, 71, 167, 122, 186, 173, 157, 221, 152, 61, 170, 93, 150, 216, 114, 192, 88, 224, 62, 76, 102, 144, 222, 85, 128, 160, 131, 75, 42, 108, 237, 57, 81, 96, 86, 44, 138, 112, 208, 31, 74, 38, 139, 51, 110, 72, 137, 111, 46, 164, 195, 64, 94, 80, 34, 207, 169, 171, 12, 21, 225, 54, 95, 248, 213, 146, 78, 166, 4, 48, 136, 43, 30, 22, 103, 69, 147, 56, 35, 104, 140, 129, 26, 37, 97, 19, 193, 203, 99, 151, 14, 55, 65, 36, 87, 202, 91, 185, 196, 23, 77, 82, 141, 239, 179, 32, 236, 47, 50, 40, 209, 17, 217, 233, 251, 218, 121, 219, 119, 6, 187, 132, 205, 254, 252, 27, 84, 161, 29, 124, 204, 228, 176, 73, 49, 39, 45, 83, 105, 2, 245, 24, 223, 68, 79, 155, 188, 15, 92, 11, 220, 189, 148, 172, 9, 199, 162, 28, 130, 159, 198, 52, 194, 70, 5, 206, 59, 13, 60, 156, 8, 190, 183, 135, 229, 238, 107, 235, 242, 191, 175, 197, 100, 7, 123, 149, 154, 174, 182, 18, 89, 165, 53, 101, 184, 163, 158, 210, 247, 98, 90, 133, 125, 168, 58, 41, 113, 200, 246, 249, 67, 215, 214, 16, 115, 118, 120, 153, 10, 25, 145, 20, 63, 230, 240, 134, 177, 226, 241, 250, 116, 243, 180, 109, 33, 178, 106, 227, 231, 181, 234, 3, 143, 211, 201, 66, 212, 232, 117, 127, 255, 126, 253 };
88
private static final int[] _LOG = new int[]{ 255, 0, 1, 25, 2, 50, 26, 198, 3, 223, 51, 238, 27, 104, 199, 75, 4, 100, 224, 14, 52, 141, 239, 129, 28, 193, 105, 248, 200, 8, 76, 113, 5, 138, 101, 47, 225, 36, 15, 33, 53, 147, 142, 218, 240, 18, 130, 69, 29, 181, 194, 125, 106, 39, 249, 185, 201, 154, 9, 120, 77, 228, 114, 166, 6, 191, 139, 98, 102, 221, 48, 253, 226, 152, 37, 179, 16, 145, 34, 136, 54, 208, 148, 206, 143, 150, 219, 189, 241, 210, 19, 92, 131, 56, 70, 64, 30, 66, 182, 163, 195, 72, 126, 110, 107, 58, 40, 84, 250, 133, 186, 61, 202, 94, 155, 159, 10, 21, 121, 43, 78, 212, 229, 172, 115, 243, 167, 87, 7, 112, 192, 247, 140, 128, 99, 13, 103, 74, 222, 237, 49, 197, 254, 24, 227, 165, 153, 119, 38, 184, 180, 124, 17, 68, 146, 217, 35, 32, 137, 46, 55, 63, 209, 91, 149, 188, 207, 205, 144, 135, 151, 178, 220, 252, 190, 97, 242, 86, 211, 171, 20, 42, 93, 158, 132, 60, 57, 83, 71, 109, 65, 162, 31, 45, 67, 216, 183, 123, 164, 118, 196, 23, 73, 236, 127, 12, 111, 246, 108, 161, 59, 82, 41, 157, 85, 170, 251, 96, 134, 177, 187, 204, 62, 90, 203, 89, 95, 176, 156, 169, 160, 81, 11, 245, 22, 235, 122, 117, 44, 215, 79, 174, 213, 233, 230, 231, 173, 232, 116, 214, 244, 234, 168, 80, 88, 175 };
99
private static final int[] _SQR = new int[]{ 0, 1, 4, 5, 16, 17, 20, 21, 64, 65, 68, 69, 80, 81, 84, 85, 29, 28, 25, 24, 13, 12, 9, 8, 93, 92, 89, 88, 77, 76, 73, 72, 116, 117, 112, 113, 100, 101, 96, 97, 52, 53, 48, 49, 36, 37, 32, 33, 105, 104, 109, 108, 121, 120, 125, 124, 41, 40, 45, 44, 57, 56, 61, 60, 205, 204, 201, 200, 221, 220, 217, 216, 141, 140, 137, 136, 157, 156, 153, 152, 208, 209, 212, 213, 192, 193, 196, 197, 144, 145, 148, 149, 128, 129, 132, 133, 185, 184, 189, 188, 169, 168, 173, 172, 249, 248, 253, 252, 233, 232, 237, 236, 164, 165, 160, 161, 180, 181, 176, 177, 228, 229, 224, 225, 244, 245, 240, 241, 19, 18, 23, 22, 3, 2, 7, 6, 83, 82, 87, 86, 67, 66, 71, 70, 14, 15, 10, 11, 30, 31, 26, 27, 78, 79, 74, 75, 94, 95, 90, 91, 103, 102, 99, 98, 119, 118, 115, 114, 39, 38, 35, 34, 55, 54, 51, 50, 122, 123, 126, 127, 106, 107, 110, 111, 58, 59, 62, 63, 42, 43, 46, 47, 222, 223, 218, 219, 206, 207, 202, 203, 158, 159, 154, 155, 142, 143, 138, 139, 195, 194, 199, 198, 211, 210, 215, 214, 131, 130, 135, 134, 147, 146, 151, 150, 170, 171, 174, 175, 186, 187, 190, 191, 234, 235, 238, 239, 250, 251, 254, 255, 183, 182, 179, 178, 167, 166, 163, 162, 247, 246, 243, 242, 231, 230, 227, 226 };
1010

1111
static int div(int a, int b)
1212
{
13-
// int ma = -a >> 31; // a != 0
14-
// int mb = -b >> 31; // b != 0
15-
// return ma & mb & _EXP[mod(HQCParameters.GF_MUL_ORDER + _LOG[a] - _LOG[b])];
1613
return mul(a, inv(b));
1714
}
1815

1916
static int inv(int a)
2017
{
21-
// int ma = -a >> 31; // a != 0
22-
// return ma & _EXP[HQCParameters.GF_MUL_ORDER - _LOG[a]];
2318
return _INV[a];
2419
}
2520

26-
static int mod(int a)
21+
private static int mod1(int a)
2722
{
28-
int t = a - HQCParameters.GF_MUL_ORDER;
29-
return t + ((t >> 31) & HQCParameters.GF_MUL_ORDER);
23+
return a + (a >>> 24);
24+
}
25+
26+
private static int mod2(int a)
27+
{
28+
return mod1(a - HQCParameters.GF_MUL_ORDER);
29+
}
30+
31+
private static int mod(int a)
32+
{
33+
return mod2((a & 0xFF) + (a >>> 8));
3034
}
3135

3236
static int mul(int a, int b)
3337
{
34-
int ma = -a >> 31; // a != 0
35-
int mb = -b >> 31; // b != 0
36-
return ma & mb & _EXP[mod(_LOG[a] + _LOG[b])];
38+
int m = (-a & -b) >> 31; // { a, b } != 0
39+
return m & _EXP[mod2(_LOG[a] + _LOG[b])];
40+
}
41+
42+
static int mul3(int a, int b, int c)
43+
{
44+
int m = (-a & -b & -c) >> 31; // { a, b, c } != 0
45+
return m & _EXP[mod(_LOG[a] + _LOG[b] + _LOG[c])];
3746
}
3847

3948
static int sqr(int a)
4049
{
41-
// int ma = -a >> 31; // a != 0
42-
// return ma & _EXP[mod(_LOG[a] * 2)];
4350
return _SQR[a];
4451
}
4552
}

core/src/main/java/org/bouncycastle/pqc/crypto/hqc/GF2PolynomialCalculator.java renamed to core/src/main/java/org/bouncycastle/pqc/crypto/hqc/GF2x.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import org.bouncycastle.util.Arrays;
55
import org.bouncycastle.util.Pack;
66

7-
class GF2PolynomialCalculator
7+
class GF2x
88
{
99
private final int bits;
1010
private final int size;
1111
private final int sizeExt;
1212

13-
GF2PolynomialCalculator(int n)
13+
GF2x(int n)
1414
{
1515
if ((n & 0xFFFF0001) != 1)
1616
throw new IllegalArgumentException();
@@ -25,6 +25,11 @@ void addTo(long[] x, long[] z)
2525
Nat.xorTo64(size, x, z);
2626
}
2727

28+
void clear(long[] z)
29+
{
30+
Nat.zero64(size, z);
31+
}
32+
2833
long[] create()
2934
{
3035
return new long[size];

0 commit comments

Comments
 (0)