mirror of
https://github.com/arnaucube/circomlib.git
synced 2026-02-06 18:56:43 +01:00
first commit
This commit is contained in:
98
test/babyjub.js
Normal file
98
test/babyjub.js
Normal file
@@ -0,0 +1,98 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const zkSnark = require("zksnark");
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
|
||||
describe("Baby Jub test", () => {
|
||||
it("Should add point (0,1) and (0,1)", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
|
||||
const circuit = new zkSnark.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
|
||||
const input={
|
||||
x1: zkSnark.bigInt(0),
|
||||
y1: zkSnark.bigInt(1),
|
||||
x2: zkSnark.bigInt(0),
|
||||
y2: zkSnark.bigInt(1)
|
||||
}
|
||||
|
||||
const w = circuit.calculateWitness(input);
|
||||
|
||||
const xout = w[circuit.getSignalIdx("main.xout")];
|
||||
const yout = w[circuit.getSignalIdx("main.yout")];
|
||||
|
||||
assert(xout.equals(0));
|
||||
assert(yout.equals(1));
|
||||
});
|
||||
|
||||
it("Should add 2 same numbers", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
|
||||
const circuit = new zkSnark.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
|
||||
const input={
|
||||
x1: zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
y1: zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
x2: zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
y2: zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
|
||||
}
|
||||
|
||||
const w = circuit.calculateWitness(input);
|
||||
|
||||
const xout = w[circuit.getSignalIdx("main.xout")];
|
||||
const yout = w[circuit.getSignalIdx("main.yout")];
|
||||
|
||||
assert(xout.equals(zkSnark.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
|
||||
assert(yout.equals(zkSnark.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
|
||||
});
|
||||
|
||||
it("Should add 2 different numbers", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
|
||||
const circuit = new zkSnark.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
|
||||
const input={
|
||||
x1: zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
y1: zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
x2: zkSnark.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
|
||||
y2: zkSnark.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
|
||||
}
|
||||
|
||||
const w = circuit.calculateWitness(input);
|
||||
|
||||
const xout = w[circuit.getSignalIdx("main.xout")];
|
||||
const yout = w[circuit.getSignalIdx("main.yout")];
|
||||
|
||||
console.log(xout.toString());
|
||||
console.log(yout.toString());
|
||||
|
||||
assert(xout.equals(zkSnark.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
|
||||
assert(yout.equals(zkSnark.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
|
||||
});
|
||||
});
|
||||
3
test/circuits/babyadd_tester.circom
Normal file
3
test/circuits/babyadd_tester.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuit/babyjub.circom";
|
||||
|
||||
component main = BabyAdd();
|
||||
24
test/circuits/exp_test.circom
Normal file
24
test/circuits/exp_test.circom
Normal file
@@ -0,0 +1,24 @@
|
||||
include "../../circuit/exp.circom";
|
||||
include "../../node_modules/circom/circuits/sha256/bitify.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input in;
|
||||
signal output out[2];
|
||||
|
||||
component n2b = Num2Bits(253);
|
||||
component exp = Exp(253);
|
||||
|
||||
var i;
|
||||
|
||||
in ==> n2b.in;
|
||||
|
||||
for (i=0; i<253; i++) {
|
||||
n2b.out[i] ==> exp.in[i];
|
||||
}
|
||||
|
||||
exp.out[0] ==> out[0];
|
||||
exp.out[1] ==> out[1];
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
20
test/circuits/exp_test_min.circom
Normal file
20
test/circuits/exp_test_min.circom
Normal file
@@ -0,0 +1,20 @@
|
||||
include "../../circuit/exp.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input in[256];
|
||||
signal output out[2];
|
||||
|
||||
var i;
|
||||
|
||||
component exp = Exp(256);
|
||||
|
||||
for (i=0; i<256; i++) {
|
||||
in[i] ==> exp.in[i];
|
||||
}
|
||||
|
||||
exp.out[0] ==> out[0];
|
||||
exp.out[1] ==> out[1];
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
3
test/circuits/expw4table_test.circom
Normal file
3
test/circuits/expw4table_test.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuit/ExpW4Table.circom";
|
||||
|
||||
component main = ExpW4Table(0);
|
||||
3
test/circuits/expw4table_test3.circom
Normal file
3
test/circuits/expw4table_test3.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuit/ExpW4Table.circom";
|
||||
|
||||
component main = ExpW4Table(3);
|
||||
54
test/circuits/mux4_1.circom
Normal file
54
test/circuits/mux4_1.circom
Normal file
@@ -0,0 +1,54 @@
|
||||
include "../../circuit/mux4.circom";
|
||||
include "../../node_modules/circom/circuits/sha256/bitify.circom";
|
||||
|
||||
|
||||
template Constants() {
|
||||
var i;
|
||||
signal output out[16];
|
||||
|
||||
out[0] <== 123;
|
||||
out[1] <== 456;
|
||||
out[2] <== 789;
|
||||
out[3] <== 012;
|
||||
out[4] <== 111;
|
||||
out[5] <== 222;
|
||||
out[6] <== 333;
|
||||
out[7] <== 4546;
|
||||
out[8] <== 134523;
|
||||
out[9] <== 44356;
|
||||
out[10] <== 15623;
|
||||
out[11] <== 4566;
|
||||
out[12] <== 1223;
|
||||
out[13] <== 4546;
|
||||
out[14] <== 4256;
|
||||
out[15] <== 4456;
|
||||
|
||||
/*
|
||||
for (i=0;i<16; i++) {
|
||||
out[i] <== i*2+100;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
template Main() {
|
||||
var i;
|
||||
signal private input selector;
|
||||
signal output out;
|
||||
|
||||
component mux = Mux4();
|
||||
component n2b = Num2Bits(4);
|
||||
component cst = Constants();
|
||||
|
||||
selector ==> n2b.in;
|
||||
for (i=0; i<4; i++) {
|
||||
n2b.out[i] ==> mux.s[i];
|
||||
}
|
||||
for (i=0; i<16; i++) {
|
||||
cst.out[i] ==> mux.c[i];
|
||||
}
|
||||
|
||||
mux.out ==> out;
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
168
test/exp.js
Normal file
168
test/exp.js
Normal file
@@ -0,0 +1,168 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const zkSnark = require("zksnark");
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
|
||||
const q=21888242871839275222246405745257275088548364400416034343698204186575808495617n
|
||||
function addPoint(a,b) {
|
||||
const cta = 168700n;
|
||||
const d = 168696n;
|
||||
|
||||
const res = [];
|
||||
res[0] = bigInt((a[0]*b[1] + b[0]*a[1]) * bigInt(1n + d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
|
||||
res[1] = bigInt((a[1]*b[1] - cta*a[0]*b[0]) * bigInt(1n - d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
|
||||
return res;
|
||||
}
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
describe("Exponentioation test", () => {
|
||||
it("Should generate the Exponentiation table in k=0", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "expw4table_test.circom"));
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
|
||||
const circuit = new zkSnark.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
|
||||
const w = circuit.calculateWitness({});
|
||||
|
||||
let g = [zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")]
|
||||
|
||||
dbl= [zkSnark.bigInt("0"), zkSnark.bigInt("1")];
|
||||
|
||||
for (let i=0; i<16; i++) {
|
||||
const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)];
|
||||
const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)];
|
||||
/*
|
||||
console.log(xout1.toString());
|
||||
console.log(yout1.toString());
|
||||
console.log(dbl[0]);
|
||||
console.log(dbl[1]);
|
||||
*/
|
||||
assert(xout1.equals(dbl[0]));
|
||||
assert(yout1.equals(dbl[1]));
|
||||
|
||||
dbl = addPoint([xout1, yout1],g);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
it("Should generate the Exponentiation table in k=3", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "expw4table_test3.circom"));
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
|
||||
const circuit = new zkSnark.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
|
||||
const w = circuit.calculateWitness({});
|
||||
|
||||
let g = [zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")]
|
||||
|
||||
for (let i=0; i<12;i++) {
|
||||
g = addPoint(g,g);
|
||||
}
|
||||
|
||||
dbl= [zkSnark.bigInt("0"), zkSnark.bigInt("1")];
|
||||
|
||||
for (let i=0; i<16; i++) {
|
||||
const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)];
|
||||
const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)];
|
||||
|
||||
/*
|
||||
console.log(xout1.toString());
|
||||
console.log(yout1.toString());
|
||||
console.log(dbl[0]);
|
||||
console.log(dbl[1]);
|
||||
*/
|
||||
assert(xout1.equals(dbl[0]));
|
||||
assert(yout1.equals(dbl[1]));
|
||||
|
||||
dbl = addPoint([xout1, yout1],g);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
it("Should exponentiate g^31", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "exp_test.circom"));
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
|
||||
const circuit = new zkSnark.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
|
||||
const w = circuit.calculateWitness({"in": 31});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
let g = [zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")]
|
||||
|
||||
let c = [0n, 1n];
|
||||
|
||||
for (let i=0; i<31;i++) {
|
||||
c = addPoint(c,g);
|
||||
}
|
||||
|
||||
const xout = w[circuit.getSignalIdx(`main.out[0]`)];
|
||||
const yout = w[circuit.getSignalIdx(`main.out[1]`)];
|
||||
|
||||
/*
|
||||
console.log(xout.toString());
|
||||
console.log(yout.toString());
|
||||
*/
|
||||
assert(xout.equals(c[0]));
|
||||
assert(yout.equals(c[1]));
|
||||
|
||||
console.log("-------")
|
||||
const w2 = circuit.calculateWitness({"in": (1n<<252n)+1n});
|
||||
|
||||
const xout2 = w2[circuit.getSignalIdx(`main.out[0]`)];
|
||||
const yout2 = w2[circuit.getSignalIdx(`main.out[1]`)];
|
||||
|
||||
c = [g[0], g[1]];
|
||||
for (let i=0; i<252;i++) {
|
||||
c = addPoint(c,c);
|
||||
}
|
||||
c = addPoint(c,g);
|
||||
/*
|
||||
console.log(xout2.toString());
|
||||
console.log(yout2.toString());
|
||||
console.log(c[0].toString());
|
||||
console.log(c[1].toString());
|
||||
*/
|
||||
assert(xout2.equals(c[0]));
|
||||
assert(yout2.equals(c[1]));
|
||||
|
||||
}).timeout(10000000);
|
||||
|
||||
it("Number of constrains for 256 bits", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "exp_test_min.circom"));
|
||||
|
||||
const circuit = new zkSnark.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
}).timeout(10000000);
|
||||
|
||||
});
|
||||
33
test/multiplexer.js
Normal file
33
test/multiplexer.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const zkSnark = require("zksnark");
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
|
||||
describe("Mux4 test", () => {
|
||||
it("Should create a constant multiplexer", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mux4_1.circom"));
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
|
||||
const circuit = new zkSnark.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
|
||||
for (i=0; i<16; i++) {
|
||||
const w = circuit.calculateWitness({ "selector": zkSnark.bigInt(i).toString() });
|
||||
|
||||
assert(w[0].equals(zkSnark.bigInt(1)));
|
||||
|
||||
console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
// assert(w[circuit.getSignalIdx("main.out")].equals(zkSnark.bigInt("100").add(zkSnark.bigInt(i))));
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user