[Cosm Logo]

Big Number Functions


CosmBNInit

Syntax

#include "cosm/bignum.h"
s32 CosmBNInit( cosm_BN * x );

Description

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

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNSet

Syntax

#include "cosm/bignum.h"
s32 CosmBNSet( cosm_BN * x, const cosm_BN * a );

Description

x = a.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNSets32

Syntax

#include "cosm/bignum.h"
s32 CosmBNSets32( cosm_BN * x, const s32 a );

Description

x = (s32) a.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNGets32

Syntax

#include "cosm/bignum.h"
s32 CosmBNGets32( s32 * x, const cosm_BN * a );

Description

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

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNCmp

Syntax

#include "cosm/bignum.h"
s32 CosmBNCmp( const cosm_BN * a, const cosm_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





CosmBNOdd

Syntax

#include "cosm/bignum.h"
s32 CosmBNOdd( const cosm_BN * x );

Description

Is x odd?

Return Values

1 if x is odd, 0 if even.

Errors

Example





CosmBNZero

Syntax

#include "cosm/bignum.h"
s32 CosmBNZero( const cosm_BN * x );

Description

Is x zero?

Return Values

1 if x is zero, 0 otherwise.

Errors

Example





CosmBNBits

Syntax

#include "cosm/bignum.h"
s32 CosmBNBits( u32 * x, const cosm_BN * a );

Description

x = bits in a, could be 0.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNAdd

Syntax

#include "cosm/bignum.h"
s32 CosmBNAdd( cosm_BN * x, const cosm_BN * a, const cosm_BN * b );

Description

x = a + b.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNSub

Syntax

#include "cosm/bignum.h"
s32 CosmBNSub( cosm_BN * x, const cosm_BN * a, const cosm_BN * b );

Description

x = a - b.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNMul

Syntax

#include "cosm/bignum.h"
s32 CosmBNMul( cosm_BN * x, const cosm_BN * a, const cosm_BN * b );

Description

x = a * b.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNDiv

Syntax

#include "cosm/bignum.h"
s32 CosmBNDiv( cosm_BN * x, const cosm_BN * a, const cosm_BN * b );

Description

x = a / b.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNMod

Syntax

#include "cosm/bignum.h"
s32 CosmBNMod( cosm_BN * x, const cosm_BN * a, const cosm_BN * b );

Description

x = a % b.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNModExp

Syntax

#include "cosm/bignum.h"
s32 CosmBNModExp( cosm_BN * x, const cosm_BN * a, const cosm_BN * e,
  const cosm_BN * m );

Description

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

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNModInv

Syntax

#include "cosm/bignum.h"
s32 CosmBNModInv( cosm_BN * x, const cosm_BN * a, const cosm_BN * m );

Description

x = Inverse of a modulo m.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNGCD

Syntax

#include "cosm/bignum.h"
s32 CosmBNGCD( cosm_BN * x, const cosm_BN * a, const cosm_BN * b );

Description

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

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNStr

Syntax

#include "cosm/bignum.h"
s32 CosmBNStr( cosm_BN * x, void ** end, const void * string, u32 radix );

Description

Convert the string written in radix to the bignum x. 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 COSM_PASS on success, or sets result to 0 and returns COSM_FAIL on failure.

Errors

Example





CosmBNPrint

Syntax

#include "cosm/bignum.h"
u32 CosmBNPrint( cosm_FILE * file, utf8 * string, u32 max_bytes,
  u32 radix, cosm_BN * x );

Description

Prints x to the string in the radix. No more than max_bytes-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_bytes.

Errors

Example





CosmBNFree

Syntax

#include "cosm/bignum.h"
void CosmBNFree( cosm_BN * x );

Description

Free the bignum internals.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNLoad

Syntax

#include "cosm/bignum.h"
s32 CosmBNLoad( cosm_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 (COSM_BN_MAXBITS). Do not load more bits the number originally had when saved.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





CosmBNSave

Syntax

#include "cosm/bignum.h"
s32 CosmBNSave( u8 * bytes, const cosm_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 (COSM_BN_MAXBITS). Care should be taken to record how many bits were in the number for later loading.

Return Values

COSM_PASS or COSM_FAIL on an error.

Errors

Example





© Copyright Mithral Communications & Design Inc. 1995-2017. All rights reserved. Mithral® and Cosm® are trademarks of Mithral Communications & Design Inc.
Document last modified: Jun 01, 2012