summaryrefslogtreecommitdiff
path: root/ecc-mul-m.c
blob: f0ceb92476e978f9bdb686378f276f0dc2184de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* ecc-mul-m.c

   Point multiplication using Montgomery curve representation.

   Copyright (C) 2014 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>

#include "ecc.h"
#include "ecc-internal.h"

void
ecc_mul_m (const struct ecc_modulo *m,
	   mp_limb_t a24,
	   unsigned bit_low, unsigned bit_high,
	   mp_limb_t *qx, const uint8_t *n, const mp_limb_t *px,
	   mp_limb_t *scratch)
{
  unsigned i;
  mp_limb_t swap;

#define x2 (scratch)
#define z2 (scratch + m->size)
#define x3 (scratch + 2*m->size)
#define z3 (scratch + 3*m->size)

  /* Formulas from RFC 7748:

       A = x_2 + z_2
       AA = A^2
       B = x_2 - z_2
       BB = B^2
       E = AA - BB
       C = x_3 + z_3
       D = x_3 - z_3
       DA = D * A
       CB = C * B
       x_3 = (DA + CB)^2
       z_3 = x_1 * (DA - CB)^2
       x_2 = AA * BB
       z_2 = E * (AA + a24 * E)

     For pure doubling, we use:

       A = x_2 + z_2
       AA = A^2
       B = x_2 - z_2
       BB = B^2
       E = AA - BB
       x3 = AA * BB
       z3 =  E * (AA + a24 * E)
  */

#define A (scratch + 4*m->size)
#define AA A
#define D (scratch + 5*m->size)
#define DA D

#define tp (scratch + 6*m->size)

  /* For the doubling formulas. */
#define B D
#define BB D
#define E D

  /* Initialize, x2 = px, z2 = 1 */
  mpn_copyi (x2, px, m->size);
  z2[0] = 1;
  mpn_zero (z2+1, m->size - 1);

  /* Get x3, z3 from doubling. Since most significant bit is forced to 1. */
  ecc_mod_add (m, A, x2, z2);
  ecc_mod_sub (m, B, x2, z2);
  ecc_mod_sqr (m, AA, A, tp);
  ecc_mod_sqr (m, BB, B, tp);
  ecc_mod_mul (m, x3, AA, BB, tp);
  ecc_mod_sub (m, E, AA, BB);
  ecc_mod_addmul_1 (m, AA, E, a24);
  ecc_mod_mul (m, z3, E, AA, tp);

  for (i = bit_high, swap = 0; i >= bit_low; i--)
    {
      mp_limb_t bit = (n[i/8] >> (i & 7)) & 1;

      mpn_cnd_swap (swap ^ bit, x2, x3, 2*m->size);
      swap = bit;

      ecc_mod_add (m, A, x2, z2);
      ecc_mod_sub (m, D, x3, z3);
      ecc_mod_mul (m, DA, D, A, tp);
      ecc_mod_sqr (m, AA, A, tp);

      /* Store B, BB and E at z2 */
      ecc_mod_sub (m, z2, x2, z2);	/* B */
      /* Store C and CB at z3 */
      ecc_mod_add (m, z3, x3, z3);	/* C */
      ecc_mod_mul (m, z3, z3, z2, tp);	/* CB */
      ecc_mod_sqr (m, z2, z2, tp);	/* BB */

      /* Finish x2 */
      ecc_mod_mul (m, x2, AA, z2, tp);

      ecc_mod_sub (m, z2, AA, z2);	/* E */

      /* Finish z2 */
      ecc_mod_addmul_1 (m, AA, z2, a24);
      ecc_mod_mul (m, z2, z2, AA, tp);

      /* Finish x3 */
      ecc_mod_add (m, x3, DA, z3);
      ecc_mod_sqr (m, x3, x3, tp);

      /* Finish z3 */
      ecc_mod_sub (m, z3, DA, z3);	/* DA - CB */
      ecc_mod_sqr (m, z3, z3, tp);
      ecc_mod_mul (m, z3, z3, px, tp);
    }
  mpn_cnd_swap (swap, x2, x3, 2*m->size);

  /* Do the low zero bits, just duplicating x2 */
  for (i = 0; i < bit_low; i++)
    {
      ecc_mod_add (m, A, x2, z2);
      ecc_mod_sub (m, B, x2, z2);
      ecc_mod_sqr (m, AA, A, tp);
      ecc_mod_sqr (m, BB, B, tp);
      ecc_mod_mul (m, x2, AA, BB, tp);
      ecc_mod_sub (m, E, AA, BB);
      ecc_mod_addmul_1 (m, AA, E, a24);
      ecc_mod_mul (m, z2, E, AA, tp);
    }
  assert (m->invert_itch <= 7 * m->size);
  m->invert (m, x3, z2, z3 + m->size);
  ecc_mod_mul_canonical (m, qx, x2, x3, z3);
}