Big Number Functions
CosmBNInitSyntax#include "cosm/bignum.h" s32 CosmBNInit( cosm_BN * x ); DescriptionInitialize 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 ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNSetSyntax#include "cosm/bignum.h" s32 CosmBNSet( cosm_BN * x, const cosm_BN * a ); Descriptionx = a. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNSets32Syntax#include "cosm/bignum.h" s32 CosmBNSets32( cosm_BN * x, const s32 a ); Descriptionx = (s32) a. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNGets32Syntax#include "cosm/bignum.h" s32 CosmBNGets32( s32 * x, const cosm_BN * a ); Descriptionx = (s32) a. Nasty truncation will occur if the number is more then 31 bits long. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNCmpSyntax#include "cosm/bignum.h" s32 CosmBNCmp( const cosm_BN * a, const cosm_BN * b ); DescriptionCompare the numbers a and b. Return Values-1 if a < b, 0 if a == b, 1 if a > b. ErrorsExampleCosmBNOddSyntax#include "cosm/bignum.h" s32 CosmBNOdd( const cosm_BN * x ); DescriptionIs x odd? Return Values1 if x is odd, 0 if even. ErrorsExampleCosmBNZeroSyntax#include "cosm/bignum.h" s32 CosmBNZero( const cosm_BN * x ); DescriptionIs x zero? Return Values1 if x is zero, 0 otherwise. ErrorsExampleCosmBNBitsSyntax#include "cosm/bignum.h" s32 CosmBNBits( u32 * x, const cosm_BN * a ); Descriptionx = bits in a, could be 0. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNAddSyntax#include "cosm/bignum.h" s32 CosmBNAdd( cosm_BN * x, const cosm_BN * a, const cosm_BN * b ); Descriptionx = a + b. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNSubSyntax#include "cosm/bignum.h" s32 CosmBNSub( cosm_BN * x, const cosm_BN * a, const cosm_BN * b ); Descriptionx = a - b. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNMulSyntax#include "cosm/bignum.h" s32 CosmBNMul( cosm_BN * x, const cosm_BN * a, const cosm_BN * b ); Descriptionx = a * b. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNDivSyntax#include "cosm/bignum.h" s32 CosmBNDiv( cosm_BN * x, const cosm_BN * a, const cosm_BN * b ); Descriptionx = a / b. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNModSyntax#include "cosm/bignum.h" s32 CosmBNMod( cosm_BN * x, const cosm_BN * a, const cosm_BN * b ); Descriptionx = a % b. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNModExpSyntax#include "cosm/bignum.h" s32 CosmBNModExp( cosm_BN * x, const cosm_BN * a, const cosm_BN * e, const cosm_BN * m ); Descriptionx = a to the e-th power modulo m. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNModInvSyntax#include "cosm/bignum.h" s32 CosmBNModInv( cosm_BN * x, const cosm_BN * a, const cosm_BN * m ); Descriptionx = Inverse of a modulo m. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNGCDSyntax#include "cosm/bignum.h" s32 CosmBNGCD( cosm_BN * x, const cosm_BN * a, const cosm_BN * b ); Descriptionx = Greatest Common Divisor (GCD) of a and b. 1 if a and b are relatively prime. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNStrSyntax#include "cosm/bignum.h" s32 CosmBNStr( cosm_BN * x, void ** end, const void * string, u32 radix ); DescriptionConvert 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 ValuesSets result to number and returns COSM_PASS on success, or sets result to 0 and returns COSM_FAIL on failure. ErrorsExampleCosmBNPrintSyntax#include "cosm/bignum.h" u32 CosmBNPrint( cosm_FILE * file, utf8 * string, u32 max_bytes, u32 radix, cosm_BN * x ); DescriptionPrints 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 ValuesNumber of characters written to string, or -1 if truncated due to max_bytes. ErrorsExampleCosmBNFreeSyntax#include "cosm/bignum.h" void CosmBNFree( cosm_BN * x ); DescriptionFree the bignum internals. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNLoadSyntax#include "cosm/bignum.h" s32 CosmBNLoad( cosm_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 (COSM_BN_MAXBITS). Do not load more bits the number originally had when saved. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExampleCosmBNSaveSyntax#include "cosm/bignum.h" s32 CosmBNSave( u8 * bytes, const cosm_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 (COSM_BN_MAXBITS). Care should be taken to record how many bits were in the number for later loading. Return ValuesCOSM_PASS or COSM_FAIL on an error. ErrorsExample
© Copyright Mithral Communications & Design Inc.
1995-2024.
All rights reserved.
Mithral® and Cosm® are trademarks of
Mithral Communications & Design Inc.
|