light_circuitlib_rs/gnark/
non_inclusion_json_formatter.rs

1use crate::gnark::helpers::big_int_to_string;
2use crate::{
3    gnark::helpers::create_json_from_struct,
4    init_merkle_tree::non_inclusion_merkle_tree_inputs_26,
5    non_inclusion::merkle_non_inclusion_proof_inputs::{
6        NonInclusionMerkleProofInputs, NonInclusionProofInputs,
7    },
8};
9use num_traits::ToPrimitive;
10use serde::Serialize;
11
12#[derive(Serialize, Debug)]
13pub struct BatchNonInclusionJsonStruct {
14    #[serde(rename(serialize = "new-addresses"))]
15    pub inputs: Vec<NonInclusionJsonStruct>,
16}
17
18#[derive(Serialize, Clone, Debug)]
19pub struct NonInclusionJsonStruct {
20    root: String,
21    value: String,
22
23    #[serde(rename(serialize = "pathIndex"))]
24    path_index: u32,
25
26    #[serde(rename(serialize = "pathElements"))]
27    path_elements: Vec<String>,
28
29    #[serde(rename(serialize = "leafLowerRangeValue"))]
30    leaf_lower_range_value: String,
31
32    #[serde(rename(serialize = "leafHigherRangeValue"))]
33    leaf_higher_range_value: String,
34
35    #[serde(rename(serialize = "leafIndex"))]
36    leaf_index: u32,
37}
38
39impl BatchNonInclusionJsonStruct {
40    fn new_with_public_inputs(number_of_utxos: usize) -> (Self, NonInclusionMerkleProofInputs) {
41        let merkle_inputs = non_inclusion_merkle_tree_inputs_26();
42
43        let input = NonInclusionJsonStruct {
44            root: big_int_to_string(&merkle_inputs.root),
45            value: big_int_to_string(&merkle_inputs.value),
46            path_elements: merkle_inputs
47                .merkle_proof_hashed_indexed_element_leaf
48                .iter()
49                .map(big_int_to_string)
50                .collect(),
51            path_index: merkle_inputs
52                .index_hashed_indexed_element_leaf
53                .to_u32()
54                .unwrap(),
55            leaf_index: merkle_inputs.leaf_index.to_u32().unwrap(),
56            leaf_lower_range_value: big_int_to_string(&merkle_inputs.leaf_lower_range_value),
57            leaf_higher_range_value: big_int_to_string(&merkle_inputs.leaf_higher_range_value),
58        };
59        let inputs = vec![input; number_of_utxos];
60        (Self { inputs }, merkle_inputs)
61    }
62
63    #[allow(clippy::inherent_to_string)]
64    pub fn to_string(&self) -> String {
65        create_json_from_struct(&self)
66    }
67
68    pub fn from_non_inclusion_proof_inputs(inputs: &NonInclusionProofInputs) -> Self {
69        let mut proof_inputs: Vec<NonInclusionJsonStruct> = Vec::new();
70        for input in inputs.0 {
71            let prof_input = NonInclusionJsonStruct {
72                root: big_int_to_string(&input.root),
73                value: big_int_to_string(&input.value),
74                path_index: input.index_hashed_indexed_element_leaf.to_u32().unwrap(),
75                path_elements: input
76                    .merkle_proof_hashed_indexed_element_leaf
77                    .iter()
78                    .map(big_int_to_string)
79                    .collect(),
80                leaf_index: input.leaf_index.to_u32().unwrap(),
81                leaf_lower_range_value: big_int_to_string(&input.leaf_lower_range_value),
82                leaf_higher_range_value: big_int_to_string(&input.leaf_higher_range_value),
83            };
84            proof_inputs.push(prof_input);
85        }
86
87        Self {
88            inputs: proof_inputs,
89        }
90    }
91}
92
93pub fn inclusion_inputs_string(number_of_utxos: usize) -> (String, NonInclusionMerkleProofInputs) {
94    let (json_struct, public_inputs) =
95        BatchNonInclusionJsonStruct::new_with_public_inputs(number_of_utxos);
96    (json_struct.to_string(), public_inputs)
97}