Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ If you clone or link this repo as `opencircom` in your project root, compile wit
circom your.circom --r1cs --wasm -o build -l opencircom/circuits
```

## Compatibility

Poseidon compatibility with circomlib is covered by `test/poseidon_compat_test.js`.
The suite compares opencircom and circomlib wrapper circuits for `nInputs = 1, 2, 4, 16`
against fixed witness vectors. `circomlib` is a dev-only test dependency; the published
opencircom circuits remain dependency-free. No intentional Poseidon output differences
are documented.

## Include in Hardhat or Foundry

### Install
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"devDependencies": {
"chai": "^4.3.10",
"circom_tester": "^0.0.21",
"circomlib": "^2.0.5",
"mocha": "^10.7.3",
"snarkjs": "^0.7.5"
}
Expand Down
15 changes: 15 additions & 0 deletions test/circuits/poseidon16_test.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma circom 2.0.0;

include "../../circuits/hashing/poseidon.circom";

template Poseidon16Test() {
signal input in[16];
signal output out;
component p = Poseidon(16);
for (var i = 0; i < 16; i++) {
p.inputs[i] <== in[i];
}
out <== p.out;
}

component main = Poseidon16Test();
13 changes: 13 additions & 0 deletions test/circuits/poseidon1_test.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pragma circom 2.0.0;

include "../../circuits/hashing/poseidon.circom";

template Poseidon1Test() {
signal input in[1];
signal output out;
component p = Poseidon(1);
p.inputs[0] <== in[0];
out <== p.out;
}

component main = Poseidon1Test();
15 changes: 15 additions & 0 deletions test/circuits/poseidon_circomlib_16_test.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma circom 2.0.0;

include "../../node_modules/circomlib/circuits/poseidon.circom";

template CircomlibPoseidon16Test() {
signal input in[16];
signal output out;
component p = Poseidon(16);
for (var i = 0; i < 16; i++) {
p.inputs[i] <== in[i];
}
out <== p.out;
}

component main = CircomlibPoseidon16Test();
13 changes: 13 additions & 0 deletions test/circuits/poseidon_circomlib_1_test.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pragma circom 2.0.0;

include "../../node_modules/circomlib/circuits/poseidon.circom";

template CircomlibPoseidon1Test() {
signal input in[1];
signal output out;
component p = Poseidon(1);
p.inputs[0] <== in[0];
out <== p.out;
}

component main = CircomlibPoseidon1Test();
15 changes: 15 additions & 0 deletions test/circuits/poseidon_circomlib_2_test.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma circom 2.0.0;

include "../../node_modules/circomlib/circuits/poseidon.circom";

template CircomlibPoseidon2Test() {
signal input in[2];
signal output out;
component p = Poseidon(2);
for (var i = 0; i < 2; i++) {
p.inputs[i] <== in[i];
}
out <== p.out;
}

component main = CircomlibPoseidon2Test();
15 changes: 15 additions & 0 deletions test/circuits/poseidon_circomlib_4_test.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma circom 2.0.0;

include "../../node_modules/circomlib/circuits/poseidon.circom";

template CircomlibPoseidon4Test() {
signal input in[4];
signal output out;
component p = Poseidon(4);
for (var i = 0; i < 4; i++) {
p.inputs[i] <== in[i];
}
out <== p.out;
}

component main = CircomlibPoseidon4Test();
65 changes: 65 additions & 0 deletions test/poseidon_compat_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const chai = require("chai");
const path = require("path");
const wasm_tester = require("circom_tester").wasm;

const assert = chai.assert;

const OPEN_CIRCUITS = {
1: "poseidon1_test.circom",
2: "poseidon2_test.circom",
4: "poseidon4_test.circom",
16: "poseidon16_test.circom",
};

const CIRCOMLIB_CIRCUITS = {
1: "poseidon_circomlib_1_test.circom",
2: "poseidon_circomlib_2_test.circom",
4: "poseidon_circomlib_4_test.circom",
16: "poseidon_circomlib_16_test.circom",
};

function vectorSet(width) {
return [
Array(width).fill(0),
Array.from({ length: width }, (_, i) => i + 1),
Array.from({ length: width }, (_, i) => String((i + 1) * 17)),
];
}

describe("Poseidon compatibility with circomlib", function () {
const circuits = {};
this.timeout(120000);

before(async () => {
for (const width of Object.keys(OPEN_CIRCUITS)) {
circuits[width] = {
opencircom: await wasm_tester(path.join(__dirname, "circuits", OPEN_CIRCUITS[width]), {
output: path.join(__dirname, "..", "build"),
recompile: false,
}),
circomlib: await wasm_tester(path.join(__dirname, "circuits", CIRCOMLIB_CIRCUITS[width]), {
output: path.join(__dirname, "..", "build"),
recompile: false,
}),
};
}
});

for (const width of [1, 2, 4, 16]) {
it(`matches circomlib Poseidon(${width}) reference outputs`, async () => {
for (const inputs of vectorSet(width)) {
const opencircomWitness = await circuits[width].opencircom.calculateWitness({ in: inputs }, true);
const circomlibWitness = await circuits[width].circomlib.calculateWitness({ in: inputs }, true);

await circuits[width].opencircom.checkConstraints(opencircomWitness);
await circuits[width].circomlib.checkConstraints(circomlibWitness);

assert.equal(
opencircomWitness[1].toString(),
circomlibWitness[1].toString(),
`Poseidon(${width}) mismatch for ${JSON.stringify(inputs)}`
);
}
});
}
});