|
| 1 | +//! Constant-time helpers. |
| 2 | +// TODO(tarcieri): replace this with `crypto-bigint`? |
| 3 | + |
| 4 | +use subtle::{Choice, ConditionallySelectable, ConstantTimeEq}; |
| 5 | + |
| 6 | +/// Count the number of leading zeros in constant-time. |
| 7 | +#[inline] |
| 8 | +pub(crate) fn leading_zeros(n: &[u8]) -> u32 { |
| 9 | + n[0].leading_zeros() |
| 10 | +} |
| 11 | + |
| 12 | +/// Constant-time bitwise right shift. |
| 13 | +#[inline] |
| 14 | +pub(crate) fn rshift(n: &mut [u8], shift: u32) { |
| 15 | + debug_assert!(shift < 8); |
| 16 | + let mask = (1 << shift) - 1; |
| 17 | + let mut carry = 0; |
| 18 | + |
| 19 | + for byte in n.iter_mut() { |
| 20 | + let new_carry = (*byte & mask) << (8 - shift); |
| 21 | + *byte = (*byte >> shift) | carry; |
| 22 | + carry = new_carry; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// Constant-time test that a given byte slice contains only zeroes. |
| 27 | +#[inline] |
| 28 | +pub(crate) fn is_zero(n: &[u8]) -> Choice { |
| 29 | + let mut ret = Choice::from(1); |
| 30 | + |
| 31 | + for byte in n { |
| 32 | + ret.conditional_assign(&Choice::from(0), byte.ct_ne(&0)); |
| 33 | + } |
| 34 | + |
| 35 | + ret |
| 36 | +} |
| 37 | + |
| 38 | +/// Constant-time less than. |
| 39 | +/// |
| 40 | +/// Inputs are interpreted as big endian integers. |
| 41 | +#[inline] |
| 42 | +pub(crate) fn lt(a: &[u8], b: &[u8]) -> Choice { |
| 43 | + debug_assert_eq!(a.len(), b.len()); |
| 44 | + |
| 45 | + let mut borrow = 0; |
| 46 | + |
| 47 | + // Perform subtraction with borrow a byte-at-a-time, interpreting a |
| 48 | + // no-borrow condition as the less-than case |
| 49 | + for (&a, &b) in a.iter().zip(b.iter()).rev() { |
| 50 | + let c = (b as u16).wrapping_add(borrow >> (u8::BITS - 1)); |
| 51 | + borrow = (a as u16).wrapping_sub(c) >> u8::BITS as u8; |
| 52 | + } |
| 53 | + |
| 54 | + !borrow.ct_eq(&0) |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg(test)] |
| 58 | +mod tests { |
| 59 | + const A: [u8; 4] = [0, 0, 0, 0]; |
| 60 | + const B: [u8; 4] = [0, 0, 0, 1]; |
| 61 | + const C: [u8; 4] = [0xFF, 0, 0, 0]; |
| 62 | + const D: [u8; 4] = [0xFF, 0, 0, 1]; |
| 63 | + const E: [u8; 4] = [0xFF, 0xFF, 0xFF, 0xFE]; |
| 64 | + const F: [u8; 4] = [0xFF, 0xFF, 0xFF, 0xFF]; |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn ct_is_zero() { |
| 68 | + use super::is_zero; |
| 69 | + assert_eq!(is_zero(&A).unwrap_u8(), 1); |
| 70 | + assert_eq!(is_zero(&B).unwrap_u8(), 0); |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn ct_lt() { |
| 75 | + use super::lt; |
| 76 | + |
| 77 | + assert_eq!(lt(&A, &A).unwrap_u8(), 0); |
| 78 | + assert_eq!(lt(&B, &B).unwrap_u8(), 0); |
| 79 | + assert_eq!(lt(&C, &C).unwrap_u8(), 0); |
| 80 | + assert_eq!(lt(&D, &D).unwrap_u8(), 0); |
| 81 | + assert_eq!(lt(&E, &E).unwrap_u8(), 0); |
| 82 | + assert_eq!(lt(&F, &F).unwrap_u8(), 0); |
| 83 | + |
| 84 | + assert_eq!(lt(&A, &B).unwrap_u8(), 1); |
| 85 | + assert_eq!(lt(&A, &C).unwrap_u8(), 1); |
| 86 | + assert_eq!(lt(&B, &A).unwrap_u8(), 0); |
| 87 | + assert_eq!(lt(&C, &A).unwrap_u8(), 0); |
| 88 | + |
| 89 | + assert_eq!(lt(&B, &C).unwrap_u8(), 1); |
| 90 | + assert_eq!(lt(&B, &D).unwrap_u8(), 1); |
| 91 | + assert_eq!(lt(&C, &B).unwrap_u8(), 0); |
| 92 | + assert_eq!(lt(&D, &B).unwrap_u8(), 0); |
| 93 | + |
| 94 | + assert_eq!(lt(&C, &D).unwrap_u8(), 1); |
| 95 | + assert_eq!(lt(&C, &E).unwrap_u8(), 1); |
| 96 | + assert_eq!(lt(&D, &C).unwrap_u8(), 0); |
| 97 | + assert_eq!(lt(&E, &C).unwrap_u8(), 0); |
| 98 | + |
| 99 | + assert_eq!(lt(&E, &F).unwrap_u8(), 1); |
| 100 | + assert_eq!(lt(&F, &E).unwrap_u8(), 0); |
| 101 | + } |
| 102 | +} |
0 commit comments