diff --git a/rln/src/circuit.rs b/rln/src/circuit.rs index a893774..305ed08 100644 --- a/rln/src/circuit.rs +++ b/rln/src/circuit.rs @@ -77,7 +77,7 @@ pub fn zkey_from_folder( } // Loads the verification key from a bytes vector -pub fn vk_from_raw(vk_data: &Vec, zkey_data: &Vec) -> Result> { +pub fn vk_from_raw(vk_data: &[u8], zkey_data: &Vec) -> Result> { let verifying_key: VerifyingKey; if !vk_data.is_empty() { diff --git a/rln/src/pm_tree_adapter.rs b/rln/src/pm_tree_adapter.rs index c42ce47..3007c7d 100644 --- a/rln/src/pm_tree_adapter.rs +++ b/rln/src/pm_tree_adapter.rs @@ -246,7 +246,8 @@ impl ZerokitMerkleTree for PmTree { let data = self.tree.db.get(METADATA_KEY)?; if data.is_none() { - return Err(Report::msg("metadata does not exist")); + // send empty Metadata + return Ok(Vec::new()); } Ok(data.unwrap()) } diff --git a/rln/src/public_api_tests.rs b/rln/src/public_api_tests.rs index f0e5774..1216e9d 100644 --- a/rln/src/public_api_tests.rs +++ b/rln/src/public_api_tests.rs @@ -977,7 +977,7 @@ fn test_get_leaf() { } #[test] -fn test_metadata() { +fn test_valid_metadata() { let tree_height = TEST_TREE_HEIGHT; let input_buffer = @@ -993,3 +993,18 @@ fn test_metadata() { assert_eq!(arbitrary_metadata, received_metadata); } + +#[test] +fn test_empty_metadata() { + let tree_height = TEST_TREE_HEIGHT; + + let input_buffer = + Cursor::new(json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string()); + let rln = RLN::new(tree_height, input_buffer).unwrap(); + + let mut buffer = Cursor::new(Vec::::new()); + rln.get_metadata(&mut buffer).unwrap(); + let received_metadata = buffer.into_inner(); + + assert_eq!(received_metadata.len(), 0); +} diff --git a/rln/tests/ffi.rs b/rln/tests/ffi.rs index f4624c4..9151c2d 100644 --- a/rln/tests/ffi.rs +++ b/rln/tests/ffi.rs @@ -341,7 +341,6 @@ mod test { fn test_merkle_proof_ffi() { let tree_height = TEST_TREE_HEIGHT; let leaf_index = 3; - let user_message_limit = 1; // We create a RLN instance let mut rln_pointer = MaybeUninit::<*mut RLN>::uninit(); @@ -671,7 +670,7 @@ mod test { fn test_rln_proof_ffi() { let tree_height = TEST_TREE_HEIGHT; let no_of_leaves = 256; - let user_message_limit = Fr::from(65535); + let user_message_limit = Fr::from(100); // We generate a vector of random leaves let mut leaves: Vec = Vec::new(); @@ -704,7 +703,6 @@ mod test { let result_data = <&[u8]>::from(&output_buffer).to_vec(); let (identity_secret_hash, read) = bytes_le_to_fr(&result_data); let (id_commitment, _) = bytes_le_to_fr(&result_data[read..].to_vec()); - let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit]); let identity_index: usize = no_of_leaves; @@ -717,7 +715,6 @@ mod test { let rln_identifier = hash_to_field(b"test-rln-identifier"); let external_nullifier = utils_poseidon_hash(&[epoch, rln_identifier]); - let user_message_limit = Fr::from(100); let message_id = Fr::from(0); let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit]); @@ -727,9 +724,6 @@ mod test { let success = set_next_leaf(rln_pointer, input_buffer); assert!(success, "set next leaf call failed"); - // We generate a random rln_identifier - let rln_identifier = hash_to_field(b"test-rln-identifier"); - // We prepare input for generate_rln_proof API // input_data is [ identity_secret<32> | id_index<8> | user_message_limit<32> | message_id<32> | external_nullifier<32> | signal_len<8> | signal ] let mut serialized: Vec = Vec::new(); @@ -819,7 +813,6 @@ mod test { let message_id = Fr::from(0); // We set as leaf rate_commitment, its index would be equal to no_of_leaves - let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit]); let leaf_ser = fr_to_bytes_le(&rate_commitment); let input_buffer = &Buffer::from(leaf_ser.as_ref()); let success = set_next_leaf(rln_pointer, input_buffer); @@ -1268,7 +1261,7 @@ mod test { } #[test] - fn test_metadata() { + fn test_valid_metadata() { // We create a RLN instance let tree_height = TEST_TREE_HEIGHT; @@ -1294,4 +1287,25 @@ mod test { assert_eq!(result_data, seed_bytes.to_vec()); } + + #[test] + fn test_empty_metadata() { + // We create a RLN instance + let tree_height = TEST_TREE_HEIGHT; + + let mut rln_pointer = MaybeUninit::<*mut RLN>::uninit(); + let input_config = json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string(); + let input_buffer = &Buffer::from(input_config.as_bytes()); + let success = new(tree_height, input_buffer, rln_pointer.as_mut_ptr()); + assert!(success, "RLN object creation failed"); + let rln_pointer = unsafe { &mut *rln_pointer.assume_init() }; + + let mut output_buffer = MaybeUninit::::uninit(); + let success = get_metadata(rln_pointer, output_buffer.as_mut_ptr()); + assert!(success, "get_metadata call failed"); + + let output_buffer = unsafe { output_buffer.assume_init() }; + + assert_eq!(output_buffer.len, 0); + } }