From 6850fa9083d5d51b3e2ae35a021962148b8f9878 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 12 Oct 2022 23:11:38 +0700 Subject: Skip redundant cipher init in Frodo-AES --- .../src/pqc/crypto/frodo/FrodoMatrixGenerator.cs | 30 ++------ crypto/test/src/pqc/crypto/test/FrodoVectorTest.cs | 81 ++++++++-------------- 2 files changed, 35 insertions(+), 76 deletions(-) diff --git a/crypto/src/pqc/crypto/frodo/FrodoMatrixGenerator.cs b/crypto/src/pqc/crypto/frodo/FrodoMatrixGenerator.cs index 468e00fbd..ce985c9ab 100644 --- a/crypto/src/pqc/crypto/frodo/FrodoMatrixGenerator.cs +++ b/crypto/src/pqc/crypto/frodo/FrodoMatrixGenerator.cs @@ -1,9 +1,7 @@ - - using System; + using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; -using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; @@ -62,17 +60,19 @@ namespace Org.BouncyCastle.Pqc.Crypto.Frodo internal class Aes128MatrixGenerator : FrodoMatrixGenerator { - BufferedBlockCipher cipher; + private readonly IBlockCipher m_cipher; public Aes128MatrixGenerator(int n, int q) : base(n, q) { - cipher = new BufferedBlockCipher(AesUtilities.CreateEngine()); - + m_cipher = AesUtilities.CreateEngine(); } internal override short[] GenMatrix(byte[] seedA) { + KeyParameter kp = new KeyParameter(seedA); + m_cipher.Init(true, kp); + // """Generate matrix A using AES-128 (FrodoKEM specification, Algorithm 7)""" // A = [[None for j in range(self.n)] for i in range(self.n)] short[] A = new short[n * n]; @@ -96,7 +96,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Frodo // struct.pack_into(' 0 && !sampler.SkipTest(buf["count"])) + if (buf.Count > 0) { - String count = buf["count"]; - if (!"0".Equals(count)) - { - // randomly skip tests after zero. - // if (rnd.nextBoolean()) - // { - // continue; - // } - } + string count = buf["count"]; + if (sampler.SkipTest(count)) + continue; + Console.Write($"test case: {count}"); byte[] seed = Hex.Decode(buf["seed"]); // seed for nist secure random -- cgit 1.4.1