From 7a6c9b5eb5a3d27be80c68eab583bd05e45bd310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Stro=CC=88mbergson?= Date: Thu, 23 Apr 2015 11:25:10 +0200 Subject: Adding a generator for tests to the modexp model and core. --- src/testgenerator/.classpath | 6 +++ src/testgenerator/.gitignore | 1 + src/testgenerator/.project | 17 ++++++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++++ .../modexp/testgenerator/TestGenerator.java | 19 +++++++ .../modexp/testgenerator/TestGeneratorBasic.java | 35 +++++++++++++ .../crypttech/modexp/testgenerator/TestVector.java | 23 ++++++++ .../org/crypttech/modexp/testgenerator/Util.java | 28 ++++++++++ .../modexp/testgenerator/format/GeneratorC.java | 61 ++++++++++++++++++++++ .../testgenerator/format/ModExpTestFormater.java | 26 +++++++++ 10 files changed, 227 insertions(+) create mode 100644 src/testgenerator/.classpath create mode 100644 src/testgenerator/.gitignore create mode 100644 src/testgenerator/.project create mode 100644 src/testgenerator/.settings/org.eclipse.jdt.core.prefs create mode 100644 src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGenerator.java create mode 100644 src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGeneratorBasic.java create mode 100644 src/testgenerator/src/org/crypttech/modexp/testgenerator/TestVector.java create mode 100644 src/testgenerator/src/org/crypttech/modexp/testgenerator/Util.java create mode 100644 src/testgenerator/src/org/crypttech/modexp/testgenerator/format/GeneratorC.java create mode 100644 src/testgenerator/src/org/crypttech/modexp/testgenerator/format/ModExpTestFormater.java (limited to 'src') diff --git a/src/testgenerator/.classpath b/src/testgenerator/.classpath new file mode 100644 index 0000000..fb565a5 --- /dev/null +++ b/src/testgenerator/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/testgenerator/.gitignore b/src/testgenerator/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/src/testgenerator/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/src/testgenerator/.project b/src/testgenerator/.project new file mode 100644 index 0000000..aa04832 --- /dev/null +++ b/src/testgenerator/.project @@ -0,0 +1,17 @@ + + + TestGenerator + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/testgenerator/.settings/org.eclipse.jdt.core.prefs b/src/testgenerator/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..7341ab1 --- /dev/null +++ b/src/testgenerator/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGenerator.java b/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGenerator.java new file mode 100644 index 0000000..af5850b --- /dev/null +++ b/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGenerator.java @@ -0,0 +1,19 @@ +package org.crypttech.modexp.testgenerator; + +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; + +import org.crypttech.modexp.testgenerator.format.GeneratorC; + +public class TestGenerator { + public static void main(String[] argv) throws Exception { + List vectors = new ArrayList(); + vectors.addAll(TestGeneratorBasic.getTestVectors()); + PrintWriter pw = new PrintWriter("../model/c/src/autogenerated_tests.c"); + try (GeneratorC genC = new GeneratorC(pw)) { + for (TestVector vector : vectors) + genC.format(vector); + } + } +} diff --git a/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGeneratorBasic.java b/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGeneratorBasic.java new file mode 100644 index 0000000..71b56ac --- /dev/null +++ b/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGeneratorBasic.java @@ -0,0 +1,35 @@ +package org.crypttech.modexp.testgenerator; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class TestGeneratorBasic { + public static final List getTestVectors() { + Random rng = new Random(0); //any static seed + ArrayList list = new ArrayList(); + + generateTestVectors(rng, list, 33, 2); + generateTestVectors(rng, list, 30, 1); + //generateTestVectors(rng, list, 32, 1); //will generate failing tests in C model + //generateTestVectors(rng, list, 31, 1); //will generate failing tests in C model + return list; + } + + private static void generateTestVectors(Random rng, + ArrayList list, int bitLength, int wordLength) { + for(int i = 0; i < 10; i++) { + final long seed = rng.nextLong(); + rng.setSeed(seed); + BigInteger m = BigInteger.probablePrime(bitLength, rng); + BigInteger x = BigInteger.probablePrime(bitLength, rng); + BigInteger e = BigInteger.probablePrime(bitLength, rng); + BigInteger z = x.modPow(e, m); + TestVector tv = Util.generateTestVector("BASIC", Long.toString(seed), wordLength, m, x, e, z); + list.add(tv); + } + } + + +} diff --git a/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestVector.java b/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestVector.java new file mode 100644 index 0000000..a9931c1 --- /dev/null +++ b/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestVector.java @@ -0,0 +1,23 @@ +package org.crypttech.modexp.testgenerator; + +public class TestVector { + public final String generator; + public final String seed; + public final int length; + public final int[] X; + public final int[] E; + public final int[] M; + public final int[] expected; + + public TestVector(String generator, String seed, int length, int[] x, + int[] e, int[] m, int[] expected) { + super(); + this.generator = generator; + this.seed = seed; + this.length = length; + X = x; + E = e; + M = m; + this.expected = expected; + } +} diff --git a/src/testgenerator/src/org/crypttech/modexp/testgenerator/Util.java b/src/testgenerator/src/org/crypttech/modexp/testgenerator/Util.java new file mode 100644 index 0000000..5fbe052 --- /dev/null +++ b/src/testgenerator/src/org/crypttech/modexp/testgenerator/Util.java @@ -0,0 +1,28 @@ +package org.crypttech.modexp.testgenerator; + +import java.math.BigInteger; + +public class Util { + public static TestVector generateTestVector(String generator, String seed, int length, + BigInteger m, BigInteger x, BigInteger e, BigInteger z) { + //System.out.printf("%s %s %s %s\n", m.toString(16), x.toString(16), e.toString(16), z.toString(16)); + int[] mi = toInt(length, m); + int[] xi = toInt(length, x); + int[] ei = toInt(length, e); + int[] zi = toInt(length, z); + return new TestVector(generator, seed, length, xi, ei, mi, zi); + } + + + public static int[] toInt(int length, BigInteger bi) { + byte[] ba = bi.toByteArray(); + int[] ia = new int[length]; + for (int j = ba.length-1; j >= 0; j--) { + int changeByte = ((ba.length-1-j)%4); + int jj = length -1 - (ba.length-1-j)/4; + ia[jj] |= (ba[j] & 0xff) << (changeByte*8); + } + return ia; + } + +} diff --git a/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/GeneratorC.java b/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/GeneratorC.java new file mode 100644 index 0000000..fbfd852 --- /dev/null +++ b/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/GeneratorC.java @@ -0,0 +1,61 @@ +package org.crypttech.modexp.testgenerator.format; + +import java.io.PrintWriter; + +import org.crypttech.modexp.testgenerator.TestVector; + +public class GeneratorC extends ModExpTestFormater { + private static final char LF = (char) 10; + + public GeneratorC(PrintWriter pw) { + super(pw); + out("#include " + LF); + out("#include " + LF); + out("#include \"montgomery_array.h\"" + LF); + out("#include \"bignum_uint32_t.h\"" + LF); + } + + StringBuilder footer = new StringBuilder(); + + @Override + public void format(TestVector testVector) { + String testname = ("autogenerated_" + testVector.generator + "_" + testVector.seed) + .replace("-", "M"); + footer.append(" ").append(testname).append("();").append(LF); + + StringBuilder sb = new StringBuilder(); + sb.append("void ").append(testname).append("(void) {").append(LF); + sb.append(" printf(\"=== ").append(testname).append(" ===\\n\");") + .append(LF); + appendCArray(sb, "X", testVector.X); + appendCArray(sb, "E", testVector.E); + appendCArray(sb, "M", testVector.M); + appendCArray(sb, "expected", testVector.expected); + int[] Z = new int[testVector.length]; + appendCArray(sb, "Z", Z); + sb.append(" mod_exp_array(").append(testVector.length) + .append(", X, E, M, Z);").append(LF); + sb.append(" assertArrayEquals(").append(testVector.length) + .append(", expected, Z);").append(LF); + sb.append("}").append(LF); + out(sb.toString()); + } + + private void appendCArray(StringBuilder sb, String arrayName, int[] array) { + sb.append(" uint32_t ").append(arrayName).append("[] = "); + sb.append("{ "); + for (int m : array) + sb.append(String.format("0x%08x, ", m)); + sb.replace(sb.length() - 2, sb.length(), " };"); + sb.append(LF); + } + + @Override + public void close() throws Exception { + out("void autogenerated_tests(void) {" + LF); + out(footer.toString()); + out("}" + LF); + super.close(); + } + +} diff --git a/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/ModExpTestFormater.java b/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/ModExpTestFormater.java new file mode 100644 index 0000000..c15ca88 --- /dev/null +++ b/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/ModExpTestFormater.java @@ -0,0 +1,26 @@ +package org.crypttech.modexp.testgenerator.format; + +import java.io.PrintWriter; + +import org.crypttech.modexp.testgenerator.TestVector; + +public abstract class ModExpTestFormater implements AutoCloseable { + private PrintWriter pw; + + public ModExpTestFormater(PrintWriter pw) { + this.pw = pw; + } + + public abstract void format(TestVector testVector); + + protected final void out(String s) { + pw.print(s); + } + + @Override + public void close() throws Exception { + pw.close(); + } + + +} -- cgit v1.2.3