light_circuitlib_rs/gnark/
inclusion_json_formatter.rs

1use crate::gnark::helpers::{big_int_to_string, create_json_from_struct};
2use crate::{
3    inclusion::{
4        merkle_inclusion_proof_inputs::{InclusionMerkleProofInputs, InclusionProofInputs},
5        merkle_tree_info::MerkleTreeInfo,
6    },
7    init_merkle_tree::inclusion_merkle_tree_inputs,
8};
9use num_traits::ToPrimitive;
10use serde::Serialize;
11
12#[derive(Serialize, Debug)]
13pub struct BatchInclusionJsonStruct {
14    #[serde(rename(serialize = "input-compressed-accounts"))]
15    pub inputs: Vec<InclusionJsonStruct>,
16}
17
18#[allow(non_snake_case)]
19#[derive(Serialize, Clone, Debug)]
20pub struct InclusionJsonStruct {
21    root: String,
22    leaf: String,
23    pathIndex: u32,
24    pathElements: Vec<String>,
25}
26
27impl BatchInclusionJsonStruct {
28    fn new_with_public_inputs(number_of_utxos: usize) -> (Self, InclusionMerkleProofInputs) {
29        let merkle_inputs = inclusion_merkle_tree_inputs(MerkleTreeInfo::H26);
30
31        let input = InclusionJsonStruct {
32            root: big_int_to_string(&merkle_inputs.root),
33            leaf: big_int_to_string(&merkle_inputs.leaf),
34            pathElements: merkle_inputs
35                .path_elements
36                .iter()
37                .map(big_int_to_string)
38                .collect(),
39            pathIndex: merkle_inputs.path_index.to_u32().unwrap(),
40        };
41
42        let inputs = vec![input; number_of_utxos];
43
44        (Self { inputs }, merkle_inputs)
45    }
46
47    #[allow(clippy::inherent_to_string)]
48    pub fn to_string(&self) -> String {
49        create_json_from_struct(&self)
50    }
51
52    pub fn from_inclusion_proof_inputs(inputs: &InclusionProofInputs) -> Self {
53        let mut proof_inputs: Vec<InclusionJsonStruct> = Vec::new();
54        for input in inputs.0 {
55            let prof_input = InclusionJsonStruct {
56                root: big_int_to_string(&input.root),
57                leaf: big_int_to_string(&input.leaf),
58                pathIndex: input.path_index.to_u32().unwrap(),
59                pathElements: input.path_elements.iter().map(big_int_to_string).collect(),
60            };
61            proof_inputs.push(prof_input);
62        }
63
64        Self {
65            inputs: proof_inputs,
66        }
67    }
68}
69
70pub fn inclusion_inputs_string(number_of_utxos: usize) -> (String, InclusionMerkleProofInputs) {
71    let (json_struct, public_inputs) =
72        BatchInclusionJsonStruct::new_with_public_inputs(number_of_utxos);
73    (json_struct.to_string(), public_inputs)
74}