Coverage Report

Created: 2026-05-16 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nettle/ecc-point.c
Line
Count
Source
1
/* ecc-point.c
2
3
   Copyright (C) 2013, 2014 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
void
42
ecc_point_init (struct ecc_point *p, const struct ecc_curve *ecc)
43
93.1k
{
44
93.1k
  p->ecc = ecc;
45
93.1k
  p->p = gmp_alloc_limbs (2*ecc->p.size);
46
93.1k
}
47
48
void
49
ecc_point_clear (struct ecc_point *p)
50
93.1k
{
51
93.1k
  gmp_free_limbs (p->p, 2*p->ecc->p.size);
52
93.1k
}
53
54
int
55
ecc_point_set (struct ecc_point *p, const mpz_t x, const mpz_t y)
56
16.8k
{
57
16.8k
  mp_size_t size;  
58
16.8k
  mpz_t m, lhs, rhs;
59
16.8k
  mpz_t t;
60
16.8k
  int res;
61
62
16.8k
  size = p->ecc->p.size;
63
16.8k
  mpz_roinit_n (m, p->ecc->p.m, size);
64
  
65
16.8k
  if (mpz_sgn (x) < 0 || mpz_cmp (x, m) >= 0
66
16.8k
      || mpz_sgn (y) < 0 || mpz_cmp (y, m) >= 0)
67
131
    return 0;
68
69
16.7k
  mpz_init (lhs);
70
16.7k
  mpz_init (rhs);
71
72
16.7k
  mpz_mul (lhs, y, y);
73
  
74
16.7k
  if (p->ecc->p.bit_size == 255)
75
0
    {
76
      /* ed25519 special case. FIXME: Do in some cleaner way? */
77
0
      mpz_t x2;
78
0
      mpz_init (x2);
79
0
      mpz_mul (x2, x, x);
80
0
      mpz_mul (rhs, x2, lhs);
81
      /* Check that -x^2 + y^2 = 1 - (121665/121666) x^2 y^2
82
   or 121666 (1 + x^2 - y^2) = 121665 x^2 y^2 */
83
0
      mpz_sub (lhs, x2, lhs);
84
0
      mpz_add_ui (lhs, lhs, 1);
85
0
      mpz_mul_ui (lhs, lhs, 121666);
86
0
      mpz_mul_ui (rhs, rhs, 121665);
87
0
      mpz_clear (x2);
88
0
    }
89
16.7k
  else if (p->ecc->p.bit_size == 448)
90
0
    {
91
      /* curve448 special case. FIXME: Do in some cleaner way? */
92
0
      mpz_t x2, d;
93
0
      mpz_init (x2);
94
0
      mpz_init_set_ui (d, 39081);
95
0
      mpz_mul (x2, x, x); /* x^2 */
96
0
      mpz_mul (d, d, x2); /* 39081 x^2 */
97
0
      mpz_set_ui (rhs, 1);
98
0
      mpz_submul (rhs, d, lhs); /* 1 - 39081 x^2 y^2 */
99
      /* Check that x^2 + y^2 = 1 - 39081 x^2 y^2 */
100
0
      mpz_add (lhs, x2, lhs);  /* x^2 + y^2 */
101
0
      mpz_clear (d);
102
0
      mpz_clear (x2);
103
0
    }
104
16.7k
  else
105
16.7k
    {
106
      /* Check that y^2 = x^3 - 3*x + b (mod p) */
107
16.7k
      mpz_mul (rhs, x, x);
108
16.7k
      mpz_sub_ui (rhs, rhs, 3);
109
16.7k
      mpz_mul (rhs, rhs, x);
110
16.7k
      mpz_add (rhs, rhs, mpz_roinit_n (t, p->ecc->b, size));
111
16.7k
    }
112
113
16.7k
  res = mpz_congruent_p (lhs, rhs, mpz_roinit_n (t, p->ecc->p.m, size));
114
115
16.7k
  mpz_clear (lhs);
116
16.7k
  mpz_clear (rhs);
117
118
16.7k
  if (!res)
119
2.75k
    return 0;
120
121
14.0k
  mpz_limbs_copy (p->p, x, size);
122
14.0k
  mpz_limbs_copy (p->p + size, y, size);
123
124
14.0k
  return 1;
125
16.7k
}
126
127
void
128
ecc_point_get (const struct ecc_point *p, mpz_t x, mpz_t y)
129
36.9k
{
130
36.9k
  mp_size_t size = p->ecc->p.size;
131
36.9k
  if (x)
132
36.9k
    mpz_set_n (x, p->p, size);
133
36.9k
  if (y)
134
36.9k
    mpz_set_n (y, p->p + size, size);
135
36.9k
}