[Cosm Logo]

Big Number Functions


v3BNInit

Syntax

#include "bignum.h"
s32 v3BNInit( v3_BN * x );

Description

Initialize the bignum to 0. This isn't neccesary if you have allocated the v3_BN with v3MemAlloc. The library will handle numbers up to 2^31 - 64 bits in length or the limit of memory.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNSet

Syntax

#include "bignum.h"
s32 v3BNSet( v3_BN * x, const v3_BN * a );

Description

x = a.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNSets32

Syntax

#include "bignum.h"
s32 v3BNSets32( v3_BN * x, const s32 a );

Description

x = (s32) a.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNGets32

Syntax

#include "bignum.h"
s32 v3BNGets32( s32 * x, const v3_BN * a );

Description

x = (s32) a. Nasty truncation will occur if the number is more then 31 bits long.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNCmp

Syntax

#include "bignum.h"
s32 v3BNCmp( const v3_BN * a, const v3_BN * b );

Description

Compare the numbers a and b.

Return Values

-1 if a < b, 0 if a == b, 1 if a > b.

Errors

Example





v3BNOdd

Syntax

#include "bignum.h"
s32 v3BNOdd( const v3_BN * x );

Description

Is x odd?

Return Values

1 if x is odd, 0 if even.

Errors

Example





v3BNZero

Syntax

#include "bignum.h"
s32 v3BNZero( const v3_BN * x );

Description

Is x zero?

Return Values

1 if x is zero, 0 otherwise.

Errors

Example





v3BNBits

Syntax

#include "bignum.h"
s32 v3BNBits( u32 * x, const v3_BN * a );

Description

x = bits in a, could be 0.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNAdd

Syntax

#include "bignum.h"
s32 v3BNAdd( v3_BN * x, const v3_BN * a, const v3_BN * b );

Description

x = a + b.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNSub

Syntax

#include "bignum.h"
s32 v3BNSub( v3_BN * x, const v3_BN * a, const v3_BN * b );

Description

x = a - b.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNMul

Syntax

#include "bignum.h"
s32 v3BNMul( v3_BN * x, const v3_BN * a, const v3_BN * b );

Description

x = a * b.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNDiv

Syntax

#include "bignum.h"
s32 v3BNDiv( v3_BN * x, const v3_BN * a, const v3_BN * b );

Description

x = a / b.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNMod

Syntax

#include "bignum.h"
s32 v3BNMod( v3_BN * x, const v3_BN * a, const v3_BN * b );

Description

x = a % b.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNModExp

Syntax

#include "bignum.h"
s32 v3BNModExp( v3_BN * x, const v3_BN * a, const v3_BN * e,
  const v3_BN * m );

Description

x = a to the e-th power modulo m.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNModInv

Syntax

#include "bignum.h"
s32 v3BNModInv( v3_BN * x, const v3_BN * a, const v3_BN * m );

Description

x = Inverse of a modulo m.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNGCD

Syntax

#include "bignum.h"
s32 v3BNGCD( v3_BN * x, const v3_BN * a, const v3_BN * b );

Description

x = Greatest Common Divisor (GCD) of a and b. 1 if a and b are relatively prime.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNA

Syntax

#include "bignum.h"
s32 v3BNA( v3_BN * x, void ** end, const void * string, u32 radix );

Description

Convert the ascii string written in radix to the bignum x. ascii numbers are of the form: [space*][+|-][0|0x|0X]{0-9a-zA-Z}+ radix must be 2-36 or 0. If radix is 0, numbers starting with 0x|0X will be read as base 16, numbers starting with 0 will be interpreted as base 8, and all others will be base 10. If end is not NULL, it will be set to the character after the last character used in the number. Note that use of radixes other then 2, 8, 10, or 16 are generally useless.

Return Values

Sets result to number and returns V3_PASS on success, or sets result to 0 and returns V3_FAIL on failure.

Errors

Example





v3BNPrint

Syntax

#include "bignum.h"
u64 v3BNPrint( v3_FILE * file, ascii * string, u64 max_chars,
  u32 radix, v3_BN * x );

Description

Prints x to the ascii string in the radix. No more than max_chars-1 characters will be written to the string. radix must be from 2 and 16.

Return Values

Number of characters written to string, or -1 if truncated due to max_chars.

Errors

Example





v3BNFree

Syntax

#include "bignum.h"
void v3BNFree( v3_BN * x );

Description

Free the bignum internals.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNLoad

Syntax

#include "bignum.h"
s32 v3BNLoad( v3_BN * x, const u8 * bytes, u32 bits );

Description

Load bits bits of big endian bytes into x. bits must be a multiple of 8. The largest number you can load is 2^31 - 64 bits long (V3_BN_MAXBITS). Do not load more bits the number originally had when saved.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





v3BNSave

Syntax

#include "bignum.h"
s32 v3BNSave( u8 * bytes, const v3_BN * x, u32 max_bits,
  u32 min_bits );

Description

Save x into bytes in big endian order. If the number is more then max_bits long the function will fail and no bytes will be saved. The largest number you can save is 2^31 - 64 bits long (V3_BN_MAXBITS). Care should be taken to record how many bits were in the number for later loading.

Return Values

V3_PASS or V3_FAIL on an error.

Errors

Example





© Copyright Mithral Communications & Design Inc. 1995-2003. All rights reserved. Mithral® and Cosm® are trademarks of Mithral Communications & Design Inc.
Document last modified: May 22, 2003