Skip to content

Commit

Permalink
fix read_mem and tests for small fields. And add byteConversion for d…
Browse files Browse the repository at this point in the history
…egree4BabyBear
  • Loading branch information
Nicole authored and Nicole committed Dec 23, 2024
1 parent 44b5933 commit 9e1cee7
Show file tree
Hide file tree
Showing 3 changed files with 357 additions and 153 deletions.
44 changes: 44 additions & 0 deletions math/src/field/fields/fft_friendly/quartic_babybear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,50 @@ impl ByteConversion for [FieldElement<Babybear31PrimeField>; 4] {
}
}

impl ByteConversion for FieldElement<Degree4BabyBearExtensionField> {
fn to_bytes_be(&self) -> alloc::vec::Vec<u8> {
let mut byte_slice = ByteConversion::to_bytes_be(&self.value()[0]);
byte_slice.extend(ByteConversion::to_bytes_be(&self.value()[1]));
byte_slice.extend(ByteConversion::to_bytes_be(&self.value()[2]));
byte_slice.extend(ByteConversion::to_bytes_be(&self.value()[3]));
byte_slice
}

fn to_bytes_le(&self) -> alloc::vec::Vec<u8> {
let mut byte_slice = ByteConversion::to_bytes_le(&self.value()[0]);
byte_slice.extend(ByteConversion::to_bytes_le(&self.value()[1]));
byte_slice.extend(ByteConversion::to_bytes_le(&self.value()[2]));
byte_slice.extend(ByteConversion::to_bytes_le(&self.value()[3]));
byte_slice
}

fn from_bytes_be(bytes: &[u8]) -> Result<Self, crate::errors::ByteConversionError>
where
Self: Sized,
{
const BYTES_PER_FIELD: usize = 8;
let x0 = FieldElement::from_bytes_be(&bytes[0..BYTES_PER_FIELD])?;
let x1 = FieldElement::from_bytes_be(&bytes[BYTES_PER_FIELD..BYTES_PER_FIELD * 2])?;
let x2 = FieldElement::from_bytes_be(&bytes[BYTES_PER_FIELD * 2..BYTES_PER_FIELD * 3])?;
let x3 = FieldElement::from_bytes_be(&bytes[BYTES_PER_FIELD * 3..BYTES_PER_FIELD * 4])?;

Ok(Self::new([x0, x1, x2, x3]))
}

fn from_bytes_le(bytes: &[u8]) -> Result<Self, crate::errors::ByteConversionError>
where
Self: Sized,
{
const BYTES_PER_FIELD: usize = 8;
let x0 = FieldElement::from_bytes_le(&bytes[0..BYTES_PER_FIELD])?;
let x1 = FieldElement::from_bytes_le(&bytes[BYTES_PER_FIELD..BYTES_PER_FIELD * 2])?;
let x2 = FieldElement::from_bytes_le(&bytes[BYTES_PER_FIELD * 2..BYTES_PER_FIELD * 3])?;
let x3 = FieldElement::from_bytes_le(&bytes[BYTES_PER_FIELD * 3..BYTES_PER_FIELD * 4])?;

Ok(Self::new([x0, x1, x2, x3]))
}
}

impl IsFFTField for Degree4BabyBearExtensionField {
const TWO_ADICITY: u64 = 29;
const TWO_ADIC_PRIMITVE_ROOT_OF_UNITY: Self::BaseType = [
Expand Down
Loading

0 comments on commit 9e1cee7

Please sign in to comment.