-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
29af640
commit b394a6a
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters