mirror of
https://github.com/arnaucube/circomlib.git
synced 2026-02-07 03:06:44 +01:00
Compare commits
8 Commits
feature/sy
...
v0.0.6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7792887216 | ||
|
|
7971f0150e | ||
|
|
b3ff8b246d | ||
|
|
951e51423d | ||
|
|
9a5294dea1 | ||
|
|
2635e8d3c9 | ||
|
|
138945bfdc | ||
|
|
2f9ad59c3a |
@@ -1,14 +1,15 @@
|
||||
const bn128 = require("snarkjs").bn128;
|
||||
const bigInt = require("snarkjs").bigInt;
|
||||
const createBlakeHash = require("blake-hash");
|
||||
const assert = require("assert");
|
||||
const babyJub = require("../src/babyjub");
|
||||
|
||||
function getPoint(S) {
|
||||
const F = bn128.Fr;
|
||||
const h = createBlakeHash("blake256").update(S).digest();
|
||||
|
||||
assert(h.length == 32);
|
||||
if (h.length != 32) {
|
||||
throw new Error("Invalid length")
|
||||
}
|
||||
|
||||
let sign = false;
|
||||
if (h[31] & 0x80) {
|
||||
@@ -52,7 +53,9 @@ function generatePoint(S) {
|
||||
p = getPoint(S+"_"+sidx);
|
||||
idx++;
|
||||
}
|
||||
assert(babyJub.inCurve(p), "Point not in curve");
|
||||
if (!babyJub.inCurve(p)){
|
||||
throw new Error("Point not in curve");
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
BIN
doc/rollup_tree.monopic
Normal file
BIN
doc/rollup_tree.monopic
Normal file
Binary file not shown.
Binary file not shown.
1
index.js
1
index.js
@@ -1,3 +1,4 @@
|
||||
exports.smt = require("./src/smt");
|
||||
exports.eddsa = require("./src/eddsa");
|
||||
exports.mimc7 = require("./src/mimc7");
|
||||
exports.babyJub = require("./src/babyjub");
|
||||
|
||||
774
package-lock.json
generated
774
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circomlib",
|
||||
"version": "0.0.5",
|
||||
"version": "0.0.6",
|
||||
"description": "Basic circuits library for Circom",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
@@ -25,7 +25,7 @@
|
||||
"license": "GPL-3.0",
|
||||
"dependencies": {
|
||||
"blake-hash": "^1.1.0",
|
||||
"snarkjs": "0.1.9",
|
||||
"snarkjs": "0.1.11",
|
||||
"web3": "^1.0.0-beta.36"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -3,9 +3,7 @@ const bigInt = require("snarkjs").bigInt;
|
||||
const babyJub = require("./babyjub");
|
||||
const pedersenHash = require("./pedersenHash").hash;
|
||||
const mimc7 = require("./mimc7");
|
||||
const crypto = require("crypto");
|
||||
|
||||
exports.cratePrvKey = cratePrvKey;
|
||||
exports.prv2pub= prv2pub;
|
||||
exports.sign = sign;
|
||||
exports.signMiMC = signMiMC;
|
||||
@@ -13,12 +11,9 @@ exports.verify = verify;
|
||||
exports.verifyMiMC = verifyMiMC;
|
||||
exports.packSignature = packSignature;
|
||||
exports.unpackSignature = unpackSignature;
|
||||
exports.pruneBuffer = pruneBuffer;
|
||||
|
||||
|
||||
function cratePrvKey() {
|
||||
return crypto.randomBytes(32);
|
||||
}
|
||||
|
||||
function pruneBuffer(_buff) {
|
||||
const buff = Buffer.from(_buff);
|
||||
buff[0] = buff[0] & 0xF8;
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
//
|
||||
|
||||
|
||||
const Web3 = require("web3");
|
||||
const assert = require("assert");
|
||||
const Web3Utils = require("web3-utils");
|
||||
|
||||
class Contract {
|
||||
constructor() {
|
||||
@@ -39,7 +38,7 @@ class Contract {
|
||||
genLoadedLength = C.code.length;
|
||||
}
|
||||
|
||||
return Web3.utils.bytesToHex(C.code.concat(this.code));
|
||||
return Web3Utils.bytesToHex(C.code.concat(this.code));
|
||||
}
|
||||
|
||||
stop() { this.code.push(0x00); }
|
||||
@@ -141,7 +140,9 @@ class Contract {
|
||||
msize() { this.code.push(0x59); }
|
||||
gas() { this.code.push(0x5a); }
|
||||
label(name) {
|
||||
assert(typeof this.labels[name] == "undefined", "Label already defined");
|
||||
if (typeof this.labels[name] != "undefined") {
|
||||
throw new Error("Label already defined");
|
||||
}
|
||||
this.labels[name] = this.code.length;
|
||||
this.code.push(0x5b);
|
||||
|
||||
@@ -149,21 +150,24 @@ class Contract {
|
||||
}
|
||||
|
||||
push(data) {
|
||||
const d = Web3.utils.hexToBytes(Web3.utils.toHex(data));
|
||||
assert(d.length>0);
|
||||
assert(d.length<=32);
|
||||
const d = Web3Utils.hexToBytes(Web3Utils.toHex(data));
|
||||
if (d.length == 0 || d.length > 32) {
|
||||
throw new Error("Assertion failed");
|
||||
}
|
||||
this.code = this.code.concat([0x5F + d.length], d);
|
||||
}
|
||||
|
||||
dup(n) {
|
||||
assert(n>=0);
|
||||
assert(n<16);
|
||||
if (n < 0 || n >= 16) {
|
||||
throw new Error("Assertion failed");
|
||||
}
|
||||
this.code.push(0x80 + n);
|
||||
}
|
||||
|
||||
swap(n) {
|
||||
assert(n>=1);
|
||||
assert(n<=16);
|
||||
if (n < 1 || n > 16) {
|
||||
throw new Error("Assertion failed");
|
||||
}
|
||||
this.code.push(0x8f + n);
|
||||
}
|
||||
|
||||
|
||||
16
src/mimc7.js
16
src/mimc7.js
@@ -1,6 +1,6 @@
|
||||
const bn128 = require("snarkjs").bn128;
|
||||
const bigInt = require("snarkjs").bigInt;
|
||||
const Web3 = require("web3");
|
||||
const Web3Utils = require("web3-utils");
|
||||
const F = bn128.Fr;
|
||||
|
||||
const SEED = "mimc";
|
||||
@@ -8,8 +8,8 @@ const NROUNDS = 91;
|
||||
|
||||
exports.getIV = (seed) => {
|
||||
if (typeof seed === "undefined") seed = SEED;
|
||||
const c = Web3.utils.keccak256(seed+"_iv");
|
||||
const cn = bigInt(Web3.utils.toBN(c).toString());
|
||||
const c = Web3Utils.keccak256(seed+"_iv");
|
||||
const cn = bigInt(Web3Utils.toBN(c).toString());
|
||||
const iv = cn.mod(F.q);
|
||||
return iv;
|
||||
};
|
||||
@@ -18,13 +18,13 @@ exports.getConstants = (seed, nRounds) => {
|
||||
if (typeof seed === "undefined") seed = SEED;
|
||||
if (typeof nRounds === "undefined") nRounds = NROUNDS;
|
||||
const cts = new Array(nRounds);
|
||||
let c = Web3.utils.keccak256(SEED);
|
||||
let c = Web3Utils.keccak256(SEED);
|
||||
for (let i=1; i<nRounds; i++) {
|
||||
c = Web3.utils.keccak256(c);
|
||||
c = Web3Utils.keccak256(c);
|
||||
|
||||
const n1 = Web3.utils.toBN(c).mod(Web3.utils.toBN(F.q.toString()));
|
||||
const c2 = Web3.utils.padLeft(Web3.utils.toHex(n1), 64);
|
||||
cts[i] = bigInt(Web3.utils.toBN(c2).toString());
|
||||
const n1 = Web3Utils.toBN(c).mod(Web3Utils.toBN(F.q.toString()));
|
||||
const c2 = Web3Utils.padLeft(Web3Utils.toHex(n1), 64);
|
||||
cts[i] = bigInt(Web3Utils.toBN(c2).toString());
|
||||
}
|
||||
cts[0] = bigInt(0);
|
||||
return cts;
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
// License: LGPL-3.0+
|
||||
//
|
||||
|
||||
const Web3 = require("web3");
|
||||
const Web3Utils = require("web3-utils");
|
||||
|
||||
const Contract = require("./evmasm");
|
||||
|
||||
function createCode(seed, n) {
|
||||
|
||||
let ci = Web3.utils.keccak256(seed);
|
||||
let ci = Web3Utils.keccak256(seed);
|
||||
|
||||
const C = new Contract();
|
||||
|
||||
@@ -51,7 +51,7 @@ function createCode(seed, n) {
|
||||
C.mulmod(); // r=t^7 k q
|
||||
|
||||
for (let i=0; i<n-1; i++) {
|
||||
ci = Web3.utils.keccak256(ci);
|
||||
ci = Web3Utils.keccak256(ci);
|
||||
C.dup(2); // q r k q
|
||||
C.dup(0); // q q r k q
|
||||
C.dup(0); // q q q r k q
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
const bn128 = require("snarkjs").bn128;
|
||||
const bigInt = require("snarkjs").bigInt;
|
||||
const babyJub = require("./babyjub");
|
||||
const assert = require("assert");
|
||||
const createBlakeHash = require("blake-hash");
|
||||
|
||||
const GENPOINT_PREFIX = "PedersenGenerator";
|
||||
@@ -73,7 +72,9 @@ function getBasePoint(pointIdx) {
|
||||
|
||||
const p8 = babyJub.mulPointEscalar(p, 8);
|
||||
|
||||
assert(babyJub.inSubgroup(p8), "Point not in curve");
|
||||
if (!babyJub.inSubgroup(p8)) {
|
||||
throw new Error("Point not in curve");
|
||||
}
|
||||
|
||||
bases[pointIdx] = p8;
|
||||
return p8;
|
||||
|
||||
@@ -2,6 +2,7 @@ const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
// const crypto = require("crypto");
|
||||
|
||||
const eddsa = require("../src/eddsa.js");
|
||||
const babyJub = require("../src/babyjub.js");
|
||||
@@ -45,7 +46,7 @@ describe("EdDSA test", function () {
|
||||
it("Sign a single 10 bytes from 0 to 9", async () => {
|
||||
const msg = Buffer.from("00010203040506070809", "hex");
|
||||
|
||||
// const prvKey = eddsa.cratePrvKey();
|
||||
// const prvKey = crypto.randomBytes(32);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
|
||||
@@ -1,231 +0,0 @@
|
||||
const chai = require("chai");
|
||||
const snarkjs = require("snarkjs");
|
||||
|
||||
const smt = require("../src/smt.js");
|
||||
const mimcjs = require("../src/mimc7.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
const expect = chai.expect;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
function stringifyBigInts(o) {
|
||||
if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
|
||||
return o.toString(10);
|
||||
} else if (Array.isArray(o)) {
|
||||
return o.map(stringifyBigInts);
|
||||
} else if (typeof o == "object") {
|
||||
const res = {};
|
||||
for (let k in o) {
|
||||
res[k] = stringifyBigInts(o[k]);
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
function newEntryFromInts(a, b, c, d) {
|
||||
return {
|
||||
hi: mimcjs.hash(c, d),
|
||||
hv: mimcjs.hash(a, b),
|
||||
};
|
||||
}
|
||||
|
||||
function smtHash(arr) {
|
||||
let r = bigInt(0);
|
||||
for (let i=0; i<arr.length; i++) {
|
||||
r = mimcjs.hash(r, bigInt(arr[i]), 91 );
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
describe("SMT Javascript test", function () {
|
||||
this.timeout(100000);
|
||||
before( async () => {
|
||||
});
|
||||
|
||||
it("test MIMC7", async() => {
|
||||
//const h = mimcjs.multiHash(1,2,3);
|
||||
//const h = smtHash([1,2,3]);
|
||||
//console.log(h.toString(10));
|
||||
|
||||
const mimcjs = require("../src/mimc7.js");
|
||||
const snarkjs = require("snarkjs");
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const smt = require("../src/smt.js");
|
||||
|
||||
h = mimcjs.hash(1,2,91);
|
||||
console.log(h);
|
||||
|
||||
function smtHash(arr) {
|
||||
let r = bigInt(0);
|
||||
for (let i=0; i<arr.length; i++) {
|
||||
r = mimcjs.hash(r, bigInt(arr[i]), 91 );
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
console.log(smtHash([1, 2, 3]));
|
||||
// Test from old ver
|
||||
});
|
||||
|
||||
//it("Should calculate hindex", async() => {
|
||||
// const entry = newEntryFromInts(12, 45, 78, 41);
|
||||
// //console.log(entry.hi.toString(16));
|
||||
// expect(entry.hi.toString(16)).to.be.
|
||||
// equal("114438e8321f62c4a1708f443a5a66f9c8fcb0958e7b7008332b71442610b7a0");
|
||||
//});
|
||||
|
||||
//it("Should insert 2 elements and empty them", async () => {
|
||||
// const tree = await smt.newMemEmptyTrie();
|
||||
// const key1 = bigInt(111);
|
||||
// const value1 = bigInt(222);
|
||||
// const key2 = bigInt(333);
|
||||
// const value2 = bigInt(444);
|
||||
|
||||
// await tree.insert(key1,value1);
|
||||
// await tree.insert(key2,value2);
|
||||
// await tree.delete(key2);
|
||||
// await tree.delete(key1);
|
||||
|
||||
// assert(tree.root.isZero());
|
||||
//});
|
||||
|
||||
//it("Should insert 3 elements in dferent order and should be the same", async () => {
|
||||
// const keys = [bigInt(8), bigInt(9), bigInt(32)];
|
||||
// const values = [bigInt(88), bigInt(99), bigInt(3232)];
|
||||
// const tree1 = await smt.newMemEmptyTrie();
|
||||
// const tree2 = await smt.newMemEmptyTrie();
|
||||
// const tree3 = await smt.newMemEmptyTrie();
|
||||
// const tree4 = await smt.newMemEmptyTrie();
|
||||
// const tree5 = await smt.newMemEmptyTrie();
|
||||
// const tree6 = await smt.newMemEmptyTrie();
|
||||
|
||||
// await tree1.insert(keys[0],values[0]);
|
||||
// await tree1.insert(keys[1],values[1]);
|
||||
// await tree1.insert(keys[2],values[2]);
|
||||
|
||||
// await tree2.insert(keys[0],values[0]);
|
||||
// await tree2.insert(keys[2],values[2]);
|
||||
// await tree2.insert(keys[1],values[1]);
|
||||
|
||||
// await tree3.insert(keys[1],values[1]);
|
||||
// await tree3.insert(keys[0],values[0]);
|
||||
// await tree3.insert(keys[2],values[2]);
|
||||
|
||||
// await tree4.insert(keys[1],values[1]);
|
||||
// await tree4.insert(keys[2],values[2]);
|
||||
// await tree4.insert(keys[0],values[0]);
|
||||
|
||||
// await tree5.insert(keys[2],values[2]);
|
||||
// await tree5.insert(keys[0],values[0]);
|
||||
// await tree5.insert(keys[1],values[1]);
|
||||
|
||||
// await tree6.insert(keys[2],values[2]);
|
||||
// await tree6.insert(keys[1],values[1]);
|
||||
// await tree6.insert(keys[0],values[0]);
|
||||
|
||||
// assert(tree1.root.equals(tree2.root));
|
||||
// assert(tree2.root.equals(tree3.root));
|
||||
// assert(tree3.root.equals(tree4.root));
|
||||
// assert(tree4.root.equals(tree5.root));
|
||||
// assert(tree5.root.equals(tree6.root));
|
||||
|
||||
// assert.equal(Object.keys(tree1.db.nodes).length, Object.keys(tree2.db.nodes).length);
|
||||
// assert.equal(Object.keys(tree2.db.nodes).length, Object.keys(tree3.db.nodes).length);
|
||||
// assert.equal(Object.keys(tree3.db.nodes).length, Object.keys(tree4.db.nodes).length);
|
||||
// assert.equal(Object.keys(tree4.db.nodes).length, Object.keys(tree5.db.nodes).length);
|
||||
// assert.equal(Object.keys(tree5.db.nodes).length, Object.keys(tree6.db.nodes).length);
|
||||
|
||||
// await tree1.delete(keys[0]);
|
||||
// await tree1.delete(keys[1]);
|
||||
// await tree2.delete(keys[1]);
|
||||
// await tree2.delete(keys[0]);
|
||||
// assert(tree1.root.equals(tree2.root));
|
||||
|
||||
// await tree3.delete(keys[0]);
|
||||
// await tree3.delete(keys[2]);
|
||||
// await tree4.delete(keys[2]);
|
||||
// await tree4.delete(keys[0]);
|
||||
// assert(tree3.root.equals(tree4.root));
|
||||
|
||||
// await tree5.delete(keys[1]);
|
||||
// await tree5.delete(keys[2]);
|
||||
// await tree6.delete(keys[2]);
|
||||
// await tree6.delete(keys[1]);
|
||||
// assert(tree5.root.equals(tree6.root));
|
||||
|
||||
// await tree1.delete(keys[2]);
|
||||
// await tree2.delete(keys[2]);
|
||||
// await tree3.delete(keys[1]);
|
||||
// await tree4.delete(keys[1]);
|
||||
// await tree5.delete(keys[0]);
|
||||
// await tree6.delete(keys[0]);
|
||||
|
||||
// assert(tree1.root.isZero());
|
||||
// assert(tree2.root.isZero());
|
||||
// assert(tree3.root.isZero());
|
||||
// assert(tree4.root.isZero());
|
||||
// assert(tree5.root.isZero());
|
||||
// assert(tree6.root.isZero());
|
||||
|
||||
// assert.equal(Object.keys(tree1.db.nodes).length, 0);
|
||||
// assert.equal(Object.keys(tree2.db.nodes).length, 0);
|
||||
// assert.equal(Object.keys(tree3.db.nodes).length, 0);
|
||||
// assert.equal(Object.keys(tree4.db.nodes).length, 0);
|
||||
// assert.equal(Object.keys(tree5.db.nodes).length, 0);
|
||||
// assert.equal(Object.keys(tree6.db.nodes).length, 0);
|
||||
//});
|
||||
|
||||
//it("Insert and remove 100 numbers randomly", async () => {
|
||||
// function perm(a) {
|
||||
// const arr = a.slice();
|
||||
// const rArr = [];
|
||||
// for (let i=0; i<arr.length; i++) {
|
||||
// let rIdx = Math.floor(Math.random() * (arr.length - i));
|
||||
// rArr.push(arr[rIdx]);
|
||||
// arr[rIdx] = arr[arr.length - i - 1];
|
||||
// }
|
||||
// return rArr;
|
||||
// }
|
||||
// const tree = await smt.newMemEmptyTrie();
|
||||
// const arr = [];
|
||||
// const N = 100;
|
||||
// for (let i=0; i<N; i++) {
|
||||
// arr.push(bigInt(i));
|
||||
// }
|
||||
// const insArr = perm(arr);
|
||||
// for (let i=0; i<N; i++) {
|
||||
// await tree.insert(insArr[i], i);
|
||||
// }
|
||||
// const delArr = perm(insArr);
|
||||
// for (let i=0; i<N; i++) {
|
||||
// await tree.delete(delArr[i]);
|
||||
// }
|
||||
|
||||
// assert(tree.root.isZero());
|
||||
// assert.equal(Object.keys(tree.db.nodes).length, 0);
|
||||
//});
|
||||
|
||||
//it("Should test update", async () => {
|
||||
// const tree1 = await smt.newMemEmptyTrie();
|
||||
// const tree2 = await smt.newMemEmptyTrie();
|
||||
|
||||
// await tree1.insert(8,88);
|
||||
// await tree1.insert(9,99,);
|
||||
// await tree1.insert(32,3232);
|
||||
|
||||
// await tree2.insert(8,888);
|
||||
// await tree2.insert(9,999);
|
||||
// await tree2.insert(32,323232);
|
||||
|
||||
// await tree1.update(8, 888);
|
||||
// await tree1.update(9, 999);
|
||||
// await tree1.update(32, 323232);
|
||||
|
||||
// assert(tree1.root.equals(tree2.root));
|
||||
//});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user