From 0bc7b660495669145f3c7f2a24cfdbe1a8a2ccde Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Wed, 1 Nov 2023 10:09:45 -0700 Subject: [PATCH] bigint elem_exp_consttime: Make implementations more consistent. Use the same argument order, naming, etc. as the x86-64 version. --- src/arithmetic/bigint.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/arithmetic/bigint.rs b/src/arithmetic/bigint.rs index 2b797ac88d..cf8bccb391 100644 --- a/src/arithmetic/bigint.rs +++ b/src/arithmetic/bigint.rs @@ -454,7 +454,7 @@ pub fn elem_exp_consttime( let mut table = vec![0; TABLE_ENTRIES * num_limbs]; - fn gather(table: &[Limb], i: Window, r: &mut Elem) { + fn gather(table: &[Limb], acc: &mut Elem, i: Window) { prefixed_extern! { fn LIMBS_select_512_32( r: *mut Limb, @@ -464,22 +464,22 @@ pub fn elem_exp_consttime( ) -> bssl::Result; } Result::from(unsafe { - LIMBS_select_512_32(r.limbs.as_mut_ptr(), table.as_ptr(), r.limbs.len(), i) + LIMBS_select_512_32(acc.limbs.as_mut_ptr(), table.as_ptr(), acc.limbs.len(), i) }) .unwrap(); } fn power( table: &[Limb], - i: Window, mut acc: Elem, - mut tmp: Elem, m: &Modulus, + i: Window, + mut tmp: Elem, ) -> (Elem, Elem) { for _ in 0..WINDOW_BITS { acc = elem_squared(acc, &m.as_partial()); } - gather(table, i, &mut tmp); + gather(table, &mut tmp, i); let acc = elem_mul(&tmp, acc, m); (acc, tmp) } @@ -515,22 +515,20 @@ pub fn elem_exp_consttime( } let tmp = m.zero(); - let (r, _) = limb::fold_5_bit_windows( + let mut acc = Elem { + limbs: base.limbs, + encoding: PhantomData, + }; + let (acc, _) = limb::fold_5_bit_windows( exponent.limbs(), |initial_window| { - let mut r = Elem { - limbs: base.limbs, - encoding: PhantomData, - }; - gather(&table, initial_window, &mut r); - (r, tmp) + gather(&table, &mut acc, initial_window); + (acc, tmp) }, - |(acc, tmp), window| power(&table, window, acc, tmp, m), + |(acc, tmp), window| power(&table, acc, m, window, tmp), ); - let r = r.into_unencoded(m); - - Ok(r) + Ok(acc.into_unencoded(m)) } #[cfg(target_arch = "x86_64")]