Skip to content

Commit bf7c06a

Browse files
committed
Renaming in pqc.crypto.hqc
1 parent 7a324ba commit bf7c06a

4 files changed

Lines changed: 47 additions & 47 deletions

File tree

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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 };

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: 2 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();

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

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class HQCEngine
2727
private final int[] generatorPoly;
2828
private final int nMu;
2929
private final int pkSize;
30-
private final GF2PolynomialCalculator gf;
30+
private final GF2x gf2x;
3131
private final int rejectionThreshold;
3232
private final int cipherTextBytes;
3333

@@ -49,7 +49,7 @@ class HQCEngine
4949
this.N_BYTE = Utils.getByteSizeFromBitSize(n);
5050
this.N1N2_BYTE_64 = Utils.getByte64SizeFromBitSize(n1 * n2);
5151
this.N1N2_BYTE = Utils.getByteSizeFromBitSize(n1 * n2);
52-
this.gf = new GF2PolynomialCalculator(n);
52+
this.gf2x = new GF2x(n);
5353
this.rejectionThreshold = ((1 << 24) / n) * n;
5454
this.cipherTextBytes = N_BYTE + N1N2_BYTE + 16;
5555
}
@@ -69,9 +69,9 @@ void genKeyPair(byte[] pk, byte[] sk, SecureRandom secureRandom)
6969
// Randomly generate seeds for secret keys and public keys
7070
byte[] seedKem = new byte[SEED_BYTES]; // seedKem
7171
byte[] keypairSeed = new byte[SEED_BYTES << 1];
72-
long[] xLongBytes = gf.create();
73-
long[] yLongBytes = gf.create();
74-
long[] h = gf.create(); // s
72+
long[] xLongBytes = gf2x.create();
73+
long[] yLongBytes = gf2x.create();
74+
long[] h = gf2x.create(); // s
7575

7676
secureRandom.nextBytes(seedKem);
7777
Shake256RandomGenerator ctxKem = new Shake256RandomGenerator(seedKem, (byte)1);
@@ -87,16 +87,16 @@ void genKeyPair(byte[] pk, byte[] sk, SecureRandom secureRandom)
8787
vectSampleFixedWeight1(xLongBytes, ctxKem, w);
8888
System.arraycopy(keypairSeed, SEED_BYTES, pk, 0, SEED_BYTES);
8989
ctxKem.init(keypairSeed, SEED_BYTES, SEED_BYTES, (byte)1);
90-
gf.random(ctxKem, h);
91-
gf.mul(h, yLongBytes, h); // h is s as the output
92-
gf.addTo(xLongBytes, h); // h is s
90+
gf2x.random(ctxKem, h);
91+
gf2x.mul(h, yLongBytes, h); // h is s as the output
92+
gf2x.addTo(xLongBytes, h); // h is s
9393
Utils.fromLongArrayToByteArray(pk, SEED_BYTES, pk.length - SEED_BYTES, h);
9494
System.arraycopy(keypairSeed, 0, sk, pkSize, SEED_BYTES);
9595
System.arraycopy(pk, 0, sk, 0, pkSize);
9696
Arrays.clear(keypairSeed);
97-
gf.clear(xLongBytes);
98-
gf.clear(yLongBytes);
99-
gf.clear(h);
97+
gf2x.clear(xLongBytes);
98+
gf2x.clear(yLongBytes);
99+
gf2x.clear(h);
100100
}
101101

