2018-11-27 18:03:57 +03:00
|
|
|
const chai = require("chai");
|
|
|
|
const path = require("path");
|
2023-07-21 13:10:01 +03:00
|
|
|
const snarkjs = require("@tornado/snarkjs");
|
2018-11-27 18:03:57 +03:00
|
|
|
const compiler = require("circom");
|
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
|
|
|
|
const bigInt = snarkjs.bigInt;
|
|
|
|
|
|
|
|
const babyJub = require("../src/babyjub.js");
|
|
|
|
const pedersen = require("../src/pedersenHash.js");
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
describe("Pedersen test", function () {
|
2018-11-27 18:03:57 +03:00
|
|
|
let circuit;
|
|
|
|
this.timeout(100000);
|
2023-07-21 13:10:01 +03:00
|
|
|
before(async () => {
|
2018-11-27 18:03:57 +03:00
|
|
|
const cirDef = await compiler(path.join(__dirname, "circuits", "pedersen2_test.circom"));
|
|
|
|
|
|
|
|
circuit = new snarkjs.Circuit(cirDef);
|
|
|
|
|
|
|
|
console.log("NConstrains Pedersen2: " + circuit.nConstraints);
|
|
|
|
});
|
|
|
|
it("Should pedersen at zero", async () => {
|
|
|
|
let w, xout, yout;
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
w = circuit.calculateWitness({ in: 0 });
|
2018-11-27 18:03:57 +03:00
|
|
|
|
|
|
|
xout = w[circuit.getSignalIdx("main.out[0]")];
|
|
|
|
yout = w[circuit.getSignalIdx("main.out[1]")];
|
|
|
|
|
|
|
|
const b = Buffer.alloc(32);
|
|
|
|
|
|
|
|
const h = pedersen.hash(b);
|
|
|
|
const hP = babyJub.unpackPoint(h);
|
|
|
|
|
|
|
|
/*
|
|
|
|
console.log(`[${xout.toString()}, ${yout.toString()}]`);
|
|
|
|
console.log(`[${hP[0].toString()}, ${hP[1].toString()}]`);
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert(xout.equals(hP[0]));
|
|
|
|
assert(yout.equals(hP[1]));
|
|
|
|
});
|
|
|
|
it("Should pedersen with 253 ones", async () => {
|
|
|
|
let w, xout, yout;
|
|
|
|
|
|
|
|
const n = bigInt.one.shl(253).sub(bigInt.one);
|
|
|
|
console.log(n.toString(16));
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
w = circuit.calculateWitness({ in: n });
|
2018-11-27 18:03:57 +03:00
|
|
|
|
|
|
|
xout = w[circuit.getSignalIdx("main.out[0]")];
|
|
|
|
yout = w[circuit.getSignalIdx("main.out[1]")];
|
|
|
|
|
|
|
|
const b = Buffer.alloc(32);
|
2023-07-21 13:10:01 +03:00
|
|
|
for (let i = 0; i < 31; i++) b[i] = 0xff;
|
|
|
|
b[31] = 0x1f;
|
2018-11-27 18:03:57 +03:00
|
|
|
|
|
|
|
const h = pedersen.hash(b);
|
|
|
|
const hP = babyJub.unpackPoint(h);
|
|
|
|
|
|
|
|
/*
|
|
|
|
console.log(`[${xout.toString()}, ${yout.toString()}]`);
|
|
|
|
console.log(`[${hP[0].toString()}, ${hP[1].toString()}]`);
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert(xout.equals(hP[0]));
|
|
|
|
assert(yout.equals(hP[1]));
|
|
|
|
});
|
|
|
|
});
|