First commit

This commit is contained in:
Jordi Baylina
2018-08-09 08:21:17 +02:00
commit 281b52339c
62 changed files with 25533 additions and 0 deletions

61
test/commandline.js Normal file
View File

@@ -0,0 +1,61 @@
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);
});

2
test/jorge.prg Normal file
View File

@@ -0,0 +1,2 @@
1 * 2 * 3
* 4

141
test/mainTest.js Normal file
View File

@@ -0,0 +1,141 @@
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);
});
});

45
test/test1.jaz Normal file
View File

@@ -0,0 +1,45 @@
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
test/test10.jaz Normal file
View File

@@ -0,0 +1 @@
f(1,2)

9
test/test11.jaz Normal file
View File

@@ -0,0 +1,9 @@
a+b*c
(a+b)*c
var c + a[1].b[2] + c
f(1,2)
component f(4) d;
a++ + b++;
--a;

5
test/test12.jaz Normal file
View File

@@ -0,0 +1,5 @@
var a;
var b;
var c;
a+b*b[3+a][2];
a<<2<=2>>a

3
test/test13.jaz Normal file
View File

@@ -0,0 +1,3 @@
a <== b === c
r = a ? c ? 5 : 6 : b ? 3 : 4
c = sqrt(a**2+b**2)

8
test/test14.jaz Normal file
View File

@@ -0,0 +1,8 @@
(a + bb) + c;
include "filename.ext"
include "secondfile"
include "therdfile";
include "4th file"
{
include "fifthfile"
}

13
test/test2.jaz Normal file
View File

@@ -0,0 +1,13 @@
{
3+3
4+4
5+5
}
6
7
a <-- 3
3 --> a;
signal input b <== 4;
4 ==> signal b;
c === d;

10
test/test3.jaz Normal file
View File

@@ -0,0 +1,10 @@
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]
}

7
test/test4.jaz Normal file
View File

@@ -0,0 +1,7 @@
if (a) b; else c;
if (a) {
1
} else {
2
}
if (1) if(0) 1; else 2;

4
test/test5.jaz Normal file
View File

@@ -0,0 +1,4 @@
var a=0;
for (i=0; i<10; i=i+1) {
a=a+1;
}

7
test/test6.jaz Normal file
View File

@@ -0,0 +1,7 @@
++a;
a++;
b--;
--b;
a -- > 0;
0 < ++ b;

11
test/test7.jaz Normal file
View File

@@ -0,0 +1,11 @@
a=3
a+=3
a-=3
a*=3
a/=3
a%=3
a>>=3
a<<=3
a&=3
a|=3
a^=3

6
test/test8.jaz Normal file
View File

@@ -0,0 +1,6 @@
while (1) {
a
}
do {
xx;
} while (1)

9
test/test9.jaz Normal file
View File

@@ -0,0 +1,9 @@
function f() {
return 3;
}
function ff(a) {
return 4;
}
component c = s(a);