Skip to content

Commit a29614b

Browse files
authored
Merge pull request #31 from indextree/chore/contract_poseidon2
chore: change poseidon2 lib
2 parents b070da4 + fad3c1c commit a29614b

17 files changed

Lines changed: 357 additions & 688 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
run: RUST_TEST_THREADS=1 cargo test --test integration_tests -- --nocapture
6161

6262
- name: Run tornado classic tests
63-
run: cargo test --manifest-path tornado_classic/contracts/Cargo.toml -- --nocapture
63+
run: cargo test --manifest-path tornado_classic/contracts/Cargo.toml --features testutils -- --nocapture
6464

6565
- name: Run tornado classic wasm-cost tests
6666
run: cargo test --manifest-path tornado_classic/contracts/Cargo.toml --features wasm-cost -- --nocapture

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ This contract does not enforce access control:
8484

8585
```bash
8686
RUST_TEST_THREADS=1 cargo test --test integration_tests -- --nocapture
87-
cargo test --manifest-path tornado_classic/contracts/Cargo.toml -- --nocapture
87+
cargo test --manifest-path tornado_classic/contracts/Cargo.toml --features testutils -- --nocapture
8888
```
8989

9090
## References

test_snapshots/verify_fib_chain_proof_succeeds.1.json

Lines changed: 7 additions & 7 deletions
Large diffs are not rendered by default.

test_snapshots/verify_simple_circuit_proof_succeeds.1.json

Lines changed: 7 additions & 7 deletions
Large diffs are not rendered by default.

tests/integration_tests.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,3 @@ fn print_budget_for_deploy_and_verify() {
8080
println!("=== verify_proof budget usage ===");
8181
env.cost_estimate().budget().print();
8282
}
83-
84-
#[test]
85-
fn basic_verify_budget_test() {
86-
let vk_bytes_raw: &[u8] = include_bytes!("simple_circuit/target/vk");
87-
let proof_bin: &[u8] = include_bytes!("simple_circuit/target/proof");
88-
let pub_inputs_bin: &[u8] = include_bytes!("simple_circuit/target/public_inputs");
89-
90-
let env = Env::default();
91-
env.cost_estimate().budget().reset_unlimited();
92-
93-
let vk_bytes = Bytes::from_slice(&env, vk_bytes_raw);
94-
let client = register_client(&env, &vk_bytes);
95-
96-
// Prepare proof inputs
97-
assert_eq!(proof_bin.len(), PROOF_BYTES);
98-
let proof_bytes: Bytes = Bytes::from_slice(&env, proof_bin);
99-
let public_inputs: Bytes = Bytes::from_slice(&env, pub_inputs_bin);
100-
let proof_for_direct = proof_bytes.clone();
101-
let public_inputs_for_direct = public_inputs.clone();
102-
103-
client.verify_proof(&public_inputs_for_direct, &proof_for_direct);
104-
env.cost_estimate().budget().print();
105-
}

tornado_classic/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Tornado Classic–style Mixer (Soroban + Noir)
22

33
Scope
44
- Deposit stores commitments and rolls an on-chain Poseidon2 Merkle tree (depth 20).
5-
- Withdraw verifies a Noir UltraHonk proof against the stored root, enforces single-use nullifiers, and emits the verified recipient.
5+
- Withdraw verifies a Noir UltraHonk proof against the stored root and enforces single-use nullifiers.
66
- Educational sample: no token flow, trusted setup, and bn254 precompile missing on Soroban networks (devnet deploy still blocked).
77

88
Layout
@@ -23,14 +23,14 @@ scripts/gen_artifacts.sh # produces target/{vk,proof,public_inputs,…}
2323

2424
Run Contract Tests (includes real proof verification)
2525
```bash
26-
cargo test --manifest-path tornado_classic/contracts/Cargo.toml -- --nocapture
26+
cargo test --manifest-path tornado_classic/contracts/Cargo.toml --features testutils -- --nocapture
2727
```
2828
Key checks:
2929
- `deposit` appends to the frontier and updates the on-chain root.
30-
- `withdraw` takes separate `public_inputs` (three 32-byte values ordered `[root, nullifier_hash, recipient]`) and a `proof` blob (456 fields). The verifier contract now also expects `vk_bytes` as a distinct argument.
31-
- Nullifier mismatches or double spends fail; overwriting the root requires a configured admin actor.
30+
- `withdraw` takes separate `public_inputs` (two 32-byte values ordered `[root, nullifier_hash]`) and a `proof` blob (456 fields). The verifier contract now also expects `vk_bytes` as a distinct argument.
31+
- Invalid proofs or double spends fail; root overrides are only exposed in test builds.
3232

3333
Quick Usage Notes
34-
- Call `MixerContract::configure(admin)` once (the tests use `mock_all_auths`) before any `set_root` overrides; normal deposits keep the root up to date automatically.
34+
- Normal deposits keep the root up to date automatically.
3535
- Ensure the public inputs match the Poseidon2 tree built off committed leaves.
3636
- This repo is instructional. For production you need audited hashes, token custody, and a native bn254 verifier.

