Skip to content

Test case to illustrate how to provide input in hexadecimal format for SHA256 gadget, using different settings for bitWidthPerInputElement #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions JsnarkCircuitBuilder/src/examples/tests/hash/SHA256_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*******************************************************************************/
package examples.tests.hash;

import circuit.config.Config;
import junit.framework.TestCase;

import org.junit.Test;
Expand All @@ -13,6 +14,8 @@
import circuit.structure.Wire;
import examples.gadgets.hash.SHA256Gadget;

import java.math.BigInteger;

/**
* Tests SHA256 standard cases.
*
Expand Down Expand Up @@ -223,4 +226,62 @@ public void generateSampleInput(CircuitEvaluator e) {
}

}

@Test
public void testCase6() {

String inputStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
String expectedDigest = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1";

// Testing different settings of the bitWidthPerInputElement parameter, when the input is in hexadecimal format.
// wordSize = # of hex digits per input wire. 1 hex digit = 4 bits.

String inputStrInHex = Util.stringToHex(inputStr);

for (int wordSize = 2; wordSize <= Config.LOG2_FIELD_PRIME / 4 - 3; wordSize+=2) {

final int numHexDigitsPerInputWire = wordSize;

CircuitGenerator generator = new CircuitGenerator("SHA2_Test6") {

Wire[] inputWires;
@Override
protected void buildCircuit() {
inputWires = createInputWireArray((inputStrInHex.length()/numHexDigitsPerInputWire)
+ ((inputStrInHex.length()) % numHexDigitsPerInputWire != 0 ? 1 : 0));
Wire[] digest = new SHA256Gadget(inputWires, 4*numHexDigitsPerInputWire,
inputStrInHex.length()/2, false, true, "")
.getOutputWires();
makeOutputArray(digest);
}

@Override
public void generateSampleInput(CircuitEvaluator e) {
for (int i = 0; i < inputWires.length; i++) {
BigInteger sum = BigInteger.ZERO;
for (int j = i * numHexDigitsPerInputWire; j < (i + 1) * numHexDigitsPerInputWire
&& j < inputStrInHex.length(); j+=2) {
String substring = inputStrInHex.substring(j,
j+2);
BigInteger v = new BigInteger(substring, 16);
sum = sum.add(v.shiftLeft(((j % numHexDigitsPerInputWire)/2) * 8));
}
e.setWireValue(inputWires[i], sum);
}
}
};

generator.generateCircuit();
generator.evalCircuit();
CircuitEvaluator evaluator = generator.getCircuitEvaluator();

String outDigest = "";
for (Wire w : generator.getOutWires()) {
outDigest += Util.padZeros(
evaluator.getWireValue(w).toString(16), 8);
}
assertEquals(expectedDigest, outDigest);

}
}
}
8 changes: 8 additions & 0 deletions JsnarkCircuitBuilder/src/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,12 @@ public static Wire[] padWireArray(Wire[] a, int length, Wire p) {
return newArray;
}
}

public static String stringToHex(String inputString){
StringBuilder strB = new StringBuilder(inputString.length());
for (char ch: inputString.toCharArray()){
strB.append(String.format("%x", (int) ch));
}
return strB.toString();
}
}