@ -1,20 +0,0 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
|||
<plist version="1.0"> |
|||
<dict> |
|||
<key>CFBundleDevelopmentRegion</key> |
|||
<string>English</string> |
|||
<key>CFBundleIdentifier</key> |
|||
<string>com.apple.xcode.dsym.pedersen_test</string> |
|||
<key>CFBundleInfoDictionaryVersion</key> |
|||
<string>6.0</string> |
|||
<key>CFBundlePackageType</key> |
|||
<string>dSYM</string> |
|||
<key>CFBundleSignature</key> |
|||
<string>????</string> |
|||
<key>CFBundleShortVersionString</key> |
|||
<string>1.0</string> |
|||
<key>CFBundleVersion</key> |
|||
<string>1</string> |
|||
</dict> |
|||
</plist> |
@ -1,172 +1,117 @@ |
|||
const chai = require("chai"); |
|||
const path = require("path"); |
|||
const snarkjs = require("snarkjs"); |
|||
const compiler = require("circom"); |
|||
const bigInt = require("big-integer"); |
|||
const tester = require("circom").tester; |
|||
const babyJub = require("../src/babyjub.js"); |
|||
|
|||
const assert = chai.assert; |
|||
|
|||
const bigInt = snarkjs.bigInt; |
|||
|
|||
|
|||
const q=bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617"); |
|||
function addPoint(a,b) { |
|||
const cta = bigInt("168700"); |
|||
const d = bigInt("168696"); |
|||
|
|||
const res = []; |
|||
res[0] = bigInt((a[0]*b[1] + b[0]*a[1]) * bigInt(bigInt.one + 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(bigInt.one - 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", "escalarmulw4table_test.circom")); |
|||
describe("Exponentioation test", function () { |
|||
|
|||
// console.log(JSON.stringify(cirDef, null, 1));
|
|||
this.timeout(100000); |
|||
|
|||
// assert.equal(cirDef.nVars, 2);
|
|||
it("Should generate the Exponentiation table in k=0", async () => { |
|||
|
|||
const circuit = new snarkjs.Circuit(cirDef); |
|||
const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test.circom")); |
|||
|
|||
console.log("NConstrains: " + circuit.nConstraints); |
|||
const w = await circuit.calculateWitness({in: 1}); |
|||
|
|||
const w = circuit.calculateWitness({in: 1}); |
|||
// TODO
|
|||
// assert(circuit.checkWitness(w));
|
|||
|
|||
assert(circuit.checkWitness(w)); |
|||
let g = [ |
|||
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
|||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") |
|||
]; |
|||
|
|||
let g = [bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
|||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")] |
|||
let dbl= [bigInt("0"), bigInt("1")]; |
|||
|
|||
dbl= [bigInt("0"), snarkjs.bigInt("1")]; |
|||
const expectedOut = []; |
|||
|
|||
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); |
|||
expectedOut.push(dbl); |
|||
dbl = babyJub.addPoint(dbl,g); |
|||
} |
|||
|
|||
await circuit.assertOut(w, {out: expectedOut}); |
|||
|
|||
}); |
|||
|
|||
it("Should generate the Exponentiation table in k=3", async () => { |
|||
|
|||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom")); |
|||
|
|||
// console.log(JSON.stringify(cirDef, null, 1));
|
|||
|
|||
// assert.equal(cirDef.nVars, 2);
|
|||
const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom")); |
|||
|
|||
const circuit = new snarkjs.Circuit(cirDef); |
|||
const w = await circuit.calculateWitness({in: 1}); |
|||
|
|||
console.log("NConstrains: " + circuit.nConstraints); |
|||
// TODO
|
|||
// assert(circuit.checkWitness(w));
|
|||
|
|||
const w = circuit.calculateWitness({in: 1}); |
|||
|
|||
assert(circuit.checkWitness(w)); |
|||
|
|||
let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
|||
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")] |
|||
let g = [ |
|||
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
|||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") |
|||
]; |
|||
|
|||
for (let i=0; i<12;i++) { |
|||
g = addPoint(g,g); |
|||
g = babyJub.addPoint(g,g); |
|||
} |
|||
|
|||
dbl= [snarkjs.bigInt("0"), snarkjs.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]`)]; |
|||
let dbl= [bigInt("0"), bigInt("1")]; |
|||
|
|||
const expectedOut = []; |
|||
|
|||
// 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])); |
|||
for (let i=0; i<16; i++) { |
|||
expectedOut.push(dbl); |
|||
|
|||
dbl = addPoint([xout1, yout1],g); |
|||
dbl = babyJub.addPoint(dbl,g); |
|||
} |
|||
|
|||
await circuit.assertOut(w, {out: expectedOut}); |
|||
|
|||
}); |
|||
|
|||
it("Should exponentiate g^31", async () => { |
|||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmul_test.circom"), {reduceConstraints: true}); |
|||
|
|||
// console.log(JSON.stringify(cirDef, null, 1));
|
|||
const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test.circom")); |
|||
|
|||
// assert.equal(cirDef.nVars, 2);
|
|||
const w = await circuit.calculateWitness({"in": 31}); |
|||
|
|||
const circuit = new snarkjs.Circuit(cirDef); |
|||
// TODO
|
|||
// assert(circuit.checkWitness(w));
|
|||
|
|||
console.log("NConstrains: " + circuit.nConstraints); |
|||
let g = [ |
|||
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
|||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") |
|||
]; |
|||
|
|||
const w = circuit.calculateWitness({"in": 31}); |
|||
|
|||
assert(circuit.checkWitness(w)); |
|||
|
|||
let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
|||
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")] |
|||
|
|||
let c = [0n, 1n]; |
|||
let c = [bigInt(0), bigInt(1)]; |
|||
|
|||
for (let i=0; i<31;i++) { |
|||
c = addPoint(c,g); |
|||
c = babyJub.addPoint(c,g); |
|||
} |
|||
|
|||
const xout = w[circuit.getSignalIdx(`main.out[0]`)]; |
|||
const yout = w[circuit.getSignalIdx(`main.out[1]`)]; |
|||
await circuit.assertOut(w, {out: c}); |
|||
|
|||
/* |
|||
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]`)]; |
|||
const w2 = await circuit.calculateWitness({"in": bigInt(1).shiftLeft(252).add(bigInt.one)}); |
|||
|
|||
c = [g[0], g[1]]; |
|||
for (let i=0; i<252;i++) { |
|||
c = addPoint(c,c); |
|||
c = babyJub.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());
|
|||
c = babyJub.addPoint(c,g); |
|||
|
|||
assert(xout2.equals(c[0])); |
|||
assert(yout2.equals(c[1])); |
|||
await circuit.assertOut(w2, {out: c}); |
|||
|
|||
}).timeout(10000000); |
|||
|
|||
it("Number of constrains for 256 bits", async () => { |
|||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmul_test_min.circom")); |
|||
|
|||
const circuit = new snarkjs.Circuit(cirDef); |
|||
const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test_min.circom")); |
|||
|
|||
console.log("NConstrains: " + circuit.nConstraints); |
|||
}).timeout(10000000); |
|||
|
|||
}); |
@ -1,35 +1,26 @@ |
|||
const chai = require("chai"); |
|||
const path = require("path"); |
|||
const snarkjs = require("snarkjs"); |
|||
const compiler = require("circom"); |
|||
const tester = require("circom").tester; |
|||
|
|||
const mimcjs = require("../src/mimc7.js"); |
|||
|
|||
const assert = chai.assert; |
|||
|
|||
describe("MiMC Circuit test", function () { |
|||
let circuit; |
|||
|
|||
this.timeout(100000); |
|||
|
|||
before( async () => { |
|||
const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_test.circom")); |
|||
|
|||
circuit = new snarkjs.Circuit(cirDef); |
|||
|
|||
console.log("MiMC constraints: " + circuit.nConstraints); |
|||
circuit = await tester(path.join(__dirname, "circuits", "mimc_test.circom")); |
|||
}); |
|||
|
|||
it("Should check constrain", async () => { |
|||
const w = circuit.calculateWitness({x_in: 1, k: 2}); |
|||
|
|||
const res = w[circuit.getSignalIdx("main.out")]; |
|||
const w = await circuit.calculateWitness({x_in: 1, k: 2}); |
|||
|
|||
const res2 = mimcjs.hash(1,2,91); |
|||
|
|||
assert.equal(res.toString(), res2.toString()); |
|||
|
|||
assert(circuit.checkWitness(w)); |
|||
await circuit.assertOut(w, {out: res2}); |
|||
|
|||
// TODO
|
|||
// assert(circuit.checkWitness(w));
|
|||
}); |
|||
}); |
@ -1,23 +0,0 @@ |
|||
|
|||
include "../circuits/smt/smtverifier.circom"; |
|||
template SMT(nLevels) { |
|||
signal input root; |
|||
signal input mtp[nLevels]; |
|||
signal input hi; |
|||
signal input hv; |
|||
|
|||
component smtClaimExists = SMTVerifier(nLevels); |
|||
smtClaimExists.enabled <== 1; |
|||
smtClaimExists.fnc <== 0; |
|||
smtClaimExists.root <== root; |
|||
for (var i=0; i<nLevels; i++) { |
|||
smtClaimExists.siblings[i] <== mtp[i]; |
|||
} |
|||
smtClaimExists.oldKey <== 0; |
|||
smtClaimExists.oldValue <== 0; |
|||
smtClaimExists.isOld0 <== 0; |
|||
|
|||
smtClaimExists.key <== hi; |
|||
smtClaimExists.value <== hv; |
|||
} |
|||
component main = SMT(4); |
@ -1,98 +0,0 @@ |
|||
const path = require("path"); |
|||
const snarkjs = require("snarkjs"); |
|||
const compiler = require("circom"); |
|||
const fs = require("fs") |
|||
|
|||
const bigInt = snarkjs.bigInt; |
|||
const smt = require("../src/smt.js"); |
|||
|
|||
const circuitSource = `
|
|||
include "../circuits/smt/smtverifier.circom"; |
|||
template SMT(nLevels) { |
|||
signal input root; |
|||
signal input mtp[nLevels]; |
|||
signal input hi; |
|||
signal input hv; |
|||
|
|||
component smtClaimExists = SMTVerifier(nLevels); |
|||
smtClaimExists.enabled <== 1; |
|||
smtClaimExists.fnc <== 0; |
|||
smtClaimExists.root <== root; |
|||
for (var i=0; i<nLevels; i++) { |
|||
smtClaimExists.siblings[i] <== mtp[i]; |
|||
} |
|||
smtClaimExists.oldKey <== 0; |
|||
smtClaimExists.oldValue <== 0; |
|||
smtClaimExists.isOld0 <== 0; |
|||
|
|||
smtClaimExists.key <== hi; |
|||
smtClaimExists.value <== hv; |
|||
} |
|||
component main = SMT(4); |
|||
`;
|
|||
|
|||
describe("smt3test", function () { |
|||
this.timeout(200000); |
|||
|
|||
let circuitFileName; |
|||
|
|||
before( async () => { |
|||
circuitFileName = path.join(__dirname, ".", "rawsmt3.circom"); |
|||
fs.writeFileSync(circuitFileName,circuitSource); |
|||
}); |
|||
|
|||
const levels = 4; |
|||
async function testsmt3(e1, e2) { |
|||
let tree = await smt.newMemEmptyTrie(); |
|||
|
|||
// insert e1, e2
|
|||
await tree.insert(e1.hi, e1.hv); |
|||
await tree.insert(e2.hi, e2.hv); |
|||
|
|||
// generate proof for e1
|
|||
const findInfo = await tree.find(e1.hi); |
|||
const siblings = findInfo.siblings; |
|||
while (siblings.length < levels) siblings.push(bigInt(0)); |
|||
|
|||
const input = { |
|||
root: tree.root, |
|||
mtp: siblings, |
|||
hi: e1.hi, |
|||
hv: e1.hv, |
|||
}; |
|||
|
|||
const compiledCircuit = await compiler( |
|||
circuitFileName, |
|||
{ reduceConstraints: false } |
|||
); |
|||
|
|||
const circuit = new snarkjs.Circuit(compiledCircuit); |
|||
const witness = circuit.calculateWitness(input); |
|||
circuit.checkWitness(witness); |
|||
} |
|||
|
|||
it("TestSmts", async () => { |
|||
|
|||
const e1 = { |
|||
hi: bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839"), |
|||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"), |
|||
}; |
|||
|
|||
const e2ok = { |
|||
hi: bigInt("16498254692537945203721083102154618658340563351558973077349594629411025251262"), |
|||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"), |
|||
}; |
|||
|
|||
const e2fail = { |
|||
hi: bigInt("17195092312975762537892237130737365903429674363577646686847513978084990105579"), |
|||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"), |
|||
}; |
|||
|
|||
console.log("test e1, e2ok"); |
|||
await testsmt3(e1, e2ok); |
|||
|
|||
console.log("test e1, e2fail"); |
|||
await testsmt3(e1, e2fail); |
|||
}); |
|||
}); |
|||
|