From df2faf6055bc57c35c6c13f6cd775f4164ac175a Mon Sep 17 00:00:00 2001 From: Taylor Hornby Date: Tue, 19 Feb 2019 12:02:07 -0700 Subject: [PATCH] Fix test_uint32_shr failing on debug builds. #76 --- src/circuit/uint32.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/circuit/uint32.rs b/src/circuit/uint32.rs index fb0bfa92..0a09be31 100644 --- a/src/circuit/uint32.rs +++ b/src/circuit/uint32.rs @@ -175,7 +175,7 @@ impl UInt32 { } pub fn shr(&self, by: usize) -> Self { - let by = by % 32; + assert!(by < 32); let fill = Boolean::constant(false); @@ -656,7 +656,7 @@ mod test { let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); for _ in 0..50 { - for i in 0..60 { + for i in 0..32 { let num = rng.gen(); let a = UInt32::constant(num).shr(i); let b = UInt32::constant(num >> i); @@ -671,6 +671,19 @@ mod test { } } + #[test] + fn test_uint32_shr_overflow() { + let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); + + for _ in 0..50 { + for i in 32..60 { + let num = rng.gen(); + let result = std::panic::catch_unwind(|| UInt32::constant(num).shr(i)); + assert!(result.is_err()); + } + } + } + #[test] fn test_uint32_sha256_maj() { let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0653]);