Skip to content
Open
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
1 change: 1 addition & 0 deletions curves/bn254/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ark-ff = { workspace = true }
ark-ec = { workspace = true }
ark-std = { workspace = true }
ark-r1cs-std = { workspace = true, optional = true }
ark-serialize = { workspace = true }

[dev-dependencies]
ark-serialize = { workspace = true }
Expand Down
4 changes: 3 additions & 1 deletion curves/bn254/src/fields/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use ark_ec::bn::FromPsi6Pow;
/// This module contains relevant data structures such as compressible Fq12 and compressed Fq12
/// and the relevant compression and conversion functions.
use ark_ff::{AdditiveGroup, Field, Fp12, Fp12Config, Fp6Config, MontFp};
use ark_ff::vec::Vec;
use ark_serialize::{ CanonicalDeserialize, CanonicalSerialize};

pub type CompressibleFq12 = Fp12<CompressibleFq12Config>;

// https://eprint.iacr.org/2007/429.pdf Proposition 1
#[derive(Clone, Copy)]
#[derive(Clone, Copy, CanonicalSerialize, CanonicalDeserialize)]
pub struct CompressedFq12(pub (Fq2, Fq2));

#[derive(Clone, Copy)]
Expand Down
3 changes: 2 additions & 1 deletion ec/src/models/bn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ark_ff::{
},
CyclotomicMultSubgroup,
};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{cfg_chunks_mut, marker::PhantomData, vec::*};
use educe::Educe;
use itertools::Itertools;
Expand Down Expand Up @@ -221,7 +222,7 @@ pub trait BnConfig: 'static + Sized {
type Fp2Config: Fp2Config<Fp = Self::Fp>;
type Fp6Config: Fp6Config<Fp2Config = Self::Fp2Config>;
type Fp12Config: Fp12Config<Fp6Config = Self::Fp6Config>;
type CompressedFp12Config: FromPsi6Pow<Self> + Sized;
type CompressedFp12Config: FromPsi6Pow<Self> + Sized + Sync + CanonicalDeserialize + CanonicalSerialize;
type G1Config: SWCurveConfig<BaseField = Self::Fp>;
type G2Config: SWCurveConfig<
BaseField = Fp2<Self::Fp2Config>,
Expand Down
2 changes: 1 addition & 1 deletion ec/src/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zeroize::Zeroize;
use crate::{AffineRepr, CurveGroup, PrimeGroup, VariableBaseMSM};

