Poseidon for t=3
This commit is contained in:
parent
bdfb0fb928
commit
19bbada388
@ -36,6 +36,8 @@ exports.getMatrix = (t, seed, nRounds) => {
|
|||||||
if (typeof seed === "undefined") seed = SEED;
|
if (typeof seed === "undefined") seed = SEED;
|
||||||
if (typeof nRounds === "undefined") nRounds = NROUNDSF + NROUNDSP;
|
if (typeof nRounds === "undefined") nRounds = NROUNDSF + NROUNDSP;
|
||||||
if (typeof t === "undefined") t = T;
|
if (typeof t === "undefined") t = T;
|
||||||
|
assert(t<=6); // Force the same matrix for all.
|
||||||
|
t=6;
|
||||||
let nonce = "0000";
|
let nonce = "0000";
|
||||||
let cmatrix = getPseudoRandom(seed+"_matrix_"+nonce, t*2);
|
let cmatrix = getPseudoRandom(seed+"_matrix_"+nonce, t*2);
|
||||||
while (!allDifferent(cmatrix)) {
|
while (!allDifferent(cmatrix)) {
|
||||||
|
@ -121,7 +121,7 @@ function createCode(t, nRoundsF, nRoundsP, seed) {
|
|||||||
// We ignore the pointer and the length and just load 6 values to the state
|
// We ignore the pointer and the length and just load 6 values to the state
|
||||||
// (Stack positions 0-5) If the array is shorter, we just set zeros.
|
// (Stack positions 0-5) If the array is shorter, we just set zeros.
|
||||||
for (let i=0; i<t; i++) {
|
for (let i=0; i<t; i++) {
|
||||||
C.push(0x44+(0x20*(5-i)));
|
C.push(0x44+(0x20*(t-1-i)));
|
||||||
C.calldataload();
|
C.calldataload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
test/circuits/poseidon3_test.circom
Normal file
3
test/circuits/poseidon3_test.circom
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
include "../../circuits/poseidon.circom"
|
||||||
|
|
||||||
|
component main = Poseidon(2, 3, 8, 57);
|
@ -18,43 +18,74 @@ describe("Blake2b version test", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("Poseidon Circuit test", function () {
|
describe("Poseidon Circuit test", function () {
|
||||||
let circuit;
|
let circuit6;
|
||||||
|
let circuit3;
|
||||||
|
|
||||||
this.timeout(100000);
|
this.timeout(100000);
|
||||||
|
|
||||||
before( async () => {
|
before( async () => {
|
||||||
const cirDef = await compiler(path.join(__dirname, "circuits", "poseidon_test.circom"));
|
const cirDef6 = await compiler(path.join(__dirname, "circuits", "poseidon6_test.circom"));
|
||||||
|
circuit6 = new snarkjs.Circuit(cirDef6);
|
||||||
circuit = new snarkjs.Circuit(cirDef);
|
console.log("Poseidon6 constraints: " + circuit6.nConstraints);
|
||||||
|
const cirDef3 = await compiler(path.join(__dirname, "circuits", "poseidon3_test.circom"));
|
||||||
console.log("Poseidon constraints: " + circuit.nConstraints);
|
circuit3 = new snarkjs.Circuit(cirDef3);
|
||||||
|
console.log("Poseidon3 constraints: " + circuit3.nConstraints);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should check constrain of hash([1, 2])", async () => {
|
it("Should check constrain of hash([1, 2]) t=6", async () => {
|
||||||
const w = circuit.calculateWitness({inputs: [1, 2]});
|
const w = circuit6.calculateWitness({inputs: [1, 2]});
|
||||||
|
|
||||||
const res = w[circuit.getSignalIdx("main.out")];
|
const res = w[circuit6.getSignalIdx("main.out")];
|
||||||
|
|
||||||
const hash = poseidon.createHash(6, 8, 57);
|
const hash = poseidon.createHash(6, 8, 57);
|
||||||
|
|
||||||
const res2 = hash([1,2]);
|
const res2 = hash([1,2]);
|
||||||
assert.equal('12242166908188651009877250812424843524687801523336557272219921456462821518061', res2.toString());
|
assert.equal("12242166908188651009877250812424843524687801523336557272219921456462821518061", res2.toString());
|
||||||
assert.equal(res.toString(), res2.toString());
|
assert.equal(res.toString(), res2.toString());
|
||||||
assert(circuit.checkWitness(w));
|
assert(circuit6.checkWitness(w));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should check constrain of hash([3, 4])", async () => {
|
it("Should check constrain of hash([3, 4]) t=6", async () => {
|
||||||
const w = circuit.calculateWitness({inputs: [3, 4]});
|
const w = circuit6.calculateWitness({inputs: [3, 4]});
|
||||||
|
|
||||||
const res = w[circuit.getSignalIdx("main.out")];
|
const res = w[circuit6.getSignalIdx("main.out")];
|
||||||
|
|
||||||
const hash = poseidon.createHash(6, 8, 57);
|
const hash = poseidon.createHash(6, 8, 57);
|
||||||
|
|
||||||
const res2 = hash([3, 4]);
|
const res2 = hash([3, 4]);
|
||||||
assert.equal('17185195740979599334254027721507328033796809509313949281114643312710535000993', res2.toString());
|
assert.equal("17185195740979599334254027721507328033796809509313949281114643312710535000993", res2.toString());
|
||||||
|
|
||||||
assert.equal(res.toString(), res2.toString());
|
assert.equal(res.toString(), res2.toString());
|
||||||
|
|
||||||
assert(circuit.checkWitness(w));
|
assert(circuit6.checkWitness(w));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it("Should check constrain of hash([1, 2]) t=3", async () => {
|
||||||
|
const w = circuit3.calculateWitness({inputs: [1, 2]});
|
||||||
|
|
||||||
|
const res = w[circuit3.getSignalIdx("main.out")];
|
||||||
|
|
||||||
|
const hash = poseidon.createHash(3, 8, 57);
|
||||||
|
|
||||||
|
const res2 = hash([1,2]);
|
||||||
|
assert.equal("2104035019328376391822106787753454168168617545136592089411833517434990977743", res2.toString());
|
||||||
|
assert.equal(res.toString(), res2.toString());
|
||||||
|
assert(circuit3.checkWitness(w));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should check constrain of hash([3, 4]) t=3", async () => {
|
||||||
|
const w = circuit3.calculateWitness({inputs: [3, 4]});
|
||||||
|
|
||||||
|
const res = w[circuit3.getSignalIdx("main.out")];
|
||||||
|
|
||||||
|
const hash = poseidon.createHash(3, 8, 57);
|
||||||
|
|
||||||
|
const res2 = hash([3, 4]);
|
||||||
|
assert.equal("12456141564250880945411182508630957604732712316993112736876413121277158512223", res2.toString());
|
||||||
|
|
||||||
|
assert.equal(res.toString(), res2.toString());
|
||||||
|
|
||||||
|
assert(circuit3.checkWitness(w));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -8,13 +8,13 @@ const bigInt = require("snarkjs").bigInt;
|
|||||||
const assert = chai.assert;
|
const assert = chai.assert;
|
||||||
const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); };
|
const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); };
|
||||||
|
|
||||||
const SEED = "mimc";
|
describe("Poseidon Smart contract test", function () {
|
||||||
|
|
||||||
describe("Poseidon Smart contract test", () => {
|
|
||||||
let testrpc;
|
let testrpc;
|
||||||
let web3;
|
let web3;
|
||||||
let mimc;
|
let poseidon6;
|
||||||
|
let poseidon3;
|
||||||
let accounts;
|
let accounts;
|
||||||
|
this.timeout(100000);
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
web3 = new Web3(ganache.provider(), null, { transactionConfirmationBlocks: 1 });
|
web3 = new Web3(ganache.provider(), null, { transactionConfirmationBlocks: 1 });
|
||||||
@ -24,17 +24,23 @@ describe("Poseidon Smart contract test", () => {
|
|||||||
it("Should deploy the contract", async () => {
|
it("Should deploy the contract", async () => {
|
||||||
const C = new web3.eth.Contract(poseidonGenContract.abi);
|
const C = new web3.eth.Contract(poseidonGenContract.abi);
|
||||||
|
|
||||||
mimc = await C.deploy({
|
poseidon6 = await C.deploy({
|
||||||
data: poseidonGenContract.createCode()
|
data: poseidonGenContract.createCode(6)
|
||||||
|
}).send({
|
||||||
|
gas: 2500000,
|
||||||
|
from: accounts[0]
|
||||||
|
});
|
||||||
|
poseidon3 = await C.deploy({
|
||||||
|
data: poseidonGenContract.createCode(3)
|
||||||
}).send({
|
}).send({
|
||||||
gas: 2500000,
|
gas: 2500000,
|
||||||
from: accounts[0]
|
from: accounts[0]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Shold calculate the mimic correctly", async () => {
|
it("Shold calculate the poseidon correctly t=6", async () => {
|
||||||
|
|
||||||
const res = await mimc.methods.poseidon([1,2]).call();
|
const res = await poseidon6.methods.poseidon([1,2]).call();
|
||||||
|
|
||||||
// console.log("Cir: " + bigInt(res.toString(16)).toString(16));
|
// console.log("Cir: " + bigInt(res.toString(16)).toString(16));
|
||||||
|
|
||||||
@ -45,5 +51,19 @@ describe("Poseidon Smart contract test", () => {
|
|||||||
|
|
||||||
assert.equal(res.toString(), res2.toString());
|
assert.equal(res.toString(), res2.toString());
|
||||||
});
|
});
|
||||||
|
it("Shold calculate the poseidon correctly t=3", async () => {
|
||||||
|
|
||||||
|
const res = await poseidon3.methods.poseidon([1,2]).call();
|
||||||
|
|
||||||
|
// console.log("Cir: " + bigInt(res.toString(16)).toString(16));
|
||||||
|
|
||||||
|
const hash = Poseidon.createHash(3, 8, 57);
|
||||||
|
|
||||||
|
const res2 = hash([1,2]);
|
||||||
|
// console.log("Ref: " + bigInt(res2).toString(16));
|
||||||
|
|
||||||
|
assert.equal(res.toString(), res2.toString());
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user