Add unit tests of BE keygen related functions against Q value

This commit is contained in:
sydhds
2025-07-18 10:52:48 +02:00
parent 2749be14c6
commit ee712ea84f
4 changed files with 95 additions and 0 deletions

7
Cargo.lock generated
View File

@@ -613,6 +613,12 @@ dependencies = [
"half",
]
[[package]]
name = "claims"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bba18ee93d577a8428902687bcc2b6b45a56b1981a1f6d779731c86cc4c5db18"
[[package]]
name = "clap"
version = "4.5.39"
@@ -1639,6 +1645,7 @@ dependencies = [
"ark-std 0.5.0",
"byteorder",
"cfg-if",
"claims",
"criterion",
"document-features",
"lazy_static",

View File

@@ -64,6 +64,7 @@ document-features = { version = "0.2.11", optional = true }
[dev-dependencies]
criterion = { version = "0.6.0", features = ["html_reports"] }
claims = "0.8.0"
[features]
default = ["pmtree-ft"]

View File

@@ -1148,6 +1148,14 @@ impl RLN {
Ok(())
}
pub fn key_gen_be_2<W: Write>(&self, mut output_data: W) -> Result<(Fr, Fr), RLNError> {
let (identity_secret_hash, id_commitment) = keygen();
output_data.write_all(&fr_to_bytes_be(&identity_secret_hash))?;
output_data.write_all(&fr_to_bytes_be(&id_commitment))?;
Ok((identity_secret_hash, id_commitment))
}
/// Returns an identity trapdoor, nullifier, secret and commitment tuple.
///
/// The identity secret is the Poseidon hash of the identity trapdoor and identity nullifier.
@@ -1193,6 +1201,17 @@ impl RLN {
Ok(())
}
pub fn extended_key_gen_be_2<W: Write>(&self, mut output_data: W) -> Result<(Fr, Fr, Fr, Fr), RLNError> {
let (identity_trapdoor, identity_nullifier, identity_secret_hash, id_commitment) =
extended_keygen();
output_data.write_all(&fr_to_bytes_be(&identity_trapdoor))?;
output_data.write_all(&fr_to_bytes_be(&identity_nullifier))?;
output_data.write_all(&fr_to_bytes_be(&identity_secret_hash))?;
output_data.write_all(&fr_to_bytes_be(&id_commitment))?;
Ok((identity_trapdoor, identity_nullifier, identity_secret_hash, id_commitment))
}
/// Returns an identity secret and identity commitment pair generated using a seed.
///
/// The identity commitment is the Poseidon hash of the identity secret.

View File

@@ -13,7 +13,9 @@ mod test {
use std::fs::File;
use std::io::Read;
use std::mem::MaybeUninit;
use std::str::FromStr;
use std::time::{Duration, Instant};
use claims::assert_lt;
const NO_OF_LEAVES: usize = 256;
@@ -860,6 +862,72 @@ mod test {
assert_eq!(id_commitment, expected_id_commitment_seed_bytes.unwrap());
}
#[test]
// Tests hash to field using FFI APIs
fn test_extended_keygen_be_ffi() {
let q = ark_bn254::Fr::from_str("21888242871839275222246405745257275088548364400416034343698204186575808495616").unwrap();
let mut c = 0;
loop {
// We create a RLN instance
let rln_pointer = create_rln_instance();
// We generate a new identity tuple from an input seed
// let seed_bytes: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// let input_buffer = &Buffer::from(seed_bytes);
let mut output_buffer = MaybeUninit::<Buffer>::uninit();
let success =
extended_key_gen_be(rln_pointer, output_buffer.as_mut_ptr());
// assert!(success, "seeded key gen call failed");
let output_buffer = unsafe { output_buffer.assume_init() };
let result_data = <&[u8]>::from(&output_buffer).to_vec();
let (identity_secret_hash, id_commitment) =
deserialize_identity_pair_be(result_data);
// We check against expected values
// let expected_identity_trapdoor_seed_bytes = str_to_fr(
// "0x766ce6c7e7a01bdf5b3f257616f603918c30946fa23480f2859c597817e6716",
// 16,
// );
// let expected_identity_nullifier_seed_bytes = str_to_fr(
// "0x1f18714c7bc83b5bca9e89d404cf6f2f585bc4c0f7ed8b53742b7e2b298f50b4",
// 16,
// );
// let expected_identity_secret_hash_seed_bytes = str_to_fr(
// "0x2aca62aaa7abaf3686fff2caf00f55ab9462dc12db5b5d4bcf3994e671f8e521",
// 16,
// );
// let expected_id_commitment_seed_bytes = str_to_fr(
// "0x68b66aa0a8320d2e56842581553285393188714c48f9b17acd198b4f1734c5c",
// 16,
// );
// assert_eq!(
// identity_trapdoor,
// expected_identity_trapdoor_seed_bytes.unwrap()
// );
// assert_eq!(
// identity_nullifier,
// expected_identity_nullifier_seed_bytes.unwrap()
// );
// assert_eq!(
// identity_secret_hash,
// expected_identity_secret_hash_seed_bytes.unwrap()
// );
// assert_eq!(id_commitment, expected_id_commitment_seed_bytes.unwrap());
assert_lt!(identity_secret_hash, q);
assert_lt!(id_commitment, q);
c+=1;
if c > 1000 {
break;
}
}
}
#[test]
// Tests hash to field using FFI APIs
fn test_hash_to_field_ffi() {