From 42fb0dd858b489ac1b4c95f1b387cd40f3c4743b Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 21 Nov 2025 15:42:16 +0100 Subject: [PATCH 1/2] prf: optimize get_domain_element and get_randomness --- src/symmetric/prf/shake_to_field.rs | 44 +++++++++++------------------ 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/src/symmetric/prf/shake_to_field.rs b/src/symmetric/prf/shake_to_field.rs index 24050b0..2c02a23 100644 --- a/src/symmetric/prf/shake_to_field.rs +++ b/src/symmetric/prf/shake_to_field.rs @@ -2,18 +2,12 @@ use crate::F; use super::Pseudorandom; use p3_field::PrimeCharacteristicRing; -use p3_field::PrimeField64; use serde::{Serialize, de::DeserializeOwned}; use sha3::{ Shake128, digest::{ExtendableOutput, Update, XofReader}, }; -use num_bigint::BigUint; - -// Number of pseudorandom bytes to generate one pseudorandom field element -const PRF_BYTES_PER_FE: usize = 8; - const KEY_LENGTH: usize = 32; // 32 bytes const PRF_DOMAIN_SEP: [u8; 16] = [ 0xae, 0xae, 0x22, 0xff, 0x00, 0x01, 0xfa, 0xff, 0x21, 0xaf, 0x12, 0x00, 0x01, 0x11, 0xff, 0x00, @@ -62,19 +56,16 @@ where // Finalize the hash process and create an XofReader let mut xof_reader = hasher.finalize_xof(); - // Buffer to store the output - let mut prf_output = vec![0u8; PRF_BYTES_PER_FE * DOMAIN_LENGTH_FE]; + // Mapping bytes to field elements + std::array::from_fn(|_| { + // Buffer to store the output + let mut buf = [0u8; 8]; - // Read the extended output into the buffer - xof_reader.read(&mut prf_output); + // Read the extended output into the buffer + xof_reader.read(&mut buf); - // Mapping bytes to field elements - std::array::from_fn(|i| { - let chunk_start = i * PRF_BYTES_PER_FE; - let chunk_end = chunk_start + PRF_BYTES_PER_FE; - let integer_value = - BigUint::from_bytes_be(&prf_output[chunk_start..chunk_end]) % F::ORDER_U64; - F::from_u64(integer_value.try_into().unwrap()) + // Mapping bytes to a field element + F::from_u64(u64::from_be_bytes(buf)) }) } @@ -109,19 +100,16 @@ where // Finalize the hash process and create an XofReader let mut xof_reader = hasher.finalize_xof(); - // Buffer to store the output - let mut prf_output = vec![0u8; PRF_BYTES_PER_FE * DOMAIN_LENGTH_FE]; + // Mapping bytes to field elements + std::array::from_fn(|_| { + // Buffer to store the output + let mut buf = [0u8; 8]; - // Read the extended output into the buffer - xof_reader.read(&mut prf_output); + // Read the extended output into the buffer + xof_reader.read(&mut buf); - // Mapping bytes to field elements - std::array::from_fn(|i| { - let chunk_start = i * PRF_BYTES_PER_FE; - let chunk_end = chunk_start + PRF_BYTES_PER_FE; - let integer_value = - BigUint::from_bytes_be(&prf_output[chunk_start..chunk_end]) % F::ORDER_U64; - F::from_u64(integer_value.try_into().unwrap()) + // Mapping bytes to a field element + F::from_u64(u64::from_be_bytes(buf)) }) } From 2920ca790112511f6733799a11f7b34de34501e1 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 24 Nov 2025 17:43:19 +0100 Subject: [PATCH 2/2] some fixes --- src/symmetric/prf/shake_to_field.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/symmetric/prf/shake_to_field.rs b/src/symmetric/prf/shake_to_field.rs index 2c02a23..eef91ed 100644 --- a/src/symmetric/prf/shake_to_field.rs +++ b/src/symmetric/prf/shake_to_field.rs @@ -8,6 +8,9 @@ use sha3::{ digest::{ExtendableOutput, Update, XofReader}, }; +/// Number of pseudorandom bytes to generate one pseudorandom field element +const PRF_BYTES_PER_FE: usize = 16; + const KEY_LENGTH: usize = 32; // 32 bytes const PRF_DOMAIN_SEP: [u8; 16] = [ 0xae, 0xae, 0x22, 0xff, 0x00, 0x01, 0xfa, 0xff, 0x21, 0xaf, 0x12, 0x00, 0x01, 0x11, 0xff, 0x00, @@ -59,13 +62,13 @@ where // Mapping bytes to field elements std::array::from_fn(|_| { // Buffer to store the output - let mut buf = [0u8; 8]; + let mut buf = [0u8; PRF_BYTES_PER_FE]; // Read the extended output into the buffer xof_reader.read(&mut buf); // Mapping bytes to a field element - F::from_u64(u64::from_be_bytes(buf)) + F::from_u128(u128::from_be_bytes(buf)) }) } @@ -103,13 +106,13 @@ where // Mapping bytes to field elements std::array::from_fn(|_| { // Buffer to store the output - let mut buf = [0u8; 8]; + let mut buf = [0u8; PRF_BYTES_PER_FE]; // Read the extended output into the buffer xof_reader.read(&mut buf); // Mapping bytes to a field element - F::from_u64(u64::from_be_bytes(buf)) + F::from_u128(u128::from_be_bytes(buf)) }) }