/security/nss/lib/freebl/ecl/ecp_fp160.c

http://github.com/zpao/v8monkey · C · 179 lines · 90 code · 26 blank · 63 comment · 8 complexity · 73e0c2d38d11d0a40e2bed52d22bd035 MD5 · raw file

  1. /*
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is the elliptic curve math library for prime field curves using floating point operations.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Sun Microsystems, Inc.
  19. * Portions created by the Initial Developer are Copyright (C) 2003
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Stephen Fung <fungstep@hotmail.com>, Sun Microsystems Laboratories
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either the GNU General Public License Version 2 or later (the "GPL"), or
  27. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. #include "ecp_fp.h"
  39. #include <stdlib.h>
  40. #define ECFP_BSIZE 160
  41. #define ECFP_NUMDOUBLES 7
  42. #include "ecp_fpinc.c"
  43. /* Performs a single step of reduction, just on the uppermost float
  44. * (assumes already tidied), and then retidies. Note, this does not
  45. * guarantee that the result will be less than p, but truncates the number
  46. * of bits. */
  47. void
  48. ecfp160_singleReduce(double *d, const EC_group_fp * group)
  49. {
  50. double q;
  51. ECFP_ASSERT(group->doubleBitSize == 24);
  52. ECFP_ASSERT(group->primeBitSize == 160);
  53. ECFP_ASSERT(ECFP_NUMDOUBLES == 7);
  54. q = d[ECFP_NUMDOUBLES - 1] - ecfp_beta_160;
  55. q += group->bitSize_alpha;
  56. q -= group->bitSize_alpha;
  57. d[ECFP_NUMDOUBLES - 1] -= q;
  58. d[0] += q * ecfp_twom160;
  59. d[1] += q * ecfp_twom129;
  60. ecfp_positiveTidy(d, group);
  61. /* Assertions for the highest order term */
  62. ECFP_ASSERT(d[ECFP_NUMDOUBLES - 1] / ecfp_exp[ECFP_NUMDOUBLES - 1] ==
  63. (unsigned long long) (d[ECFP_NUMDOUBLES - 1] /
  64. ecfp_exp[ECFP_NUMDOUBLES - 1]));
  65. ECFP_ASSERT(d[ECFP_NUMDOUBLES - 1] >= 0);
  66. }
  67. /* Performs imperfect reduction. This might leave some negative terms,
  68. * and one more reduction might be required for the result to be between 0
  69. * and p-1. x should not already be reduced, i.e. should have
  70. * 2*ECFP_NUMDOUBLES significant terms. x and r can be the same, but then
  71. * the upper parts of r are not zeroed */
  72. void
  73. ecfp160_reduce(double *r, double *x, const EC_group_fp * group)
  74. {
  75. double x7, x8, q;
  76. ECFP_ASSERT(group->doubleBitSize == 24);
  77. ECFP_ASSERT(group->primeBitSize == 160);
  78. ECFP_ASSERT(ECFP_NUMDOUBLES == 7);
  79. /* Tidy just the upper bits, the lower bits can wait. */
  80. ecfp_tidyUpper(x, group);
  81. /* Assume that this is already tidied so that we have enough extra
  82. * bits */
  83. x7 = x[7] + x[13] * ecfp_twom129; /* adds bits 15-39 */
  84. /* Tidy x7, or we won't have enough bits later to add it in */
  85. q = x7 + group->alpha[8];
  86. q -= group->alpha[8];
  87. x7 -= q; /* holds bits 0-24 */
  88. x8 = x[8] + q; /* holds bits 0-25 */
  89. r[6] = x[6] + x[13] * ecfp_twom160 + x[12] * ecfp_twom129; /* adds
  90. * bits
  91. * 8-39 */
  92. r[5] = x[5] + x[12] * ecfp_twom160 + x[11] * ecfp_twom129;
  93. r[4] = x[4] + x[11] * ecfp_twom160 + x[10] * ecfp_twom129;
  94. r[3] = x[3] + x[10] * ecfp_twom160 + x[9] * ecfp_twom129;
  95. r[2] = x[2] + x[9] * ecfp_twom160 + x8 * ecfp_twom129; /* adds bits
  96. * 8-40 */
  97. r[1] = x[1] + x8 * ecfp_twom160 + x7 * ecfp_twom129; /* adds bits
  98. * 8-39 */
  99. r[0] = x[0] + x7 * ecfp_twom160;
  100. /* Tidy up just r[ECFP_NUMDOUBLES-2] so that the number of reductions
  101. * is accurate plus or minus one. (Rather than tidy all to make it
  102. * totally accurate, which is more costly.) */
  103. q = r[ECFP_NUMDOUBLES - 2] + group->alpha[ECFP_NUMDOUBLES - 1];
  104. q -= group->alpha[ECFP_NUMDOUBLES - 1];
  105. r[ECFP_NUMDOUBLES - 2] -= q;
  106. r[ECFP_NUMDOUBLES - 1] += q;
  107. /* Tidy up the excess bits on r[ECFP_NUMDOUBLES-1] using reduction */
  108. /* Use ecfp_beta so we get a positive result */
  109. q = r[ECFP_NUMDOUBLES - 1] - ecfp_beta_160;
  110. q += group->bitSize_alpha;
  111. q -= group->bitSize_alpha;
  112. r[ECFP_NUMDOUBLES - 1] -= q;
  113. r[0] += q * ecfp_twom160;
  114. r[1] += q * ecfp_twom129;
  115. /* Tidy the result */
  116. ecfp_tidyShort(r, group);
  117. }
  118. /* Sets group to use optimized calculations in this file */
  119. mp_err
  120. ec_group_set_secp160r1_fp(ECGroup *group)
  121. {
  122. EC_group_fp *fpg = NULL;
  123. /* Allocate memory for floating point group data */
  124. fpg = (EC_group_fp *) malloc(sizeof(EC_group_fp));
  125. if (fpg == NULL) {
  126. return MP_MEM;
  127. }
  128. fpg->numDoubles = ECFP_NUMDOUBLES;
  129. fpg->primeBitSize = ECFP_BSIZE;
  130. fpg->orderBitSize = 161;
  131. fpg->doubleBitSize = 24;
  132. fpg->numInts = (ECFP_BSIZE + ECL_BITS - 1) / ECL_BITS;
  133. fpg->aIsM3 = 1;
  134. fpg->ecfp_singleReduce = &ecfp160_singleReduce;
  135. fpg->ecfp_reduce = &ecfp160_reduce;
  136. fpg->ecfp_tidy = &ecfp_tidy;
  137. fpg->pt_add_jac_aff = &ecfp160_pt_add_jac_aff;
  138. fpg->pt_add_jac = &ecfp160_pt_add_jac;
  139. fpg->pt_add_jm_chud = &ecfp160_pt_add_jm_chud;
  140. fpg->pt_add_chud = &ecfp160_pt_add_chud;
  141. fpg->pt_dbl_jac = &ecfp160_pt_dbl_jac;
  142. fpg->pt_dbl_jm = &ecfp160_pt_dbl_jm;
  143. fpg->pt_dbl_aff2chud = &ecfp160_pt_dbl_aff2chud;
  144. fpg->precompute_chud = &ecfp160_precompute_chud;
  145. fpg->precompute_jac = &ecfp160_precompute_jac;
  146. group->point_mul = &ec_GFp_point_mul_wNAF_fp;
  147. group->points_mul = &ec_pts_mul_basic;
  148. group->extra1 = fpg;
  149. group->extra_free = &ec_GFp_extra_free_fp;
  150. ec_set_fp_precision(fpg);
  151. fpg->bitSize_alpha = ECFP_TWO160 * fpg->alpha[0];
  152. return MP_OKAY;
  153. }