aboutsummaryrefslogtreecommitdiff
path: root/src/testgenerator
diff options
context:
space:
mode:
Diffstat (limited to 'src/testgenerator')
-rw-r--r--src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGeneratorBasic.java13
-rw-r--r--src/testgenerator/src/org/crypttech/modexp/testgenerator/format/GeneratorVerilog.java109
-rw-r--r--src/testgenerator/src/org/crypttech/modexp/testgenerator/format/ModExpTestFormater.java1
3 files changed, 75 insertions, 48 deletions
diff --git a/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGeneratorBasic.java b/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGeneratorBasic.java
index 71b56ac..e78fea3 100644
--- a/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGeneratorBasic.java
+++ b/src/testgenerator/src/org/crypttech/modexp/testgenerator/TestGeneratorBasic.java
@@ -14,12 +14,22 @@ public class TestGeneratorBasic {
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
+ generateTestVectors(rng, list, 126, 4);
+ generateTestVectors(rng, list, 510, 16, 2);
+ generateTestVectors(rng, list, 1022, 32, 1);
+ generateTestVectors(rng, list, 2046, 64, 1);
return list;
}
private static void generateTestVectors(Random rng,
ArrayList<TestVector> list, int bitLength, int wordLength) {
- for(int i = 0; i < 10; i++) {
+ int max = 10;
+ generateTestVectors(rng, list, bitLength, wordLength, max);
+ }
+
+ private static void generateTestVectors(Random rng,
+ ArrayList<TestVector> list, int bitLength, int wordLength, int max) {
+ for(int i = 0; i < max; i++) {
final long seed = rng.nextLong();
rng.setSeed(seed);
BigInteger m = BigInteger.probablePrime(bitLength, rng);
@@ -28,6 +38,7 @@ public class TestGeneratorBasic {
BigInteger z = x.modPow(e, m);
TestVector tv = Util.generateTestVector("BASIC", Long.toString(seed), wordLength, m, x, e, z);
list.add(tv);
+ System.out.printf("%s Generated test: bits: %d seed: %x\n", TestGeneratorBasic.class.getName(), bitLength, seed);
}
}
diff --git a/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/GeneratorVerilog.java b/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/GeneratorVerilog.java
index 18efaa8..3a581da 100644
--- a/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/GeneratorVerilog.java
+++ b/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/GeneratorVerilog.java
@@ -14,7 +14,8 @@ import org.crypttech.modexp.testgenerator.TestVector;
public class GeneratorVerilog extends ModExpTestFormater {
private BufferedReader br;
private List<String> taskCalls = new ArrayList<String>();
- private boolean headerEmitted = false;
+ private boolean initilized = false;
+ private List<String> taskTemplate = new ArrayList<String>();
public GeneratorVerilog(String templateFile, String destinationFile)
throws FileNotFoundException {
@@ -26,56 +27,59 @@ public class GeneratorVerilog extends ModExpTestFormater {
@Override
public void format(TestVector testVector) {
- emitHeader();
+ init();
String testname = ("autogenerated_" + testVector.generator + "_" + testVector.seed)
.replace("-", "M");
taskCalls.add(testname + "();");
- out("task " + testname + "();");
- out("reg [31 : 0] read_data;");
- out("begin");
- out("tc_ctr = tc_ctr + 1;");
- out("$display(\"" + testname + "\");");
-
- for (int i = 0; i < testVector.X.length; i++)
- out("write_word({MESSAGE_PREFIX, 8'h%02x}, 32'h%08x);", i,
- testVector.X[i]);
-
- for (int i = 0; i < testVector.E.length; i++)
- out("write_word({EXPONENT_PREFIX, 8'h%02x}, 32'h%08x);", i,
- testVector.E[i]);
-
- out("write_word({GENERAL_PREFIX, ADDR_EXPONENT_LENGTH}, 32'h%08x););",
- testVector.E.length);
-
- for (int i = 0; i < testVector.M.length; i++)
- out("write_word({MODULUS_PREFIX, 8'h%02x}, 32'h%08x);", i,
- testVector.M[i]);
-
- out("write_word({GENERAL_PREFIX, ADDR_MODULUS_LENGTH}, 32'h%08x););",
- testVector.E.length);
-
- out("dump_memories()");
+ for (String line : taskTemplate) {
+ line = line.replace("TEMPLATE_TASK_NAME", testname);
+ if (line.contains("TEMPLATE_EXPECTED_VALUES"))
+ emitArray(line, testVector.expected);
+ else if (line.contains("TEMPLATE_MESSAGE_VALES"))
+ emitArray(line, testVector.X);
+ else if (line.contains("TEMPLATE_EXPONENT_VALES"))
+ emitArray(line, testVector.E);
+ else if (line.contains("TEMPLATE_MODULUS_VALUES"))
+ emitArray(line, testVector.M);
+ else if (line.contains("TEMPLATE_EXPECTED_LENGTH"))
+ emitLength(line, testVector.expected);
+ else if (line.contains("TEMPLATE_MESSAGE_LENGTH"))
+ emitLength(line, testVector.X);
+ else if (line.contains("TEMPLATE_EXPONENT_LENGTH"))
+ emitLength(line, testVector.E);
+ else if (line.contains("TEMPLATE_MODULUS_LENGTH"))
+ emitLength(line, testVector.M);
+ else
+ out(line);
+ }
+ }
- out("write_word({GENERAL_PREFIX, ADDR_CTRL}, 32'h%08x);",
- testVector.length);
- out("wait_ready();");
+ private void emitLength(String pattern, int[] array) {
+ String lengthHexString = hex32(array.length);
+ String line = pattern.replace("#TL#", lengthHexString);
+ out(line);
+ }
- for (int i = 0; i < testVector.expected.length; i++) {
- out("read_word({RESULT_PREFIX,8'h%02x});", i);
- out("read_data = tb_read_data;");
- out("if (read_data !== 32'h%08x))", testVector.expected[i]);
- out(" begin");
- out(" $display(\"Expected: 0x%08x, got 0x%%08x\", read_data);",
- testVector.expected[i]);
- out(" end");
+ private void emitArray(String pattern, int[] array) {
+ for (int i = 0; i < array.length; i++) {
+ String indexHexString = hex8(i);
+ String arrayHexString = hex32(array[i]);
+ String line = pattern.replace("#TI#", indexHexString).replace(
+ "#TD#", arrayHexString);
+ out(line);
}
- out("end");
- out("endtask // " + testname);
+ }
+ private String hex8(int data) {
+ return String.format("%02x", data);
}
+ private String hex32(int data) {
+ return String.format("%08x", data);
+ }
+
@Override
public void close() throws Exception {
emitMiddle();
@@ -87,10 +91,15 @@ public class GeneratorVerilog extends ModExpTestFormater {
super.close();
}
- private void emitHeader() {
- if (headerEmitted == true)
+ private void init() {
+ if (initilized == true)
return;
- headerEmitted = true;
+ initilized = true;
+ emitHeader();
+ taskTemplate.addAll(consumeTemplate("TEMPLATE_TEST_DEFINITION_END"));
+ }
+
+ private void emitHeader() {
String terminatingLine = "===TEMPLATE_HEADER_END===";
emitTemplateSection(terminatingLine);
}
@@ -106,17 +115,23 @@ public class GeneratorVerilog extends ModExpTestFormater {
}
private void emitTemplateSection(String terminatingLine) {
+ List<String> lines = consumeTemplate(terminatingLine);
+ for (String line : lines)
+ out(line);
+ }
+
+ private List<String> consumeTemplate(String terminatingLine) {
+ List<String> lines = new ArrayList<String>();
String line;
try {
while ((line = br.readLine()) != null) {
if (line.contains(terminatingLine))
- return;
- out(line);
+ break;
+ lines.add(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
- } finally {
}
+ return lines;
}
-
}
diff --git a/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/ModExpTestFormater.java b/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/ModExpTestFormater.java
index 7f9bbb3..f0104f4 100644
--- a/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/ModExpTestFormater.java
+++ b/src/testgenerator/src/org/crypttech/modexp/testgenerator/format/ModExpTestFormater.java
@@ -33,5 +33,6 @@ public abstract class ModExpTestFormater implements AutoCloseable {
@Override
public void close() throws Exception {
pw.close();
+ System.out.printf("%s closing...\n", this.getClass().getName());
}
}