@@ -417,28 +417,18 @@ impl WideScalar {
417417 }
418418}
419419
420- /// Constant-time comparison.
421- #[ inline( always) ]
422- fn ct_less ( a : u32 , b : u32 ) -> u32 {
423- // Do not convert to Choice since it is only used internally,
424- // and we don't want loss of performance.
425- ( a < b) as u32
426- }
427-
428420/// Add a to the number defined by (c0,c1,c2). c2 must never overflow.
429421fn sumadd ( a : u32 , c0 : u32 , c1 : u32 , c2 : u32 ) -> ( u32 , u32 , u32 ) {
430- let new_c0 = c0. wrapping_add ( a) ; // overflow is handled on the next line
431- let over: u32 = if new_c0 < a { 1 } else { 0 } ;
432- let new_c1 = c1. wrapping_add ( over) ; // overflow is handled on the next line
433- let new_c2 = c2 + ct_less ( new_c1, over) ; // never overflows by contract
422+ let ( new_c0, carry0) = c0. overflowing_add ( a) ;
423+ let ( new_c1, carry1) = c1. overflowing_add ( carry0 as u32 ) ;
424+ let new_c2 = c2 + ( carry1 as u32 ) ;
434425 ( new_c0, new_c1, new_c2)
435426}
436427
437- /// Add a to the number defined by (c0,c1). c1 must never overflow, c2 must be zero .
428+ /// Add a to the number defined by (c0,c1). c1 must never overflow.
438429fn sumadd_fast ( a : u32 , c0 : u32 , c1 : u32 ) -> ( u32 , u32 ) {
439- let new_c0 = c0. wrapping_add ( a) ; // overflow is handled on the next line
440- let new_c1 = c1 + ct_less ( new_c0, a) ; // never overflows by contract (verified the next line)
441- debug_assert ! ( ( new_c1 != 0 ) | ( new_c0 >= a) ) ;
430+ let ( new_c0, carry0) = c0. overflowing_add ( a) ;
431+ let new_c1 = c1 + ( carry0 as u32 ) ;
442432 ( new_c0, new_c1)
443433}
444434
@@ -448,11 +438,11 @@ fn muladd(a: u32, b: u32, c0: u32, c1: u32, c2: u32) -> (u32, u32, u32) {
448438 let th = ( t >> 32 ) as u32 ; // at most 0xFFFFFFFFFFFFFFFE
449439 let tl = t as u32 ;
450440
451- let new_c0 = c0. wrapping_add ( tl) ; // overflow is handled on the next line
452- let new_th = th + ct_less ( new_c0 , tl ) ; // at most 0xFFFFFFFFFFFFFFFF
453- let new_c1 = c1. wrapping_add ( new_th) ; // overflow is handled on the next line
454- let new_c2 = c2 + ct_less ( new_c1 , new_th ) ; // never overflows by contract (verified in the next line)
455- debug_assert ! ( ( new_c1 >= new_th ) || ( new_c2 != 0 ) ) ;
441+ let ( new_c0, carry0 ) = c0. overflowing_add ( tl) ;
442+ let new_th = th. wrapping_add ( carry0 as u32 ) ; // at most 0xFFFFFFFFFFFFFFFF
443+ let ( new_c1, carry1 ) = c1. overflowing_add ( new_th) ;
444+ let new_c2 = c2 + ( carry1 as u32 ) ;
445+
456446 ( new_c0, new_c1, new_c2)
457447}
458448
@@ -462,9 +452,9 @@ fn muladd_fast(a: u32, b: u32, c0: u32, c1: u32) -> (u32, u32) {
462452 let th = ( t >> 32 ) as u32 ; // at most 0xFFFFFFFFFFFFFFFE
463453 let tl = t as u32 ;
464454
465- let new_c0 = c0. wrapping_add ( tl) ; // overflow is handled on the next line
466- let new_th = th + ct_less ( new_c0 , tl ) ; // at most 0xFFFFFFFFFFFFFFFF
467- let new_c1 = c1 + new_th; // never overflows by contract (verified in the next line)
468- debug_assert ! ( new_c1 >= new_th ) ;
455+ let ( new_c0, carry0 ) = c0. overflowing_add ( tl) ;
456+ let new_th = th. wrapping_add ( carry0 as u32 ) ; // at most 0xFFFFFFFFFFFFFFFF
457+ let new_c1 = c1 + new_th;
458+
469459 ( new_c0, new_c1)
470460}
0 commit comments