From d220d53963feb2d5223f1ff658b9dd2d75f3422a Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Thu, 19 Dec 2024 17:33:10 -0500 Subject: [PATCH] reduce duplicate logic --- primeorder/src/risc0.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/primeorder/src/risc0.rs b/primeorder/src/risc0.rs index 60c7e21c..35cb4423 100644 --- a/primeorder/src/risc0.rs +++ b/primeorder/src/risc0.rs @@ -2,6 +2,7 @@ use crate::FieldBytes; use crate::{affine::AffinePoint, projective::ProjectivePoint}; use core::marker::PhantomData; use core::ops::Deref; +use elliptic_curve::consts::U32; use elliptic_curve::generic_array::GenericArray; use elliptic_curve::subtle::{Choice, ConditionallySelectable}; use elliptic_curve::{PrimeField, Scalar}; @@ -175,14 +176,23 @@ where } } +fn bytes_to_u32_words_le(bytes: &[u8]) -> [u32; 8] { + let mut words = [0u32; 8]; + + // Process 4 bytes at a time to create little-endian u32 words + for (i, chunk) in bytes.chunks(4).enumerate() { + // Convert each big-endian chunk to a little-endian u32 + words[7 - i] = u32::from_be_bytes(chunk.try_into().unwrap()); + } + + words +} + pub(crate) fn felt_to_u32_words_le(data: &C::FieldElement) -> [u32; 8] where C: PrimeCurveParams, { - // TODO alignment - let mut data_bytes: [u8; 32] = data.to_repr().as_slice().try_into().unwrap(); - data_bytes.reverse(); - bytemuck::cast::<_, [u32; 8]>(data_bytes) + bytes_to_u32_words_le(data.to_repr().as_slice()) } #[inline] @@ -227,16 +237,7 @@ pub(crate) fn scalar_to_words(s: &Scalar) -> [u32; 8] where C: PrimeCurveParams, { - let mut words = [0u32; 8]; - let bytes = s.to_repr(); - - // Process 4 bytes at a time to create little-endian u32 words - for (i, chunk) in bytes.as_slice().chunks(4).enumerate() { - // Convert each big-endian chunk to a little-endian u32 - words[7 - i] = u32::from_be_bytes(chunk.try_into().unwrap()); - } - - words + bytes_to_u32_words_le(s.to_repr().as_slice()) } pub(crate) mod ec_impl {