modm_donna_32bit.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Public domain by Andrew M. <liquidsun@gmail.com>
  3. */
  4. /*
  5. Arithmetic modulo the group order n = 2^252 + 27742317777372353535851937790883648493 = 7237005577332262213973186563042994240857116359379907606001950938285454250989
  6. k = 32
  7. b = 1 << 8 = 256
  8. m = 2^252 + 27742317777372353535851937790883648493 = 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed
  9. mu = floor( b^(k*2) / m ) = 0xfffffffffffffffffffffffffffffffeb2106215d086329a7ed9ce5a30a2c131b
  10. */
  11. #define bignum256modm_bits_per_limb 30
  12. #define bignum256modm_limb_size 9
  13. typedef uint32_t bignum256modm_element_t;
  14. typedef bignum256modm_element_t bignum256modm[9];
  15. /* see HAC, Alg. 14.42 Step 4 */
  16. void reduce256_modm(bignum256modm r);
  17. /*
  18. Barrett reduction, see HAC, Alg. 14.42
  19. Instead of passing in x, pre-process in to q1 and r1 for efficiency
  20. */
  21. void barrett_reduce256_modm(bignum256modm r, const bignum256modm q1, const bignum256modm r1);
  22. /* addition modulo m */
  23. void add256_modm(bignum256modm r, const bignum256modm x, const bignum256modm y);
  24. /* -x modulo m */
  25. void neg256_modm(bignum256modm r, const bignum256modm x);
  26. /* subtraction x-y modulo m */
  27. void sub256_modm(bignum256modm r, const bignum256modm x, const bignum256modm y);
  28. /* multiplication modulo m */
  29. void mul256_modm(bignum256modm r, const bignum256modm x, const bignum256modm y);
  30. void expand256_modm(bignum256modm out, const unsigned char* in, size_t len);
  31. void expand_raw256_modm(bignum256modm out, const unsigned char in[32]);
  32. int is_reduced256_modm(const bignum256modm in);
  33. void contract256_modm(unsigned char out[32], const bignum256modm in);
  34. void contract256_window4_modm(signed char r[64], const bignum256modm in);
  35. void contract256_slidingwindow_modm(signed char r[256], const bignum256modm s, int windowsize);
  36. /* 64bit uint to scalar value */
  37. void set256_modm(bignum256modm r, uint64_t v);
  38. /* scalar value to 64bit uint */
  39. int get256_modm(uint64_t* v, const bignum256modm r);
  40. /* equality test on two reduced scalar values */
  41. int eq256_modm(const bignum256modm x, const bignum256modm y);
  42. /* comparison of two reduced scalar values */
  43. int cmp256_modm(const bignum256modm x, const bignum256modm y);
  44. /* scalar null check, has to be reduced */
  45. int iszero256_modm(const bignum256modm x);
  46. /* simple copy, no reduction */
  47. void copy256_modm(bignum256modm r, const bignum256modm x);
  48. /* check if nonzero && same after reduction */
  49. int check256_modm(const bignum256modm x);
  50. /* (cc - aa * bb) % l */
  51. void mulsub256_modm(
  52. bignum256modm r,
  53. const bignum256modm a,
  54. const bignum256modm b,
  55. const bignum256modm c);
  56. /* (cc + aa * bb) % l */
  57. void muladd256_modm(
  58. bignum256modm r,
  59. const bignum256modm a,
  60. const bignum256modm b,
  61. const bignum256modm c);