Skip to content
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ remaining production gates are tracked in
- **CircuitSpec Java DSL** (recommended) — define circuits as reusable Java classes with `CircuitSpec`
- **Inline lambda DSL** — quick prototyping with `CircuitBuilder.define(api -> ...)`
- **circom interop** — use externally compiled circom/snarkjs artifacts (`.r1cs`, `.zkey`, `.wtns`)
- **Standard library** — Poseidon, MiMC, Merkle, Comparators, Binary, Mux, AliasCheck; in-circuit **Blake2b / SHA-256 / SHA-512 / HMAC-SHA512** and **Ed25519 / BIP32 / CIP-1852** key-derivation gadgets (prove Cardano key ownership without revealing the seed); symbolic `Zk*` adapters, plus operation-specific Poseidon MPF/JMT circuits in their authenticated-state modules; see the [`zeroj-circuit-lib`](zeroj-circuit-lib/README.md) gadget table and [large-state guide](docs/merkle/practical-large-state-guide.md)
- **Standard library** — Poseidon, Merkle, Comparators, Binary, Mux, AliasCheck; in-circuit **Blake2b / SHA-256 / SHA-512 / HMAC-SHA512** and **Ed25519 / BIP32 / CIP-1852** key-derivation gadgets (prove Cardano key ownership without revealing the seed); symbolic `Zk*` adapters, plus operation-specific Poseidon MPF/JMT circuits in their authenticated-state modules; see the [`zeroj-circuit-lib`](zeroj-circuit-lib/README.md) gadget table and [large-state guide](docs/merkle/practical-large-state-guide.md)
- **Multi-backend compilation** — one Java circuit can compile to R1CS for Groth16 or to PlonK

