Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: test-lpc1768 oldheating gps motorhome ... more
Diff: bignum/bignum.h
- Revision:
- 52:88d594aca377
- Child:
- 53:3888bf121010
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bignum/bignum.h Tue Jun 18 06:48:31 2019 +0000 @@ -0,0 +1,71 @@ +#include <stdint.h> +#include <stdbool.h> + +// Type definition +#define BN_ARRAY_SIZE (2048 / 32) + +typedef struct +{ + uint32_t size; + uint32_t* array; +} bn; + +//Assembly instructions +extern void BnInc2048 (uint32_t* data); +extern void BnShiftOneBitRight2048(uint32_t* data); +extern void BnShiftOneBitLeft2048 (uint32_t* data); +extern void BnSub2048 (uint32_t* acc, uint32_t* sub); + +// Assignment functions +extern void BnFromInt (bn* n, uint64_t i); +extern int BnToInt (bn* n); +extern void BnParseHex(bn* n, const char* p); +extern void BnAssign (bn* dst, bn* src); // Copy src into dst; left fills dst with zeros if src is smaller; truncates if src is larger. + +// Unary instructions +extern void BnShiftOneBitLeft (bn* a); +extern void BnShiftOneBitRight(bn* a); +extern void BnInc (bn* n); // Increment: add one to n +extern void BnDec (bn* n); // Decrement: subtract one from n +extern void BnZero (bn* n); + +// Bitwise operations +extern void BnAnd (bn* a, bn* b, bn* r); // r = a & b +extern void BnOr (bn* a, bn* b, bn* r); // r = a | b +extern void BnXor (bn* a, bn* b, bn* r); // r = a ^ b +extern void BnShiftBitsLeft (bn* a, int nbits); // a << nbits +extern void BnShiftBitsRight (bn* a, int nbits); // a >> nbits +extern void BnShiftWordsRight(bn* a, int nwords); +extern void BnShiftWordsLeft (bn* a, int nwords); + +// Http functions +extern void BigNumAsHttp (bn* n); + +// Arithmetic operations +extern void BnAdd (bn* a, bn* b, bn* r); // r = a + b +extern void BnSub (bn* a, bn* b, bn* r); // r = a - b +extern void bignum_mul (bn* a, bn* b, bn* r); // r = a * b +extern void bignum_div (bn* a, bn* b, bn* r); // r = a / b +extern void bignum_mod (bn* a, bn* b, bn* r); // r = a % b +extern void bignum_divmod(bn* a, bn* b, bn* r, bn* m); // r = a/b, m = a%b +extern void bignum_pow (bn* a, bn* b, bn* r); // r = a^b -- e.g. 2^10 => 1024 +extern void bignum_isqrt (bn* a, bn* r); // Integer square root r = a^-2 -- e.g. isqrt(5) => 2 + +// Comparison operators +extern int bignum_cmp (bn* a, bn* b); // Compare: returns +1 if larger, 0 if equal and -1 if smaller +extern bool BnIsZero (bn* n); // For comparison with zero +extern bool BnIsNonZero(bn* n); // For comparison with zero + +// Asynchronous operations +#define BIGNUM_CALC_NONE 0 +#define BIGNUM_CALC_STARTED 1 +#define BIGNUM_CALC_FINISHED 2 +extern void BnAsyncMain(void); + +extern int BigNumModExpStatus; +extern int BigNumModExpProgress; +extern uint64_t BigNumModExpMulHr; +extern uint64_t BigNumModExpModHr; +extern void BigNumModExpStart(bn* message, bn* exponent, bn* modulus, bn* result); + +