Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add more tests for light_utils main module #846

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,9 @@ impl From<UtilsError> for solana_program::program_error::ProgramError {
}
}

pub fn is_smaller_than_bn254_field_size_be(bytes: &[u8; 32]) -> Result<bool, UtilsError> {
pub fn is_smaller_than_bn254_field_size_be(bytes: &[u8; 32]) -> bool {
let bigint = BigUint::from_bytes_be(bytes);
if bigint < ark_bn254::Fr::MODULUS.into() {
Ok(true)
} else {
Ok(false)
}
bigint < ark_bn254::Fr::MODULUS.into()
}

pub fn hash_to_bn254_field_size_be(bytes: &[u8]) -> Option<([u8; 32], u8)> {
Expand All @@ -67,7 +63,7 @@ pub fn hash_to_bn254_field_size_be(bytes: &[u8]) -> Option<([u8; 32], u8)> {
// Truncates to 31 bytes so that value is less than bn254 Fr modulo
// field size.
hashed_value[0] = 0;
if let Ok(true) = is_smaller_than_bn254_field_size_be(&hashed_value) {
if is_smaller_than_bn254_field_size_be(&hashed_value) {
return Some((hashed_value, bump_seed[0]));
}
}
Expand Down Expand Up @@ -109,11 +105,28 @@ pub fn rustfmt(code: String) -> Result<Vec<u8>, anyhow::Error> {

#[cfg(test)]
mod tests {

use num_bigint::ToBigUint;
use solana_program::pubkey::Pubkey;

use crate::bigint::bigint_to_be_bytes_array;

use super::*;

#[test]
fn test_is_smaller_than_bn254_field_size_be() {
let modulus: BigUint = ark_bn254::Fr::MODULUS.into();
let modulus_bytes: [u8; 32] = bigint_to_be_bytes_array(&modulus).unwrap();
assert!(!is_smaller_than_bn254_field_size_be(&modulus_bytes));

let bigint = modulus.clone() - 1.to_biguint().unwrap();
let bigint_bytes: [u8; 32] = bigint_to_be_bytes_array(&bigint).unwrap();
assert!(is_smaller_than_bn254_field_size_be(&bigint_bytes));

let bigint = modulus + 1.to_biguint().unwrap();
let bigint_bytes: [u8; 32] = bigint_to_be_bytes_array(&bigint).unwrap();
assert!(!is_smaller_than_bn254_field_size_be(&bigint_bytes));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!


#[test]
fn test_hash_to_bn254_field_size_be() {
for _ in 0..10_000 {
Expand All @@ -122,9 +135,37 @@ mod tests {
.expect("Failed to find a hash within BN254 field size");
assert_eq!(bump, 255, "Bump seed should be 0");
assert!(
is_smaller_than_bn254_field_size_be(&hashed_value).unwrap(),
is_smaller_than_bn254_field_size_be(&hashed_value),
"Hashed value should be within BN254 field size"
);
}

let max_input = [u8::MAX; 32];
let (hashed_value, bump) = hash_to_bn254_field_size_be(max_input.as_slice())
.expect("Failed to find a hash within BN254 field size");
assert_eq!(bump, 255, "Bump seed should be 255");
assert!(
is_smaller_than_bn254_field_size_be(&hashed_value),
"Hashed value should be within BN254 field size"
);
}

#[test]
fn test_rustfmt() {
let unformatted_code = "use std::mem;

fn main() { println!(\"{}\", mem::size_of::<u64>()); }
"
.to_string();
let formatted_code = rustfmt(unformatted_code).unwrap();
assert_eq!(
String::from_utf8_lossy(&formatted_code),
"use std::mem;

fn main() {
println!(\"{}\", mem::size_of::<u64>());
}
"
);
}
}