mirror of
https://github.com/arnaucube/circomlib.git
synced 2026-02-06 18:56:43 +01:00
Merge branch 'master' into feature/jstests
This commit is contained in:
@@ -3,10 +3,12 @@ const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const createBlakeHash = require("blake-hash");
|
||||
const eddsa = require("../src/eddsa.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
const bigInt = require("snarkjs").bigInt;
|
||||
|
||||
describe("Baby Jub test", function () {
|
||||
let circuitAdd;
|
||||
@@ -22,6 +24,11 @@ describe("Baby Jub test", function () {
|
||||
const cirDefTest = await compiler(path.join(__dirname, "circuits", "babycheck_test.circom"));
|
||||
circuitTest = new snarkjs.Circuit(cirDefTest);
|
||||
console.log("NConstrains BabyTest: " + circuitTest.nConstraints);
|
||||
|
||||
const cirDefPbk = await compiler(path.join(__dirname, "circuits", "babypbk_test.circom"));
|
||||
circuitPbk = new snarkjs.Circuit(cirDefPbk);
|
||||
console.log("NConstrains BabyPbk: " + circuitPbk.nConstraints);
|
||||
|
||||
});
|
||||
|
||||
it("Should add point (0,1) and (0,1)", async () => {
|
||||
@@ -97,4 +104,22 @@ describe("Baby Jub test", function () {
|
||||
}
|
||||
});
|
||||
|
||||
it("Should extract the public key from the private one", async () => {
|
||||
|
||||
const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex");
|
||||
const pvk = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0,32));
|
||||
const S = bigInt.leBuff2int(pvk).shr(3);
|
||||
|
||||
const A = eddsa.prv2pub(rawpvk);
|
||||
|
||||
const input = {
|
||||
in : S,
|
||||
Ax : A[0],
|
||||
Ay : A[1]
|
||||
}
|
||||
|
||||
const w = circuitPbk.calculateWitness(input);
|
||||
assert(circuitPbk.checkWitness(w));
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
3
test/circuits/babypbk_test.circom
Normal file
3
test/circuits/babypbk_test.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuits/babyjub.circom";
|
||||
|
||||
component main = BabyPbk();
|
||||
3
test/circuits/eddsaposeidon_test.circom
Normal file
3
test/circuits/eddsaposeidon_test.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuits/eddsaposeidon.circom";
|
||||
|
||||
component main = EdDSAPoseidonVerifier();
|
||||
4
test/circuits/greatereqthan.circom
Normal file
4
test/circuits/greatereqthan.circom
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
include "../../circuits/comparators.circom";
|
||||
|
||||
component main = GreaterEqThan(32);
|
||||
4
test/circuits/greaterthan.circom
Normal file
4
test/circuits/greaterthan.circom
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
include "../../circuits/comparators.circom";
|
||||
|
||||
component main = GreaterThan(32);
|
||||
4
test/circuits/lesseqthan.circom
Normal file
4
test/circuits/lesseqthan.circom
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
include "../../circuits/comparators.circom";
|
||||
|
||||
component main = LessEqThan(32);
|
||||
3
test/circuits/mimc_sponge_hash_test.circom
Normal file
3
test/circuits/mimc_sponge_hash_test.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuits/mimcsponge.circom"
|
||||
|
||||
component main = MiMCSponge(2, 220, 3);
|
||||
3
test/circuits/mimc_sponge_test.circom
Normal file
3
test/circuits/mimc_sponge_test.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuits/mimcsponge.circom"
|
||||
|
||||
component main = MiMCFeistel(220);
|
||||
31
test/circuits/mux1_1.circom
Normal file
31
test/circuits/mux1_1.circom
Normal file
@@ -0,0 +1,31 @@
|
||||
include "../../circuits/mux1.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
|
||||
template Constants() {
|
||||
var i;
|
||||
signal output out[2];
|
||||
|
||||
out[0] <== 37;
|
||||
out[1] <== 47;
|
||||
}
|
||||
|
||||
template Main() {
|
||||
var i;
|
||||
signal private input selector;
|
||||
signal output out;
|
||||
|
||||
component mux = Mux1();
|
||||
component n2b = Num2Bits(1);
|
||||
component cst = Constants();
|
||||
|
||||
selector ==> n2b.in;
|
||||
n2b.out[0] ==> mux.s;
|
||||
for (i=0; i<2; i++) {
|
||||
cst.out[i] ==> mux.c[i];
|
||||
}
|
||||
|
||||
mux.out ==> out;
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
35
test/circuits/mux2_1.circom
Normal file
35
test/circuits/mux2_1.circom
Normal file
@@ -0,0 +1,35 @@
|
||||
include "../../circuits/mux2.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
|
||||
template Constants() {
|
||||
var i;
|
||||
signal output out[4];
|
||||
|
||||
out[0] <== 37;
|
||||
out[1] <== 47;
|
||||
out[2] <== 53;
|
||||
out[3] <== 71;
|
||||
}
|
||||
|
||||
template Main() {
|
||||
var i;
|
||||
signal private input selector;
|
||||
signal output out;
|
||||
|
||||
component mux = Mux2();
|
||||
component n2b = Num2Bits(2);
|
||||
component cst = Constants();
|
||||
|
||||
selector ==> n2b.in;
|
||||
for (i=0; i<2; i++) {
|
||||
n2b.out[i] ==> mux.s[i];
|
||||
}
|
||||
for (i=0; i<4; i++) {
|
||||
cst.out[i] ==> mux.c[i];
|
||||
}
|
||||
|
||||
mux.out ==> out;
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
3
test/circuits/poseidon_test.circom
Normal file
3
test/circuits/poseidon_test.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuits/poseidon.circom"
|
||||
|
||||
component main = Poseidon(2, 6, 8, 57);
|
||||
@@ -36,7 +36,7 @@ describe("Sum test", () => {
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
});
|
||||
it("Should create a comparison", async() => {
|
||||
it("Should create a comparison lessthan", async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "lessthan.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
@@ -74,4 +74,120 @@ describe("Sum test", () => {
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
});
|
||||
it("Should create a comparison lesseqthan", async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "lesseqthan.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
let witness;
|
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
});
|
||||
it("Should create a comparison greaterthan", async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "greaterthan.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
let witness;
|
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
});
|
||||
it("Should create a comparison greatereqthan", async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "greatereqthan.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstraints BalancesUpdater: " + circuit.nConstraints);
|
||||
|
||||
let witness;
|
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,7 +19,7 @@ describe("EdDSA MiMC test", function () {
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains EdDSA: " + circuit.nConstraints);
|
||||
console.log("NConstrains EdDSA MiMC: " + circuit.nConstraints);
|
||||
});
|
||||
|
||||
it("Sign a single number", async () => {
|
||||
|
||||
98
test/eddsaposeidon.js
Normal file
98
test/eddsaposeidon.js
Normal file
@@ -0,0 +1,98 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const eddsa = require("../src/eddsa.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
describe("EdDSA Poseidon test", function () {
|
||||
let circuit;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "eddsaposeidon_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains EdDSA Poseidon: " + circuit.nConstraints);
|
||||
});
|
||||
|
||||
it("Sign a single number", async () => {
|
||||
const msg = bigInt(1234);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
const pubKey = eddsa.prv2pub(prvKey);
|
||||
|
||||
const signature = eddsa.signPoseidon(prvKey, msg);
|
||||
|
||||
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
Ax: pubKey[0],
|
||||
Ay: pubKey[1],
|
||||
R8x: signature.R8[0],
|
||||
R8y: signature.R8[1],
|
||||
S: signature.S,
|
||||
M: msg});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
});
|
||||
|
||||
it("Detect Invalid signature", async () => {
|
||||
const msg = bigInt(1234);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
const pubKey = eddsa.prv2pub(prvKey);
|
||||
|
||||
|
||||
const signature = eddsa.signPoseidon(prvKey, msg);
|
||||
|
||||
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
|
||||
try {
|
||||
circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
Ax: pubKey[0],
|
||||
Ay: pubKey[1],
|
||||
R8x: signature.R8[0].add(bigInt(1)),
|
||||
R8y: signature.R8[1],
|
||||
S: signature.S,
|
||||
M: msg});
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert.equal(err.message, "Constraint doesn't match: 1 != 0");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
it("Test a dissabled circuit with a bad signature", async () => {
|
||||
const msg = bigInt(1234);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
const pubKey = eddsa.prv2pub(prvKey);
|
||||
|
||||
|
||||
const signature = eddsa.signPoseidon(prvKey, msg);
|
||||
|
||||
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
enabled: 0,
|
||||
Ax: pubKey[0],
|
||||
Ay: pubKey[1],
|
||||
R8x: signature.R8[0].add(bigInt(1)),
|
||||
R8y: signature.R8[1],
|
||||
S: signature.S,
|
||||
M: msg});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
const TestRPC = require("ganache-cli");
|
||||
const ganache = require("ganache-cli");
|
||||
const Web3 = require("web3");
|
||||
const chai = require("chai");
|
||||
const mimcGenContract = require("../src/mimc_gencontract.js");
|
||||
@@ -10,35 +10,31 @@ const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); };
|
||||
|
||||
const SEED = "mimc";
|
||||
|
||||
describe("MiMC Smart contract test", () => {
|
||||
describe("MiMC Smart contract test", function () {
|
||||
let testrpc;
|
||||
let web3;
|
||||
let mimc;
|
||||
let accounts;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before(async () => {
|
||||
testrpc = TestRPC.server({
|
||||
ws: true,
|
||||
gasLimit: 5800000,
|
||||
total_accounts: 10,
|
||||
});
|
||||
|
||||
testrpc.listen(8546, "127.0.0.1");
|
||||
|
||||
web3 = new Web3("ws://127.0.0.1:8546");
|
||||
web3 = new Web3(ganache.provider(), null, { transactionConfirmationBlocks: 1 });
|
||||
accounts = await web3.eth.getAccounts();
|
||||
});
|
||||
|
||||
after(async () => testrpc.close());
|
||||
|
||||
it("Should deploy the contract", async () => {
|
||||
const C = new web3.eth.Contract(mimcGenContract.abi);
|
||||
|
||||
mimc = await C.deploy({
|
||||
data: mimcGenContract.createCode(SEED, 91)
|
||||
data: mimcGenContract.createCode(SEED, 91),
|
||||
arguments: []
|
||||
}).send({
|
||||
gas: 1500000,
|
||||
gasPrice: '30000000000000',
|
||||
from: accounts[0]
|
||||
}).on("error", (error) => {
|
||||
console.log("ERROR: "+error);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
58
test/mimcspongecircuit.js
Normal file
58
test/mimcspongecircuit.js
Normal file
@@ -0,0 +1,58 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const mimcjs = require("../src/mimcsponge.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("MiMC Sponge Circuit test", function () {
|
||||
let circuit;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
it("Should check permutation", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_sponge_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("MiMC Feistel constraints: " + circuit.nConstraints);
|
||||
|
||||
const w = circuit.calculateWitness({xL_in: 1, xR_in: 2, k: 3});
|
||||
|
||||
const xLout = w[circuit.getSignalIdx("main.xL_out")];
|
||||
const xRout = w[circuit.getSignalIdx("main.xR_out")];
|
||||
|
||||
const out2 = mimcjs.hash(1,2,3);
|
||||
|
||||
assert.equal(xLout.toString(), out2.xL.toString());
|
||||
assert.equal(xRout.toString(), out2.xR.toString());
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
});
|
||||
|
||||
it("Should check hash", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_sponge_hash_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("MiMC Sponge constraints: " + circuit.nConstraints);
|
||||
|
||||
const w = circuit.calculateWitness({ins: [1, 2], k: 0});
|
||||
|
||||
const o1 = w[circuit.getSignalIdx("main.outs[0]")];
|
||||
const o2 = w[circuit.getSignalIdx("main.outs[1]")];
|
||||
const o3 = w[circuit.getSignalIdx("main.outs[2]")];
|
||||
|
||||
const out2 = mimcjs.multiHash([1,2], 0, 3);
|
||||
|
||||
assert.equal(o1.toString(), out2[0].toString());
|
||||
assert.equal(o2.toString(), out2[1].toString());
|
||||
assert.equal(o3.toString(), out2[2].toString());
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
});
|
||||
});
|
||||
43
test/mimcspongecontract.js
Normal file
43
test/mimcspongecontract.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const ganache = require("ganache-cli");
|
||||
const Web3 = require("web3");
|
||||
const chai = require("chai");
|
||||
const mimcGenContract = require("../src/mimcsponge_gencontract.js");
|
||||
const mimcjs = require("../src/mimcsponge.js");
|
||||
|
||||
|
||||
const assert = chai.assert;
|
||||
const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); };
|
||||
|
||||
const SEED = "mimcsponge";
|
||||
|
||||
describe("MiMC Sponge Smart contract test", () => {
|
||||
let testrpc;
|
||||
let web3;
|
||||
let mimc;
|
||||
let accounts;
|
||||
|
||||
before(async () => {
|
||||
web3 = new Web3(ganache.provider(), null, { transactionConfirmationBlocks: 1 });
|
||||
accounts = await web3.eth.getAccounts();
|
||||
});
|
||||
|
||||
it("Should deploy the contract", async () => {
|
||||
const C = new web3.eth.Contract(mimcGenContract.abi);
|
||||
|
||||
mimc = await C.deploy({
|
||||
data: mimcGenContract.createCode(SEED, 220)
|
||||
}).send({
|
||||
gas: 3500000,
|
||||
from: accounts[0]
|
||||
});
|
||||
});
|
||||
|
||||
it("Shold calculate the mimc correctly", async () => {
|
||||
const res = await mimc.methods.MiMCSponge(1,2,3).call();
|
||||
const res2 = await mimcjs.hash(1,2,3);
|
||||
|
||||
assert.equal(res.xL.toString(), res2.xL.toString());
|
||||
assert.equal(res.xR.toString(), res2.xR.toString());
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,7 +9,7 @@ const bigInt = snarkjs.bigInt;
|
||||
|
||||
|
||||
describe("Mux4 test", () => {
|
||||
it("Should create a constant multiplexer", async () => {
|
||||
it("Should create a constant multiplexer 4", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mux4_1.circom"));
|
||||
|
||||
@@ -50,7 +50,7 @@ describe("Mux4 test", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("Should create a constant multiplexer", async () => {
|
||||
it("Should create a constant multiplexer 3", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mux3_1.circom"));
|
||||
|
||||
@@ -74,6 +74,52 @@ describe("Mux4 test", () => {
|
||||
|
||||
assert(w[0].equals(bigInt(1)));
|
||||
|
||||
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
|
||||
}
|
||||
});
|
||||
it("Should create a constant multiplexer 2", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mux2_1.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains Mux2: " + circuit.nConstraints);
|
||||
|
||||
const ct8 = [
|
||||
bigInt("37"),
|
||||
bigInt("47"),
|
||||
bigInt("53"),
|
||||
bigInt("71"),
|
||||
];
|
||||
|
||||
for (let i=0; i<4; i++) {
|
||||
const w = circuit.calculateWitness({ "selector": i });
|
||||
|
||||
assert(w[0].equals(bigInt(1)));
|
||||
|
||||
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
|
||||
}
|
||||
});
|
||||
it("Should create a constant multiplexer 1", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mux1_1.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains Mux1: " + circuit.nConstraints);
|
||||
|
||||
const ct8 = [
|
||||
bigInt("37"),
|
||||
bigInt("47"),
|
||||
];
|
||||
|
||||
for (let i=0; i<2; i++) {
|
||||
const w = circuit.calculateWitness({ "selector": i });
|
||||
|
||||
assert(w[0].equals(bigInt(1)));
|
||||
|
||||
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
|
||||
}
|
||||
|
||||
60
test/poseidoncircuit.js
Normal file
60
test/poseidoncircuit.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
var blake2b = require('blake2b');
|
||||
|
||||
const poseidon = require("../src/poseidon.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("Blake2b version test", function() {
|
||||
it("Should give the expected output for blake2b version", async () => {
|
||||
var output = new Uint8Array(32);
|
||||
var input = Buffer.from('poseidon_constants');
|
||||
h = blake2b(output.length).update(input).digest('hex')
|
||||
assert.equal('e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1', h);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Poseidon Circuit test", function () {
|
||||
let circuit;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "poseidon_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("Poseidon constraints: " + circuit.nConstraints);
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([1, 2])", async () => {
|
||||
const w = circuit.calculateWitness({inputs: [1, 2]});
|
||||
|
||||
const res = w[circuit.getSignalIdx("main.out")];
|
||||
|
||||
const hash = poseidon.createHash(6, 8, 57);
|
||||
|
||||
const res2 = hash([1,2]);
|
||||
assert.equal('12242166908188651009877250812424843524687801523336557272219921456462821518061', res2.toString());
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
assert(circuit.checkWitness(w));
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([3, 4])", async () => {
|
||||
const w = circuit.calculateWitness({inputs: [3, 4]});
|
||||
|
||||
const res = w[circuit.getSignalIdx("main.out")];
|
||||
|
||||
const hash = poseidon.createHash(6, 8, 57);
|
||||
|
||||
const res2 = hash([3, 4]);
|
||||
assert.equal('17185195740979599334254027721507328033796809509313949281114643312710535000993', res2.toString());
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
});
|
||||
});
|
||||
49
test/poseidoncontract.js
Normal file
49
test/poseidoncontract.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const ganache = require("ganache-cli");
|
||||
const Web3 = require("web3");
|
||||
const chai = require("chai");
|
||||
const poseidonGenContract = require("../src/poseidon_gencontract.js");
|
||||
const Poseidon = require("../src/poseidon.js");
|
||||
const bigInt = require("snarkjs").bigInt;
|
||||
|
||||
const assert = chai.assert;
|
||||
const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); };
|
||||
|
||||
const SEED = "mimc";
|
||||
|
||||
describe("Poseidon Smart contract test", () => {
|
||||
let testrpc;
|
||||
let web3;
|
||||
let mimc;
|
||||
let accounts;
|
||||
|
||||
before(async () => {
|
||||
web3 = new Web3(ganache.provider(), null, { transactionConfirmationBlocks: 1 });
|
||||
accounts = await web3.eth.getAccounts();
|
||||
});
|
||||
|
||||
it("Should deploy the contract", async () => {
|
||||
const C = new web3.eth.Contract(poseidonGenContract.abi);
|
||||
|
||||
mimc = await C.deploy({
|
||||
data: poseidonGenContract.createCode()
|
||||
}).send({
|
||||
gas: 2500000,
|
||||
from: accounts[0]
|
||||
});
|
||||
});
|
||||
|
||||
it("Shold calculate the mimic correctly", async () => {
|
||||
|
||||
const res = await mimc.methods.poseidon([1,2]).call();
|
||||
|
||||
// console.log("Cir: " + bigInt(res.toString(16)).toString(16));
|
||||
|
||||
const hash = Poseidon.createHash(6, 8, 57);
|
||||
|
||||
const res2 = hash([1,2]);
|
||||
// console.log("Ref: " + bigInt(res2).toString(16));
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
});
|
||||
});
|
||||
|
||||
@@ -84,7 +84,7 @@ describe("SMT test", function () {
|
||||
let circuit;
|
||||
let tree;
|
||||
|
||||
this.timeout(100000);
|
||||
this.timeout(10000000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "smtprocessor10_test.circom"));
|
||||
|
||||
Reference in New Issue
Block a user