Coverage Report

Created: 2023-03-26 07:33

/src/nettle/ecc-dup-th.c
Line
Count
Source (jump to first uncovered line)
1
/* ecc-dup-th.c
2
3
   Copyright (C) 2014, 2019 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
#if HAVE_CONFIG_H
33
# include "config.h"
34
#endif
35
36
#include "ecc.h"
37
#include "ecc-internal.h"
38
39
/* Double a point on a twisted Edwards curve, in homogeneous coordinates */
40
void
41
ecc_dup_th (const struct ecc_curve *ecc,
42
      mp_limb_t *r, const mp_limb_t *p,
43
      mp_limb_t *scratch)
44
0
{
45
0
#define x1 p
46
0
#define y1 (p + ecc->p.size)
47
0
#define z1 (p + 2*ecc->p.size)
48
49
0
#define x2 r
50
0
#define y2 (r + ecc->p.size)
51
0
#define z2 (r + 2*ecc->p.size)
52
53
  /* Formulas (from djb,
54
     http://www.hyperelliptic.org/EFD/g1p/auto-twisted-projective.html#doubling-dbl-2008-bbjlp):
55
56
     B = (X1+Y1)^2
57
     C = X1^2
58
     D = Y1^2
59
     (E = a*C = -C)
60
     F = E+D
61
     H = Z1^2
62
     J = F-2*H
63
     X3 = (B-C-D)*J
64
     Y3 = F*(E-D)
65
     Z3 = F*J         (-C+D)*(-C+D - 2Z1^2)
66
67
     In the formula for Y3, we have E - D = -(C+D). To avoid explicit
68
     negation, negate all of X3, Y3, Z3, and use
69
70
     Computation  Operation Live variables
71
72
     B = (X1+Y1)^2  sqr   B
73
     C = X1^2   sqr   B, C
74
     D = Y1^2   sqr   B, C, D
75
     F = -C+D       B, C, D, F
76
     H = Z1^2   sqr   B, C, D, F, H
77
     J = 2*H - F      B, C, D, F, J
78
     X3 = (B-C-D)*J mul   C, F, J  (Replace C <-- C+D)
79
     Y3 = F*(C+D) mul   F, J
80
     Z3 = F*J   mul
81
82
     3M+4S
83
  */
84
85
0
#define C scratch
86
0
#define D (scratch + 1*ecc->p.size)
87
0
#define B (scratch + 2*ecc->p.size)
88
89
0
#define F C
90
91
0
  ecc_mod_sqr (&ecc->p, C, x1, C);  /* C */
92
0
  ecc_mod_sqr (&ecc->p, D, y1, D);  /* C, D */
93
0
  ecc_mod_add (&ecc->p, B, x1, y1);
94
0
  ecc_mod_sqr (&ecc->p, B, B, x2);  /* C, D, B */
95
96
  /* C+D stored at y' */
97
0
  ecc_mod_add (&ecc->p, y2, C, D);
98
  /* B - C - C stored at x' */
99
0
  ecc_mod_sub (&ecc->p, x2, B, y2);
100
101
0
  ecc_mod_sub (&ecc->p, F, D, C);  /* F */
102
103
  /* Use D as scratch for the following multiplies. */
104
0
  ecc_mod_mul (&ecc->p, y2, y2, F, D);
105
106
  /* H and J stored at z' */
107
0
  ecc_mod_sqr (&ecc->p, z2, z1, D);
108
0
  ecc_mod_add (&ecc->p, z2, z2, z2);
109
0
  ecc_mod_sub (&ecc->p, z2, z2, F);
110
0
  ecc_mod_mul (&ecc->p, x2, x2, z2, D);
111
0
  ecc_mod_mul (&ecc->p, z2, z2, F, D);
112
0
}