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

Merge pull request #1973 from anpage/bn_mod_sqrt

bn: Add mod_sqrt
parents ee44daf4 fa460ea6
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -73,6 +73,13 @@ extern "C" {
        m: *const BIGNUM,
        ctx: *mut BN_CTX,
    ) -> c_int;
    #[cfg(ossl110)]
    pub fn BN_mod_sqrt(
        ret: *mut BIGNUM,
        a: *const BIGNUM,
        p: *const BIGNUM,
        ctx: *mut BN_CTX,
    ) -> *mut BIGNUM;

    pub fn BN_mod_word(r: *const BIGNUM, w: BN_ULONG) -> BN_ULONG;
    pub fn BN_div_word(r: *mut BIGNUM, w: BN_ULONG) -> BN_ULONG;
+33 −0
Original line number Diff line number Diff line
@@ -639,6 +639,26 @@ impl BigNumRef {
        }
    }

    /// Places into `self` the modular square root of `a` such that `self^2 = a (mod p)`
    #[corresponds(BN_mod_sqrt)]
    #[cfg(ossl110)]
    pub fn mod_sqrt(
        &mut self,
        a: &BigNumRef,
        p: &BigNumRef,
        ctx: &mut BigNumContextRef,
    ) -> Result<(), ErrorStack> {
        unsafe {
            cvt_p(ffi::BN_mod_sqrt(
                self.as_ptr(),
                a.as_ptr(),
                p.as_ptr(),
                ctx.as_ptr(),
            ))
            .map(|_| ())
        }
    }

    /// Places the result of `a^p` in `self`.
    #[corresponds(BN_exp)]
    pub fn exp(
@@ -1455,4 +1475,17 @@ mod tests {
        b.set_const_time();
        assert!(b.is_const_time())
    }

    #[cfg(ossl110)]
    #[test]
    fn test_mod_sqrt() {
        let mut ctx = BigNumContext::new().unwrap();

        let s = BigNum::from_hex_str("47A8DD7626B9908C80ACD7E0D3344D69").unwrap();
        let p = BigNum::from_hex_str("81EF47265B58BCE5").unwrap();
        let mut out = BigNum::new().unwrap();

        out.mod_sqrt(&s, &p, &mut ctx).unwrap();
        assert_eq!(out, BigNum::from_hex_str("7C6D179E19B97BDD").unwrap());
    }
}