scclib
Stable Cloud Computing C++ Library
Public Member Functions | Static Public Member Functions | List of all members
scc::crypto::Bignum Class Reference

Big number. More...

#include <bignum.h>

Public Member Functions

 Bignum ()
 Construct and set to 0.
 
 Bignum (Bignum &&other)
 Move constructor.
 
Bignumoperator= (Bignum &&other)
 Move assignment.
 
 Bignum (const Bignum &other)
 Copy constructor.
 
Bignumoperator= (const Bignum &other)
 Copy assignment.
 
 Bignum (uint32_t w)
 Word constructor.
 
Bignumoperator= (uint32_t w)
 Word assignment.
 
 Bignum (const void *loc, int len)
 Set to data.
 
void * bn ()
 Bignum context for use by external services.
 
const void * const_bn () const
 Bignum context for use by external services.
 
void set_width (int)
 Set the number to a specified bit width.
 
void set (uint32_t)
 Set to a word. More...
 
void set (const void *, int)
 Set a positive integer from input bytes.
 
void set_2sc (const void *, int)
 Set integer from twos complement input stream.
 
int len () const
 Required length of get() output in bytes. More...
 
void get (void *, int) const
 Octal (byte) output of a positive number. More...
 
int len_2sc () const
 Get the length of octal output in twos complement form.
 
void get_2sc (void *, int) const
 Octal (byte) output, in two's complement form. More...
 
void copy (const Bignum &)
 Copy into this bignum.
 
void move (Bignum &)
 Move into this bignum. More...
 
void clear ()
 Clear and set to 0.
 
int width () const
 Number of significant bits. More...
 
void exp (const Bignum &)
 Exponent a^b.
 
void exp (uint32_t b)
 Exponent a^b.
 
void gcd (const Bignum &)
 Greatest common divisor for a and b.
 
void gen_rand (int, bool=false, bool=false)
 Generate a random number of the specified bit width. More...
 
bool is_prime (int=-1)
 Perform a Miller-Rabin prime test to test primality. More...
 
void gen_prime (int)
 Generate a random pseudo-prime number of the specified bit width. More...
 
void lshift (int)
 Left bit shift.
 
void rshift (int)
 Right bit shift.
 
void add (const Bignum &)
 Add a+b.
 
void add (uint32_t b)
 Word add a+b.
 
void sub (const Bignum &)
 Subtract a-b.
 
void sub (uint32_t b)
 Subtract an integer.
 
void mul (const Bignum &)
 Mulitply a*b.
 
void mul (uint32_t b)
 Word multiply.
 
void div (const Bignum &, Bignum *)
 Divide a/b. More...
 
void div (uint32_t b, Bignum *rem)
 Divide a/b. More...
 
void mod (const Bignum &)
 Modulus ab.
 
void mod (uint32_t b)
 Word modulus.
 
void negate ()
 Negate (change sign).
 
void bit_not ()
 Bitwise not ~a.
 
void bit_and (const Bignum &b)
 Bitwise not a&b.
 
void bit_or (const Bignum &b)
 Bitwise or a|b.
 
void bit_xor (const Bignum &b)
 Bitwise xor a^b.
 
bool is_bit_set (int) const
 Is the bit set? More...
 
void set_bit (int)
 Set a bit. More...
 
void clear_bit (int)
 Clear a bit. More...
 
void gcd (uint32_t b)
 Greatest common divisor of a and b.
 
bool is_negative () const
 Is negative?
 
int cmp (uint32_t w) const
 Arithmetic compare for an integer.
 
int cmp (const Bignum &) const
 Arithmetic compare.
 
bool operator== (const Bignum &o) const
 Equal operator.
 
bool operator!= (const Bignum &o) const
 Not equal operator.
 
bool operator< (const Bignum &o) const
 Less than operator.
 
bool operator> (const Bignum &o) const
 Greater than operator.
 
bool operator<= (const Bignum &o) const
 Less than equal operator.
 
bool operator>= (const Bignum &o) const
 Greater than equal operator.
 
bool operator== (uint32_t o) const
 Equal operator.
 
bool operator!= (uint32_t o) const
 Not equal operator.
 
bool operator< (uint32_t o) const
 Less than operator.
 
bool operator> (uint32_t o) const
 Greater than operator.
 
bool operator<= (uint32_t o) const
 Less than equal operator.
 
bool operator>= (uint32_t o) const
 Greater than equal operator.
 
Bignum operator<< (int shift) const
 Right shift operator.
 
Bignumoperator<<= (int shift)
 Right shift assign operator.
 
Bignum operator>> (int shift) const
 Right shift operator.
 
Bignumoperator>>= (int shift)
 Right shift assign operator.
 
Bignum operator+ (const Bignum &a) const
 Add operator.
 
Bignum operator+= (const Bignum &a)
 Add assign operator.
 
Bignum operator+ (uint32_t a) const
 Add operator.
 
Bignum operator+= (uint32_t a)
 Add assign operator.
 
Bignumoperator++ ()
 Prefix addition operator.
 
Bignum operator++ (int d)
 Postfix addition operator.
 
Bignum operator- ()
 Negation operator -a.
 
Bignum operator- (const Bignum &a) const
 Subtract operator.
 
Bignum operator-= (const Bignum &a)
 Subtract assign operator.
 
Bignum operator- (uint32_t a) const
 Subtract operator.
 
Bignum operator-= (uint32_t a)
 Subtract assign operator.
 
Bignumoperator-- ()
 Prefix decrement.
 
