@@ -3,180 +3,122 @@ pub mod insert;
3
3
pub mod update;
4
4
pub mod utils;
5
5
6
- // #[cfg(test)]
7
- // mod tests {
8
- // use crate::nova::batch::{Hash, MerkleProofStepCircuit, UnifiedProofStep};
9
- // use arecibo::{
10
- // provider::{Bn256Engine, GrumpkinEngine},
11
- // traits::circuit::StepCircuit,
12
- // };
13
- // use arecibo::{
14
- // traits::{circuit::TrivialCircuit, snark::default_ck_hint, Engine},
15
- // PublicParams, RecursiveSNARK,
16
- // };
17
- // use bellpepper_core::{num::AllocatedNum, test_cs::TestConstraintSystem, ConstraintSystem};
18
- // use ff::PrimeField;
19
- // use indexed_merkle_tree::{node::Node, sha256_mod, tree::IndexedMerkleTree, tree::Proof};
20
-
21
- // type E1 = Bn256Engine;
22
- // type E2 = GrumpkinEngine;
23
-
24
- // type C1 = MerkleProofStepCircuit<<E1 as Engine>::Scalar>;
25
- // type C2 = TrivialCircuit<<E2 as Engine>::Scalar>;
26
-
27
- // fn debug_circuit(circuit: &C1, z_in: &[<E1 as Engine>::Scalar]) {
28
- // let mut cs = TestConstraintSystem::<<E1 as Engine>::Scalar>::new();
29
-
30
- // let z: Vec<AllocatedNum<<E1 as Engine>::Scalar>> = z_in
31
- // .iter()
32
- // .enumerate()
33
- // .map(|(i, &value)| {
34
- // AllocatedNum::alloc(&mut cs.namespace(|| format!("input {}", i)), || Ok(value))
35
- // .expect("failed to allocate input")
36
- // })
37
- // .collect();
38
-
39
- // circuit.synthesize(&mut cs, &z).expect("synthesis failed");
40
-
41
- // println!("Constraint System:");
42
- // println!("{}", cs.pretty_print());
43
-
44
- // if !cs.is_satisfied() {
45
- // println!("Constraint system not satisfied!");
46
- // for (i, constraint) in cs.which_is_unsatisfied().iter().enumerate() {
47
- // println!("Unsatisfied Constraint {}: {:?}", i, constraint);
48
- // }
49
- // } else {
50
- // println!("All constraints satisfied.");
51
- // }
52
-
53
- // assert!(cs.is_satisfied(), "Constraints not satisfied");
54
- // }
55
-
56
- // fn create_public_params() -> PublicParams<E1, E2, C1, C2> {
57
- // let mut tree = IndexedMerkleTree::new_with_size(4).unwrap();
58
- // let test_label = sha256_mod(b"test");
59
- // let test_value = sha256_mod(b"value");
60
- // let mut test_node = Node::new_leaf(true, test_label, test_value, Node::TAIL);
61
-
62
- // let test_proof = tree.insert_node(&mut test_node).unwrap();
63
- // let test_circuit = MerkleProofStepCircuit::from_proof(Proof::Insert(test_proof))[0].clone();
64
-
65
- // let circuit_primary = test_circuit;
66
- // let circuit_secondary = TrivialCircuit::default();
67
-
68
- // PublicParams::<E1, E2, C1, C2>::setup(
69
- // &circuit_primary,
70
- // &circuit_secondary,
71
- // &*default_ck_hint(),
72
- // &*default_ck_hint(),
73
- // )
74
- // .unwrap()
75
- // }
76
-
77
- // #[test]
78
- // fn test_nova() {
79
- // let mut tree = IndexedMerkleTree::new_with_size(4).unwrap();
80
- // let initial_commitment = Hash::new(tree.get_commitment().unwrap())
81
- // .to_scalar()
82
- // .unwrap();
83
-
84
- // // create three nodes to insert
85
- // let ryan = sha256_mod(b"Ryan");
86
- // let ford = sha256_mod(b"Ford");
87
- // let sebastian = sha256_mod(b"Sebastian");
88
- // let pusch = sha256_mod(b"Pusch");
89
- // let ethan = sha256_mod(b"Ethan");
90
- // let triple_zero = sha256_mod(b"000");
91
-
92
- // let mut ryans_node = Node::new_leaf(true, ryan, ford, Node::TAIL);
93
- // let mut sebastians_node = Node::new_leaf(true, sebastian, pusch, Node::TAIL);
94
- // let mut ethans_node = Node::new_leaf(true, ethan, triple_zero, Node::TAIL);
95
-
96
- // // generate proofs for the three nodes
97
- // let first_insert_proof = tree.insert_node(&mut ryans_node).unwrap();
98
- // let second_insert_proof = tree.insert_node(&mut sebastians_node).unwrap();
99
- // let third_insert_proof = tree.insert_node(&mut ethans_node).unwrap();
100
-
101
- // // create zkSNARKs for the three proofs
102
- // let first_insert_zk_snark = Proof::Insert(first_insert_proof);
103
- // let second_insert_zk_snark = Proof::Insert(second_insert_proof);
104
- // let third_insert_zk_snark = Proof::Insert(third_insert_proof);
105
-
106
- // let proofs = vec![
107
- // first_insert_zk_snark,
108
- // second_insert_zk_snark,
109
- // third_insert_zk_snark,
110
- // ];
111
-
112
- // let circuits: Vec<C1> = proofs
113
- // .into_iter()
114
- // .flat_map(MerkleProofStepCircuit::from_proof)
115
- // .collect();
116
-
117
- // println!("Creating public params...");
118
- // let pp = create_public_params();
119
- // println!("Created public params.");
120
-
121
- // let initial_primary_inputs = vec![
122
- // initial_commitment,
123
- // <E1 as Engine>::Scalar::zero(), // initial existing node label
124
- // <E1 as Engine>::Scalar::zero(), // initial missing node label
125
- // ];
126
-
127
- // let secondary_circuit = TrivialCircuit::default();
128
-
129
- // println!("Creating recursive snark...");
130
- // let recursive_snark_result = RecursiveSNARK::new(
131
- // &pp,
132
- // &circuits[0],
133
- // &secondary_circuit,
134
- // &initial_primary_inputs,
135
- // &[<E2 as Engine>::Scalar::from(2u64)],
136
- // );
137
-
138
- // let mut z1_scalars = initial_primary_inputs;
139
- // let mut z2_scalars = [<E2 as Engine>::Scalar::from(2u64)];
140
-
141
- // match recursive_snark_result {
142
- // Ok(mut recursive_snark) => {
143
- // println!("Created recursive snark successfully.");
144
-
145
- // for (i, circuit) in circuits.iter().enumerate() {
146
- // println!("Step: {i}");
147
-
148
- // debug_circuit(circuit, &z1_scalars);
149
-
150
- // let prove_result = recursive_snark.prove_step(&pp, circuit, &secondary_circuit);
151
-
152
- // match prove_result {
153
- // Ok(_) => {
154
- // println!("Prove step {i} succeeded");
155
- // }
156
- // Err(e) => {
157
- // println!("Prove step {i} failed with error: {:?}", e);
158
- // panic!("Test failed at prove step {i}");
159
- // }
160
- // }
161
-
162
- // let verify_result =
163
- // recursive_snark.verify(&pp, i + 1, &z1_scalars, &z2_scalars);
164
-
165
- // match verify_result {
166
- // Ok(_) => {
167
- // println!("Verify step {i} succeeded")
168
- // }
169
- // Err(e) => {
170
- // println!("Verify step {i} failed with error: {:?}", e);
171
- // panic!("Test failed at verify step {i}");
172
- // }
173
- // }
174
- // }
175
- // }
176
- // Err(e) => {
177
- // println!("Failed to create recursive snark. Error: {:?}", e);
178
- // panic!("Test failed during recursive snark creation");
179
- // }
180
- // }
181
- // }
182
- // }
6
+ #[ cfg( test) ]
7
+ mod tests {
8
+ use crate :: common:: Hashchain ;
9
+ use crate :: tree:: { Hasher , KeyDirectoryTree , SnarkableTree } ;
10
+ use jmt:: mock:: MockTreeStore ;
11
+ use jmt:: KeyHash ;
12
+ use std:: sync:: Arc ;
13
+
14
+ #[ test]
15
+ fn test_key_directory_tree ( ) {
16
+ let store = Arc :: new ( MockTreeStore :: default ( ) ) ;
17
+ let mut tree = KeyDirectoryTree :: new ( store) ;
18
+
19
+ println ! ( "Initial tree state: {:?}" , tree. get_commitment( ) ) ;
20
+
21
+ // Test insert
22
+ let hc1 = Hashchain :: new ( "key_1" . into ( ) ) ;
23
+ let key1 = hc1. get_keyhash ( ) ;
24
+ let insert_proof = tree
25
+ . insert ( key1, hc1. clone ( ) )
26
+ . expect ( "Insert should succeed" ) ;
27
+ assert ! ( insert_proof. verify( ) . is_ok( ) ) ;
28
+ tree. write_batch ( ) . expect ( "Write batch should succeed" ) ;
29
+
30
+ println ! ( "After first insert: {:?}" , tree. get_commitment( ) ) ;
31
+
32
+ // Test get after insert
33
+ // Test get after insert
34
+ let get_result = tree. get ( key1) . expect ( "Get should succeed" ) ;
35
+ println ! ( "Get result after insert: {:?}" , get_result) ;
36
+ assert_eq ! ( get_result. expect( "Key should exist" ) , hc1) ;
37
+
38
+ // Test update
39
+ let mut hc1_updated = hc1. clone ( ) ;
40
+ hc1_updated
41
+ . add ( "new_value" . into ( ) )
42
+ . expect ( "Add to hashchain should succeed" ) ;
43
+ let update_proof = tree
44
+ . update ( key1, hc1_updated. clone ( ) )
45
+ . expect ( "Update should succeed" ) ;
46
+ assert ! ( update_proof. verify( ) . is_ok( ) ) ;
47
+ tree. write_batch ( ) . expect ( "Write batch should succeed" ) ;
48
+
49
+ // Test get after update
50
+ let get_result_after_update = tree. get ( key1) . expect ( "Get should succeed" ) ;
51
+ assert_eq ! (
52
+ get_result_after_update. expect( "Key should exist" ) ,
53
+ hc1_updated
54
+ ) ;
55
+
56
+ // Test insert duplicate key
57
+ let insert_duplicate_result = tree. insert ( key1, hc1. clone ( ) ) ;
58
+ assert ! ( insert_duplicate_result. is_err( ) ) ;
59
+
60
+ // Test update non-existing key
61
+ let non_existing_key = KeyHash :: with :: < Hasher > ( b"non_existing_key" ) ;
62
+ let update_non_existing_result = tree. update ( non_existing_key, hc1. clone ( ) ) ;
63
+ assert ! ( update_non_existing_result. is_err( ) ) ;
64
+
65
+ // Test get non-existing key
66
+ let get_non_existing_result = tree. get ( non_existing_key) . expect ( "Get should not fail" ) ;
67
+ assert ! ( get_non_existing_result. is_err( ) ) ;
68
+ if let Err ( non_membership_proof) = get_non_existing_result {
69
+ assert ! ( non_membership_proof. verify( ) . is_ok( ) ) ;
70
+ }
71
+
72
+ // Test multiple inserts and updates
73
+ let hc2 = Hashchain :: new ( "key_2" . into ( ) ) ;
74
+ let key2 = hc2. get_keyhash ( ) ;
75
+ tree. insert ( key2, hc2. clone ( ) )
76
+ . expect ( "Insert should succeed" ) ;
77
+ tree. write_batch ( ) . expect ( "Write batch should succeed" ) ;
78
+
79
+ let mut hc2_updated = hc2. clone ( ) ;
80
+ hc2_updated
81
+ . add ( "value2" . into ( ) )
82
+ . expect ( "Add to hashchain should succeed" ) ;
83
+ tree. update ( key2, hc2_updated. clone ( ) )
84
+ . expect ( "Update should succeed" ) ;
85
+ tree. write_batch ( ) . expect ( "Write batch should succeed" ) ;
86
+
87
+ assert_eq ! ( tree. get( key2) . unwrap( ) . unwrap( ) , hc2_updated) ;
88
+
89
+ // Test root hash changes
90
+ let root_before = tree
91
+ . get_commitment ( )
92
+ . expect ( "Get commitment should succeed" ) ;
93
+ let hc3 = Hashchain :: new ( "key_3" . into ( ) ) ;
94
+ let key3 = hc3. get_keyhash ( ) ;
95
+ tree. insert ( key3, hc3) . expect ( "Insert should succeed" ) ;
96
+ tree. write_batch ( ) . expect ( "Write batch should succeed" ) ;
97
+ let root_after = tree
98
+ . get_commitment ( )
99
+ . expect ( "Get commitment should succeed" ) ;
100
+
101
+ assert_ne ! ( root_before, root_after) ;
102
+
103
+ // Test batch writing
104
+ let hc4 = Hashchain :: new ( "key_4" . into ( ) ) ;
105
+ let hc5 = Hashchain :: new ( "key_5" . into ( ) ) ;
106
+ let key4 = hc4. get_keyhash ( ) ;
107
+ let key5 = hc5. get_keyhash ( ) ;
108
+
109
+ tree. insert ( key4, hc4. clone ( ) )
110
+ . expect ( "Insert should succeed" ) ;
111
+ tree. insert ( key5, hc5. clone ( ) )
112
+ . expect ( "Insert should succeed" ) ;
113
+
114
+ // Before writing the batch
115
+ assert ! ( tree. get( key4) . unwrap( ) . is_err( ) ) ;
116
+ assert ! ( tree. get( key5) . unwrap( ) . is_err( ) ) ;
117
+
118
+ tree. write_batch ( ) . expect ( "Write batch should succeed" ) ;
119
+
120
+ // After writing the batch
121
+ assert_eq ! ( tree. get( key4) . unwrap( ) . unwrap( ) , hc4) ;
122
+ assert_eq ! ( tree. get( key5) . unwrap( ) . unwrap( ) , hc5) ;
123
+ }
124
+ }
0 commit comments