Permalink
Browse files

Update in ModGadget + added ModConstantGadget

  • Loading branch information...
akosba committed Jan 15, 2016
1 parent 0f7e8cd commit 7c919d7517da9fc016f23192a027433c08668772
View
@@ -7,3 +7,6 @@ bin
.project
.settings
.classpath
+*.arith
+!JsnarkCirctuitBuilder/auction_10.arith
+*.in
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * Author: Ahmed Kosba <akosba@cs.umd.edu>
+ *******************************************************************************/
+package examples.gadgets;
+
+import java.math.BigInteger;
+
+import circuit.eval.CircuitEvaluator;
+import circuit.eval.Instruction;
+import circuit.operations.Gadget;
+import circuit.structure.Wire;
+
+/**
+ * This gadget provides the remainder of a % b, where b is a circuit constant.
+ *
+ *
+ */
+
+public class ModConstantGadget extends Gadget {
+
+ private Wire a;
+ private BigInteger b;
+ private Wire r;
+ private Wire q;
+
+ private int bitwidth; // a's bitwidth
+
+ public ModConstantGadget(Wire a, int bitwidth, BigInteger b, String...desc) {
+ super(desc);
+ this.a = a;
+ this.b = b;
+ this.bitwidth = bitwidth;
+ if(b.signum() != 1){
+ throw new IllegalArgumentException("b must be a positive constant. Signed operations not supported yet.");
+ }
+ if(bitwidth < b.bitLength()){
+ throw new IllegalArgumentException("a's bitwidth < b's bitwidth -- This gadget is not needed.");
+ }
+ // TODO: add further checks.
+
+ buildCircuit();
+ }
+
+ private void buildCircuit() {
+
+ r = generator.createProverWitnessWire("mod result");
+ q = generator.createProverWitnessWire("division result");
+
+ generator.specifyProverWitnessComputation(new Instruction() {
+ @Override
+ public void evaluate(CircuitEvaluator evaluator) {
+ BigInteger aValue = evaluator.getWireValue(a);
+ BigInteger rValue = aValue.mod(b);
+ evaluator.setWireValue(r, rValue);
+ BigInteger qValue = aValue.divide(b);
+ evaluator.setWireValue(q, qValue);
+ }
+
+ });
+
+ int bBitwidth = b.bitLength();
+ r.restrictBitLength(bBitwidth);
+ q.restrictBitLength(bitwidth - bBitwidth + 1);
+ generator.addOneAssertion(r.isLessThan(b, bBitwidth));
+ generator.addEqualityAssertion(q.mul(b).add(r), a);
+ }
+
+ @Override
+ public Wire[] getOutputWires() {
+ return new Wire[] { r };
+ }
+
+}
@@ -24,17 +24,15 @@
private Wire r;
private Wire q;
- private int bitwidth1;
- private int bitwidth2;
+ private int bitwidth; // bitwidth for both a, b
- public ModGadget(Wire a, int bitwidth1, Wire b, int bitwidth2, String...desc) {
+ public ModGadget(Wire a, Wire b, int bitwidth, String...desc) {
super(desc);
this.a = a;
this.b = b;
- this.bitwidth1 = bitwidth1;
- this.bitwidth2 = bitwidth2;
- if(bitwidth1 < bitwidth2){
- throw new IllegalArgumentException("bitwidth1 < bitwidth2 -- This gadget is most probably not needed.");
+ this.bitwidth = bitwidth;
+ if(bitwidth > 126){
+ throw new IllegalArgumentException("Bitwidth not supported yet.");
}
buildCircuit();
}
@@ -57,9 +55,9 @@ public void evaluate(CircuitEvaluator evaluator) {
});
- r.restrictBitLength(bitwidth2);
- q.restrictBitLength(bitwidth1 - bitwidth2 + 1);
- generator.addOneAssertion(r.isLessThan(b, bitwidth2));
+ r.restrictBitLength(bitwidth);
+ q.restrictBitLength(bitwidth);
+ generator.addOneAssertion(r.isLessThan(b, bitwidth));
generator.addEqualityAssertion(q.mul(b).add(r), a);
}
@@ -12,6 +12,7 @@
import circuit.eval.CircuitEvaluator;
import circuit.structure.CircuitGenerator;
import circuit.structure.Wire;
+import examples.gadgets.ModConstantGadget;
import examples.gadgets.ModGadget;
@@ -21,8 +22,8 @@
@Test
public void testCase1() {
- int a = 62178522;
- int b = 257;
+ int a = 1262178522;
+ int b = 257; // b will be an input to the circuit
CircuitGenerator generator = new CircuitGenerator("Mod_Test1") {
@@ -32,8 +33,10 @@ public void testCase1() {
protected void buildCircuit() {
inputWires = createInputWireArray(2);
- Wire r = new ModGadget(inputWires[0], (int) Math.ceil(Math.log10(a) / Math.log10(2)), inputWires[1],
- (int) Math.ceil(Math.log10(b) / Math.log10(2))).getOutputWires()[0];
+// Wire r = new ModGadget(inputWires[0], (int) Math.ceil(Math.log10(a) / Math.log10(2)), inputWires[1],
+// (int) Math.ceil(Math.log10(b) / Math.log10(2))).getOutputWires()[0];
+
+ Wire r = new ModGadget(inputWires[0], inputWires[1], 32).getOutputWires()[0];
makeOutput(r);
}
@@ -52,5 +55,38 @@ public void generateSampleInput(CircuitEvaluator e) {
Wire rWire = generator.getOutWires().get(0);
assertEquals(evaluator.getWireValue(rWire), BigInteger.valueOf(a % b));
}
+
+ @Test
+ public void testCase2() {
+
+ int a = 1262178522;
+ int b = 257; // b will be a constant
+
+ CircuitGenerator generator = new CircuitGenerator("Mod_Test2") {
+
+ Wire[] inputWires;
+
+ @Override
+ protected void buildCircuit() {
+
+ inputWires = createInputWireArray(1);
+ Wire r = new ModConstantGadget(inputWires[0], 32, BigInteger.valueOf(b)).getOutputWires()[0];
+ makeOutput(r);
+ }
+
+ @Override
+ public void generateSampleInput(CircuitEvaluator e) {
+ e.setWireValue(inputWires[0], a);
+ }
+ };
+
+ generator.generateCircuit();
+ CircuitEvaluator evaluator = new CircuitEvaluator(generator);
+ generator.generateSampleInput(evaluator);
+ evaluator.evaluate();
+ Wire rWire = generator.getOutWires().get(0);
+ assertEquals(evaluator.getWireValue(rWire), BigInteger.valueOf(a % b));
+ }
+
}

0 comments on commit 7c919d7

Please sign in to comment.