Skip to content

Commit d16ebf4

Browse files
laruizloclaude
andcommitted
docs: tighten comments in ct.rs and its tests
- compress the unsigned macro preamble to the ordering-comparison rationale; the signed-trick contrast no longer applies after the is_lt fix - align the unsigned is_bit_set doc with the signed one - shorten the u64 select test comment to the mask-width fact it checks Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent f35329b commit d16ebf4

2 files changed

Lines changed: 7 additions & 14 deletions

File tree

crypto/utils/src/ct.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,10 @@ signed_condition_impl!(i64, i32);
191191
// (there's probably no noticeable performance difference u8 and u64 bit ops on a 64-bit machine,
192192
// but there would be on a 8, 16, or 32-bit machine.)
193193
//
194-
// The unsigned widths share one macro-generated impl so that code which is generic over its
195-
// word size (e.g. a u64-or-u32 limb type) can be written once against identical method names.
196-
// The i64 constructions above do NOT carry over: they rely on signed representation tricks
197-
// (arithmetic shift for `is_negative`, the sign of `x - y` for `is_lt`) whose overflow
198-
// reasoning is invalid for full-range unsigned values. The unsigned constructions below use
199-
// the standard two's-complement mask identities instead, and deliberately omit ordering
200-
// comparisons: multi-word callers derive `lt` from their subtraction borrow chain and convert
201-
// it with `from_msb`.
194+
// The unsigned widths share one macro-generated impl so that width-generic limb code can be
195+
// written once against identical method names. Ordering comparisons are deliberately omitted:
196+
// multi-word callers derive `lt` from their subtraction borrow chain and convert it with
197+
// `from_msb`.
202198
macro_rules! unsigned_condition_impl {
203199
($($t:ty),+) => {
204200
$(
@@ -238,9 +234,7 @@ macro_rules! unsigned_condition_impl {
238234
Self((0 as $t).wrapping_sub(value >> (<$t>::BITS - 1)))
239235
}
240236
/// TRUE iff bit `bit` of `value` is set. The bit index must be public data
241-
/// (the shift amount is timing-visible on some targets). Same shape as the
242-
/// signed `is_bit_set`; the index type follows the `u32` shift-count
243-
/// convention of `core`.
237+
/// (the shift amount is timing-visible on some targets).
244238
pub const fn is_bit_set(value: $t, bit: u32) -> Self {
245239
Self::from_lsb(value >> bit)
246240
}

crypto/utils/tests/ct_tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,8 @@ mod u64_tests {
235235
let val1: u64 = 0xDEADBEEFCAFEBABE;
236236
let val2: u64 = 0x0000000000000000;
237237

238-
// This test is CRITICAL.
239-
// If TRUE was defined as '1' (like i64), this would fail because 'select' relies on bitwise mask.
240-
// It requires TRUE to be u64::MAX (all 1s) to preserve the full bits of val1.
238+
// select is a bitwise mask, so TRUE must be u64::MAX (all 1s), not 1, to
239+
// preserve every bit of val1.
241240
assert_eq!(t.select(val1, val2), val1);
242241
assert_eq!(f.select(val1, val2), val2);
243242

0 commit comments

Comments
 (0)