### Generate Proofs
Expand Down Expand Up @@ -261,7 +261,7 @@ The **pure Java prover and verifier require no optional dependencies**.
| [`zeroj-crypto-blst`](zeroj-crypto-blst/) | **Opt-in blst prover backend** — thin bridge wiring `zeroj-blst`'s native MSM into the `zeroj-crypto` prover SPI (keeps `zeroj-crypto` pure-Java by default; pure Java matches blst at large sizes since ADR-0033/0034) |
| [`zeroj-tools`](zeroj-tools/) | Reusable operator tools and the `zeroj-ceremony` CLI — snarkjs-compatible Groth16 phase-2 contributor (`ZkeyContributor`), hash-to-G2 challenge derivation; embeddable in coordinator services and wallets |
| [`zeroj-circuit-dsl`](zeroj-circuit-dsl/) | Java Circuit DSL — define circuits with CircuitSpec, compile to R1CS/PlonK |
| [`zeroj-circuit-lib`](zeroj-circuit-lib/) | Circuit standard library — Poseidon, MiMC, Merkle, Comparators, Binary, Mux, AliasCheck; in-circuit Blake2b / SHA-256 / SHA-512 / HMAC-SHA512 and Ed25519 / BIP32 / CIP-1852 key derivation; symbolic adapters; [per-gadget status](zeroj-circuit-lib/README.md#gadget-status) |
| [`zeroj-circuit-lib`](zeroj-circuit-lib/) | Circuit standard library — Poseidon, Merkle, Comparators, Binary, Mux, AliasCheck; in-circuit Blake2b / SHA-256 / SHA-512 / HMAC-SHA512 and Ed25519 / BIP32 / CIP-1852 key derivation; symbolic adapters; [per-gadget status](zeroj-circuit-lib/README.md#gadget-status) |
| [`zeroj-onchain-julc`](zeroj-onchain-julc/) | Reusable Plutus V3 on-chain verifiers and libraries via Julc; Groth16 is the primary supported path, PlonK BLS12-381 validators/libraries are experimental opt-in |

#### Explicit Opt-In Product Modules
Expand Down
37 changes: 22 additions & 15 deletions docs/adr/circuit-annotation/cardano-gadget-support-matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ Relevant source:
| `Mux` | Conditional select and array access | Generic | Direct scalar select through `ZkBool.select` | Yes on BLS12-381 Groth16 | Add `ZkArray.select` or dynamic array access helper if needed. |
| `AliasCheck` | Canonical field representation check | Generic | Mostly covered by `ZkUInt` range checks | Yes on BLS12-381 Groth16 | Optional `ZkAliasCheck` for raw `ZkField`. |
| `Sha256`, `Sha512` | Full standard SHA-2 digests of fixed-length byte-aligned messages | Bit-oriented; supported scalar fields | `ZkSha256.hash`, `ZkSha512.hash` | BLS12-381 Groth16 building blocks; SHA-256 experimental pending external review | Bind the complete digest with constrained packing; use exact byte serialization. Official vectors, compiled-relation negatives and optional Circom assurance in ADR-0063. |
| `MiMC` / `SignalMiMC` | MiMC-7 two-input hash | BN254 only in current circuit lib | Direct through `ZkMiMC` | No for Cardano on-chain, because the circuit requires BN254 | Use Poseidon for Cardano. Consider a separate BLS12-381 MiMC variant only if there is a concrete interop need. |
| `MiMCSponge` | Variable-length MiMC sponge | BN254 only, because it calls `MiMC` | No direct `ZkMiMCSponge` | No for Cardano on-chain | Add symbolic wrapper only for BN254/off-chain legacy use. |
| `MiMC` / `SignalMiMC` | Legacy MiMC-7 keyed permutation (**deprecated for removal — insecure**) | BN254 only | Deprecated (`ZkMiMC`) | **Do not use** — trivially invertible keyed bijection (no Davies-Meyer / Miyaguchi-Preneel feed-forward), not a collision-resistant hash or circomlib-compatible | Deprecated for removal (`@Deprecated(forRemoval = true)`). Use `Poseidon` with explicit `PoseidonParamsBLS12_381T3.INSTANCE`. |
| `MiMCSponge` | Legacy MiMC-7 sponge wrapper (**deprecated for removal — insecure**) | BN254 only | None | **Do not use** — wraps broken `MiMC`, not circomlib-compatible | Deprecated for removal (`@Deprecated(forRemoval = true)`). Use `Poseidon` / `PoseidonN`. |
| `Poseidon` | Two-input Poseidon T3 hash | BN254 default; BLS12-381 supported with explicit params | Direct through `ZkPoseidon` for two inputs | Yes if `PoseidonParamsBLS12_381T3.INSTANCE` is used | Make BLS12-381 params prominent in Cardano examples. |
| `PoseidonN` | Variable-arity folded Poseidon | BN254 default; BLS12-381 supported with explicit params | Direct through `ZkPoseidonN` with explicit params | Yes if `PoseidonParamsBLS12_381T3.INSTANCE` is used | None. |
| `Merkle` / `SignalMerkle` | Fixed-depth Merkle membership | Hash-dependent | Direct through `ZkMerkle`; params-aware Poseidon helpers are available | Yes when `ZkMerkle.*Poseidon(..., PoseidonParamsBLS12_381T3.INSTANCE, ...)` is used | None. |
| `ZkMerkle.HashType.MIMC` | Merkle with MiMC | BN254 only | Direct | No for Cardano on-chain | Mark as BN254/off-chain in docs. |
| `ZkMerkle.HashType.MIMC` | Legacy Merkle with MiMC (**deprecated for removal — insecure**) | BN254 only | Deprecated | **Do not use** — delegates to broken invertible `MiMC` | Deprecated for removal (`@Deprecated(forRemoval = true)`). Use `ZkMerkle.*Poseidon(..., PoseidonParamsBLS12_381T3.INSTANCE, ...)`. |
| `ZkMerkle.HashType.POSEIDON` | Merkle with default Poseidon | BN254 by default today | Direct | No if using default enum path | Use params-aware `ZkMerkle.*Poseidon(...)` for Cardano. |
| `ZkMerkle` with custom hash lambda | Merkle with caller-provided hash | Depends on lambda | Direct | Yes with a BLS12-381-compatible lambda | Keep as advanced escape hatch. |
| `ZkMpf*` / `zeroj-mpf-poseidon` | CCL Poseidon MPF inclusion, two non-inclusion forms, value update, and split insertion | BLS12-381 Poseidon authenticated-state v1 only | Strict CCL verification plus canonical operation-specific witnesses/templates | Pure-Java Groth16 and Julc VM paths passed on a retained 5M root; external review, production setup, Yaci, and public-network gates remain. Not compatible with native Aiken/Blake2b MPF roots. | S9 covered the measured 5M root at 56,635 constraints and ~4.17 s prove. Physical delete/canonical collapse is deferred. See ADR-0042. |
Expand Down Expand Up @@ -190,18 +190,25 @@ The verifier rejects empty `IC` lists and any mismatch where
Generated fixed-count validators remain a possible future optimization if
budget-critical circuits need lower script cost.

### MiMC Is BN254-Only in the Circuit Library

The current `MiMC`, `SignalMiMC`, `ZkMiMC`, and `MiMCSponge` path is BN254-only.
`MiMC.hash(...)` calls `api.requireField(FieldConfig.BN254)`.

This is not a problem for off-chain BN254 circuits, but it is not a good default
for Cardano. Cardano-facing examples should use BLS12-381 Poseidon instead.

Existing examples that compute MiMC with a BLS12-381 prime outside the circuit
should be treated as legacy/test code that needs migration or clarification.
The in-circuit gadget now correctly prevents compiling that path as a
BLS12-381 circuit.
### MiMC Is Deprecated for Removal (Insecure Invertible Bijection)

The legacy `MiMC`, `SignalMiMC`, `ZkMiMC`, `MiMCSponge`, and `ZkMerkle.HashType.MIMC`
APIs are **deprecated for removal (`@Deprecated(forRemoval = true)`)** and must not be
used in new or existing circuits. `MiMC.hash(...)` computes a 91-round keyed bijection
on BN254 without Miyaguchi–Preneel / Davies–Meyer feed-forward (allowing trivial
preimage, collision, and Merkle-proof forgery), derives round constants with SHA-256
rather than Keccak-256, and is not circomlib-compatible.

**Existing-deployment migration:** upgrading ZeroJ only emits compile-time deprecation
warnings — existing MiMC commitments, Merkle roots, compiled circuits, and verification
keys remain usable under the broken relation. Deployments that ever used MiMC must:

1. Recompute all commitments and Merkle roots from trusted source data using BLS12-381
Poseidon (`PoseidonParamsBLS12_381T3.INSTANCE`).
2. Recompile the circuit with `Poseidon` / `ZkPoseidon` / `ZkMerkle.*Poseidon(...)`.
3. Regenerate circuit-specific proving and verifying keys (including a new Groth16
phase-2 ceremony where applicable).
4. Retire acceptance under the old MiMC verification key.

### Poseidon Defaults Are BN254 for Back Compatibility

Expand Down
2 changes: 1 addition & 1 deletion docs/architecture-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Java circuit definition and compilation:
- `CircuitBuilder` / `CircuitAPI` -- define circuits in Java
- `SignalBuilder` -- OO Signal-style API
- Compiles to R1CS (Groth16) or PlonK gates
- `zeroj-circuit-lib` -- Poseidon, MiMC, Merkle, comparators, binary ops
- `zeroj-circuit-lib` -- Poseidon, Merkle, comparators, binary ops, SHA-2, BLAKE2b, Jubjub/Ed25519 gadgets

### Layer 6: Orchestration (`zeroj-backend-spi`)
`VerifierRegistry` and `VerifierOrchestrator` route verification requests to the
Expand Down
28 changes: 16 additions & 12 deletions docs/circuit-annotation-user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ ZkPoseidonN.hash(zk, PoseidonParamsBLS12_381T3.INSTANCE, owner, assetId, nonce);
ZkMerkle.isMemberPoseidon(zk, PoseidonParamsBLS12_381T3.INSTANCE, leaf, root, siblings, pathBits);
```

Do not use no-params Poseidon or MiMC as Cardano defaults. The no-params
Poseidon overload is retained for BN254 compatibility, and `ZkMiMC` delegates
to the BN254-only MiMC gadget. The full gadget and curve support matrix is in
Do not use no-params Poseidon as a Cardano default, and do not use `ZkMiMC` or
`ZkMerkle.HashType.MIMC` in any circuit. The no-params Poseidon overload is
retained for BN254 compatibility, while the `MiMC` gadget family is deprecated
for removal as an insecure, invertible keyed permutation (not a hash function).
The full gadget and curve support matrix is in
[`docs/adr/circuit-annotation/cardano-gadget-support-matrix.md`](adr/circuit-annotation/cardano-gadget-support-matrix.md).

## Authoring Rules
Expand Down Expand Up @@ -156,9 +158,10 @@ circuits, the rendered `nameTemplate` is a readable prefix; generated names also
append a canonical parameter suffix to avoid collisions between different
parameter sets.

`ZkMerkle.HashType.MIMC` and `ZkMerkle.HashType.POSEIDON` remain useful for
BN254/off-chain compatibility. Today they are not the Cardano default path:
`MIMC` is BN254-only, and `POSEIDON` uses the no-params Poseidon overload. Use
`ZkMerkle.HashType.MIMC` is **deprecated for removal (insecure)** because the
underlying `MiMC` gadget is an invertible keyed bijection rather than a
collision-resistant hash. `ZkMerkle.HashType.POSEIDON` uses the no-params
BN254-oriented Poseidon overload. For Cardano and all new Merkle circuits, use
`ZkMerkle.computeRootPoseidon`, `isMemberPoseidon`, or `verifyPoseidon` with
explicit BLS12-381 Poseidon params.

Expand Down Expand Up @@ -330,11 +333,12 @@ bind them as secret `ZkUInt` inputs, using 252 bits for `kModL` and 4 bits for
variable-arity hashing. For Cardano, pass
`PoseidonParamsBLS12_381T3.INSTANCE` explicitly; there is intentionally no
no-params symbolic overload.
- `ZkMiMC` is BN254-only because the underlying MiMC gadget is BN254-only.
Treat MiMC-based annotated circuits as BN254/off-chain unless a separate
BLS12-381 MiMC variant is added.
- `ZkMerkle.HashType.MIMC` and the no-params `HashType.POSEIDON` path are not
Cardano defaults today. Use `ZkMerkle.*Poseidon(...)` with explicit
BLS12-381 Poseidon params for Cardano Merkle circuits.
- `ZkMiMC` and `ZkMerkle.HashType.MIMC` are **deprecated for removal (insecure)**:
the underlying `MiMC` gadget is an invertible keyed bijection (not a
collision-resistant hash) and is scheduled for removal. Use `ZkPoseidon` /
`ZkMerkle.*Poseidon(...)` with explicit `PoseidonParamsBLS12_381T3.INSTANCE`.
- The no-params `ZkMerkle.HashType.POSEIDON` path is not a Cardano default
today. Use `ZkMerkle.*Poseidon(...)` with explicit BLS12-381 Poseidon params
for Cardano Merkle circuits.
- `ZkBits` and `ZkBytes` store one constrained field element per bit or byte;
packed byte encodings are deferred.
28 changes: 11 additions & 17 deletions docs/circuit-dsl-user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,40 +379,35 @@ Poseidon parameters, also import

### Hash Functions

MiMC is BN254-only in the current circuit library. For Cardano/BLS12-381
circuits, use Poseidon with explicit BLS12-381 parameters.
For Cardano/BLS12-381 circuits (and all new circuits), use Poseidon with explicit BLS12-381
parameters (`PoseidonParamsBLS12_381T3.INSTANCE`).

```java
// MiMC-7 hash (2 inputs -> 1 output, ~364 constraints, BN254-only)
var hash = MiMC.hash(api, api.var("left"), api.var("right"));
> **Do not use `MiMC`, `SignalMiMC`, `MiMCSponge`, or `ZkMiMC`:** they are deprecated for removal
> because `MiMC.hash` computes an invertible keyed bijection without feed-forward (allowing trivial
> preimage and Merkle proof forgery) and is not circomlib-compatible.

```java
// Poseidon hash (2 inputs -> 1 output, ~330 constraints, Cardano/BLS12-381)
var hash = Poseidon.hash(
api,
PoseidonParamsBLS12_381T3.INSTANCE,
api.var("in0"),
api.var("in1"));

// Variable-arity Poseidon (N inputs via left-fold)
// Fixed-arity folded Poseidon (N inputs via left-fold)
var hash = PoseidonN.hash(
api,
PoseidonParamsBLS12_381T3.INSTANCE,
api.var("a"),
api.var("b"),
api.var("c"),
api.var("d"));

// MiMC Sponge (variable-length input, single or multi-output)
var hash = MiMCSponge.hash(api, new Variable[]{api.var("in0"), api.var("in1"), api.var("in2")});
var outputs = MiMCSponge.hashMulti(api, inputs, 2); // 2 outputs
```

Signal API equivalents:
```java
Signal hash = SignalMiMC.hash(c, left, right);
Signal hash = SignalPoseidon.hash(c, PoseidonParamsBLS12_381T3.INSTANCE, in0, in1);
Signal hash = PoseidonN.hash(c, PoseidonParamsBLS12_381T3.INSTANCE, a, b, inputC, inputD);
Signal hash = MiMCSponge.hash(c, new Signal[]{in0, in1, in2});
```

### Comparators
Expand All @@ -426,13 +421,12 @@ var smallest = Comparators.min(api, a, b, 8);
### Merkle Proof

```java
// Verify a Merkle proof with any hash function
Merkle.verifyProof(api, leaf, root, siblings, pathBits, MiMC::hash);
// Verify a Merkle proof with BLS12-381 Poseidon
Merkle.verifyProof(api, leaf, root, siblings, pathBits,
(ctx, l, r) -> Poseidon.hash(ctx, PoseidonParamsBLS12_381T3.INSTANCE, l, r));
```

For a depth-20 tree: 20 × 364 ≈ 7,280 constraints with MiMC, or 20 × 330 ≈ 6,600 with Poseidon.
For a depth-20 tree: 20 × 330 ≈ 6,600 constraints with Poseidon.

### Binary Utilities

Expand Down Expand Up @@ -787,7 +781,7 @@ See the [Pure Java Prover Guide](pure-java-prover-guide.md) for the complete pip

4. **Prefer `select` over branching** — ZK circuits can't branch. Use `api.select(cond, a, b)` for conditional logic.

5. **Use ZK-friendly hashes** — Poseidon (~300 constraints) and MiMC (~364 constraints) are much cheaper than SHA-256 (~25,000 constraints) inside circuits. For Cardano/BLS12-381 circuits, use Poseidon with explicit BLS12-381 parameters; MiMC is BN254-only in the current circuit library.
5. **Use ZK-friendly hashes** — Poseidon (~330 constraints on BLS12-381) is much cheaper than SHA-256 (~25,000 constraints) inside circuits. For Cardano/BLS12-381 circuits, use Poseidon with explicit BLS12-381 parameters (`PoseidonParamsBLS12_381T3.INSTANCE`). Do not use the deprecated `MiMC` gadgets.

## Module Dependencies

Expand All @@ -797,7 +791,7 @@ implementation platform('org.zeroj:zeroj-bom-core:0.1.0')
// Circuit DSL (define and compile circuits)
implementation 'org.zeroj:zeroj-circuit-dsl'

// Standard library (Poseidon, MiMC, Merkle, Comparators, Binary, Mux, AliasCheck)
// Standard library (Poseidon, Merkle, Comparators, Binary, Mux, AliasCheck)
implementation 'org.zeroj:zeroj-circuit-lib'

// Pure Java prover
Expand Down
5 changes: 2 additions & 3 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,8 @@ regression tests first — the runnable applications and tutorials live in
### Example Circuits

- **Sealed Bid Auction** -- prove bid >= reserve without revealing bid amount
- **Anonymous Voting** -- prove vote is 0/1 with a MiMC commitment in the
BN254/off-chain reference flow. For Cardano/BLS12-381 circuits, use Poseidon
with explicit BLS12-381 parameters.
- **Anonymous Voting** -- prove vote is 0/1 with a BLS12-381 Poseidon commitment
(`PoseidonParamsBLS12_381T3.INSTANCE`)
- **Balance Threshold** -- prove balance >= threshold without revealing exact balance

See the [integration-tests README](../zeroj-integration-tests/README.md) for what each test protects.
Expand Down
4 changes: 2 additions & 2 deletions docs/pure-java-prover-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ testImplementation 'com.bloxbean.cardano:cardano-client-lib'

| Test | Circuit | What It Proves |
|------|---------|---------------|
| `SealedBidPureJavaE2ETest` | Sealed bid auction (497 constraints) | MiMC hash + range comparison; BN254/off-chain reference unless migrated to BLS12-381 Poseidon |
| `AnonymousVotingPureJavaE2ETest` | Anonymous voting (367 constraints) | MiMC commitment + boolean; BN254/off-chain reference unless migrated to BLS12-381 Poseidon |
| `SealedBidPureJavaE2ETest` | Sealed bid auction | BLS12-381 Poseidon commitment + range comparison |
| `AnonymousVotingPureJavaE2ETest` | Anonymous voting | BLS12-381 Poseidon commitment + boolean constraint |
| `BalanceThresholdPureJavaE2ETest` | Balance threshold (132 constraints) | Range comparison |
| `PureJavaProverYaciE2ETest` | Multiplier | **Full stack: prove → Yaci DevKit on-chain** |
| `Groth16BLS381ZkeyEndToEndTest` | Multiplier + Cubic | snarkjs .zkey import → Java prove → pairing verify |
Expand Down
Loading
Loading