Skip to content

Commit

Permalink
Implement u256 prime field (#137)
Browse files Browse the repository at this point in the history
* fix mul_two_256_bit_integers_works_4() test

Test mul_two_256_bit_integers_works_4() fixed after the bug  in the * operator patched

* Update element.rs

* Update element.rs

* get rid of space

* Update element.rs

* Generalize IsMontgomeryConfiguration and MontgomeryBackendPrimeField

Solves #126

* Extended U256 Montgomery backed prime fields test

* Implemented U256PrimeField

solves #95

* Conflicts fixed

* fmt

* Get rid of unused commented lines

* Update montgomery_backed_prime_fields.rs
  • Loading branch information
GianfrancoBazzani authored Mar 4, 2023
1 parent 3c681a3 commit f9ef87e
Show file tree
Hide file tree
Showing 8 changed files with 797 additions and 587 deletions.
8 changes: 5 additions & 3 deletions crypto/src/hash/poseidon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,22 @@ where
#[cfg(test)]
mod tests {
use lambdaworks_math::{
field::fields::u384_prime_field::{IsMontgomeryConfiguration, MontgomeryBackendPrimeField},
field::fields::montgomery_backed_prime_fields::{
IsMontgomeryConfiguration, U384PrimeField,
},
unsigned_integer::element::U384,
};

use super::*;

#[derive(Clone, Debug)]
pub struct TestFieldConfig;
impl IsMontgomeryConfiguration for TestFieldConfig {
impl IsMontgomeryConfiguration<6> for TestFieldConfig {
const MODULUS: U384 =
U384::from("2000000000000080000000000000000000000000000000000000000000000001");
}

pub type PoseidonTestField = MontgomeryBackendPrimeField<TestFieldConfig>;
pub type PoseidonTestField = U384PrimeField<TestFieldConfig>;
type TestFieldElement = FieldElement<PoseidonTestField>;

pub fn load_test_parameters() -> Result<Parameters<PoseidonTestField>, String> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::field::{
element::FieldElement,
fields::u384_prime_field::{IsMontgomeryConfiguration, MontgomeryBackendPrimeField},
fields::montgomery_backed_prime_fields::{
IsMontgomeryConfiguration, MontgomeryBackendPrimeField,
},
};
use crate::unsigned_integer::element::U384;

Expand All @@ -9,11 +11,11 @@ pub const BLS12377_PRIME_FIELD_ORDER: U384 = U384::from("1ae3a4617c510eac63b05c0
// FPBLS12377
#[derive(Clone, Debug)]
pub struct BLS12377FieldConfig;
impl IsMontgomeryConfiguration for BLS12377FieldConfig {
impl IsMontgomeryConfiguration<6> for BLS12377FieldConfig {
const MODULUS: U384 = BLS12377_PRIME_FIELD_ORDER;
}

pub type BLS12377PrimeField = MontgomeryBackendPrimeField<BLS12377FieldConfig>;
pub type BLS12377PrimeField = MontgomeryBackendPrimeField<BLS12377FieldConfig, 6>;

impl FieldElement<BLS12377PrimeField> {
pub fn new_base(a_hex: &str) -> Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::field::{
cubic::{CubicExtensionField, HasCubicNonResidue},
quadratic::{HasQuadraticNonResidue, QuadraticExtensionField},
},
fields::u384_prime_field::{IsMontgomeryConfiguration, MontgomeryBackendPrimeField},
fields::montgomery_backed_prime_fields::{
IsMontgomeryConfiguration, MontgomeryBackendPrimeField,
},
};
use crate::unsigned_integer::element::U384;

Expand All @@ -13,11 +15,11 @@ pub const BLS12381_PRIME_FIELD_ORDER: U384 = U384::from("1a0111ea397fe69a4b1ba7b
// FPBLS12381
#[derive(Clone, Debug)]
pub struct BLS12381FieldConfig;
impl IsMontgomeryConfiguration for BLS12381FieldConfig {
impl IsMontgomeryConfiguration<6> for BLS12381FieldConfig {
const MODULUS: U384 = BLS12381_PRIME_FIELD_ORDER;
}

pub type BLS12381PrimeField = MontgomeryBackendPrimeField<BLS12381FieldConfig>;
pub type BLS12381PrimeField = MontgomeryBackendPrimeField<BLS12381FieldConfig, 6>;

#[derive(Debug, Clone)]
pub struct LevelOneResidue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::elliptic_curve::short_weierstrass::point::ShortWeierstrassProjectivePoint;
use crate::elliptic_curve::traits::IsEllipticCurve;
use crate::field::fields::u384_prime_field::{
use crate::field::fields::montgomery_backed_prime_fields::{
IsMontgomeryConfiguration, MontgomeryBackendPrimeField,
};
use crate::unsigned_integer::element::U384;
Expand All @@ -21,11 +21,11 @@ pub const TEST_CURVE_2_MAIN_SUBGROUP_ORDER: U384 = U384::from("40a065fb5a76390de
// FPBLS12381
#[derive(Clone, Debug)]
pub struct TestCurve2MontgomeryConfig;
impl IsMontgomeryConfiguration for TestCurve2MontgomeryConfig {
impl IsMontgomeryConfiguration<6> for TestCurve2MontgomeryConfig {
const MODULUS: U384 = TEST_CURVE_2_PRIME_FIELD_ORDER;
}

type TestCurve2PrimeField = MontgomeryBackendPrimeField<TestCurve2MontgomeryConfig>;
type TestCurve2PrimeField = MontgomeryBackendPrimeField<TestCurve2MontgomeryConfig, 6>;

/// In F59 the element -1 is not a square. We use this property
/// to construct a Quadratic Field Extension out of it by adding
Expand Down
2 changes: 1 addition & 1 deletion math/src/field/fields/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod u384_prime_field;
pub mod montgomery_backed_prime_fields;
/// Implementation of prime fields over 64 bit unsigned integers.
pub mod u64_prime_field;
Loading

0 comments on commit f9ef87e

Please sign in to comment.