Struct openssl::bn::BigNum [] [src]

pub struct BigNum(_);

An owned, signed, arbitrary-precision integer.

BigNum provides wrappers around OpenSSL's checked arithmetic functions. Additionally, it implements the standard operators (std::ops), which perform unchecked arithmetic, unwrapping the returned Result of the checked operations.

Methods

impl BigNum
[src]

fn new() -> Result<BigNumErrorStack>

Creates a new BigNum with the value 0.

fn new_from(n: c_ulong) -> Result<BigNumErrorStack>

Creates a new BigNum with the given value.

fn from_dec_str(s: &str) -> Result<BigNumErrorStack>

Creates a BigNum from a decimal string.

fn from_hex_str(s: &str) -> Result<BigNumErrorStack>

Creates a BigNum from a hexadecimal string.

unsafe fn from_ptr(handle: *mut BIGNUM) -> BigNum

fn new_from_slice(n: &[u8]) -> Result<BigNumErrorStack>

Creates a new BigNum from an unsigned, big-endian encoded number of arbitrary length.

let bignum = BigNum::new_from_slice(&[0x12, 0x00, 0x34]).unwrap();

assert_eq!(bignum, BigNum::new_from(0x120034).unwrap());

fn checked_generate_prime(bits: i32, safe: bool, add: Option<&BigNum>, rem: Option<&BigNum>) -> Result<BigNumErrorStack>

Generates a prime number.

Parameters

  • bits: The length of the prime in bits (lower bound).
  • safe: If true, returns a "safe" prime p so that (p-1)/2 is also prime.
  • add/rem: If add is set to Some(add), p % add == rem will hold, where p is the generated prime and rem is 1 if not specified (None).

fn checked_new_random(bits: i32, prop: RNGProperty, odd: bool) -> Result<BigNumErrorStack>

Generates a cryptographically strong pseudo-random BigNum.

Parameters

  • bits: Length of the number in bits.
  • prop: The desired properties of the number.
  • odd: If true, the generated number will be odd.

fn checked_new_pseudo_random(bits: i32, prop: RNGProperty, odd: bool) -> Result<BigNumErrorStack>

The cryptographically weak counterpart to checked_new_random.

Methods from Deref<Target=BigNumRef<'static>>

fn checked_sqr(&self) -> Result<BigNumErrorStack>

Returns the square of self.

let ref n = BigNum::new_from(10).unwrap();
let squared = BigNum::new_from(100).unwrap();

assert_eq!(n.checked_sqr().unwrap(), squared);
assert_eq!(n * n, squared);

fn checked_nnmod(&self, n: &BigNumRef) -> Result<BigNumErrorStack>

Returns the unsigned remainder of the division self / n.

fn checked_mod_add(&self, a: &BigNumRef, n: &BigNumRef) -> Result<BigNumErrorStack>

Equivalent to (self + a) mod n.

let ref s = BigNum::new_from(10).unwrap();
let ref a = BigNum::new_from(20).unwrap();
let ref n = BigNum::new_from(29).unwrap();
let result = BigNum::new_from(1).unwrap();

assert_eq!(s.checked_mod_add(a, n).unwrap(), result);

fn checked_mod_sub(&self, a: &BigNumRef, n: &BigNumRef) -> Result<BigNumErrorStack>

Equivalent to (self - a) mod n.

fn checked_mod_mul(&self, a: &BigNumRef, n: &BigNumRef) -> Result<BigNumErrorStack>

Equivalent to (self * a) mod n.

fn checked_mod_sqr(&self, n: &BigNumRef) -> Result<BigNumErrorStack>

Equivalent to self² mod n.

fn checked_exp(&self, p: &BigNumRef) -> Result<BigNumErrorStack>

Raises self to the pth power.

fn checked_mod_exp(&self, p: &BigNumRef, n: &BigNumRef) -> Result<BigNumErrorStack>

Equivalent to self.checked_exp(p) mod n.

fn checked_mod_inv(&self, n: &BigNumRef) -> Result<BigNumErrorStack>

Calculates the modular multiplicative inverse of self modulo n, that is, an integer r such that (self * r) % n == 1.

fn add_word(&mut self, w: c_ulong) -> Result<()ErrorStack>

Add an unsigned long to self. This is more efficient than adding a BigNum.

fn sub_word(&mut self, w: c_ulong) -> Result<()ErrorStack>

fn mul_word(&mut self, w: c_ulong) -> Result<()ErrorStack>

fn div_word(&mut self, w: c_ulong) -> Result<c_ulong, ErrorStack>

