Skip to content

Commit 48a3213

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents 8cef288 + 07a52b8 commit 48a3213

File tree

1 file changed

+11
-21
lines changed
  • core/src/main/java/org/bouncycastle/pqc/crypto/mlkem

1 file changed

+11
-21
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/mlkem/Poly.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -223,39 +223,29 @@ else if (engine.getKyberPolyCompressedBytes() == 160)
223223

224224
public byte[] toBytes()
225225
{
226+
conditionalSubQ();
227+
226228
byte[] r = new byte[MLKEMEngine.KyberPolyBytes];
227-
short t0, t1;
228-
this.conditionalSubQ();
229229
for (int i = 0; i < MLKEMEngine.KyberN / 2; i++)
230230
{
231-
t0 = this.getCoeffIndex(2 * i);
232-
t1 = this.getCoeffIndex(2 * i + 1);
233-
r[3 * i] = (byte)(t0 >> 0);
231+
short t0 = coeffs[2 * i + 0];
232+
short t1 = coeffs[2 * i + 1];
233+
r[3 * i + 0] = (byte)(t0 >> 0);
234234
r[3 * i + 1] = (byte)((t0 >> 8) | (t1 << 4));
235235
r[3 * i + 2] = (byte)(t1 >> 4);
236236
}
237-
238237
return r;
239-
240238
}
241239

242240
public void fromBytes(byte[] inpBytes)
243241
{
244-
int i;
245-
for (i = 0; i < MLKEMEngine.KyberN / 2; i++)
242+
for (int i = 0; i < MLKEMEngine.KyberN / 2; ++i)
246243
{
247-
this.setCoeffIndex(2 * i, (short)(
248-
(
249-
((inpBytes[3 * i + 0] & 0xFF) >> 0)
250-
| ((inpBytes[3 * i + 1] & 0xFF) << 8)
251-
) & 0xFFF)
252-
);
253-
this.setCoeffIndex(2 * i + 1, (short)(
254-
(
255-
((inpBytes[3 * i + 1] & 0xFF) >> 4)
256-
| (long)((inpBytes[3 * i + 2] & 0xFF) << 4)
257-
) & 0xFFF)
258-
);
244+
int a0 = inpBytes[3 * i + 0] & 0xFF;
245+
int a1 = inpBytes[3 * i + 1] & 0xFF;
246+
int a2 = inpBytes[3 * i + 2] & 0xFF;
247+
coeffs[2 * i + 0] = (short)(((a0 >> 0) | (a1 << 8)) & 0xFFF);
248+
coeffs[2 * i + 1] = (short)(((a1 >> 4) | (a2 << 4)) & 0xFFF);
259249
}
260250
}
261251

0 commit comments

Comments
 (0)