Skip to content

Commit 183faaa

Browse files
author
Ari
committed
adding in montu128 helpers
1 parent ad2e286 commit 183faaa

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

ff/src/fields/models/fp/montgomery_backend.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,11 @@ pub trait MontConfig<const N: usize>: 'static + Sync + Send + Sized {
493493
// return Fp::zero();
494494
// }
495495
let fe = Self::from_bigint_mixed::<M>(x.magnitude);
496-
if x.is_positive { fe } else { -fe }
496+
if x.is_positive {
497+
fe
498+
} else {
499+
-fe
500+
}
497501
}
498502

499503
/// Construct from a signed big integer with high 32-bit tail and K low 64-bit limbs.
@@ -503,13 +507,20 @@ pub trait MontConfig<const N: usize>: 'static + Sync + Send + Sized {
503507
fn from_signed_bigint_hi32<const K: usize, const KPLUS1: usize>(
504508
x: crate::biginteger::SignedBigIntHi32<K>,
505509
) -> Fp<MontBackend<Self, N>, N> {
506-
debug_assert!(KPLUS1 == K + 1, "from_signed_bigint_hi32 requires KPLUS1 = K + 1");
510+
debug_assert!(
511+
KPLUS1 == K + 1,
512+
"from_signed_bigint_hi32 requires KPLUS1 = K + 1"
513+
);
507514
// if x.is_zero() {
508515
// return Fp::zero();
509516
// }
510517
let mag = x.magnitude_as_bigint_nplus1::<KPLUS1>();
511518
let fe = Self::from_bigint_mixed::<KPLUS1>(mag);
512-
if x.is_positive() { fe } else { -fe }
519+
if x.is_positive() {
520+
fe
521+
} else {
522+
-fe
523+
}
513524
}
514525

515526
#[inline]
@@ -882,9 +893,7 @@ impl<T: MontConfig<N>, const N: usize> Fp<MontBackend<T, N>, N> {
882893
/// Implementation folds from high to low using the existing N+1 Barrett kernel.
883894
/// Precondition: L >= N. For performance, prefer small L close to N..N+3 when possible.
884895
#[inline(always)]
885-
pub fn from_barrett_reduce<const L: usize, const NPLUS1: usize>(
886-
unreduced: BigInt<L>,
887-
) -> Self {
896+
pub fn from_barrett_reduce<const L: usize, const NPLUS1: usize>(unreduced: BigInt<L>) -> Self {
888897
debug_assert!(NPLUS1 == N + 1);
889898
debug_assert!(L >= N);
890899

@@ -1094,6 +1103,14 @@ impl<T: MontConfig<N>, const N: usize> Fp<MontBackend<T, N>, N> {
10941103
*self = self.const_cios_mul_rhs_hi2(hi as u64, (hi >> 64) as u64);
10951104
}
10961105

1106+
/// Returns self * rhs_high_limbs, where RHS is zero in low N-2 limbs and has its top two
1107+
/// limbs provided by `hi` (low 64 -> limb N-2, high 64 -> limb N-1). Equivalent to K=2.
1108+
/// At the cost 2 extra words of storage uses no bit shift instructions to extract higher limbs
1109+
/// as in mul_hi_u128
1110+
#[inline]
1111+
pub const fn mul_hi_bigint_u128(self, big_int_repre: [u64; 4]) -> Self {
1112+
self.const_cios_mul_rhs_hi2(big_int_repre[2], big_int_repre[3])
1113+
}
10971114
/// Returns self * rhs_high_limbs, where RHS is zero in low N-2 limbs and has its top two
10981115
/// limbs provided by `hi` (low 64 -> limb N-2, high 64 -> limb N-1). Equivalent to K=2.
10991116
#[inline]

0 commit comments

Comments
 (0)