Merge pull request #606 from cjcole/master

Fix order of arguments to BN_rand_range and BN_pseudo_rand_range
This commit is contained in:
Steven Fackler 2017-03-26 05:01:29 +01:00 committed by GitHub
commit ba2460d38d
1 changed files with 20 additions and 2 deletions

View File

@ -106,12 +106,12 @@ impl BigNumRef {
/// Places a cryptographically-secure pseudo-random number nonnegative
/// number less than `self` in `rnd`.
pub fn rand_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack> {
unsafe { cvt(ffi::BN_rand_range(self.as_ptr(), rnd.as_ptr())).map(|_| ()) }
unsafe { cvt(ffi::BN_rand_range(rnd.as_ptr(), self.as_ptr())).map(|_| ()) }
}
/// The cryptographically weak counterpart to `rand_in_range`.
pub fn pseudo_rand_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack> {
unsafe { cvt(ffi::BN_pseudo_rand_range(self.as_ptr(), rnd.as_ptr())).map(|_| ()) }
unsafe { cvt(ffi::BN_pseudo_rand_range(rnd.as_ptr(), self.as_ptr())).map(|_| ()) }
}
/// Sets bit `n`. Equivalent to `self |= (1 << n)`.
@ -933,6 +933,24 @@ mod tests {
assert!(a == a.shl(1).shr(1));
}
#[test]
fn test_rand_range() {
let range = BigNum::from_u32(909829283).unwrap();
let mut result = BigNum::from_dec_str(
&range.to_dec_str().unwrap()).unwrap();
range.rand_range(&mut result).unwrap();
assert!(result >= BigNum::from_u32(0).unwrap() && result < range);
}
#[test]
fn test_pseudo_rand_range() {
let range = BigNum::from_u32(909829283).unwrap();
let mut result = BigNum::from_dec_str(
&range.to_dec_str().unwrap()).unwrap();
range.pseudo_rand_range(&mut result).unwrap();
assert!(result >= BigNum::from_u32(0).unwrap() && result < range);
}
#[test]
fn test_prime_numbers() {
let a = BigNum::from_u32(19029017).unwrap();