Bignum operator-- (int d)
 Postfix decrement.
 
Bignum operator* (const Bignum &a) const
 Multiply operator.
 
Bignum operator*= (const Bignum &a)
 Multiply assign operator.
 
Bignum operator* (uint32_t a) const
 Multiply operator.
 
Bignum operator*= (uint32_t a)
 Multiply assign operator.
 
Bignum operator/ (const Bignum &a) const
 Divide operator.
 
Bignum operator/= (const Bignum &a)
 Divide assign operator.
 
Bignum operator/ (uint32_t a) const
 Divide operator.
 
Bignum operator/= (uint32_t a)
 Divide assign operator.
 
Bignum operator% (const Bignum &a) const
 Modulus operator.
 
Bignum operator%= (const Bignum &a)
 Modulus assign operator.
 
Bignum operator% (uint32_t a) const
 Modulus operator.
 
Bignum operator%= (uint32_t a)
 Modulus assign operator.
 
Bignum operator& (const Bignum &a) const
 And operator.
 
Bignum operator&= (const Bignum &a)
 And assign operator.
 
Bignum operator& (uint32_t a) const
 And operator.
 
Bignum operator&= (uint32_t a)
 And assign operator.
 
Bignum operator| (const Bignum &a) const
 Or operator.
 
Bignum operator|= (const Bignum &a)
 Or assign operator.
 
Bignum operator| (uint32_t a) const
 Or operator.
 
Bignum operator|= (uint32_t a)
 Or assign operator.
 
Bignum operator^ (const Bignum &a) const
 Xor operator.
 
Bignum operator^= (const Bignum &a)
 Xor assign operator.
 
Bignum operator^ (uint32_t a) const
 Xor operator.
 
Bignum operator^= (uint32_t a)
 Xor assign operator.
 
Bignum operator~ () const
 Not operator.
 
std::string str (bool=false) const
 Return a string representation in decimal or hex. More...
 

Static Public Member Functions

static bool is_prime (uint32_t w)
 Is prime for an integer.
 

Detailed Description

Big number.

Examples
crypto/unittest/bignum.cc, and scclib/crypto/unittest/der.cc.

Definition at line 58 of file bignum.h.

Member Function Documentation

◆ clear_bit()

void scc::crypto::Bignum::clear_bit ( int  )

Clear a bit.

Bits are 0 indexed.

Parameters
bit_numberbit to set, must be >= 0.

◆ div() [1/2]

void scc::crypto::Bignum::div ( const Bignum ,
Bignum  
)

Divide a/b.

Optional remainder.

◆ div() [2/2]

void scc::crypto::Bignum::div ( uint32_t  b,
Bignum rem 
)
inline

Divide a/b.

Optional remainder.

Definition at line 257 of file bignum.h.

◆ gen_prime()

void scc::crypto::Bignum::gen_prime ( int  )

Generate a random pseudo-prime number of the specified bit width.

Parameters
bit_widthThe size of the prime in bits.

◆ gen_rand()

void scc::crypto::Bignum::gen_rand ( int  ,
bool  = false,
bool  = false 
)

Generate a random number of the specified bit width.

A cryptographically strong number has the upper two bits set, so that the product of two such numbers will always have 2*bit_width size.

Parameters
bit_widthThe size of the prime in bits, must be > 0. If strong, must be > 1.
strongIf this is true, generate a cryptographically strong number (top two bits are on).
oddIf this is true, generates an odd number.

◆ get()

void scc::crypto::Bignum::get ( void *  ,
int   
) const

Octal (byte) output of a positive number.

Parameters
locdata location
lenlength of data

Throws an exception if the number is negative.

◆ get_2sc()

void scc::crypto::Bignum::get_2sc ( void *  ,
int   
) const

Octal (byte) output, in two's complement form.

Returns the minimum number of bytes required.

Example:

128 bin 0000 0000 1000 0000 hex 00 80 127 bin 0111 1111 hex 7f -1 bin 1111 1111 hex ff -127 bin 1000 0001 hex 81 -128 bin 1000 0000 hex 80 -129 bin 1111 1111 0111 1111 hex ff 7f

Parameters
locdata location
lenlength of data

◆ is_bit_set()

bool scc::crypto::Bignum::is_bit_set ( int  ) const

Is the bit set?

Bits are 0 indexed.

Parameters
bit_numberbit to set, must be >= 0.

◆ is_prime()

bool scc::crypto::Bignum::is_prime ( int  = -1)

Perform a Miller-Rabin prime test to test primality.

The default calculates primes with expected 1/10^80 probability of false positive.

Parameters
trialsNumber of trials, <= 0 means calculate trials automatically.

◆ len()

int scc::crypto::Bignum::len ( ) const

Required length of get() output in bytes.

The number zero has length 1.

◆ move()

void scc::crypto::Bignum::move ( Bignum )

Move into this bignum.

The other bignum will be initialized and set to zero.

◆ set()

void scc::crypto::Bignum::set ( uint32_t  )

Set to a word.

This setter will not necessarily zero out all data.

◆ set_bit()

void scc::crypto::Bignum::set_bit ( int  )

Set a bit.

Bits are 0 indexed. Expands the number if necessary.

Parameters
bit_numberbit to set, must be >= 0.

◆ str()

std::string scc::crypto::Bignum::str ( bool  = false) const

Return a string representation in decimal or hex.

Parameters
hexemit hexadecimal

◆ width()

int scc::crypto::Bignum::width ( ) const

Number of significant bits.

The number 0 has width 1.


The documentation for this class was generated from the following file: