Fix order of arguments to BN_rand_range and BN_pseudo_rand_range
This commit is contained in:
parent
3aec0a38bf
commit
d239e04c70
|
|
@ -106,12 +106,12 @@ impl BigNumRef {
|
||||||
/// Places a cryptographically-secure pseudo-random number nonnegative
|
/// Places a cryptographically-secure pseudo-random number nonnegative
|
||||||
/// number less than `self` in `rnd`.
|
/// number less than `self` in `rnd`.
|
||||||
pub fn rand_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack> {
|
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`.
|
/// The cryptographically weak counterpart to `rand_in_range`.
|
||||||
pub fn pseudo_rand_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack> {
|
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)`.
|
/// Sets bit `n`. Equivalent to `self |= (1 << n)`.
|
||||||
|
|
@ -933,6 +933,24 @@ mod tests {
|
||||||
assert!(a == a.shl(1).shr(1));
|
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]
|
#[test]
|
||||||
fn test_prime_numbers() {
|
fn test_prime_numbers() {
|
||||||
let a = BigNum::from_u32(19029017).unwrap();
|
let a = BigNum::from_u32(19029017).unwrap();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue