Unverified Commit a072e8ac authored by Steven Fackler's avatar Steven Fackler Committed by GitHub
Browse files

Merge pull request #1981 from alex/bn-is-odd-even

Added is_even and is_odd on BN
parents c4e787ad 24638986
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ extern "C" {
    pub fn BN_set_negative(bn: *mut BIGNUM, n: c_int);
    #[cfg(any(ossl110, libressl350))]
    pub fn BN_is_negative(b: *const BIGNUM) -> c_int;
    #[cfg(any(ossl110, libressl350))]
    pub fn BN_is_odd(b: *const BIGNUM) -> c_int;

    pub fn BN_div(
        dv: *mut BIGNUM,
+27 −0
Original line number Diff line number Diff line
@@ -335,6 +335,20 @@ impl BigNumRef {
        unsafe { BN_is_negative(self.as_ptr()) == 1 }
    }

    /// Returns `true` is `self` is even.
    #[corresponds(BN_is_even)]
    #[cfg(any(ossl110, boringssl, libressl350))]
    pub fn is_even(&self) -> bool {
        !self.is_odd()
    }

    /// Returns `true` is `self` is odd.
    #[corresponds(BN_is_odd)]
    #[cfg(any(ossl110, boringssl, libressl350))]
    pub fn is_odd(&self) -> bool {
        unsafe { ffi::BN_is_odd(self.as_ptr()) == 1 }
    }

    /// Returns the number of significant bits in `self`.
    #[corresponds(BN_num_bits)]
    #[allow(clippy::unnecessary_cast)]
@@ -1488,4 +1502,17 @@ mod tests {
        out.mod_sqrt(&s, &p, &mut ctx).unwrap();
        assert_eq!(out, BigNum::from_hex_str("7C6D179E19B97BDD").unwrap());
    }

    #[test]
    #[cfg(any(ossl110, boringssl, libressl350))]
    fn test_odd_even() {
        let a = BigNum::from_u32(17).unwrap();
        let b = BigNum::from_u32(18).unwrap();

        assert!(a.is_odd());
        assert!(!b.is_odd());

        assert!(!a.is_even());
        assert!(b.is_even());
    }
}