mirror of
https://github.com/arnaucube/circomlib.git
synced 2026-02-06 18:56:43 +01:00
Update web3
This commit is contained in:
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);
|
||||
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();
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const TestRPC = require("ganache-cli");
|
||||
const ganache = require("ganache-cli");
|
||||
const Web3 = require("web3");
|
||||
const chai = require("chai");
|
||||
const poseidonGenContract = require("../src/poseidon_gencontract.js");
|
||||
@@ -17,20 +17,10 @@ describe("Poseidon Smart contract test", () => {
|
||||
let accounts;
|
||||
|
||||
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(poseidonGenContract.abi);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user