Coverage Report

Created: 2024-06-28 06:39

/src/nettle-with-libgmp/ecc-add-jja.c
Line
Count
Source
1
/* ecc-add-jj.c
2
3
   Copyright (C) 2013 Niels Möller
4
5
   This file is part of GNU Nettle.
6
7
   GNU Nettle is free software: you can redistribute it and/or
8
   modify it under the terms of either:
9
10
     * the GNU Lesser General Public License as published by the Free
11
       Software Foundation; either version 3 of the License, or (at your
12
       option) any later version.
13
14
   or
15
16
     * the GNU General Public License as published by the Free
17
       Software Foundation; either version 2 of the License, or (at your
18
       option) any later version.
19
20
   or both in parallel, as here.
21
22
   GNU Nettle is distributed in the hope that it will be useful,
23
   but WITHOUT ANY WARRANTY; without even the implied warranty of
24
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
   General Public License for more details.
26
27
   You should have received copies of the GNU General Public License and
28
   the GNU Lesser General Public License along with this program.  If
29
   not, see http://www.gnu.org/licenses/.
30
*/
31
32
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
33
34
#if HAVE_CONFIG_H
35
# include "config.h"
36
#endif
37
38
#include "ecc.h"
39
#include "ecc-internal.h"
40
41
/* NOTE: Behaviour for corner cases:
42
43
   + p = 0   ==>  r = 0 (invalid except if also q = 0)
44
45
   + q = 0   ==>  r = invalid
46
47
   + p = -q  ==>  r = 0, correct!
48
49
   + p = q   ==>  r = 0, invalid
50
*/
51
52
void
53
ecc_add_jja (const struct ecc_curve *ecc,
54
       mp_limb_t *r, const mp_limb_t *p, const mp_limb_t *q,
55
       mp_limb_t *scratch)
56
148k
{
57
296k
#define x1  p
58
296k
#define y1 (p + ecc->p.size)
59
445k
#define z1 (p + 2*ecc->p.size)
60
148k
#define x2  q
61
148k
#define y2 (q + ecc->p.size)
62
63
742k
#define x3  r
64
593k
#define y3 (r + ecc->p.size)
65
1.03M
#define z3 (r + 2*ecc->p.size)
66
67
  /* Formulas, from djb,
68
     http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b):
69
70
     Computation    Operation Live variables
71
     
72
      ZZ = Z_1^2    sqr   ZZ
73
      H = X_2*ZZ - X_1    mul (djb: U_2)  ZZ, H
74
      HH = H^2      sqr   ZZ, H, HH
75
      ZZZ = ZZ*Z_1    mul   ZZ, H, HH, ZZZ
76
      Z_3 = (Z_1+H)^2-ZZ-HH sqr   H, HH, ZZZ
77
      W = 2 (Y_2*ZZZ - Y_1) mul (djb: S_2)  H, HH, W
78
      I = 4*HH          H, W, I
79
      J = H*I     mul   W, I, J
80
      V = X_1*I     mul   W, J, V
81
      X_3 = W^2-J-2*V   sqr   W, J, V
82
      Y_3 = W*(V-X_3)-2*Y_1*J mul, mul
83
  */
84
1.92M
#define zz  scratch
85
1.63M
#define h  (scratch + ecc->p.size)
86
1.48M
#define w (scratch + 2*ecc->p.size)
87
445k
#define hh zz
88
445k
#define i zz
89
445k
#define v zz
90
742k
#define j h
91
1.63M
#define tp (scratch + 3*ecc->p.size)
92
93
  /* zz */
94
148k
  ecc_mod_sqr (&ecc->p, zz, z1, tp);  /* zz */
95
  /* h*/
96
148k
  ecc_mod_mul (&ecc->p, h, x2, zz, tp);  /* zz, h */
97
148k
  ecc_mod_sub (&ecc->p, h, h, x1);
98
  /* Do z^3 early, store at w. */
99
148k
  ecc_mod_mul (&ecc->p, w, zz, z1, tp);  /* zz, h, w */
100
  /* z_3 */
101
148k
  ecc_mod_add (&ecc->p, z3, z1, h);
102
148k
  ecc_mod_sqr (&ecc->p, z3, z3, tp);
103
148k
  ecc_mod_sub (&ecc->p, z3, z3, zz);  /* h, w */
104
  /* hh */
105
148k
  ecc_mod_sqr (&ecc->p, hh, h, tp);  /* h, w, hh */
106
148k
  ecc_mod_sub (&ecc->p, z3, z3, hh);
107
  
108
  /* w */
109
148k
  ecc_mod_mul (&ecc->p, w, y2, w, tp);
110
148k
  ecc_mod_sub (&ecc->p, w, w, y1);
111
148k
  ecc_mod_add (&ecc->p, w, w, w);
112
  
113
  /* i replaces hh */
114
148k
  ecc_mod_mul_1 (&ecc->p, i, hh, 4); /* h, w, i */
115
  /* j replaces h */
116
148k
  ecc_mod_mul (&ecc->p, j, i, h, tp);  /* w, i, j */
117
118
  /* v replaces i */
119
148k
  ecc_mod_mul (&ecc->p, v, x1, i, tp);
120
121
  /* x_3 */
122
148k
  ecc_mod_sqr (&ecc->p, x3, w, tp);
123
148k
  ecc_mod_sub (&ecc->p, x3, x3, j);
124
148k
  ecc_mod_submul_1 (&ecc->p, x3, v, 2);
125
126
  /* y_3 */
127
148k
  ecc_mod_mul (&ecc->p, j, y1, j, tp);
128
148k
  ecc_mod_sub (&ecc->p, y3, v, x3);
129
148k
  ecc_mod_mul (&ecc->p, y3, y3, w, tp);
130
148k
  ecc_mod_submul_1 (&ecc->p, y3, j, 2);
131
148k
}