@@ -15,66 +15,42 @@ class SnovaEngine
1515{
1616 private final SnovaParameters params ;
1717 private final int l ;
18+ private final int lsq ;
19+ private final int m ;
20+ private final int v ;
21+ private final int o ;
22+ private final int alpha ;
23+ private final int n ;
1824 final byte [][] S ;
1925 final int [][] xS ;
2026
2127 public SnovaEngine (SnovaParameters params )
2228 {
2329 this .params = params ;
2430 this .l = params .getL ();
25- int lsq = l * l ;
26- S = new byte [l ][lsq ];
27- xS = new int [l ][lsq ];
28- be_aI (S [0 ], 0 , (byte )1 );
29- beTheS (S [1 ]);
30- for (int index = 2 ; index < l ; ++index )
31- {
32- GF16Utils .gf16mMul (S [index - 1 ], S [1 ], S [index ], l );
33- }
34-
35- for (int index = 0 ; index < l ; ++index )
36- {
37- for (int ij = 0 ; ij < lsq ; ++ij )
38- {
39- xS [index ][ij ] = GF16Utils .gf16FromNibble (S [index ][ij ]);
40- }
41- }
31+ this .lsq = params .getLsq ();
32+ this .m = params .getM ();
33+ this .v = params .getV ();
34+ this .o = params .getO ();
35+ this .alpha = params .getAlpha ();
36+ this .n = params .getN ();
37+ S = SnovaParameters .sSet .get (l );
38+ xS = SnovaParameters .xSSet .get (l );
4239 }
4340
44- public void be_aI (byte [] target , int off , byte a )
41+ static void be_aI (byte [] target , int off , byte a , int l )
4542 {
46- // // Mask 'a' to ensure it's a valid 4-bit GF16 element
47- // a = (byte)(a & 0x0F);
43+ // Ensure 'a' iss a valid 4-bit GF16 element
4844 int l1 = l + 1 ;
4945 for (int i = 0 ; i < l ; ++i , off += l1 )
5046 {
5147 target [off ] = a ;
5248 }
5349 }
5450
55- private void beTheS (byte [] target )
56- {
57- // Set all elements to 8 - (i + j) in GF16 (4-bit values)
58- for (int i = 0 , il = 0 ; i < l ; ++i , il += l )
59- {
60- for (int j = 0 ; j < l ; ++j )
61- {
62- int value = 8 - (i + j );
63- target [il + j ] = (byte )(value & 0x0F ); // Mask to 4 bits
64- }
65- }
66-
67- // Special case for rank 5
68- if (l == 5 )
69- {
70- target [24 ] = (byte )9 ; // Set (4,4) to 9
71- }
72- }
73-
7451 // Constant-time GF16 matrix generation
7552 public void genAFqSCT (byte [] c , int cOff , byte [] ptMatrix )
7653 {
77- int lsq = l * l ;
7854 int [] xTemp = new int [lsq ];
7955 int l1 = l + 1 ;
8056 // Initialize diagonal with c[0]
@@ -102,11 +78,6 @@ public void genAFqSCT(byte[] c, int cOff, byte[] ptMatrix)
10278 for (int ij = 0 ; ij < lsq ; ij ++)
10379 {
10480 xTemp [ij ] ^= cX * xS [l - 1 ][ij ];
105- }
106-
107- // Convert to nibbles and clear temp
108- for (int ij = 0 ; ij < lsq ; ij ++)
109- {
11081 ptMatrix [ij ] = GF16Utils .gf16ToNibble (xTemp [ij ]);
11182 }
11283 Arrays .fill (xTemp , 0 ); // Secure clear
@@ -298,7 +269,7 @@ private byte determinant5x5(byte[] m, int off)
298269
299270 private void generateASMatrixTo (byte [] target , int off , byte a )
300271 {
301- for (int i = 0 ; i < l ; i ++)
272+ for (int i = 0 , ixl = off ; i < l ; i ++, ixl += l )
302273 {
303274 for (int j = 0 ; j < l ; j ++)
304275 {
@@ -307,15 +278,15 @@ private void generateASMatrixTo(byte[] target, int off, byte a)
307278 {
308279 coefficient = 9 ;
309280 }
310- target [i * l + j + off ] ^= GF16 .mul (coefficient , a );
281+ target [ixl + j ] ^= GF16 .mul (coefficient , a );
311282 }
312283 }
313284 }
314285
315286 public void genAFqS (byte [] c , int cOff , byte [] ptMatrix , int off )
316287 {
317288 // Initialize with be_aI
318- be_aI (ptMatrix , off , c [cOff ]);
289+ be_aI (ptMatrix , off , c [cOff ], l );
319290
320291 // Process middle terms
321292 for (int i = 1 ; i < l - 1 ; ++i )
@@ -341,12 +312,6 @@ private void gf16mScaleTo(byte[] a, byte k, byte[] c, int cOff)
341312
342313 public void genF (MapGroup2 map2 , MapGroup1 map1 , byte [][][] T12 )
343314 {
344- int m = params .getM ();
345- int v = params .getV ();
346- int o = params .getO ();
347- int l = params .getL ();
348- int lsq = l * l ;
349-
350315 // Copy initial matrices
351316 copy4DMatrix (map1 .p11 , map2 .f11 , m , v , v , lsq );
352317 copy4DMatrix (map1 .p12 , map2 .f12 , m , v , o , lsq );
@@ -386,12 +351,6 @@ private static void copy4DMatrix(byte[][][][] src, byte[][][][] dest, int dim1,
386351
387352 public void genP22 (byte [] outP22 , byte [][][] T12 , byte [][][][] P21 , byte [][][][] F12 )
388353 {
389- int m = params .getM ();
390- int o = params .getO ();
391- int v = params .getV ();
392- int l = params .getL ();
393- int lsq = l * l ;
394-
395354 // Initialize P22 with zeros
396355 byte [] P22 = new byte [m * o * o * lsq ];
397356
@@ -420,8 +379,8 @@ public void genP22(byte[] outP22, byte[][][] T12, byte[][][][] P21, byte[][][][]
420379
421380 void genSeedsAndT12 (byte [][][] T12 , byte [] skSeed )
422381 {
423- int bytesPrngPrivate = (params . getV () * params . getO () * params . getL () + 1 ) >>> 1 ;
424- int gf16sPrngPrivate = params . getV () * params . getO () * params . getL () ;
382+ int bytesPrngPrivate = (v * o * l + 1 ) >>> 1 ;
383+ int gf16sPrngPrivate = v * o * l ;
425384 byte [] prngOutput = new byte [bytesPrngPrivate ];
426385
427386 // Generate PRNG output using SHAKE-256
@@ -435,10 +394,9 @@ void genSeedsAndT12(byte[][][] T12, byte[] skSeed)
435394
436395 // Generate T12 matrices
437396 int ptArray = 0 ;
438- int l = params .getL ();
439- for (int j = 0 ; j < params .getV (); j ++)
397+ for (int j = 0 ; j < v ; j ++)
440398 {
441- for (int k = 0 ; k < params . getO () ; k ++)
399+ for (int k = 0 ; k < o ; k ++)
442400 {
443401 //gen_a_FqS_ct
444402 genAFqSCT (gf16PrngOutput , ptArray , T12 [j ][k ]);
@@ -449,14 +407,6 @@ void genSeedsAndT12(byte[][][] T12, byte[] skSeed)
449407
450408 void genABQP (MapGroup1 map1 , byte [] pkSeed , byte [] fixedAbq )
451409 {
452- int l = params .getL ();
453- int lsq = l * l ;
454- int m = params .getM ();
455- int alpha = params .getAlpha ();
456- int v = params .getV ();
457- int o = params .getO ();
458- int n = v + o ;
459-
460410 int gf16sPrngPublic = lsq * (2 * m * alpha + m * (n * n - m * m )) + l * 2 * m * alpha ;
461411 byte [] qTemp = new byte [(m * alpha * lsq + m * alpha * lsq ) / l ];
462412 byte [] prngOutput = new byte [(gf16sPrngPublic + 1 ) >> 1 ];
@@ -500,7 +450,6 @@ void genABQP(MapGroup1 map1, byte[] pkSeed, byte[] fixedAbq)
500450 ctrCipher .init (true , params );
501451 int blockSize = ctrCipher .getBlockSize (); // typically 16 bytes
502452 byte [] zeroBlock = new byte [blockSize ]; // block of zeros
503- byte [] blockOut = new byte [blockSize ];
504453
505454 int offset = 0 ;
506455 // Process full blocks
@@ -512,21 +461,21 @@ void genABQP(MapGroup1 map1, byte[] pkSeed, byte[] fixedAbq)
512461 // Process any remaining partial block.
513462 if (offset < prngOutput .length )
514463 {
515- ctrCipher .processBlock (zeroBlock , 0 , blockOut , 0 );
464+ ctrCipher .processBlock (zeroBlock , 0 , zeroBlock , 0 );
516465 int remaining = prngOutput .length - offset ;
517- System .arraycopy (blockOut , 0 , prngOutput , offset , remaining );
466+ System .arraycopy (zeroBlock , 0 , prngOutput , offset , remaining );
518467 }
519468 }
520- // if ((lsq & 1) == 0)
521- // {
522- // map1.decode(prngOutput, params, (gf16sPrngPublic - qTemp.length) >> 1);
523- // }
524- // else
525- // {
526- byte [] temp = new byte [gf16sPrngPublic - qTemp .length ];
527- GF16 .decode (prngOutput , temp , temp .length );
528- map1 .fill (temp );
529- // }
469+ if ((lsq & 1 ) == 0 )
470+ {
471+ map1 .decode (prngOutput , params , (gf16sPrngPublic - qTemp .length ) >> 1 );
472+ }
473+ else
474+ {
475+ byte [] temp = new byte [gf16sPrngPublic - qTemp .length ];
476+ GF16 .decode (prngOutput , temp , temp .length );
477+ map1 .fill (temp );
478+ }
530479 if (l >= 4 )
531480 {
532481 GF16 .decode (prngOutput , (gf16sPrngPublic - qTemp .length ) >> 1 , qTemp , 0 , qTemp .length );
0 commit comments