You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

134 lines
4.0 KiB

  1. /** @brief Generic arithmetic which has to be compiled per field. */
  2. #include "field.h"
  3. static const gf MODULUS = {FIELD_LITERAL(
  4. $(ser(modulus,gf_lit_limb_bits))
  5. )};
  6. #if P_MOD_8 == 5
  7. const gf SQRT_MINUS_ONE = {FIELD_LITERAL(
  8. $(ser(msqrt(-1,modulus),gf_lit_limb_bits) if modulus % 4 == 1 else "/* NOPE */")
  9. )};
  10. #endif
  11. /** Serialize to wire format. */
  12. void gf_serialize (uint8_t serial[SER_BYTES], const gf x, int with_hibit) {
  13. gf red;
  14. gf_copy(red, x);
  15. gf_strong_reduce(red);
  16. if (!with_hibit) { assert(gf_hibit(red) == 0); }
  17. unsigned int j=0, fill=0;
  18. dword_t buffer = 0;
  19. UNROLL for (unsigned int i=0; i<(with_hibit ? X_SER_BYTES : SER_BYTES); i++) {
  20. if (fill < 8 && j < NLIMBS) {
  21. buffer |= ((dword_t)red->limb[LIMBPERM(j)]) << fill;
  22. fill += LIMB_PLACE_VALUE(LIMBPERM(j));
  23. j++;
  24. }
  25. serial[i] = buffer;
  26. fill -= 8;
  27. buffer >>= 8;
  28. }
  29. }
  30. /** Return high bit of x = low bit of 2x mod p */
  31. mask_t gf_hibit(const gf x) {
  32. gf y;
  33. gf_add(y,x,x);
  34. gf_strong_reduce(y);
  35. return -(y->limb[0]&1);
  36. }
  37. /** Return high bit of x = low bit of 2x mod p */
  38. mask_t gf_lobit(const gf x) {
  39. gf y;
  40. gf_copy(y,x);
  41. gf_strong_reduce(y);
  42. return -(y->limb[0]&1);
  43. }
  44. /** Deserialize from wire format; return -1 on success and 0 on failure. */
  45. mask_t gf_deserialize (gf x, const uint8_t serial[SER_BYTES], int with_hibit, uint8_t hi_nmask) {
  46. unsigned int j=0, fill=0;
  47. dword_t buffer = 0;
  48. dsword_t scarry = 0;
  49. const unsigned nbytes = with_hibit ? X_SER_BYTES : SER_BYTES;
  50. UNROLL for (unsigned int i=0; i<NLIMBS; i++) {
  51. UNROLL while (fill < LIMB_PLACE_VALUE(LIMBPERM(i)) && j < nbytes) {
  52. uint8_t sj = serial[j];
  53. if (j==nbytes-1) sj &= ~hi_nmask;
  54. buffer |= ((dword_t)sj) << fill;
  55. fill += 8;
  56. j++;
  57. }
  58. x->limb[LIMBPERM(i)] = (i<NLIMBS-1) ? buffer & LIMB_MASK(LIMBPERM(i)) : buffer;
  59. fill -= LIMB_PLACE_VALUE(LIMBPERM(i));
  60. buffer >>= LIMB_PLACE_VALUE(LIMBPERM(i));
  61. scarry = (scarry + x->limb[LIMBPERM(i)] - MODULUS->limb[LIMBPERM(i)]) >> (8*sizeof(word_t));
  62. }
  63. mask_t succ = with_hibit ? -(mask_t)1 : ~gf_hibit(x);
  64. return succ & word_is_zero(buffer) & ~word_is_zero(scarry);
  65. }
  66. /** Reduce to canonical form. */
  67. void gf_strong_reduce (gf a) {
  68. /* first, clear high */
  69. gf_weak_reduce(a); /* Determined to have negligible perf impact. */
  70. /* now the total is less than 2p */
  71. /* compute total_value - p. No need to reduce mod p. */
  72. dsword_t scarry = 0;
  73. for (unsigned int i=0; i<NLIMBS; i++) {
  74. scarry = scarry + a->limb[LIMBPERM(i)] - MODULUS->limb[LIMBPERM(i)];
  75. a->limb[LIMBPERM(i)] = scarry & LIMB_MASK(LIMBPERM(i));
  76. scarry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
  77. }
  78. /* uncommon case: it was >= p, so now scarry = 0 and this = x
  79. * common case: it was < p, so now scarry = -1 and this = x - p + 2^255
  80. * so let's add back in p. will carry back off the top for 2^255.
  81. */
  82. assert(word_is_zero(scarry) | word_is_zero(scarry+1));
  83. word_t scarry_0 = scarry;
  84. dword_t carry = 0;
  85. /* add it back */
  86. for (unsigned int i=0; i<NLIMBS; i++) {
  87. carry = carry + a->limb[LIMBPERM(i)] + (scarry_0 & MODULUS->limb[LIMBPERM(i)]);
  88. a->limb[LIMBPERM(i)] = carry & LIMB_MASK(LIMBPERM(i));
  89. carry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
  90. }
  91. assert(word_is_zero(carry + scarry_0));
  92. }
  93. /** Subtract two gf elements d=a-b */
  94. void gf_sub (gf d, const gf a, const gf b) {
  95. gf_sub_RAW ( d, a, b );
  96. gf_bias( d, 2 );
  97. gf_weak_reduce ( d );
  98. }
  99. /** Add two field elements d = a+b */
  100. void gf_add (gf d, const gf a, const gf b) {
  101. gf_add_RAW ( d, a, b );
  102. gf_weak_reduce ( d );
  103. }
  104. /** Compare a==b */
  105. mask_t gf_eq(const gf a, const gf b) {
  106. gf c;
  107. gf_sub(c,a,b);
  108. gf_strong_reduce(c);
  109. mask_t ret=0;
  110. for (unsigned int i=0; i<NLIMBS; i++) {
  111. ret |= c->limb[LIMBPERM(i)];
  112. }
  113. return word_is_zero(ret);
  114. }