mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-06 18:56:40 +01:00
First test added
This commit is contained in:
18
test/circuits/constants_test.jaz
Normal file
18
test/circuits/constants_test.jaz
Normal file
@@ -0,0 +1,18 @@
|
||||
include "../../circuits/sha256/constants.jaz"
|
||||
|
||||
template A() {
|
||||
signal input in;
|
||||
component h0;
|
||||
h0 = K(8);
|
||||
|
||||
var lc = 0;
|
||||
var e = 1;
|
||||
for (var i=0; i<32; i++) {
|
||||
lc = lc + e*h0.out[i];
|
||||
e *= 2;
|
||||
}
|
||||
|
||||
lc === in;
|
||||
}
|
||||
|
||||
component main = A();
|
||||
26
test/circuits/sum_test.jaz
Normal file
26
test/circuits/sum_test.jaz
Normal file
@@ -0,0 +1,26 @@
|
||||
include "bitify.jaz"
|
||||
include "binsum.jaz"
|
||||
|
||||
template A() {
|
||||
signal private input a;
|
||||
signal input b;
|
||||
signal output out;
|
||||
|
||||
component n2ba = Num2Bits(32);
|
||||
component n2bb = Num2Bits(32);
|
||||
component sum = BinSum(32,2);
|
||||
component b2n = Bits2Num(32);
|
||||
|
||||
n2ba.in <== a;
|
||||
n2bb.in <== b;
|
||||
|
||||
for (var i=0; i<32; i++) {
|
||||
sum.in[0][i] <== n2ba.out[i];
|
||||
sum.in[1][i] <== n2bb.out[i];
|
||||
b2n.in[i] <== sum.out[i];
|
||||
}
|
||||
|
||||
out <== b2n.out;
|
||||
}
|
||||
|
||||
component main = A();
|
||||
@@ -1,61 +0,0 @@
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const cmd=require("node-cmd");
|
||||
const util = require("util");
|
||||
const assert = require("assert");
|
||||
|
||||
const claimUtils = require("../src/claimUtils.js");
|
||||
|
||||
cmd.get[util.promisify.custom] = (c) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
cmd.get(c, (err, data, stderr) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve([data, stderr]);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const getAsync = util.promisify(cmd.get);
|
||||
const mkdir = util.promisify(fs.mkdir);
|
||||
const writeFile = util.promisify(fs.writeFile);
|
||||
|
||||
describe("command line", () => {
|
||||
|
||||
let tmpPath;
|
||||
before(async () => {
|
||||
tmpPath = path.join(__dirname, "..", "tmp");
|
||||
if (!fs.existsSync(tmpPath)) {
|
||||
await mkdir(tmpPath, 0o744);
|
||||
}
|
||||
process.chdir(tmpPath);
|
||||
});
|
||||
|
||||
it("Should create a tree from a claim files", async () => {
|
||||
|
||||
let i;
|
||||
let claims = [];
|
||||
for (i=0; i<100; i++) {
|
||||
const b = Buffer.from([ i / 256, i % 256 ]);
|
||||
claims[i] = claimUtils.buildClaim("0x01", "0x02", "0x03", b).toString("hex");
|
||||
}
|
||||
|
||||
claims = claims.sort();
|
||||
const claimsFile = path.join(tmpPath, "claims100.hex");
|
||||
const dbFile = path.join(tmpPath, "claims100.db");
|
||||
await writeFile(claimsFile, claims.join("\n"), "utf8");
|
||||
|
||||
await getAsync(`${path.join("..", "cli.js")} -d ${dbFile} add ${claimsFile} `);
|
||||
|
||||
const data = await getAsync(`${path.join("..", "cli.js")} -d ${dbFile} export`);
|
||||
let claims2 = data[0].split("\n");
|
||||
|
||||
claims2 = claims2.filter(function(n){ return n.length>0; });
|
||||
claims2 = claims2.sort();
|
||||
|
||||
assert.equal(claims2.join("\n"), claims.join("\n"));
|
||||
|
||||
}).timeout(20000);
|
||||
});
|
||||
@@ -1,2 +0,0 @@
|
||||
1 * 2 * 3
|
||||
* 4
|
||||
141
test/mainTest.js
141
test/mainTest.js
@@ -1,141 +0,0 @@
|
||||
const assert = require("assert");
|
||||
|
||||
const StaticMerkle = require("../src/StaticMerkle.js");
|
||||
const MemDB = require("../src/dbMem.js");
|
||||
const hash = require("../src/hashKeccak.js");
|
||||
const buffUtils = require("../src/buffUtils.js");
|
||||
const claimUtils = require("../src/claimUtils.js");
|
||||
|
||||
describe("static merkle", () => {
|
||||
before(async () => {
|
||||
|
||||
});
|
||||
|
||||
it("Create an empty tring of 0 levels", async () => {
|
||||
const dbPrv0 = await MemDB();
|
||||
const SM0 = await StaticMerkle(hash, dbPrv0, 0);
|
||||
const empty = SM0.root;
|
||||
assert.equal(buffUtils.toHex(empty), "0x0000000000000000000000000000000000000000000000000000000000000000");
|
||||
});
|
||||
|
||||
it("create an empty", async () => {
|
||||
const dbPrv = await MemDB();
|
||||
const SM140 = await StaticMerkle(hash, dbPrv, 140);
|
||||
const empty = SM140.root;
|
||||
assert.equal(buffUtils.toHex(empty), "0x0000000000000000000000000000000000000000000000000000000000000000");
|
||||
});
|
||||
|
||||
it("should add and remove a claim", async() => {
|
||||
const dbPrv = await MemDB();
|
||||
const SM140 = await StaticMerkle(hash, dbPrv, 140);
|
||||
const empty = SM140.root;
|
||||
const claim = claimUtils.buildClaim("0x01", "0x02", "0x03", "0x04");
|
||||
await SM140.addClaim(claim);
|
||||
assert.equal(buffUtils.toHex(SM140.root), "0xd3d9ad5e3c0b38c4e3eb411e9e3114b5ed8fb5c4bc69158329feb1a62743cda1");
|
||||
await SM140.removeClaim(claim);
|
||||
assert.equal(buffUtils.toHex(SM140.root), buffUtils.toHex(empty));
|
||||
|
||||
assert.equal(SM140.tx.inserts.length, 0);
|
||||
|
||||
});
|
||||
|
||||
it("should add two claims in different order and should be the same", async () => {
|
||||
const dbPrv_1 = await MemDB();
|
||||
const SM140_1 = await StaticMerkle(hash, dbPrv_1, 140);
|
||||
const dbPrv_2 = await MemDB();
|
||||
const SM140_2 = await StaticMerkle(hash, dbPrv_2, 140);
|
||||
const empty = SM140_1.root;
|
||||
const claim1 = claimUtils.buildClaim("0x01", "0x02", "0x03", "0x04");
|
||||
const claim2 = claimUtils.buildClaim("0x01", "0x02", "0x03", "0x05");
|
||||
|
||||
await SM140_1.addClaim(claim1);
|
||||
await SM140_1.addClaim(claim2);
|
||||
|
||||
await SM140_2.addClaim(claim2);
|
||||
await SM140_2.addClaim(claim1);
|
||||
|
||||
assert.equal(buffUtils.toHex(SM140_1.root), buffUtils.toHex(SM140_2.root));
|
||||
|
||||
await SM140_1.removeClaim(claim1);
|
||||
await SM140_1.removeClaim(claim2);
|
||||
assert.equal(buffUtils.toHex(SM140_1.root), buffUtils.toHex(empty));
|
||||
|
||||
await SM140_2.removeClaim(claim2);
|
||||
await SM140_2.removeClaim(claim1);
|
||||
assert.equal(buffUtils.toHex(SM140_2.root), buffUtils.toHex(empty));
|
||||
|
||||
});
|
||||
|
||||
it("should add 10 claims and remove them in different order", async () => {
|
||||
const dbPrv = await MemDB();
|
||||
const SM140 = await StaticMerkle(hash, dbPrv, 140);
|
||||
const empty = SM140.root;
|
||||
const claims = [];
|
||||
let i;
|
||||
for (i=0; i<10; i++) {
|
||||
const b = Buffer.from([ i / 256, i % 256 ]);
|
||||
claims[i] = claimUtils.buildClaim("0x01", "0x02", "0x03", b);
|
||||
}
|
||||
|
||||
for (i=0;i<claims.length; i++) {
|
||||
await SM140.addClaim(claims[i]);
|
||||
}
|
||||
|
||||
assert.equal(buffUtils.toHex(SM140.root), "0xb57c288d5c018c56610a3fb783062c9b199221734c8c5617795b57cbdbd4347f");
|
||||
|
||||
for (i=0;i<claims.length; i++) {
|
||||
await SM140.removeClaim(claims[i]);
|
||||
}
|
||||
|
||||
assert.equal(buffUtils.toHex(SM140.root), buffUtils.toHex(empty));
|
||||
assert.equal(SM140.tx.inserts.length, 0);
|
||||
});
|
||||
|
||||
it("Should give the same root when added a repeated claim", async () => {
|
||||
const dbPrv = await MemDB();
|
||||
const SM140 = await StaticMerkle(hash, dbPrv, 140);
|
||||
const empty = SM140.root;
|
||||
const claims = [];
|
||||
let i;
|
||||
for (i=0; i<100; i++) {
|
||||
const b = Buffer.from([ i % 10 ]);
|
||||
claims[i] = claimUtils.buildClaim("0x01", "0x02", "0x03", b);
|
||||
}
|
||||
|
||||
for (i=0;i<claims.length; i++) {
|
||||
await SM140.addClaim(claims[i]);
|
||||
}
|
||||
|
||||
assert.equal(buffUtils.toHex(SM140.root), "0xb57c288d5c018c56610a3fb783062c9b199221734c8c5617795b57cbdbd4347f");
|
||||
|
||||
for (i=0;i<claims.length; i++) {
|
||||
await SM140.removeClaim(claims[i]);
|
||||
}
|
||||
|
||||
assert.equal(buffUtils.toHex(SM140.root), buffUtils.toHex(empty));
|
||||
assert.equal(SM140.tx.inserts.length, 0);
|
||||
}).timeout(20000);
|
||||
|
||||
it("Should create a merkle proof and verify it ok", async () => {
|
||||
const dbPrv = await MemDB();
|
||||
const SM140 = await StaticMerkle(hash, dbPrv, 140);
|
||||
const empty = SM140.root;
|
||||
const claim1 = claimUtils.buildClaim("0x01", "0x02", "0x03", "0x04");
|
||||
const claim2 = claimUtils.buildClaim("0x01", "0x02", "0x03", "0x05");
|
||||
|
||||
await SM140.addClaim(claim1);
|
||||
await SM140.addClaim(claim2);
|
||||
|
||||
const mp = await SM140.getMerkeProof(claim1);
|
||||
|
||||
assert.equal(SM140.checkClaim(SM140.root, claim1, mp), true);
|
||||
assert.equal(SM140.checkClaim(empty, claim1, mp), false);
|
||||
assert.equal(SM140.checkClaim(empty, claim2, mp), false);
|
||||
|
||||
const mp1 = await SM140.getMerkeProof(claim1);
|
||||
assert.equal(SM140.checkClaim(SM140.root, claim1, mp1), true);
|
||||
const mp2 = await SM140.getMerkeProof(claim2);
|
||||
assert.equal(SM140.checkClaim(SM140.root, claim2, mp2), true);
|
||||
});
|
||||
|
||||
});
|
||||
14
test/sha256.js
Normal file
14
test/sha256.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
|
||||
const compiler = require("../index.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("SHA256 test", () => {
|
||||
it("Should create a constant circuit", async () => {
|
||||
|
||||
const cir = await compiler(path.join(__dirname, "circuits", "constants_test.jaz"));
|
||||
assert.equal(cir.nVars, 2);
|
||||
});
|
||||
});
|
||||
@@ -1,45 +0,0 @@
|
||||
11+12; 13;
|
||||
14; 15;
|
||||
/* Multi Line
|
||||
comment */
|
||||
/*** / * /* **/
|
||||
// Single line comment /* sss */
|
||||
16; 0x1f; 0xAa;
|
||||
12; id1; A; B; A+B;
|
||||
A*B+A*B+3;
|
||||
4/2;
|
||||
4/3;
|
||||
4/3*3;
|
||||
8/2;
|
||||
0/2;
|
||||
2/1;
|
||||
8 % 5;
|
||||
-1; +--1;
|
||||
(3+4)*(5*2);
|
||||
0xFF & 0x12;
|
||||
1 << 8;
|
||||
-1 >> 257;
|
||||
-1 << 257;
|
||||
-1 >> 256;
|
||||
-1 << 256;
|
||||
-1 >> 250;
|
||||
-1 << 250;
|
||||
33 == 33;
|
||||
33 == 34;
|
||||
3>3;
|
||||
3>=3;
|
||||
3<=3;
|
||||
3<3;
|
||||
3 && 0;
|
||||
0 && 3;
|
||||
3 && 3;
|
||||
0 && 0;
|
||||
!3;
|
||||
!0;
|
||||
!!8;
|
||||
2**3;
|
||||
(-1)**(-1);
|
||||
a[3];
|
||||
a[3][b] + b[4][c][d];
|
||||
func() + func(a) + func(a,b);
|
||||
3*4==6+6 && 2+1==3 ? 3+3*2 : (3+2)*6
|
||||
@@ -1 +0,0 @@
|
||||
f(1,2)
|
||||
@@ -1,9 +0,0 @@
|
||||
a+b*c
|
||||
(a+b)*c
|
||||
var c + a[1].b[2] + c
|
||||
f(1,2)
|
||||
component f(4) d;
|
||||
|
||||
a++ + b++;
|
||||
|
||||
--a;
|
||||
@@ -1,5 +0,0 @@
|
||||
var a;
|
||||
var b;
|
||||
var c;
|
||||
a+b*b[3+a][2];
|
||||
a<<2<=2>>a
|
||||
@@ -1,3 +0,0 @@
|
||||
a <== b === c
|
||||
r = a ? c ? 5 : 6 : b ? 3 : 4
|
||||
c = sqrt(a**2+b**2)
|
||||
@@ -1,8 +0,0 @@
|
||||
(a + bb) + c;
|
||||
include "filename.ext"
|
||||
include "secondfile"
|
||||
include "therdfile";
|
||||
include "4th file"
|
||||
{
|
||||
include "fifthfile"
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
{
|
||||
3+3
|
||||
4+4
|
||||
5+5
|
||||
}
|
||||
6
|
||||
7
|
||||
a <-- 3
|
||||
3 --> a;
|
||||
signal input b <== 4;
|
||||
4 ==> signal b;
|
||||
c === d;
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
signal input b <== 3;
|
||||
signal o[44] <-- 2;
|
||||
a[1][2].b[3] <== 4
|
||||
function f(a,b) {
|
||||
c=a+b;
|
||||
}
|
||||
|
||||
component b(b1,b2) {
|
||||
b1 <== b2[3].pin[4]
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
if (a) b; else c;
|
||||
if (a) {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
}
|
||||
if (1) if(0) 1; else 2;
|
||||
@@ -1,4 +0,0 @@
|
||||
var a=0;
|
||||
for (i=0; i<10; i=i+1) {
|
||||
a=a+1;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
++a;
|
||||
a++;
|
||||
b--;
|
||||
--b;
|
||||
a -- > 0;
|
||||
0 < ++ b;
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
a=3
|
||||
a+=3
|
||||
a-=3
|
||||
a*=3
|
||||
a/=3
|
||||
a%=3
|
||||
a>>=3
|
||||
a<<=3
|
||||
a&=3
|
||||
a|=3
|
||||
a^=3
|
||||
@@ -1,6 +0,0 @@
|
||||
while (1) {
|
||||
a
|
||||
}
|
||||
do {
|
||||
xx;
|
||||
} while (1)
|
||||
@@ -1,9 +0,0 @@
|
||||
function f() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
function ff(a) {
|
||||
return 4;
|
||||
}
|
||||
|
||||
component c = s(a);
|
||||
Reference in New Issue
Block a user