Skip to content

Commit 7d4df68

Browse files
author
gefeili
committed
Refactor in SnovaEngine
1 parent d3ed581 commit 7d4df68

File tree

1 file changed

+36
-49
lines changed

1 file changed

+36
-49
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/snova/SnovaEngine.java

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ public void makeInvertibleByAddingAS(byte[] source, int off)
132132
return;
133133
}
134134

135-
136135
byte[] temp = new byte[l * l];
137136

138137
for (int a = 1; a < 16; a++)
@@ -166,29 +165,23 @@ private byte gf16Determinant(byte[] matrix, int off)
166165

167166
private byte determinant2x2(byte[] m, int off)
168167
{
169-
return gf16Add(
170-
gf16Mul(getGF16m(m, 0, off), getGF16m(m, 1, off + 1)),
171-
gf16Mul(getGF16m(m, 0, off + 1), getGF16m(m, 1, off)));
168+
return (byte)
169+
(gf16Mul(getGF16m(m, 0, off), getGF16m(m, 1, off + 1)) ^
170+
gf16Mul(getGF16m(m, 0, off + 1), getGF16m(m, 1, off)));
172171
}
173172

174173
private byte determinant3x3(byte[] m, int off, int i0, int i1, int i2)
175174
{
176-
return gf16Add(
177-
gf16Add(
178-
gf16Mul(getGF16m(m, 0, off + i0), gf16Add(
179-
gf16Mul(getGF16m(m, 1, off + i1), getGF16m(m, 2, off + i2)),
180-
gf16Mul(getGF16m(m, 1, off + i2), getGF16m(m, 2, off + i1))
181-
)),
182-
gf16Mul(getGF16m(m, 0, off + i1), gf16Add(
183-
gf16Mul(getGF16m(m, 1, off + i0), getGF16m(m, 2, off + i2)),
184-
gf16Mul(getGF16m(m, 1, off + i2), getGF16m(m, 2, off + i0))
185-
))
186-
),
187-
gf16Mul(getGF16m(m, 0, off + i2), gf16Add(
188-
gf16Mul(getGF16m(m, 1, off + i0), getGF16m(m, 2, off + i1)),
189-
gf16Mul(getGF16m(m, 1, off + i1), getGF16m(m, 2, off + i0))
190-
))
191-
);
175+
return (byte)(
176+
gf16Mul(getGF16m(m, 0, off + i0), (byte)(
177+
gf16Mul(getGF16m(m, 1, off + i1), getGF16m(m, 2, off + i2)) ^
178+
gf16Mul(getGF16m(m, 1, off + i2), getGF16m(m, 2, off + i1)))) ^
179+
gf16Mul(getGF16m(m, 0, off + i1), (byte)(
180+
gf16Mul(getGF16m(m, 1, off + i0), getGF16m(m, 2, off + i2)) ^
181+
gf16Mul(getGF16m(m, 1, off + i2), getGF16m(m, 2, off + i0)))) ^
182+
gf16Mul(getGF16m(m, 0, off + i2), (byte)(
183+
gf16Mul(getGF16m(m, 1, off + i0), getGF16m(m, 2, off + i1)) ^
184+
gf16Mul(getGF16m(m, 1, off + i1), getGF16m(m, 2, off + i0)))));
192185
}
193186

194187
private byte determinant4x4(byte[] m, int off)
@@ -501,7 +494,29 @@ void genABQP(MapGroup1 map1, byte[] pkSeed, byte[] fixedAbq)
501494

502495
if (params.isPkExpandShake())
503496
{
504-
snovaShake(pkSeed, prngOutput.length, prngOutput);
497+
final int SHAKE128_RATE = 168; // 1344-bit rate = 168 bytes
498+
long blockCounter = 0;
499+
int offset = 0;
500+
int remaining = prngOutput.length;
501+
byte[] counterBytes = new byte[8];
502+
SHAKEDigest shake = new SHAKEDigest(128);
503+
while (remaining > 0)
504+
{
505+
// Process seed + counter
506+
shake.update(pkSeed, 0, pkSeed.length);
507+
Pack.longToLittleEndian(blockCounter, counterBytes, 0);
508+
shake.update(counterBytes, 0, 8);
509+
510+
// Calculate bytes to generate in this iteration
511+
int bytesToGenerate = Math.min(remaining, SHAKE128_RATE);
512+
513+
// Generate output (XOF mode)
514+
shake.doFinal(prngOutput, offset, bytesToGenerate);
515+
516+
offset += bytesToGenerate;
517+
remaining -= bytesToGenerate;
518+
blockCounter++;
519+
}
505520
}
506521
else
507522
{
@@ -583,32 +598,4 @@ void genABQP(MapGroup1 map1, byte[] pkSeed, byte[] fixedAbq)
583598
MapGroup1.fillAlpha(fixedAbq, o * alpha * lsq * 3, map1.qAlpha2, (m - 3) * o * alpha * lsq);
584599
}
585600
}
586-
587-
public static void snovaShake(byte[] ptSeed, int outputBytes, byte[] out)
588-
{
589-
final int SHAKE128_RATE = 168; // 1344-bit rate = 168 bytes
590-
long blockCounter = 0;
591-
int offset = 0;
592-
int remaining = outputBytes;
593-
byte[] counterBytes = new byte[8];
594-
while (remaining > 0)
595-
{
596-
SHAKEDigest shake = new SHAKEDigest(128);
597-
598-
// Process seed + counter
599-
shake.update(ptSeed, 0, ptSeed.length);
600-
Pack.longToLittleEndian(blockCounter, counterBytes, 0);
601-
shake.update(counterBytes, 0, 8);
602-
603-
// Calculate bytes to generate in this iteration
604-
int bytesToGenerate = Math.min(remaining, SHAKE128_RATE);
605-
606-
// Generate output (XOF mode)
607-
shake.doFinal(out, offset, bytesToGenerate);
608-
609-
offset += bytesToGenerate;
610-
remaining -= bytesToGenerate;
611-
blockCounter++;
612-
}
613-
}
614601
}

0 commit comments

Comments
 (0)