1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use ark_ff::PrimeField;
use kimchi::circuits::{constraints::ConstraintSystem, gate::CircuitGate};
use mina_curves::pasta::Fp;
use serde::Serialize;
use wasm_bindgen::prelude::wasm_bindgen;

use crate::pasta_fp_plonk_index::WasmPastaFpPlonkIndex;

#[derive(Serialize)]
struct Circuit<F>
where
    F: PrimeField,
{
    public_input_size: usize,
    #[serde(bound = "CircuitGate<F>: Serialize")]
    gates: Vec<CircuitGate<F>>,
}

impl<F> From<&ConstraintSystem<F>> for Circuit<F>
where
    F: PrimeField,
{
    fn from(cs: &ConstraintSystem<F>) -> Self {
        Circuit {
            public_input_size: cs.public,
            gates: cs.gates.clone(),
        }
    }
}

#[wasm_bindgen]
pub fn prover_to_json(prover_index: &WasmPastaFpPlonkIndex) -> String {
    let circuit: Circuit<Fp> = (&prover_index.0.cs).into();
    serde_json::to_string(&circuit).expect("couldn't serialize constraints")
}