Skip to content

Commit

Permalink
Add Babybear field (#549)
Browse files Browse the repository at this point in the history
* starting with babybear field and add function

* implemented all traits for babybear

* added test for the babybear field

* Babybear field from MontgomeryBackendPrimeField

* added tests to babybear.rs

* added requested changes and isFFTField trait

* ran `cargo fmt` to pass the lint check.

* deleted the isFFTField trait implementation

* deleted .vscode directory
  • Loading branch information
mdvillagra authored Sep 15, 2023
1 parent 29af640 commit b394a6a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
101 changes: 101 additions & 0 deletions math/src/field/fields/fft_friendly/babybear.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use crate::{
field::{
element::FieldElement,
fields::montgomery_backed_prime_fields::{IsModulus, MontgomeryBackendPrimeField},
},
unsigned_integer::element::U64,
};

pub type U64MontgomeryBackendPrimeField<T> = MontgomeryBackendPrimeField<T, 1>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MontgomeryConfigBabybear31PrimeField;
impl IsModulus<U64> for MontgomeryConfigBabybear31PrimeField {
//Babybear Prime p = 2^31 - 2^27 + 1 = 0x78000001
const MODULUS: U64 = U64::from_u64(2013265921);
}

pub type Babybear31PrimeField =
U64MontgomeryBackendPrimeField<MontgomeryConfigBabybear31PrimeField>;

impl FieldElement<Babybear31PrimeField> {
pub fn to_bytes_le(&self) -> [u8; 8] {
let limbs = self.representative().limbs;
limbs[0].to_le_bytes()
}

pub fn to_bytes_be(&self) -> [u8; 8] {
let limbs = self.representative().limbs;
limbs[0].to_be_bytes()
}
}

impl PartialOrd for FieldElement<Babybear31PrimeField> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.representative().partial_cmp(&other.representative())
}
}

impl Ord for FieldElement<Babybear31PrimeField> {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.representative().cmp(&other.representative())
}
}

#[cfg(test)]
mod test_babybear_31_bytes_ops {
use super::Babybear31PrimeField;
use crate::{field::element::FieldElement, traits::ByteConversion};

#[test]
#[cfg(feature = "std")]
fn byte_serialization_for_a_number_matches_with_byte_conversion_implementation_le() {
let element = FieldElement::<Babybear31PrimeField>::from_hex_unchecked(
"\
0123456701234567\
",
);
let bytes = element.to_bytes_le();
let expected_bytes: [u8; 8] = ByteConversion::to_bytes_le(&element).try_into().unwrap();
assert_eq!(bytes, expected_bytes);
}

#[test]
#[cfg(feature = "std")]
fn byte_serialization_for_a_number_matches_with_byte_conversion_implementation_be() {
let element = FieldElement::<Babybear31PrimeField>::from_hex_unchecked(
"\
0123456701234567\
",
);
let bytes = element.to_bytes_be();
let expected_bytes: [u8; 8] = ByteConversion::to_bytes_be(&element).try_into().unwrap();
assert_eq!(bytes, expected_bytes);
}

#[test]

fn byte_serialization_and_deserialization_works_le() {
let element = FieldElement::<Babybear31PrimeField>::from_hex_unchecked(
"\
7654321076543210\
",
);
let bytes = element.to_bytes_le();
let from_bytes = FieldElement::<Babybear31PrimeField>::from_bytes_le(&bytes).unwrap();
assert_eq!(element, from_bytes);
}

#[test]

fn byte_serialization_and_deserialization_works_be() {
let element = FieldElement::<Babybear31PrimeField>::from_hex_unchecked(
"\
7654321076543210\
",
);
let bytes = element.to_bytes_be();
let from_bytes = FieldElement::<Babybear31PrimeField>::from_bytes_be(&bytes).unwrap();
assert_eq!(element, from_bytes);
}
}
2 changes: 2 additions & 0 deletions math/src/field/fields/fft_friendly/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/// Implemenation of the Babybear Prime field p = 2^31 - 2^27 + 1
pub mod babybear;
/// Implementation of two-adic prime field over 256 bit unsigned integers.
pub mod stark_252_prime_field;
1 change: 1 addition & 0 deletions math/src/unsigned_integer/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use core::fmt::{self, Debug, Display};
pub type U384 = UnsignedInteger<6>;
pub type U256 = UnsignedInteger<4>;
pub type U128 = UnsignedInteger<2>;
pub type U64 = UnsignedInteger<1>;

/// A big unsigned integer in base 2^{64} represented
/// as fixed-size array `limbs` of `u64` components.
Expand Down

0 comments on commit b394a6a

Please sign in to comment.