tornado_classic/circuit/Nargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ type = "bin"
44
authors = ["demo"]
55

66
[dependencies]
7-
poseidon2 = { tag = "v0.4.0", git = "https://github.com/TaceoLabs/noir-poseidon", directory = "poseidon2" }
7+
poseidon = { tag = "v0.2.0", git = "https://github.com/noir-lang/poseidon" }
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
nullifier = "3"
2-
secret = "7"
1+
nullifier = "6364136223846793006"
2+
secret = "13885033948157127959"
33
path_siblings = [
4-
"100", "200", "300", "400", "500",
5-
"600", "700", "800", "900", "1000",
6-
"1100", "1200", "1300", "1400", "1500",
7-
"1600", "1700", "1800", "1900", "2000"
4+
"14678909342070756876", "14340359694176818205", "3490389784639564826", "2377159206977889939", "11136134660641191128",
5+
"5776246781640716793", "12360490266823512006", "7783159857423531983", "9857737466630432868", "13004095818351146389",
6+
"8116512559640647218", "12170057637489489099", "18267517067855658160", "10007402826525227761", "8371399485941705566",
7+
"945482789575819655", "8503743914302291388", "4681333673231752717", "8045448350685608778", "12297150624705552899"
88
]
99
path_bits = [
10-
"0", "1", "0", "1", "0",
11-
"1", "0", "1", "0", "1",
12-
"0", "1", "0", "1", "0",
13-
"1", "0", "1", "0", "1"
10+
"0", "1", "0", "1", "0",
11+
"1", "0", "1", "0", "1",
12+
"0", "1", "0", "1", "0",
13+
"1", "0", "1", "0", "1"
1414
]
1515

16-
# Public values are now outputs of the circuit (root, nullifier_hash, commitment).
16+
# Public values are now outputs of the circuit (root, nullifier_hash).
1717
# They no longer need to be provided here.
18-
nullifier_hash = "20880561256956177504774055209049453687035310356692429666068591658666436689465"
19-
root = "7949686520350552737436242943561594277139156253983408896060570356798148564531"
20-
recipient = "0"
18+
nullifier_hash = "1565608087449049416619061943903198865776183104520976486117587040849008336411"
19+
root = "2634098511163448549597990951329529553826382520699944715793596572254420631460"
2120
path_index = "699050"

tornado_classic/circuit/scripts/gen_artifacts.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set -euo pipefail
33

44
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
55
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
6+
REPO_ROOT="$(cd "${PROJECT_ROOT}/../.." && pwd)"
67
export PATH="$HOME/.nargo/bin:$HOME/.bb/bin:${SCRIPT_DIR}:${PATH}"
78
cd "${PROJECT_ROOT}"
89

@@ -79,6 +80,12 @@ fi
7980
echo "[1/4] nargo compile"
8081
"${NARGO_BIN}" compile
8182

83+
if [[ "${GENERATE_PROVER:-1}" != "0" ]]; then
84+
echo "[i] Generating Prover.toml inputs (seed=${TORNADO_SEED:-1})"
85+
(cd "${REPO_ROOT}" && TORNADO_GENERATE=1 TORNADO_SEED="${TORNADO_SEED:-1}" \
86+
cargo run --example populate_publics --manifest-path tornado_classic/contracts/Cargo.toml --features std)
87+
fi
88+
8289
echo "[2/4] nargo execute (solve witness)"
8390
"${NARGO_BIN}" execute
8491

tornado_classic/circuit/src/main.nr

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Tornado Classic-style circuit
22
// - Private inputs: nullifier, secret, Merkle path (siblings, path_bits)
3-
// - Public inputs: root, nullifier_hash, recipient
4-
// - Hash function uses Poseidon2 (via dep::poseidon2)
3+
// - Public inputs: root, nullifier_hash
4+
// - Hash function uses Poseidon2 (via dep::poseidon)
55
// - Aligns with on-chain frontier Merkle: H = Poseidon2_2to1 (first limb),
66
// zero[0] = 0; zero[i+1] = H(zero[i], zero[i])
77

8-
use dep::poseidon2;
8+
use dep::poseidon::poseidon2::Poseidon2;
99

1010
fn hash2(a: Field, b: Field) -> Field {
11-
poseidon2::bn254::hash_2([a, b])
11+
Poseidon2::hash([a, b], 2)
1212
}
1313

1414
fn constrain_bit(bit: Field) {
@@ -35,7 +35,6 @@ fn compute_root(leaf: Field, path_siblings: [Field; 20], path_bits: [Field; 20])
3535
pub fn main(
3636
root: pub Field,
3737
nullifier_hash: pub Field,
38-
recipient: pub Field,
3938
nullifier: Field,
4039
secret: Field,
4140
path_siblings: [Field; 20],
@@ -52,6 +51,4 @@ pub fn main(
5251
let computed_root = compute_root(leaf, path_siblings, path_bits);
5352
assert(computed_root == root);
5453

55-
let recipient_square = recipient * recipient;
56-
assert(recipient_square == recipient * recipient);
5754
}

0 commit comments

Comments
 (0)