From 45cf8767e312e80c30b192a446accada08ab696c Mon Sep 17 00:00:00 2001 From: lyn1dev <78355114+lyn1dev@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:06:31 +0100 Subject: [PATCH] ed448-goldilocks: apply final conditional swap in Montgomery ladder RFC 7748 Section 5 specifies that the Montgomery ladder concludes with a conditional swap based on the final swap value: (x_2, x_3) = cswap(swap, x_2, x_3) and (z_2, z_3) = cswap(swap, z_2, z_3). In ed448-goldilocks, the ladder loop uses the lazy swap form (choice = swap ^ bit; swap = bit). For odd scalars, bit 0 is 1 so swap is 1 at loop termination, leaving x0 and x1 swapped and returning [k+1]P instead of [k]P. Applying conditional_swap with Choice::from(swap) restores x0 to [k]P for all scalars. Closes #1988 --- ed448-goldilocks/src/montgomery.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ed448-goldilocks/src/montgomery.rs b/ed448-goldilocks/src/montgomery.rs index 3fef4d895..72d49ff6f 100644 --- a/ed448-goldilocks/src/montgomery.rs +++ b/ed448-goldilocks/src/montgomery.rs @@ -105,6 +105,7 @@ impl Mul<&EdwardsScalar> for &MontgomeryPoint { swap = bit; } + ProjectiveMontgomeryPoint::conditional_swap(&mut x0, &mut x1, Choice::from(swap)); x0.to_affine() } } @@ -237,4 +238,18 @@ mod tests { let goldilocks_point = bp.scalar_mul(&scalar); assert_eq!(goldilocks_point.to_montgomery(), montgomery_res); } + + #[test] + fn test_montgomery_ladder_odd_scalars() { + use crate::GOLDILOCKS_BASE_POINT as bp; + let montgomery_bp = bp.to_montgomery(); + for k in 1u32..=16 { + let scalar = EdwardsScalar::from(k); + assert_eq!( + bp.scalar_mul(&scalar).to_montgomery(), + &montgomery_bp * &scalar, + "k = {k}" + ); + } + } }