pub trait CompressedPairing: Pairing {
type CompressedTargetField;
type CompressedTargetField: Sync + CanonicalSerialize + CanonicalDeserialize;

fn compressed_final_exponentiation(
f: MillerLoopOutput<Self>,
Expand Down
12 changes: 5 additions & 7 deletions ec/src/scalar_mul/variable_base/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ark_ff::prelude::*;
use ark_ff::biginteger::{S128, S64};
use ark_ff::prelude::*;
use ark_std::{
borrow::Borrow,
cfg_chunks, cfg_into_iter, cfg_iter,
Expand Down Expand Up @@ -640,9 +640,8 @@ pub fn msm_s64<V: VariableBaseMSM>(
Either::Right(b)
}
});
let (negative_scalars, non_negative_scalars): (Vec<u64>, Vec<u64>) = scalars
.iter()
.partition_map(|s| {
let (negative_scalars, non_negative_scalars): (Vec<u64>, Vec<u64>) =
scalars.iter().partition_map(|s| {
let mag = s.magnitude_as_u64();
if !s.sign() {
Either::Left(mag)
Expand Down Expand Up @@ -688,9 +687,8 @@ pub fn msm_s128<V: VariableBaseMSM>(
Either::Right(b)
}
});
let (negative_scalars, non_negative_scalars): (Vec<u128>, Vec<u128>) = scalars
.iter()
.partition_map(|s| {
let (negative_scalars, non_negative_scalars): (Vec<u128>, Vec<u128>) =
scalars.iter().partition_map(|s| {
let mag = s.magnitude_as_u128();
if !s.sign() {
Either::Left(mag)
Expand Down
10 changes: 5 additions & 5 deletions ff/src/biginteger/i8_or_i96.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::biginteger::{S160, S224};
use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use allocative::Allocative;
use ark_serialize::{
CanonicalDeserialize, CanonicalSerialize, Compress, Read, SerializationError, Valid, Validate,
Write,
};
use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};

/// Compact signed integer optimized for the common `i8` case, widening to a 96-bit
/// split representation when needed (low 64 bits in `large_lo`, next 32 bits in `large_hi`).
Expand Down Expand Up @@ -563,8 +563,8 @@ impl Mul<S160> for I8OrI96 {
let mut c2 = c1;
let r2 = mac_with_carry!(0u64, x0, b2, &mut c2);

let r3_low = ((c2 as u128)
+ crate::biginteger::arithmetic::widening_mul(x1, b2)) as u64;
let r3_low =
((c2 as u128) + crate::biginteger::arithmetic::widening_mul(x1, b2)) as u64;
let hi32 = (r3_low & 0xFFFF_FFFF) as u32;
(r0, r1, r2, hi32)
}
Expand All @@ -588,8 +588,8 @@ impl Mul<S160> for I8OrI96 {
let mut r2 = mac_with_carry!(0u64, x0, b2, &mut c2);
r2 = mac_with_carry!(r2, x1, b1, &mut c2);

let r3_low = ((c2 as u128)
+ crate::biginteger::arithmetic::widening_mul(x1, b2)) as u64;
let r3_low =
((c2 as u128) + crate::biginteger::arithmetic::widening_mul(x1, b2)) as u64;
let hi32 = (r3_low & 0xFFFF_FFFF) as u32;
(r0, r1, r2, hi32)
}
Expand Down
71 changes: 57 additions & 14 deletions ff/src/biginteger/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ impl<const N: usize> SignedBigInt<N> {
/// Debug-asserts that M <= N.
#[inline]
pub fn zero_extend_from<const M: usize>(smaller: &SignedBigInt<M>) -> SignedBigInt<N> {
debug_assert!(M <= N, "cannot zero-extend: source has more limbs than destination");
debug_assert!(
M <= N,
"cannot zero-extend: source has more limbs than destination"
);
let widened_mag = BigInt::<N>::zero_extend_from::<M>(&smaller.magnitude);
SignedBigInt::from_bigint(widened_mag, smaller.is_positive)
}
Expand All @@ -223,16 +226,25 @@ impl<const N: usize> SignedBigInt<N> {
pub fn add_trunc<const M: usize>(&self, rhs: &SignedBigInt<N>) -> SignedBigInt<M> {
if self.is_positive == rhs.is_positive {
let mag = self.magnitude.add_trunc::<N, M>(&rhs.magnitude);
return SignedBigInt::<M> { magnitude: mag, is_positive: self.is_positive };
return SignedBigInt::<M> {
magnitude: mag,
is_positive: self.is_positive,
};
}
match self.magnitude.cmp(&rhs.magnitude) {
Ordering::Greater | Ordering::Equal => {
let mag = self.magnitude.sub_trunc::<N, M>(&rhs.magnitude);
SignedBigInt::<M> { magnitude: mag, is_positive: self.is_positive }
SignedBigInt::<M> {
magnitude: mag,
is_positive: self.is_positive,
}
},
Ordering::Less => {
let mag = rhs.magnitude.sub_trunc::<N, M>(&self.magnitude);
SignedBigInt::<M> { magnitude: mag, is_positive: rhs.is_positive }
SignedBigInt::<M> {
magnitude: mag,
is_positive: rhs.is_positive,
}
},
}
}
Expand All @@ -242,16 +254,25 @@ impl<const N: usize> SignedBigInt<N> {
pub fn sub_trunc<const M: usize>(&self, rhs: &SignedBigInt<N>) -> SignedBigInt<M> {
if self.is_positive != rhs.is_positive {
let mag = self.magnitude.add_trunc::<N, M>(&rhs.magnitude);
return SignedBigInt::<M> { magnitude: mag, is_positive: self.is_positive };
return SignedBigInt::<M> {
magnitude: mag,
is_positive: self.is_positive,
};
}
match self.magnitude.cmp(&rhs.magnitude) {
Ordering::Greater | Ordering::Equal => {
let mag = self.magnitude.sub_trunc::<N, M>(&rhs.magnitude);
SignedBigInt::<M> { magnitude: mag, is_positive: self.is_positive }
SignedBigInt::<M> {
magnitude: mag,
is_positive: self.is_positive,
}
},
Ordering::Less => {
let mag = rhs.magnitude.sub_trunc::<N, M>(&self.magnitude);
SignedBigInt::<M> { magnitude: mag, is_positive: !self.is_positive }
SignedBigInt::<M> {
magnitude: mag,
is_positive: !self.is_positive,
}
},
}
}
Expand All @@ -265,16 +286,25 @@ impl<const N: usize> SignedBigInt<N> {
) -> SignedBigInt<P> {
if self.is_positive == rhs.is_positive {
let mag = self.magnitude.add_trunc::<M, P>(&rhs.magnitude);
return SignedBigInt::<P> { magnitude: mag, is_positive: self.is_positive };
return SignedBigInt::<P> {
magnitude: mag,
is_positive: self.is_positive,
};
}
match self.cmp_magnitude_mixed(rhs) {
Ordering::Greater | Ordering::Equal => {
let mag = self.magnitude.sub_trunc::<M, P>(&rhs.magnitude);
SignedBigInt::<P> { magnitude: mag, is_positive: self.is_positive }
SignedBigInt::<P> {
magnitude: mag,
is_positive: self.is_positive,
}
},
Ordering::Less => {
let mag = rhs.magnitude.sub_trunc::<N, P>(&self.magnitude);
SignedBigInt::<P> { magnitude: mag, is_positive: rhs.is_positive }
SignedBigInt::<P> {
magnitude: mag,
is_positive: rhs.is_positive,
}
},
}
}
Expand Down Expand Up @@ -373,16 +403,25 @@ impl<const N: usize> SignedBigInt<N> {
) -> SignedBigInt<P> {
if self.is_positive != rhs.is_positive {
let mag = self.magnitude.add_trunc::<M, P>(&rhs.magnitude);
return SignedBigInt::<P> { magnitude: mag, is_positive: self.is_positive };
return SignedBigInt::<P> {
magnitude: mag,
is_positive: self.is_positive,
};
}
match self.cmp_magnitude_mixed(rhs) {
Ordering::Greater | Ordering::Equal => {
let mag = self.magnitude.sub_trunc::<M, P>(&rhs.magnitude);
SignedBigInt::<P> { magnitude: mag, is_positive: self.is_positive }
SignedBigInt::<P> {
magnitude: mag,
is_positive: self.is_positive,
}
},
Ordering::Less => {
let mag = rhs.magnitude.sub_trunc::<N, P>(&self.magnitude);
SignedBigInt::<P> { magnitude: mag, is_positive: !self.is_positive }
SignedBigInt::<P> {
magnitude: mag,
is_positive: !self.is_positive,
}
},
}
}
Expand Down Expand Up @@ -696,7 +735,11 @@ impl<const N: usize> core::cmp::Ord for SignedBigInt<N> {
(false, true) => Ordering::Less,
_ => {
let ord = self.magnitude.cmp(&other.magnitude);
if self.is_positive { ord } else { ord.reverse() }
if self.is_positive {
ord
} else {
ord.reverse()
}
},
}
}
Expand Down
29 changes: 21 additions & 8 deletions ff/src/biginteger/signed_hi_32.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::biginteger::{BigInt, SignedBigInt, S128, S64};
use allocative::Allocative;
use ark_std::cmp::Ordering;
use ark_std::vec::Vec;
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use crate::biginteger::{BigInt, SignedBigInt, S64, S128};
use ark_serialize::{
CanonicalDeserialize, CanonicalSerialize, Compress, Read, SerializationError, Valid, Validate,
Write,
};
use ark_std::cmp::Ordering;
use ark_std::vec::Vec;
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};

/// Compact signed big-integer parameterized by limb count `N` (total width = `N*64 + 32` bits).
///
Expand Down Expand Up @@ -312,7 +312,10 @@ impl<const N: usize> SignedBigIntHi32<N> {
/// This ignores the sign; pair with `is_positive()` if you need a signed value.
#[inline]
pub fn magnitude_as_bigint_nplus1<const NPLUS1: usize>(&self) -> BigInt<NPLUS1> {
debug_assert!(NPLUS1 == N + 1, "NPLUS1 must be N+1 for SignedBigIntHi32 magnitude pack");
debug_assert!(
NPLUS1 == N + 1,
"NPLUS1 must be N+1 for SignedBigIntHi32 magnitude pack"
);
let mut limbs = [0u64; NPLUS1];
if N > 0 {
limbs[..N].copy_from_slice(&self.magnitude_lo);
Expand All @@ -327,7 +330,10 @@ impl<const N: usize> SignedBigIntHi32<N> {
/// Debug-asserts that M <= N.
#[inline]
pub fn zero_extend_from<const M: usize>(smaller: &SignedBigIntHi32<M>) -> SignedBigIntHi32<N> {
debug_assert!(M <= N, "cannot zero-extend: source has more limbs than destination");
debug_assert!(
M <= N,
"cannot zero-extend: source has more limbs than destination"
);
if N == M {
return SignedBigIntHi32::<N>::new(
// copy to avoid borrowing issues
Expand Down Expand Up @@ -357,7 +363,10 @@ impl<const N: usize> SignedBigIntHi32<N> {
/// Debug-asserts that NPLUS1 == N + 1.
#[inline]
pub fn to_signed_bigint_nplus1<const NPLUS1: usize>(&self) -> SignedBigInt<NPLUS1> {
debug_assert!(NPLUS1 == N + 1, "to_signed_bigint_nplus1 requires NPLUS1 = N + 1");
debug_assert!(
NPLUS1 == N + 1,
"to_signed_bigint_nplus1 requires NPLUS1 = N + 1"
);
let mut limbs = [0u64; NPLUS1];
if N > 0 {
limbs[..N].copy_from_slice(self.magnitude_lo());
Expand Down Expand Up @@ -579,7 +588,11 @@ impl<const N: usize> core::cmp::Ord for SignedBigIntHi32<N> {
(false, true) => Ordering::Less,
_ => {
let ord = self.compare_magnitudes(other);
if self.is_positive { ord } else { ord.reverse() }
if self.is_positive {
ord
} else {
ord.reverse()
}
},
}
}
Expand Down
Loading
Loading