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