Pure-MoonBit BN254 optimal Ate bilinear pairing, R1CS/QAP/FFT, Groth16 zk-SNARK prover & verifier with snarkjs/EIP-197 interoperability and zero-upload browser studio
| Layer | Modules | Highlights |
|---|---|---|
| 256-bit Montgomery Field Arithmetic | u256.mbt, fp.mbt, fr.mbt | 4-limb UInt64 CIOS Montgomery multiplication, canonical < p / < r validation, -th primitive roots of unity in |
| Tower Extensions | fq2.mbt, fq6.mbt, fq12.mbt | Karatsuba arithmetic, sparse mul_by_034, Granger-Scott cyclotomic_square, Frobenius maps |
| BN254 Groups & Pairing | g1.mbt, g2.mbt, pairing.mbt | Jacobian projective arithmetic, Pippenger MSM (g1_msm, g2_msm), strict prime-order subgroup check, Optimal Ate pairing with shared final exponentiation, EIP-196/197 wire codecs |
| R1CS / QAP / Groth16 | r1cs.mbt, circuit_builder.mbt, poseidon.mbt, fft.mbt, groth16.mbt | Composable CircuitBuilder DSL (is_zero, is_equal, mux, num2bits, less_than), BN254 Poseidon sponge & arbitrary-depth Merkle circuits (~980+ constraints), Coset FFT quotient , full Groth16 setup / prove / verify / randomized groth16_verify_batch |
| Interop & Visualization | snarkjs_codec.mbt, iden3_codec.mbt, solidity.mbt, viz.mbt, report.mbt | Standard snarkjs JSON codec, official iden3 binary .r1cs (v1) & .wtns (v2) encoder/decoder, EVM Solidity ^0.8.20 Groth16Verifier.sol & calldata exporter, native R1CS & Pairing SVGs |
fn main raise {
let (r1cs, witness) = @zk.circuit_cubic(@zk.Fr::from_u64(3UL))
let (pk, vk) = @zk.groth16_setup(r1cs)
let proof = @zk.groth16_prove(pk, r1cs, witness)
let ok = @zk.groth16_verify(vk, r1cs.public_inputs(witness), proof)
println("verified: \{ok}")
}moon build --target native --release cmd/main
# Generate standard snarkjs JSON artifacts + SVGs for any built-in circuit
./_build/native/release/build/cmd/main/main prove cubic --x 3 --out-dir out/
# Verify any standard snarkjs BN254 Groth16 proof
./_build/native/release/build/cmd/main/main verify out/verification_key.json out/public.json out/proof.json
# Verify an Ethereum EIP-197 hex pairing vector
./_build/native/release/build/cmd/main/main eip197 empty| Circuit | Constraints | Wires | snarkjs@0.7.4 Verify | Tampered Input/Proof | Emitted Artifacts |
|---|---|---|---|---|---|
| cubic (x^3 + x + 5 = 35) | 3 | 5 | PASS (TRUE) | REJECTED (FALSE) | JSON + .r1cs/.wtns + .sol + SVG |
| factor (a * b = n) | 3 | 6 | PASS (TRUE) | REJECTED (FALSE) | JSON + .r1cs/.wtns + .sol + SVG |
| mimc7 | 42 | 45 | PASS (TRUE) | REJECTED (FALSE) | JSON + .r1cs/.wtns + .sol + SVG |
| range8 (8-bit range + salt) | 51 | 53 | PASS (TRUE) | REJECTED (FALSE) | JSON + .r1cs/.wtns + .sol + SVG |
| merkle_mimc | 89 | 93 | PASS (TRUE) | REJECTED (FALSE) | JSON + .r1cs/.wtns + .sol + SVG |
| poseidon_commit (Semaphore-style + 16b range) | 507 | 510 | PASS (TRUE) | REJECTED (FALSE) | JSON + .r1cs/.wtns + .sol + SVG |
| poseidon_merkle (BN254 Grain-LFSR, depth 4) | 989 | 995 | PASS (TRUE) | REJECTED (FALSE) | JSON + .r1cs/.wtns + .sol + SVG |
# Four-target tests (--deny-warn)
for target in wasm wasm-gc js native; do
moon check --target "$target" --deny-warn
moon test --target "$target" --deny-warn
done
# External Oracle: Ethereum EIP-197 vectors + Bidirectional official snarkjs@0.7.4 cross-verification
python3 demo/verify_zk.py
# Zero-upload browser studio build + Node smoke test
python3 demo/build_web.pyfn CircuitBuilder::assert_less_than(self : CircuitBuilder, a : Wire, b : Wire, num_bits : Int, label : String) -> Unitfn CircuitBuilder::assert_range(self : CircuitBuilder, x : Wire, num_bits : Int, label : String) -> Unitfn CircuitBuilder::less_than(self : CircuitBuilder, a : Wire, b : Wire, num_bits : Int, label : String) -> Wirefn CircuitBuilder::mux(self : CircuitBuilder, sel : Wire, if_zero : Wire, if_one : Wire, label : String) -> Wirefn CircuitBuilder::num2bits(self : CircuitBuilder, x : Wire, num_bits : Int, label : String) -> Array[Wire]pub struct Constraint {
a : LinearCombination
b : LinearCombination
c : LinearCombination
label : String
} derive(Eq, Debug)impl Add for G1Projectiveimpl Neg for G1Projectiveimpl Sub for G1Projectiveimpl Add for G2Projectiveimpl Neg for G2Projectiveimpl Sub for G2Projectivefn LinearCombination::add_lc(self : LinearCombination, other : LinearCombination) -> LinearCombinationfn LinearCombination::add_term(self : LinearCombination, wire : Int, coeff : Fr) -> LinearCombinationfn LinearCombination::sub_lc(self : LinearCombination, other : LinearCombination) -> LinearCombinationpub struct R1cs {
name : String
num_public : Int
num_wires : Int
wire_names : Array[String]
constraints : Array[Constraint]
} derive(Eq, Debug)pub struct R1csBuilder {
name : String
num_public : Int
has_private : Bool
wire_names : Array[String]
witness : Array[Fr]
constraints : Array[Constraint]
}fn R1csBuilder::enforce(self : R1csBuilder, a : LinearCombination, b : LinearCombination, c : LinearCombination, label? : String) -> Unitfn groth16_verify(vk : VerifyingKey, public_inputs : Array[Fr], proof : Proof) -> Bool raise ZkErrorfn next_power_of_two(n : Int) -> (Int, Int)fn render_pairing_svg(circuit_name : String, num_constraints : Int, num_wires : Int, num_public : Int, verified : Bool, proof : Proof) -> Stringfn verify_snarkjs_json(vk_json : String, public_json : String, proof_json : String) -> Bool raise ZkErrorInstall
Download zipPure-MoonBit BN254 optimal Ate bilinear pairing, R1CS/QAP/FFT, Groth16 zk-SNARK prover & verifier with snarkjs/EIP-197 interoperability and zero-upload browser studio