Skip to content

Commit

Permalink
add challenge input generators
Browse files Browse the repository at this point in the history
  • Loading branch information
therealyingtong committed Jun 13, 2019
1 parent eb66b77 commit d3fa26d
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 27 deletions.
19 changes: 19 additions & 0 deletions 1_simple_arithmetic/generate_challenge_input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require("fs");

var a = [2,2,2,2];
var b = [4,4,4,4];
var c = [6,6,6,6];
var d = [24,24,24,24];

const inputs = {
"a": a,
"b": b,
"c": c,
"d": d
}

fs.writeFileSync(
"./input.json",
JSON.stringify(inputs),
"utf-8"
);
23 changes: 23 additions & 0 deletions 1_simple_arithmetic/sample_challenge_circuit.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
template SimpleChecks(k) {
signal private input a[k];
signal private input b[k];
signal input c[k];
signal private input d[k];
signal output out;

var sum = 0;
for (var i = 0; i < k; i++){
// force a + b = c
a[i] + b[i] === c[i];

// force b * c = d
b[i] * c[i] === d[i];

// add up c and d arrays
sum = sum + c[i] + d[i];
}
// output sum of c and d arrays
out <== sum;
}

component main = SimpleChecks(4);
29 changes: 12 additions & 17 deletions 1_simple_arithmetic/sample_circuit.circom
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
template SimpleChecks(k) {
signal private input a[k];
signal private input b[k];
signal input c[k];
signal private input d[k];
template SimpleChecks() {
signal private input a;
signal private input b;
signal input c;
signal private input d;
signal output out;

var sum = 0;
for (var i = 0; i < k; i++){
// force a + b = c
a[i] + b[i] === c[i];
// force a + b = c
a + b === c;

// force b * c = d
b[i] * c[i] === d[i];
// force b * c = d
b * c === d;

// add up c and d arrays
sum = sum + c[i] + d[i];
}
// output sum of c and d arrays
out <== sum;
// output c + d
out <== c + d;
}

component main = SimpleChecks(4);
component main = SimpleChecks();
25 changes: 25 additions & 0 deletions 2_verify_eddsa/generate_challenge_input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require("fs");
const eddsa = require("../circomlib/src/eddsa.js");
const mimcjs = require("../circomlib/src/mimc7.js");

const preimage = [123,456,789];
const M = mimcjs.multiHash(preimage);
const prvKey = Buffer.from('1'.toString().padStart(64,'0'), "hex");
const pubKey = eddsa.prv2pub(prvKey);

const signature = eddsa.signMiMC(prvKey, M);

const inputs = {
"from_x": pubKey[0].toString(),
"from_y": pubKey[1].toString(),
"R8x": signature['R8'][0].toString(),
"R8y": signature['R8'][1].toString(),
"S": signature['S'].toString(),
"preimage": preimage
}

fs.writeFileSync(
"./input.json",
JSON.stringify(inputs),
"utf-8"
);
30 changes: 30 additions & 0 deletions 2_verify_eddsa/sample_challenge_circuit.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
include "../circomlib/circuits/eddsamimc.circom";
include "../circomlib/circuits/mimc.circom";

template VerifyEdDSAMiMC(k) {

// k is length of preimage

signal input from_x;
signal input from_y;
signal input R8x;
signal input R8y;
signal input S;
signal private input preimage[k];

component M = MultiMiMC7(k,91);
for (var i = 0; i < k; i++){
M.in[i] <== preimage[i];
}

component verifier = EdDSAMiMCVerifier();
verifier.enabled <== 1;
verifier.Ax <== from_x;
verifier.Ay <== from_y;
verifier.R8x <== R8x;
verifier.R8y <== R8y;
verifier.S <== S;
verifier.M <== M.out;
}

component main = VerifyEdDSAMiMC(3);
13 changes: 4 additions & 9 deletions 2_verify_eddsa/sample_circuit.circom
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include "../circomlib/circuits/eddsamimc.circom";
include "../circomlib/circuits/mimc.circom";

template VerifyEdDSAMiMC(k) {
template VerifyEdDSAMiMC() {

// k is length of preimage

Expand All @@ -10,12 +10,7 @@ template VerifyEdDSAMiMC(k) {
signal input R8x;
signal input R8y;
signal input S;
signal private input preimage[k];

component M = MultiMiMC7(k,91);
for (var i = 0; i < k; i++){
M.in[i] <== preimage[i];
}
signal input M;

component verifier = EdDSAMiMCVerifier();
verifier.enabled <== 1;
Expand All @@ -24,7 +19,7 @@ template VerifyEdDSAMiMC(k) {
verifier.R8x <== R8x;
verifier.R8y <== R8y;
verifier.S <== S;
verifier.M <== M.out;
verifier.M <== M;
}

component main = VerifyEdDSAMiMC(3);
component main = VerifyEdDSAMiMC();
27 changes: 27 additions & 0 deletions 3_verify_merkle/generate_challenge_input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const fs = require("fs");
const mimcjs = require("../circomlib/src/mimc7.js");
const mimcMerkle = require('./MiMCMerkle.js')

const leaf1 = mimcjs.multiHash([1,2,3])
const leaf2 = mimcjs.multiHash([4,5,6])
const leaf3 = mimcjs.multiHash([7,8,9])
const leaf4 = mimcjs.multiHash([9,8,7])
const leafArray = [leaf1,leaf2,leaf3,leaf4]

const tree = mimcMerkle.treeFromLeafArray(leafArray)
const root = tree[0][0];
const leaf1Proof = mimcMerkle.getProof(0, tree, leafArray)
const leaf1Pos = mimcMerkle.idxToBinaryPos(0, 2)

const inputs = {
"preimage": [1,2,3],
"root": root,
"paths2_root": [leaf1Proof[0].toString(),leaf1Proof[1].toString()],
"paths2_root_pos": leaf1Pos
}

fs.writeFileSync(
"./input.json",
JSON.stringify(inputs),
"utf-8"
);
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ In RollupNC, processing a single transaction involves:
- crediting the receiver account
- updating the `accounts_root`to get `final_root`

Create a file called `circuit.circom` and put in this code. Fill in the components in the lower half. Then, compile your circuit and test it against the `input.json` generated by running `node generate_circuit_input.js`.
Create a file called `circuit.circom` and put in this code. Fill in the signals for each component. Then, compile your circuit and test it against the `input.json` generated by running `node generate_circuit_input.js`.
```
include "./leaf_existence.circom";
include "./verify_eddsamimc.circom";
Expand Down

0 comments on commit d3fa26d

Please sign in to comment.