Andrew Boyson / lpc1768

Dependents:   test-lpc1768 oldheating gps motorhome ... more

Committer:
andrewboyson
Date:
Tue Jun 18 06:48:31 2019 +0000
Revision:
52:88d594aca377
Child:
53:3888bf121010
Added bignum module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 52:88d594aca377 1 #include <stdint.h>
andrewboyson 52:88d594aca377 2 #include <stdbool.h>
andrewboyson 52:88d594aca377 3
andrewboyson 52:88d594aca377 4 // Type definition
andrewboyson 52:88d594aca377 5 #define BN_ARRAY_SIZE (2048 / 32)
andrewboyson 52:88d594aca377 6
andrewboyson 52:88d594aca377 7 typedef struct
andrewboyson 52:88d594aca377 8 {
andrewboyson 52:88d594aca377 9 uint32_t size;
andrewboyson 52:88d594aca377 10 uint32_t* array;
andrewboyson 52:88d594aca377 11 } bn;
andrewboyson 52:88d594aca377 12
andrewboyson 52:88d594aca377 13 //Assembly instructions
andrewboyson 52:88d594aca377 14 extern void BnInc2048 (uint32_t* data);
andrewboyson 52:88d594aca377 15 extern void BnShiftOneBitRight2048(uint32_t* data);
andrewboyson 52:88d594aca377 16 extern void BnShiftOneBitLeft2048 (uint32_t* data);
andrewboyson 52:88d594aca377 17 extern void BnSub2048 (uint32_t* acc, uint32_t* sub);
andrewboyson 52:88d594aca377 18
andrewboyson 52:88d594aca377 19 // Assignment functions
andrewboyson 52:88d594aca377 20 extern void BnFromInt (bn* n, uint64_t i);
andrewboyson 52:88d594aca377 21 extern int BnToInt (bn* n);
andrewboyson 52:88d594aca377 22 extern void BnParseHex(bn* n, const char* p);
andrewboyson 52:88d594aca377 23 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.
andrewboyson 52:88d594aca377 24
andrewboyson 52:88d594aca377 25 // Unary instructions
andrewboyson 52:88d594aca377 26 extern void BnShiftOneBitLeft (bn* a);
andrewboyson 52:88d594aca377 27 extern void BnShiftOneBitRight(bn* a);
andrewboyson 52:88d594aca377 28 extern void BnInc (bn* n); // Increment: add one to n
andrewboyson 52:88d594aca377 29 extern void BnDec (bn* n); // Decrement: subtract one from n
andrewboyson 52:88d594aca377 30 extern void BnZero (bn* n);
andrewboyson 52:88d594aca377 31
andrewboyson 52:88d594aca377 32 // Bitwise operations
andrewboyson 52:88d594aca377 33 extern void BnAnd (bn* a, bn* b, bn* r); // r = a & b
andrewboyson 52:88d594aca377 34 extern void BnOr (bn* a, bn* b, bn* r); // r = a | b
andrewboyson 52:88d594aca377 35 extern void BnXor (bn* a, bn* b, bn* r); // r = a ^ b
andrewboyson 52:88d594aca377 36 extern void BnShiftBitsLeft (bn* a, int nbits); // a << nbits
andrewboyson 52:88d594aca377 37 extern void BnShiftBitsRight (bn* a, int nbits); // a >> nbits
andrewboyson 52:88d594aca377 38 extern void BnShiftWordsRight(bn* a, int nwords);
andrewboyson 52:88d594aca377 39 extern void BnShiftWordsLeft (bn* a, int nwords);
andrewboyson 52:88d594aca377 40
andrewboyson 52:88d594aca377 41 // Http functions
andrewboyson 52:88d594aca377 42 extern void BigNumAsHttp (bn* n);
andrewboyson 52:88d594aca377 43
andrewboyson 52:88d594aca377 44 // Arithmetic operations
andrewboyson 52:88d594aca377 45 extern void BnAdd (bn* a, bn* b, bn* r); // r = a + b
andrewboyson 52:88d594aca377 46 extern void BnSub (bn* a, bn* b, bn* r); // r = a - b
andrewboyson 52:88d594aca377 47 extern void bignum_mul (bn* a, bn* b, bn* r); // r = a * b
andrewboyson 52:88d594aca377 48 extern void bignum_div (bn* a, bn* b, bn* r); // r = a / b
andrewboyson 52:88d594aca377 49 extern void bignum_mod (bn* a, bn* b, bn* r); // r = a % b
andrewboyson 52:88d594aca377 50 extern void bignum_divmod(bn* a, bn* b, bn* r, bn* m); // r = a/b, m = a%b
andrewboyson 52:88d594aca377 51 extern void bignum_pow (bn* a, bn* b, bn* r); // r = a^b -- e.g. 2^10 => 1024
andrewboyson 52:88d594aca377 52 extern void bignum_isqrt (bn* a, bn* r); // Integer square root r = a^-2 -- e.g. isqrt(5) => 2
andrewboyson 52:88d594aca377 53
andrewboyson 52:88d594aca377 54 // Comparison operators
andrewboyson 52:88d594aca377 55 extern int bignum_cmp (bn* a, bn* b); // Compare: returns +1 if larger, 0 if equal and -1 if smaller
andrewboyson 52:88d594aca377 56 extern bool BnIsZero (bn* n); // For comparison with zero
andrewboyson 52:88d594aca377 57 extern bool BnIsNonZero(bn* n); // For comparison with zero
andrewboyson 52:88d594aca377 58
andrewboyson 52:88d594aca377 59 // Asynchronous operations
andrewboyson 52:88d594aca377 60 #define BIGNUM_CALC_NONE 0
andrewboyson 52:88d594aca377 61 #define BIGNUM_CALC_STARTED 1
andrewboyson 52:88d594aca377 62 #define BIGNUM_CALC_FINISHED 2
andrewboyson 52:88d594aca377 63 extern void BnAsyncMain(void);
andrewboyson 52:88d594aca377 64
andrewboyson 52:88d594aca377 65 extern int BigNumModExpStatus;
andrewboyson 52:88d594aca377 66 extern int BigNumModExpProgress;
andrewboyson 52:88d594aca377 67 extern uint64_t BigNumModExpMulHr;
andrewboyson 52:88d594aca377 68 extern uint64_t BigNumModExpModHr;
andrewboyson 52:88d594aca377 69 extern void BigNumModExpStart(bn* message, bn* exponent, bn* modulus, bn* result);
andrewboyson 52:88d594aca377 70
andrewboyson 52:88d594aca377 71