Skip to content

Commit dcc7784

Browse files
committed
limbs: Add more testing of big endian limb parsing.
1 parent 63dc1e5 commit dcc7784

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/limb.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ prefixed_extern! {
371371
mod tests {
372372
use super::*;
373373
use alloc::vec::Vec;
374+
use cfg_if::cfg_if;
374375

375376
const MAX: LeakyLimb = LeakyLimb::MAX;
376377

@@ -519,7 +520,53 @@ mod tests {
519520
assert_eq!(&[0xbeeff00d, 0, 0, 0], &result);
520521
}
521522

522-
// XXX: This is a weak set of tests. TODO: expand it.
523+
cfg_if! {
524+
if #[cfg(target_pointer_width = "64")] {
525+
static TEST_CASES: &[(&[u8], &[Limb])] = &[
526+
(&[1], &[1, 0]),
527+
(&[1, 2], &[0x102, 0]),
528+
(&[1, 2, 3], &[0x10203, 0]),
529+
(&[1, 2, 3, 4], &[0x102_0304, 0]),
530+
(&[1, 2, 3, 4, 5], &[0x1_0203_0405, 0]),
531+
(&[1, 2, 3, 4, 5, 6], &[0x102_0304_0506, 0]),
532+
(&[1, 2, 3, 4, 5, 6, 7], &[0x1_0203_0405_0607, 0]),
533+
(&[1, 2, 3, 4, 5, 6, 7, 8], &[0x102_0304_0506_0708, 0]),
534+
(&[1, 2, 3, 4, 5, 6, 7, 8, 9], &[0x0203_0405_0607_0809, 0x1]),
535+
(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa], &[0x0304_0506_0708_090a, 0x102]),
536+
(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb], &[0x0405_0607_0809_0a0b, 0x1_0203]),
537+
];
538+
for (be_bytes, limbs) in TEST_CASES {
539+
let mut buf = [0; 2];
540+
parse_big_endian_and_pad_consttime(untrusted::Input::from(be_bytes), &mut buf)
541+
.unwrap();
542+
assert_eq!(limbs, &buf, "({be_bytes:x?}, {limbs:x?}");
543+
}
544+
} else if #[cfg(target_pointer_width = "32")] {
545+
static TEST_CASES: &[(&[u8], &[Limb])] = &[
546+
(&[1], &[1, 0, 0]),
547+
(&[1, 2], &[0x102, 0, 0]),
548+
(&[1, 2, 3], &[0x10203, 0, 0]),
549+
(&[1, 2, 3, 4], &[0x102_0304, 0, 0]),
550+
(&[1, 2, 3, 4, 5], &[0x0203_0405, 0x1, 0]),
551+
(&[1, 2, 3, 4, 5, 6], &[0x0304_0506, 0x102, 0]),
552+
(&[1, 2, 3, 4, 5, 6, 7], &[0x0405_0607, 0x1_0203, 0]),
553+
(&[1, 2, 3, 4, 5, 6, 7, 8], &[0x0506_0708, 0x102_0304, 0]),
554+
(&[1, 2, 3, 4, 5, 6, 7, 8, 9], &[0x0607_0809, 0x0203_0405, 0x1]),
555+
(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa], &[0x0708_090a, 0x0304_0506, 0x102]),
556+
(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb], &[0x0809_0a0b, 0x0405_0607, 0x1_0203]),
557+
];
558+
for (be_bytes, limbs) in TEST_CASES {
559+
let mut buf = [0; 3];
560+
parse_big_endian_and_pad_consttime(untrusted::Input::from(be_bytes), &mut buf)
561+
.unwrap();
562+
assert_eq!(limbs, &buf, "({be_bytes:x?}, {limbs:x?}");
563+
}
564+
} else {
565+
panic!("Unsupported target_pointer_width");
566+
}
567+
568+
// XXX: This is a weak set of tests. TODO: expand it.
569+
}
523570
}
524571

525572
#[test]

0 commit comments

Comments
 (0)