use crate::field::{hash_to_prime_256, sqrt_mod_prime};
use crate::primality::is_prime;
use crate::uint::{Uint, U256, U512};
use crate::Error;
pub fn is_prime_be_32(bytes: &[u8; 32]) -> bool {
match U256::from_be_bytes(bytes) {
Ok(n) => is_prime(&n),
Err(_) => false,
}
}
pub fn is_prime_be_slice(bytes: &[u8]) -> Result<bool, Error> {
let n = U512::from_be_bytes(bytes)?;
Ok(is_prime(&n))
}
pub fn hash_to_prime_be_32(digest: &[u8; 32], out_prime: &mut [u8; 32]) -> Result<u32, Error> {
let (p, steps) = hash_to_prime_256(digest)?;
p.write_be_bytes(out_prime)?;
Ok(steps)
}
pub fn sqrt_mod_prime_be_32(
a_bytes: &[u8; 32],
p_bytes: &[u8; 32],
out_root: &mut [u8; 32],
) -> Result<bool, Error> {
let a = U256::from_be_bytes(a_bytes)?;
let p = U256::from_be_bytes(p_bytes)?;
match sqrt_mod_prime(&a, &p)? {
Some(r) => {
r.write_be_bytes(out_root)?;
Ok(true)
}
None => {
out_root.fill(0);
Ok(false)
}
}
}
pub fn sqrt_mod_prime_be<const LIMBS: usize>(
a_bytes: &[u8],
p_bytes: &[u8],
out_root: &mut [u8],
) -> Result<bool, Error> {
let a = Uint::<LIMBS>::from_be_bytes(a_bytes)?;
let p = Uint::<LIMBS>::from_be_bytes(p_bytes)?;
match sqrt_mod_prime(&a, &p)? {
Some(r) => {
r.write_be_bytes(out_root)?;
Ok(true)
}
None => {
out_root.fill(0);
Ok(false)
}
}
}