Skip to content

Commit d8e44b0

Browse files
authored
k256: simplify internal helper functions in the scalar arithmetic (#917)
1 parent aac9320 commit d8e44b0

File tree

2 files changed

+30
-50
lines changed

2 files changed

+30
-50
lines changed

k256/src/arithmetic/scalar/wide32.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
429421
fn 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.
438429
fn 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
}

k256/src/arithmetic/scalar/wide64.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,18 @@ impl WideScalar {
221221
}
222222
}
223223

224-
/// Constant-time comparison.
225-
#[inline(always)]
226-
fn ct_less(a: u64, b: u64) -> u64 {
227-
// Do not convert to Choice since it is only used internally,
228-
// and we don't want loss of performance.
229-
(a < b) as u64
230-
}
231-
232224
/// Add a to the number defined by (c0,c1,c2). c2 must never overflow.
233225
fn sumadd(a: u64, c0: u64, c1: u64, c2: u64) -> (u64, u64, u64) {
234-
let new_c0 = c0.wrapping_add(a); // overflow is handled on the next line
235-
let over = ct_less(new_c0, a);
236-
let new_c1 = c1.wrapping_add(over); // overflow is handled on the next line
237-
let new_c2 = c2 + ct_less(new_c1, over); // never overflows by contract
226+
let (new_c0, carry0) = c0.overflowing_add(a);
227+
let (new_c1, carry1) = c1.overflowing_add(carry0 as u64);
228+
let new_c2 = c2 + (carry1 as u64);
238229
(new_c0, new_c1, new_c2)
239230
}
240231

241-
/// Add a to the number defined by (c0,c1). c1 must never overflow, c2 must be zero.
232+
/// Add a to the number defined by (c0,c1). c1 must never overflow.
242233
fn sumadd_fast(a: u64, c0: u64, c1: u64) -> (u64, u64) {
243-
let new_c0 = c0.wrapping_add(a); // overflow is handled on the next line
244-
let new_c1 = c1 + ct_less(new_c0, a); // never overflows by contract (verified the next line)
245-
debug_assert!((new_c1 != 0) | (new_c0 >= a));
234+
let (new_c0, carry0) = c0.overflowing_add(a);
235+
let new_c1 = c1 + (carry0 as u64);
246236
(new_c0, new_c1)
247237
}
248238

@@ -252,11 +242,11 @@ fn muladd(a: u64, b: u64, c0: u64, c1: u64, c2: u64) -> (u64, u64, u64) {
252242
let th = (t >> 64) as u64; // at most 0xFFFFFFFFFFFFFFFE
253243
let tl = t as u64;
254244

255-
let new_c0 = c0.wrapping_add(tl); // overflow is handled on the next line
256-
let new_th = th + u64::from(new_c0 < tl); // at most 0xFFFFFFFFFFFFFFFF
257-
let new_c1 = c1.wrapping_add(new_th); // overflow is handled on the next line
258-
let new_c2 = c2 + ct_less(new_c1, new_th); // never overflows by contract (verified in the next line)
259-
debug_assert!((new_c1 >= new_th) || (new_c2 != 0));
245+
let (new_c0, carry0) = c0.overflowing_add(tl);
246+
let new_th = th.wrapping_add(carry0 as u64); // at most 0xFFFFFFFFFFFFFFFF
247+
let (new_c1, carry1) = c1.overflowing_add(new_th);
248+
let new_c2 = c2 + (carry1 as u64);
249+
260250
(new_c0, new_c1, new_c2)
261251
}
262252

@@ -266,9 +256,9 @@ fn muladd_fast(a: u64, b: u64, c0: u64, c1: u64) -> (u64, u64) {
266256
let th = (t >> 64) as u64; // at most 0xFFFFFFFFFFFFFFFE
267257
let tl = t as u64;
268258

269-
let new_c0 = c0.wrapping_add(tl); // overflow is handled on the next line
270-
let new_th = th + ct_less(new_c0, tl); // at most 0xFFFFFFFFFFFFFFFF
271-
let new_c1 = c1 + new_th; // never overflows by contract (verified in the next line)
272-
debug_assert!(new_c1 >= new_th);
259+
let (new_c0, carry0) = c0.overflowing_add(tl);
260+
let new_th = th.wrapping_add(carry0 as u64); // at most 0xFFFFFFFFFFFFFFFF
261+
let new_c1 = c1 + new_th;
262+
273263
(new_c0, new_c1)
274264
}

0 commit comments

Comments
 (0)