-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Babybear field #549
Merged
Merged
Add Babybear field #549
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8c24af3
starting with babybear field and add function
mdvillagra e008b4b
implemented all traits for babybear
mdvillagra 8233c08
added test for the babybear field
mdvillagra 6f32048
Merge branch 'main' of https://github.com/mdvillagra/lambdaworks into…
mdvillagra 9c2d333
Merge branch 'lambdaclass:main' into main
mdvillagra c260508
Babybear field from MontgomeryBackendPrimeField
mdvillagra fbb79e8
added tests to babybear.rs
mdvillagra b180b79
added requested changes and isFFTField trait
mdvillagra 80fbf1d
Merge branch 'lambdaclass:main' into main
mdvillagra 19f209e
Merge branch 'main' into mdvillagra/issue539
mdvillagra ba436e6
Merge pull request #1 from mdvillagra/mdvillagra/issue539
mdvillagra 2132bbb
ran `cargo fmt` to pass the lint check.
mdvillagra 315ddfb
deleted the isFFTField trait implementation
mdvillagra 442dabd
deleted .vscode directory
mdvillagra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't needed, but it's ok, we can document this later and remove if needed. Doesn't hurt either