fn mod_word(&self, w: c_ulong) -> Result<c_ulong, ErrorStack>

fn checked_gcd(&self, a: &BigNumRef) -> Result<BigNumErrorStack>

Computes the greatest common denominator of self and a.

fn is_prime(&self, checks: i32) -> Result<boolErrorStack>

Checks whether self is prime.

Performs a Miller-Rabin probabilistic primality test with checks iterations.

Return Value

Returns true if self is prime with an error probability of less than 0.25 ^ checks.

fn is_prime_fast(&self, checks: i32, do_trial_division: bool) -> Result<boolErrorStack>

Checks whether self is prime with optional trial division.

If do_trial_division is true, first performs trial division by a number of small primes. Then, like is_prime, performs a Miller-Rabin probabilistic primality test with checks iterations.

Return Value

Returns true if self is prime with an error probability of less than 0.25 ^ checks.

fn checked_rand_in_range(&self) -> Result<BigNumErrorStack>

Generates a cryptographically strong pseudo-random BigNum r in the range 0 <= r < self.

fn checked_pseudo_rand_in_range(&self) -> Result<BigNumErrorStack>

The cryptographically weak counterpart to checked_rand_in_range.

fn set_bit(&mut self, n: i32) -> Result<()ErrorStack>

Sets bit n. Equivalent to self |= (1 << n).

When setting a bit outside of self, it is expanded.

fn clear_bit(&mut self, n: i32) -> Result<()ErrorStack>

Clears bit n, setting it to 0. Equivalent to self &= ~(1 << n).

When clearing a bit outside of self, an error is returned.

fn is_bit_set(&self, n: i32) -> bool

Returns true if the nth bit of self is set to 1, false otherwise.

fn mask_bits(&mut self, n: i32) -> Result<()ErrorStack>

Truncates self to the lowest n bits.

An error occurs if self is already shorter than n bits.

fn checked_shl1(&self) -> Result<BigNumErrorStack>

Returns self, shifted left by 1 bit. self may be negative.

let ref s = BigNum::new_from(0b0100).unwrap();
let result = BigNum::new_from(0b1000).unwrap();

assert_eq!(s.checked_shl1().unwrap(), result);
let ref s = -BigNum::new_from(8).unwrap();
let result = -BigNum::new_from(16).unwrap();

// (-8) << 1 == -16
assert_eq!(s.checked_shl1().unwrap(), result);

fn checked_shr1(&self) -> Result<BigNumErrorStack>

Returns self, shifted right by 1 bit. self may be negative.

fn checked_add(&self, a: &BigNumRef) -> Result<BigNumErrorStack>

fn checked_sub(&self, a: &BigNumRef) -> Result<BigNumErrorStack>

fn checked_mul(&self, a: &BigNumRef) -> Result<BigNumErrorStack>

fn checked_div(&self, a: &BigNumRef) -> Result<BigNumErrorStack>

fn checked_mod(&self, a: &BigNumRef) -> Result<BigNumErrorStack>

fn checked_shl(&self, a: &i32) -> Result<BigNumErrorStack>

fn checked_shr(&self, a: &i32) -> Result<BigNumErrorStack>

fn to_owned(&self) -> Result<BigNumErrorStack>

fn negate(&mut self)

Inverts the sign of self.

let mut s = BigNum::new_from(8).unwrap();

s.negate();
assert_eq!(s, -BigNum::new_from(8).unwrap());
s.negate();
assert_eq!(s, BigNum::new_from(8).unwrap());

fn abs_cmp(&self, oth: &BigNumRef) -> Ordering

Compare the absolute values of self and oth.

let s = -BigNum::new_from(8).unwrap();
let o = BigNum::new_from(8).unwrap();

assert_eq!(s.abs_cmp(&o), Ordering::Equal);

fn is_negative(&self) -> bool

fn num_bits(&self) -> i32

Returns the number of significant bits in self.

fn num_bytes(&self) -> i32

Returns the size of self in bytes.

fn as_ptr(&self) -> *mut BIGNUM

fn to_vec(&self) -> Vec<u8>

Returns a big-endian byte vector representation of the absolute value of self.

self can be recreated by using new_from_slice.

let s = -BigNum::new_from(4543).unwrap();
let r = BigNum::new_from(4543).unwrap();

let s_vec = s.to_vec();
assert_eq!(BigNum::new_from_slice(&s_vec).unwrap(), r);

fn to_dec_str(&self) -> String

Returns a decimal string representation of self.

let s = -BigNum::new_from(12345).unwrap();

assert_eq!(s.to_dec_str(), "-12345");

