Big Number Functions
v3BNInitSyntax#include "bignum.h" s32 v3BNInit( v3_BN * x ); DescriptionInitialize 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 ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNSetSyntax#include "bignum.h" s32 v3BNSet( v3_BN * x, const v3_BN * a ); Descriptionx = a. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNSets32Syntax#include "bignum.h" s32 v3BNSets32( v3_BN * x, const s32 a ); Descriptionx = (s32) a. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNGets32Syntax#include "bignum.h" s32 v3BNGets32( s32 * x, const v3_BN * a ); Descriptionx = (s32) a. Nasty truncation will occur if the number is more then 31 bits long. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNCmpSyntax#include "bignum.h" s32 v3BNCmp( const v3_BN * a, const v3_BN * b ); DescriptionCompare the numbers a and b. Return Values-1 if a < b, 0 if a == b, 1 if a > b. ErrorsExamplev3BNOddSyntax#include "bignum.h" s32 v3BNOdd( const v3_BN * x ); DescriptionIs x odd? Return Values1 if x is odd, 0 if even. ErrorsExamplev3BNZeroSyntax#include "bignum.h" s32 v3BNZero( const v3_BN * x ); DescriptionIs x zero? Return Values1 if x is zero, 0 otherwise. ErrorsExamplev3BNBitsSyntax#include "bignum.h" s32 v3BNBits( u32 * x, const v3_BN * a ); Descriptionx = bits in a, could be 0. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNAddSyntax#include "bignum.h" s32 v3BNAdd( v3_BN * x, const v3_BN * a, const v3_BN * b ); Descriptionx = a + b. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNSubSyntax#include "bignum.h" s32 v3BNSub( v3_BN * x, const v3_BN * a, const v3_BN * b ); Descriptionx = a - b. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNMulSyntax#include "bignum.h" s32 v3BNMul( v3_BN * x, const v3_BN * a, const v3_BN * b ); Descriptionx = a * b. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNDivSyntax#include "bignum.h" s32 v3BNDiv( v3_BN * x, const v3_BN * a, const v3_BN * b ); Descriptionx = a / b. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNModSyntax#include "bignum.h" s32 v3BNMod( v3_BN * x, const v3_BN * a, const v3_BN * b ); Descriptionx = a % b. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNModExpSyntax#include "bignum.h" s32 v3BNModExp( v3_BN * x, const v3_BN * a, const v3_BN * e, const v3_BN * m ); Descriptionx = a to the e-th power modulo m. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNModInvSyntax#include "bignum.h" s32 v3BNModInv( v3_BN * x, const v3_BN * a, const v3_BN * m ); Descriptionx = Inverse of a modulo m. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNGCDSyntax#include "bignum.h" s32 v3BNGCD( v3_BN * x, const v3_BN * a, const v3_BN * b ); Descriptionx = Greatest Common Divisor (GCD) of a and b. 1 if a and b are relatively prime. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNASyntax#include "bignum.h" s32 v3BNA( v3_BN * x, void ** end, const void * string, u32 radix ); DescriptionConvert 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 ValuesSets result to number and returns V3_PASS on success, or sets result to 0 and returns V3_FAIL on failure. ErrorsExamplev3BNPrintSyntax#include "bignum.h" u64 v3BNPrint( v3_FILE * file, ascii * string, u64 max_chars, u32 radix, v3_BN * x ); DescriptionPrints 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 ValuesNumber of characters written to string, or -1 if truncated due to max_chars. ErrorsExamplev3BNFreeSyntax#include "bignum.h" void v3BNFree( v3_BN * x ); DescriptionFree the bignum internals. Return ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNLoadSyntax#include "bignum.h" s32 v3BNLoad( v3_BN * x, const u8 * bytes, u32 bits ); DescriptionLoad 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 ValuesV3_PASS or V3_FAIL on an error. ErrorsExamplev3BNSaveSyntax#include "bignum.h" s32 v3BNSave( u8 * bytes, const v3_BN * x, u32 max_bits, u32 min_bits ); DescriptionSave 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 ValuesV3_PASS or V3_FAIL on an error. ErrorsExample
© Copyright Mithral Communications & Design Inc.
1995-2003.
All rights reserved.
Mithral® and Cosm® are trademarks of
Mithral Communications & Design Inc.
|