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, fft.mbt, groth16.mbt | 5 built-in circuits (cubic, factor, mimc7, merkle_mimc, range8), Coset FFT quotient , full Groth16 setup / prove / verify |
| Interop & Visualization | snarkjs_codec.mbt, viz.mbt, report.mbt | Standard snarkjs verification_key.json / proof.json / public.json codec, native R1CS & Pairing pipeline 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# 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.pypub 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