Lines Matching refs:BigNum

0 //! BigNum implementation
4 //! of BigNum uses dynamically assigned memory to store an array of bit chunks. This
12 //! use openssl::bn::BigNum;
16 //! let a = BigNum::new()?; // a = 0
17 //! let b = BigNum::from_dec_str("1234567890123456789012345")?;
67 /// Options for the most significant bits of a randomly generated `BigNum`.
90 /// BigNum values are stored dynamically and therefore can be expensive
92 /// internally when passing BigNum values between subroutines.
129 /// Perform large number mathematics. Create a new BigNum
135 /// [`new`]: struct.BigNum.html#method.new
136 /// [`Dref<Target = BigNumRef>`]: struct.BigNum.html#deref-methods
141 /// use openssl::bn::BigNum;
144 /// let little_big = BigNum::from_u32(std::u32::MAX)?;
150 pub struct BigNum;
151 /// Reference to a [`BigNum`]
153 /// [`BigNum`]: struct.BigNum.html
158 /// Erases the memory used by this `BigNum`, resetting its value to 0.
302 /// Creates a new BigNum with the same value.
304 pub fn to_owned(&self) -> Result<BigNum, ErrorStack> {
305 unsafe { cvt_p(ffi::BN_dup(self.as_ptr())).map(|b| BigNum::from_ptr(b)) }
320 /// # use openssl::bn::BigNum;
322 /// let s = -BigNum::from_u32(8).unwrap();
323 /// let o = BigNum::from_u32(8).unwrap();
364 /// Generates a cryptographically strong pseudo-random `BigNum`, placing it in `self`.
375 /// use openssl::bn::{BigNum, MsbOption};
378 /// fn generate_random() -> Result< BigNum, ErrorStack > {
379 /// let mut big = BigNum::new()?;
430 /// use openssl::bn::BigNum;
433 /// fn generate_weak_prime() -> Result< BigNum, ErrorStack > {
434 /// let mut big = BigNum::new()?;
811 /// # use openssl::bn::BigNum;
812 /// let s = -BigNum::from_u32(4543).unwrap();
813 /// let r = BigNum::from_u32(4543).unwrap();
816 /// assert_eq!(BigNum::from_slice(&s_vec).unwrap(), r);
837 /// # use openssl::bn::BigNum;
838 /// let bn = BigNum::from_u32(0x4543).unwrap();
846 /// let bn = -BigNum::from_u32(0x4543).unwrap();
864 /// # use openssl::bn::BigNum;
865 /// let s = -BigNum::from_u32(12345).unwrap();
880 /// # use openssl::bn::BigNum;
881 /// let s = -BigNum::from_u32(0x99ff).unwrap();
919 /// Returns true if `self` was created with [`BigNum::new_secure`].
930 impl BigNum {
931 /// Creates a new `BigNum` with the value 0.
933 pub fn new() -> Result<BigNum, ErrorStack> {
937 Ok(BigNum::from_ptr(v))
941 /// Returns a new secure `BigNum`.
944 pub fn new_secure() -> Result<BigNum, ErrorStack> {
948 Ok(BigNum::from_ptr(v))
952 /// Creates a new `BigNum` with the given value.
954 pub fn from_u32(n: u32) -> Result<BigNum, ErrorStack> {
955 BigNum::new().and_then(|v| unsafe {
960 /// Creates a `BigNum` from a decimal string.
962 pub fn from_dec_str(s: &str) -> Result<BigNum, ErrorStack> {
968 Ok(BigNum::from_ptr(bn))
972 /// Creates a `BigNum` from a hexadecimal string.
974 pub fn from_hex_str(s: &str) -> Result<BigNum, ErrorStack> {
980 Ok(BigNum::from_ptr(bn))
991 pub fn get_rfc2409_prime_768() -> Result<BigNum, ErrorStack> {
994 cvt_p(BN_get_rfc2409_prime_768(ptr::null_mut())).map(BigNum)
1005 pub fn get_rfc2409_prime_1024() -> Result<BigNum, ErrorStack> {
1008 cvt_p(BN_get_rfc2409_prime_1024(ptr::null_mut())).map(BigNum)
1019 pub fn get_rfc3526_prime_1536() -> Result<BigNum, ErrorStack> {
1022 cvt_p(BN_get_rfc3526_prime_1536(ptr::null_mut())).map(BigNum)
1033 pub fn get_rfc3526_prime_2048() -> Result<BigNum, ErrorStack> {
1036 cvt_p(BN_get_rfc3526_prime_2048(ptr::null_mut())).map(BigNum)
1047 pub fn get_rfc3526_prime_3072() -> Result<BigNum, ErrorStack> {
1050 cvt_p(BN_get_rfc3526_prime_3072(ptr::null_mut())).map(BigNum)
1061 pub fn get_rfc3526_prime_4096() -> Result<BigNum, ErrorStack> {
1064 cvt_p(BN_get_rfc3526_prime_4096(ptr::null_mut())).map(BigNum)
1075 pub fn get_rfc3526_prime_6144() -> Result<BigNum, ErrorStack> {
1078 cvt_p(BN_get_rfc3526_prime_6144(ptr::null_mut())).map(BigNum)
1089 pub fn get_rfc3526_prime_8192() -> Result<BigNum, ErrorStack> {
1092 cvt_p(BN_get_rfc3526_prime_8192(ptr::null_mut())).map(BigNum)
1096 /// Creates a new `BigNum` from an unsigned, big-endian encoded number of arbitrary length.
1103 /// # use openssl::bn::BigNum;
1104 /// let bignum = BigNum::from_slice(&[0x12, 0x00, 0x34]).unwrap();
1106 /// assert_eq!(bignum, BigNum::from_u32(0x120034).unwrap());
1109 pub fn from_slice(n: &[u8]) -> Result<BigNum, ErrorStack> {
1119 .map(|p| BigNum::from_ptr(p))
1123 /// Copies data from a slice overwriting what was in the BigNum.
1126 /// [secure BigNum][`BigNum::new_secure`].
1131 /// # use openssl::bn::BigNum;
1132 /// let mut bignum = BigNum::new().unwrap();
1135 /// assert_eq!(bignum, BigNum::from_u32(0x120034).unwrap());
1157 impl fmt::Debug for BigNum {
1175 impl fmt::Display for BigNum {
1190 impl PartialEq<BigNum> for BigNumRef {
1191 fn eq(&self, oth: &BigNum) -> bool {
1198 impl PartialEq for BigNum {
1199 fn eq(&self, oth: &BigNum) -> bool {
1204 impl PartialEq<BigNumRef> for BigNum {
1210 impl Eq for BigNum {}
1218 impl PartialOrd<BigNum> for BigNumRef {
1219 fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering> {
1230 impl PartialOrd for BigNum {
1231 fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering> {
1236 impl PartialOrd<BigNumRef> for BigNum {
1242 impl Ord for BigNum {
1243 fn cmp(&self, oth: &BigNum) -> Ordering {
1250 impl<'a, 'b> $t<&'b BigNum> for &'a BigNumRef {
1251 type Output = BigNum;
1253 fn $m(self, oth: &BigNum) -> BigNum {
1258 impl<'a, 'b> $t<&'b BigNumRef> for &'a BigNum {
1259 type Output = BigNum;
1261 fn $m(self, oth: &BigNumRef) -> BigNum {
1266 impl<'a, 'b> $t<&'b BigNum> for &'a BigNum {
1267 type Output = BigNum;
1269 fn $m(self, oth: &BigNum) -> BigNum {
1277 type Output = BigNum;
1279 fn add(self, oth: &BigNumRef) -> BigNum {
1280 let mut r = BigNum::new().unwrap();
1289 type Output = BigNum;
1291 fn sub(self, oth: &BigNumRef) -> BigNum {
1292 let mut r = BigNum::new().unwrap();
1301 type Output = BigNum;
1303 fn mul(self, oth: &BigNumRef) -> BigNum {
1305 let mut r = BigNum::new().unwrap();
1314 type Output = BigNum;
1316 fn div(self, oth: &'b BigNumRef) -> BigNum {
1318 let mut r = BigNum::new().unwrap();
1327 type Output = BigNum;
1329 fn rem(self, oth: &'b BigNumRef) -> BigNum {
1331 let mut r = BigNum::new().unwrap();
1340 type Output = BigNum;
1342 fn shl(self, n: i32) -> BigNum {
1343 let mut r = BigNum::new().unwrap();
1349 impl<'a> Shl<i32> for &'a BigNum {
1350 type Output = BigNum;
1352 fn shl(self, n: i32) -> BigNum {
1358 type Output = BigNum;
1360 fn shr(self, n: i32) -> BigNum {
1361 let mut r = BigNum::new().unwrap();
1367 impl<'a> Shr<i32> for &'a BigNum {
1368 type Output = BigNum;
1370 fn shr(self, n: i32) -> BigNum {
1376 type Output = BigNum;
1378 fn neg(self) -> BigNum {
1383 impl<'a> Neg for &'a BigNum {
1384 type Output = BigNum;
1386 fn neg(self) -> BigNum {
1391 impl Neg for BigNum {
1392 type Output = BigNum;
1394 fn neg(mut self) -> BigNum {
1403 use crate::bn::{BigNum, BigNumContext};
1407 let v0 = BigNum::from_u32(10_203_004).unwrap();
1409 let v1 = BigNum::from_slice(&vec).unwrap();
1416 let a = BigNum::from_u32(909_829_283).unwrap();
1424 let a = BigNum::from_u32(909_829_283).unwrap();
1432 let range = BigNum::from_u32(909_829_283).unwrap();
1433 let mut result = BigNum::from_dec_str(&range.to_dec_str().unwrap()).unwrap();
1435 assert!(result >= BigNum::from_u32(0).unwrap() && result < range);
1441 let range = BigNum::from_u32(909_829_283).unwrap();
1442 let mut result = BigNum::from_dec_str(&range.to_dec_str().unwrap()).unwrap();
1444 assert!(result >= BigNum::from_u32(0).unwrap() && result < range);
1450 let a = BigNum::from_u32(19_029_017).unwrap();
1451 let mut p = BigNum::new().unwrap();
1463 let a = BigNum::from_u32(8).unwrap();
1464 let b = BigNum::from_u32(3).unwrap();
1466 let mut remainder = BigNum::new().unwrap();
1469 assert!(remainder.eq(&BigNum::from_u32(2).unwrap()));
1475 let a = BigNum::new().unwrap();
1478 let b = BigNum::new_secure().unwrap();
1485 let a = BigNum::new().unwrap();
1488 let mut b = BigNum::new().unwrap();
1498 let s = BigNum::from_hex_str("47A8DD7626B9908C80ACD7E0D3344D69").unwrap();
1499 let p = BigNum::from_hex_str("81EF47265B58BCE5").unwrap();
1500 let mut out = BigNum::new().unwrap();
1503 assert_eq!(out, BigNum::from_hex_str("7C6D179E19B97BDD").unwrap());
1509 let a = BigNum::from_u32(17).unwrap();
1510 let b = BigNum::from_u32(18).unwrap();