102102
/**
@@ -112,7 +112,7 @@ void encaps(byte[] u, byte[] v, byte[] kTheta, byte[] pk, byte[] salt, SecureRan
112112
// 1. Randomly generate m
113113
byte[] m = new byte[k];
114114
byte[] hashEkKem = new byte[SEED_BYTES];
115-
long[] u64 = gf.create();
115+
long[] u64 = gf2x.create();
116116
long[] v64 = new long[N1N2_BYTE_64];
117117

118118
secureRandom.nextBytes(m);
@@ -123,7 +123,7 @@ void encaps(byte[] u, byte[] v, byte[] kTheta, byte[] pk, byte[] salt, SecureRan
123123
pkeEncrypt(u64, v64, pk, m, kTheta, SEED_BYTES);
124124
Utils.fromLongArrayToByteArray(u, 0, u.length, u64);
125125
Utils.fromLongArrayToByteArray(v, 0, v.length, v64);
126-
gf.clear(u64);
126+
gf2x.clear(u64);
127127
Arrays.clear(v64);
128128
Arrays.clear(m);
129129
Arrays.clear(hashEkKem);
@@ -140,10 +140,10 @@ void encaps(byte[] u, byte[] v, byte[] kTheta, byte[] pk, byte[] salt, SecureRan
140140
int decaps(byte[] ss, byte[] ct, byte[] sk)
141141
{
142142
// Extract Y and Public Keys from sk
143-
long[] u64 = gf.create();
144-
long[] v64 = gf.create();
145-
long[] cKemPrimeU64 = gf.create(); // tmpLong
146-
long[] cKemPrimeV64 = gf.create(); // y
143+
long[] u64 = gf2x.create();
144+
long[] v64 = gf2x.create();
145+
long[] cKemPrimeU64 = gf2x.create(); // tmpLong
146+
long[] cKemPrimeV64 = gf2x.create(); // y
147147
byte[] hashEkKem = new byte[SEED_BYTES];
148148
byte[] kThetaPrime = new byte[32 + SEED_BYTES];
149149
byte[] mPrime = new byte[k];
@@ -158,9 +158,9 @@ int decaps(byte[] ss, byte[] ct, byte[] sk)
158158
Utils.fromByteArrayToLongArray(v64, ct, N_BYTE, N1N2_BYTE);
159159

160160
// cKemPrimeU64 is tmpLong
161-
gf.mul(cKemPrimeV64, u64, cKemPrimeU64);
161+
gf2x.mul(cKemPrimeV64, u64, cKemPrimeU64);
162162
vectTruncate(cKemPrimeU64);
163-
gf.addTo(v64, cKemPrimeU64);
163+
gf2x.addTo(v64, cKemPrimeU64);
164164

165165
ReedMuller.decode(tmp, cKemPrimeU64, n1, mulParam);
166166
ReedSolomon.decode(mPrime, tmp, n1, fft, delta, k, g);
@@ -173,17 +173,17 @@ int decaps(byte[] ss, byte[] ct, byte[] sk)
173173
pkeEncrypt(cKemPrimeU64, cKemPrimeV64, sk, mPrime, kThetaPrime, 32);
174174
hashGJ(kBar, 256, hashEkKem, sk, pkSize + SEED_BYTES, k, ct, 0, ct.length, (byte)3);
175175

176-
int result = (int)(gf.equalTo(u64, cKemPrimeU64) & gf.equalTo(v64, cKemPrimeV64));
176+
int result = (int)(gf2x.equalTo(u64, cKemPrimeU64) & gf2x.equalTo(v64, cKemPrimeV64));
177177

178178
for (int i = 0; i < k; i++)
179179
{
180180
ss[i] = (byte)(((ss[i] & result) ^ (kBar[i] & ~result)) & 0xff);
181181
}
182182

183-
gf.clear(u64);
184-
gf.clear(v64);
185-
gf.clear(cKemPrimeU64);
186-
gf.clear(cKemPrimeV64);
183+
gf2x.clear(u64);
184+
gf2x.clear(v64);
185+
gf2x.clear(cKemPrimeU64);
186+
gf2x.clear(cKemPrimeV64);
187187
Arrays.clear(hashEkKem);
188188
Arrays.clear(kThetaPrime);
189189
Arrays.clear(mPrime);
@@ -194,30 +194,30 @@ int decaps(byte[] ss, byte[] ct, byte[] sk)
194194

195195
private void pkeEncrypt(long[] u, long[] v, byte[] ekPke, byte[] m, byte[] theta, int thetaOff)
196196
{
197-
long[] e = gf.create(); // r2
198-
long[] tmp = gf.create(); // s, h1, h
197+
long[] e = gf2x.create(); // r2
198+
long[] tmp = gf2x.create(); // s, h1, h
199199
byte[] res = new byte[n1];
200200

201201
ReedSolomon.encode(res, m, n1, k, g, generatorPoly);
202202
ReedMuller.encode(v, res, n1, mulParam);
203203

204204
Shake256RandomGenerator randomGenerator = new Shake256RandomGenerator(ekPke, 0, SEED_BYTES, (byte)1);
205-
gf.random(randomGenerator, tmp);
205+
gf2x.random(randomGenerator, tmp);
206206

207207
randomGenerator.init(theta, thetaOff, SEED_BYTES, (byte)1);
208208
vectSampleFixedWeights2(randomGenerator, e, wr); // e is r2
209-
gf.mul(tmp, e, u); // e is r2
209+
gf2x.mul(tmp, e, u); // e is r2
210210
Utils.fromByteArrayToLongArray(tmp, ekPke, SEED_BYTES, pkSize - SEED_BYTES);
211-
gf.mul(tmp, e, tmp);
211+
gf2x.mul(tmp, e, tmp);
212212
vectSampleFixedWeights2(randomGenerator, e, wr);
213-
gf.addTo(e, tmp);
213+
gf2x.addTo(e, tmp);
214214
vectTruncate(tmp);
215215
Nat.xorTo64(N1N2_BYTE_64, tmp, v);
216216

217217
vectSampleFixedWeights2(randomGenerator, tmp, wr);// tmp is r1
218-
gf.addTo(tmp, u);
219-
gf.clear(e);
220-
gf.clear(tmp);
218+
gf2x.addTo(tmp, u);
219+
gf2x.clear(e);
220+
gf2x.clear(tmp);
221221
Arrays.clear(res);
222222
}
223223

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static void encode(byte[] codeWord, byte[] message, int n1, int paramK, int para
2222

2323
for (int j = 0; j < paramG; j++)
2424
{
25-
tmp[j] = GFCalculator.mul(gateValue, rsPoly[j]);
25+
tmp[j] = GF.mul(gateValue, rsPoly[j]);
2626
}
2727

2828
for (int j = n1 - paramK - 1; j > 0; j--)
@@ -98,7 +98,7 @@ private static void computeSyndromes(int[] syndromes, byte[] codeWord, int delta
9898
{
9999
for (int j = 1; j < n1; j++)
100100
{
101-
syndromes[i] ^= GFCalculator.mul(Utils.toUnsigned8bits(codeWord[j]), alpha[i][j - 1]);
101+
syndromes[i] ^= GF.mul(Utils.toUnsigned8bits(codeWord[j]), alpha[i][j - 1]);
102102
}
103103
syndromes[i] ^= Utils.toUnsigned8bits(codeWord[0]);
104104
}
@@ -121,11 +121,11 @@ private static int computeELP(int[] sigma, int[] syndromes, int delta)
121121
{
122122
System.arraycopy(sigma, 0, sigmaDup, 0, delta + 1);
123123
int degSigmaDup = degSigma;
124-
int dd = GFCalculator.div(d, dp);
124+
int dd = GF.div(d, dp);
125125

126126
for (int j = 1; j <= i + 1 && j <= delta; j++)
127127
{
128-
sigma[j] ^= GFCalculator.mul(dd, sigmaP[j]);
128+
sigma[j] ^= GF.mul(dd, sigmaP[j]);
129129
}
130130

131131
int degX = Utils.toUnsigned16Bits(i - pp);
@@ -155,7 +155,7 @@ private static int computeELP(int[] sigma, int[] syndromes, int delta)
155155

156156
for (int k = 1; k <= i + 1 && k <= delta; k++)
157157
{
158-
d ^= GFCalculator.mul(sigma[k], syndromes[i + 1 - k]);
158+
d ^= GF.mul(sigma[k], syndromes[i + 1 - k]);
159159
}
160160
}
161161
return degSigma;
@@ -179,7 +179,7 @@ private static void computeZx(int[] output, int[] sigma, int deg, int[] syndrome
179179
output[i] ^= (mask) & syndromes[i - 1];
180180
for (int j = 1; j < i; j++)
181181
{
182-
output[i] ^= (mask) & GFCalculator.mul(sigma[j], syndromes[i - j - 1]);
182+
output[i] ^= (mask) & GF.mul(sigma[j], syndromes[i - j - 1]);
183183
}
184184
}
185185
}
@@ -207,22 +207,22 @@ private static void computeErrors(int[] res, int[] zx, byte[] errorCompactSet, i
207207
{
208208
int temp1 = 1;
209209
int temp2 = 1;
210-
int inv = GFCalculator.inv(betaSet[i]);
210+
int inv = GF.inv(betaSet[i]);
211211
int invPow = 1;
212212

213213
for (int j = 1; j <= delta; j++)
214214
{
215-
invPow = GFCalculator.mul(invPow, inv);
216-
temp1 ^= GFCalculator.mul(invPow, zx[j]);
215+
invPow = GF.mul(invPow, inv);
216+
temp1 ^= GF.mul(invPow, zx[j]);
217217
}
218218

219219
for (int j = 1; j < delta; j++)
220220
{
221-
temp2 = GFCalculator.mul(temp2, 1 ^ GFCalculator.mul(inv, betaSet[(i + j) % delta]));
221+
temp2 = GF.mul(temp2, 1 ^ GF.mul(inv, betaSet[(i + j) % delta]));
222222
}
223223

224224
int mask1 = i < deltaCount1 ? 0xffff : 0;
225-
eSet[i] = mask1 & GFCalculator.div(temp1, temp2);
225+
eSet[i] = mask1 & GF.div(temp1, temp2);
226226
}
227227

228228
int deltaCount2 = 0;

0 commit comments

Comments
 (0)