Skip to content
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

Make modular reduction work for large byte sizes #78

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/fields/fp/arkworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ impl PrimeField for Fp {
}

fn from_le_bytes_mod_order(bytes: &[u8]) -> Self {
assert!(bytes.len() == 48);

let mut t = [0u8; 48];
t.copy_from_slice(&bytes[..48]);
let modulus_field_montgomery = Fp::from_raw_bytes(&t);

modulus_field_montgomery
bytes
.chunks(48)
.map(|x| {
let mut padded = [0u8; 48];
padded[..x.len()].copy_from_slice(x);
Self::from_raw_bytes(&padded)
}) // [X, 2^(384) * X, ...]
.rev()
.fold(Self::zero(), |acc, x| acc * (FIELD_SIZE_POWER_OF_TWO) + x) // let acc =
}
}

Expand Down Expand Up @@ -578,7 +580,7 @@ mod tests {

proptest! {
#[test]
fn test_from_le_bytes_mod_order_vs_naive(bytes in any::<[u8; 48]>()) {
fn test_from_le_bytes_mod_order_vs_naive(bytes in any::<[u8; 128]>()) {
let way1 = Fp::from_le_bytes_mod_order(&bytes);
let way2 = naive_from_le_bytes_mod_order(&bytes);
assert_eq!(way1, way2);
Expand Down
5 changes: 5 additions & 0 deletions src/fields/fp/u32/arkworks_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ pub const TWO_ADIC_ROOT_OF_UNITY: Fp = Fp::from_montgomery_limbs([
2031790878, 3754616354, 3204826524, 1913374628, 226001622, 631062918, 2984565398, 3626713688,
1739907172, 3086590412, 1450066569, 16622719,
]);

pub const FIELD_SIZE_POWER_OF_TWO: Fp = Fp::from_montgomery_limbs([
2483080482, 3079039084, 2953064881, 53083306, 1658238061, 581300497, 2189280172, 3219094787,
1098451961, 2206110448, 512838536, 7208139,
]);
9 changes: 9 additions & 0 deletions src/fields/fp/u64/arkworks_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,12 @@ pub const TWO_ADIC_ROOT_OF_UNITY: Fp = Fp::from_montgomery_limbs([
13256804877427073124,
71394035925664393,
]);

pub const FIELD_SIZE_POWER_OF_TWO: Fp = Fp::from_montgomery_limbs([
13224372171368877346,
227991066186625457,
2496666625421784173,
13825906835078366124,
9475172226622360569,
30958721782860680,
]);
19 changes: 11 additions & 8 deletions src/fields/fq/arkworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ark_serialize::{
CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
CanonicalSerializeWithFlags, Compress, EmptyFlags, Flags, SerializationError, Valid, Validate,
};
use ark_std::iterable::Iterable;
use ark_std::{rand, str::FromStr, string::ToString, One, Zero};
use core::convert::TryInto;
use core::{
Expand Down Expand Up @@ -52,13 +53,15 @@ impl PrimeField for Fq {
}

fn from_le_bytes_mod_order(bytes: &[u8]) -> Self {
assert!(bytes.len() == 32);

let mut t = [0u8; 32];
t.copy_from_slice(&bytes[..32]);
let modulus_field_montgomery = Fq::from_raw_bytes(&t);

modulus_field_montgomery
bytes
.chunks(32)
.map(|x| {
let mut padded = [0u8; 32];
padded[..x.len()].copy_from_slice(x);
Self::from_raw_bytes(&padded)
}) // [X, 2^256 * X, ...]
.rev()
.fold(Self::zero(), |acc, x| acc * (FIELD_SIZE_POWER_OF_TWO) + x) // let acc =
}
}

Expand Down Expand Up @@ -575,7 +578,7 @@ mod tests {

proptest! {
#[test]
fn test_from_le_bytes_mod_order_vs_naive(bytes in any::<[u8; 32]>()) {
fn test_from_le_bytes_mod_order_vs_naive(bytes in any::<[u8; 80]>()) {
let way1 = Fq::from_le_bytes_mod_order(&bytes);
let way2 = naive_from_le_bytes_mod_order(&bytes);
assert_eq!(way1, way2);
Expand Down
4 changes: 4 additions & 0 deletions src/fields/fq/u32/arkworks_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ pub const MULTIPLICATIVE_GENERATOR: Fq = Fq::from_montgomery_limbs([
pub const TWO_ADIC_ROOT_OF_UNITY: Fq = Fq::from_montgomery_limbs([
3661289032, 2944457293, 4231536044, 1579301595, 3002672421, 62522982, 1005564110, 261123163,
]);

pub const FIELD_SIZE_POWER_OF_TWO: Fq = Fq::from_montgomery_limbs([
3093398907, 634746810, 2288015647, 3425445813, 3856434579, 2815164559, 4025600313, 18864871,
]);
7 changes: 7 additions & 0 deletions src/fields/fq/u64/arkworks_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ pub const TWO_ADIC_ROOT_OF_UNITY: Fq = Fq::from_montgomery_limbs([
268534165941069093,
1121515446318641358,
]);

pub const FIELD_SIZE_POWER_OF_TWO: Fq = Fq::from_montgomery_limbs([
2726216793283724667,
14712177743343147295,
12091039717619697043,
81024008013859129,
]);
18 changes: 10 additions & 8 deletions src/fields/fr/arkworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ impl PrimeField for Fr {
}

fn from_le_bytes_mod_order(bytes: &[u8]) -> Self {
assert!(bytes.len() == 32);

let mut t = [0u8; 32];
t.copy_from_slice(&bytes[..32]);
let modulus_field_montgomery = Fr::from_raw_bytes(&t);

modulus_field_montgomery
bytes
.chunks(32)
.map(|x| {
let mut padded = [0u8; 32];
padded[..x.len()].copy_from_slice(x);
Self::from_raw_bytes(&padded)
}) // [X, 2^256 * X, ...]
.rev()
.fold(Self::zero(), |acc, x| acc * (FIELD_SIZE_POWER_OF_TWO) + x) // let acc =
}
}

Expand Down Expand Up @@ -561,7 +563,7 @@ mod tests {

proptest! {
#[test]
fn test_from_le_bytes_mod_order_vs_naive(bytes in any::<[u8; 32]>()) {
fn test_from_le_bytes_mod_order_vs_naive(bytes in any::<[u8; 80]>()) {
let way1 = Fr::from_le_bytes_mod_order(&bytes);
let way2 = naive_from_le_bytes_mod_order(&bytes);
assert_eq!(way1, way2);
Expand Down
4 changes: 4 additions & 0 deletions src/fields/fr/u32/arkworks_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ pub const MULTIPLICATIVE_GENERATOR: Fr = Fr::from_montgomery_limbs([
pub const TWO_ADIC_ROOT_OF_UNITY: Fr = Fr::from_montgomery_limbs([
4072134089, 3532211007, 687987897, 3182963395, 1946006545, 2981045345, 3353731427, 11842004,
]);

pub const FIELD_SIZE_POWER_OF_TWO: Fr = Fr::from_montgomery_limbs([
1784009822, 928422349, 2054412915, 4130980853, 3439491617, 3389392028, 4009941057, 75160094,
]);
7 changes: 7 additions & 0 deletions src/fields/fr/u64/arkworks_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ pub const TWO_ADIC_ROOT_OF_UNITY: Fr = Fr::from_montgomery_limbs([
12803492244414043445,
50841023252832411,
]);

pub const FIELD_SIZE_POWER_OF_TWO: Fr = Fr::from_montgomery_limbs([
3987543627614508126,
17742427666091596403,
14557327917022607905,
322810149704226881,
]);
Loading