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

pub struct BigNum(_);

A 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]

Creates a new BigNum with the value 0.

Creates a new BigNum with the given value.

Creates a BigNum from a decimal string.

Creates a BigNum from a hexadecimal string.

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());

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);

Returns the unsigned remainder of the division self / n.

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);

Equivalent to (self - a) mod n.

Equivalent to (self * a) mod n.

Equivalent to self² mod n.

Raises self to the pth power.

Equivalent to self.checked_exp(p) mod n.

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

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

Computes the greatest common denominator of self and a.

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).

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.

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.

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.

The cryptographically weak counterpart to checked_new_random.

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

The cryptographically weak counterpart to checked_rand_in_range.

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

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

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

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

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

Truncates self to the lowest n bits.

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

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);

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

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());

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);

Returns the number of significant bits in self.

Returns the size of self in bytes.

Important traits for 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);

Returns a decimal string representation of self.

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

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

Returns a hexadecimal string representation of self.

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

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

Trait Implementations

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

The resulting type after applying the + operator.

Performs the + operation.

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

The resulting type after applying the - operator.

Performs the - operation.

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

The resulting type after applying the * operator.

Performs the * operation.

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

The resulting type after applying the / operator.

Performs the / operation.

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

The resulting type after applying the % operator.

Performs the % operation.

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

The resulting type after applying the << operator.

Performs the << operation.

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

The resulting type after applying the >> operator.

Performs the >> operation.

impl Clone for BigNum
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Neg for BigNum
[src]

The resulting type after applying the - operator.

Performs the unary - operation.

impl Debug for BigNum
[src]

Formats the value using the given formatter. Read more

impl Eq for BigNum
[src]

impl PartialEq for BigNum
[src]

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

This method tests for !=.

impl Ord for BigNum
[src]

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

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl PartialOrd for BigNum
[src]

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

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

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

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

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

impl Drop for BigNum
[src]

Executes the destructor for this type. Read more

Auto Trait Implementations

impl !Send for BigNum

impl !Sync for BigNum