fn to_hex_str(&self) -> String

Returns a hexadecimal string representation of self.

let s = -BigNum::new_from(0x99ff).unwrap();

assert_eq!(s.to_hex_str(), "-99FF");

Trait Implementations

impl Drop for BigNum
[src]

fn drop(&mut self)

A method called when the value goes out of scope. Read more

impl Deref for BigNum
[src]

type Target = BigNumRef<'static>

The resulting type after dereferencing

fn deref(&self) -> &BigNumRef<'static>

The method called to dereference a value

impl DerefMut for BigNum
[src]

fn deref_mut(&mut self) -> &mut BigNumRef<'static>

The method called to mutably dereference a value

impl AsRef<BigNumRef<'static>> for BigNum
[src]

fn as_ref(&self) -> &BigNumRef<'static>

Performs the conversion.

impl Debug for BigNum
[src]

fn fmt(&self, f: &mut Formatter) -> Result

Formats the value using the given formatter.

impl Display for BigNum
[src]

fn fmt(&self, f: &mut Formatter) -> Result

Formats the value using the given formatter.

impl PartialEq for BigNum
[src]

fn eq(&self, oth: &BigNum) -> bool

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, other: &Rhs) -> bool
1.0.0

This method tests for !=.

impl<'a> PartialEq<BigNumRef<'a>> for BigNum
[src]

fn eq(&self, oth: &BigNumRef) -> bool

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, other: &Rhs) -> bool
1.0.0

This method tests for !=.

impl Eq for BigNum
[src]

impl PartialOrd for BigNum
[src]

fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more

fn lt(&self, other: &Rhs) -> bool
1.0.0

This method tests less than (for self and other) and is used by the < operator. Read more

fn le(&self, other: &Rhs) -> bool
1.0.0

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

fn gt(&self, other: &Rhs) -> bool
1.0.0

This method tests greater than (for self and other) and is used by the > operator. Read more

fn ge(&self, other: &Rhs) -> bool
1.0.0

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<'a> PartialOrd<BigNumRef<'a>> for BigNum
[src]

fn partial_cmp(&self, oth: &BigNumRef) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more

fn lt(&self, other: &Rhs) -> bool
1.0.0

This method tests less than (for self and other) and is used by the < operator. Read more

fn le(&self, other: &Rhs) -> bool
1.0.0

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

fn gt(&self, other: &Rhs) -> bool
1.0.0

This method tests greater than (for self and other) and is used by the > operator. Read more

fn ge(&self, other: &Rhs) -> bool
1.0.0

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl Ord for BigNum
[src]

fn cmp(&self, oth: &BigNum) -> Ordering

This method returns an Ordering between self and other. Read more

impl<'a, 'b> Sub<&'b BigNum> for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the - operator

fn sub(self, oth: &BigNum) -> BigNum

The method for the - operator

impl<'a, 'b> Sub<&'b BigNumRef<'b>> for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the - operator

fn sub(self, oth: &BigNumRef) -> BigNum

The method for the - operator

impl<'a, 'b> Mul<&'b BigNum> for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the * operator

fn mul(self, oth: &BigNum) -> BigNum

The method for the * operator

impl<'a, 'b> Mul<&'b BigNumRef<'b>> for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the * operator

fn mul(self, oth: &BigNumRef) -> BigNum

The method for the * operator

impl<'a, 'b> Div<&'b BigNum> for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the / operator

fn div(self, oth: &'b BigNum) -> BigNum

The method for the / operator

impl<'a, 'b> Div<&'b BigNumRef<'b>> for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the / operator

fn div(self, oth: &'b BigNumRef<'b>) -> BigNum

The method for the / operator

impl<'a, 'b> Rem<&'b BigNumRef<'b>> for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the % operator

fn rem(self, oth: &'b BigNumRef<'b>) -> BigNum

The method for the % operator

impl<'a, 'b> Rem<&'b BigNum> for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the % operator

fn rem(self, oth: &'b BigNum) -> BigNum

The method for the % operator

impl<'a> Shl<i32> for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the << operator

fn shl(self, n: i32) -> BigNum

The method for the << operator

impl<'a> Shr<i32> for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the >> operator

fn shr(self, n: i32) -> BigNum

The method for the >> operator

impl<'a> Neg for &'a BigNum
[src]

type Output = BigNum

The resulting type after applying the - operator

fn neg(self) -> BigNum

The method for the unary - operator

impl Neg for BigNum
[src]

type Output = BigNum

The resulting type after applying the - operator

fn neg(self) -> BigNum

The method for the unary - operator