Coverage Report

Created: 2024-11-25 06:29

/src/nettle/ecc-mul-g-eh.c
Line
Count
Source
1
/* ecc-mul-g-eh.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 <assert.h>
39
40
#include "ecc.h"
41
#include "ecc-internal.h"
42
43
void
44
ecc_mul_g_eh (const struct ecc_curve *ecc, mp_limb_t *r,
45
        const mp_limb_t *np, mp_limb_t *scratch)
46
17.6k
{
47
  /* Scratch need determined by the ecc_add_eh call. Current total is
48
     7 * ecc->p.size, at most 392 bytes (for curve448). */
49
2.10M
#define tp scratch
50
1.05M
#define scratch_out (scratch + 3*ecc->p.size)
51
52
17.6k
  unsigned k, c;
53
17.6k
  unsigned i, j;
54
17.6k
  unsigned bit_rows;
55
56
17.6k
  k = ecc->pippenger_k;
57
17.6k
  c = ecc->pippenger_c;
58
59
17.6k
  bit_rows = (ecc->p.bit_size + k - 1) / k;
60
61
  /* x = 0, y = 1, z = 1 */
62
17.6k
  mpn_zero (r, 3*ecc->p.size);
63
17.6k
  r[ecc->p.size] = r[2*ecc->p.size] = 1;
64
65
443k
  for (i = k; i-- > 0; )
66
425k
    {
67
425k
      ecc->dup (ecc, r, r, scratch);
68
1.47M
      for (j = 0; j * c < bit_rows; j++)
69
1.05M
  {
70
1.05M
    unsigned bits;
71
    /* Avoid the mp_bitcnt_t type for compatibility with older GMP
72
       versions. */
73
1.05M
    unsigned bit_index;
74
    
75
    /* Extract c bits from n, stride k, starting at i + kcj,
76
       ending at i + k (cj + c - 1)*/
77
7.36M
    for (bits = 0, bit_index = i + k*(c*j+c); bit_index > i + k*c*j; )
78
6.30M
      {
79
6.30M
        mp_size_t limb_index;
80
6.30M
        unsigned shift;
81
        
82
6.30M
        bit_index -= k;
83
84
6.30M
        limb_index = bit_index / GMP_NUMB_BITS;
85
6.30M
        if (limb_index >= ecc->p.size)
86
141k
    continue;
87
88
6.16M
        shift = bit_index % GMP_NUMB_BITS;
89
6.16M
        bits = (bits << 1) | ((np[limb_index] >> shift) & 1);
90
6.16M
      }
91
1.05M
    mpn_sec_tabselect (tp,
92
1.05M
           (ecc->pippenger_table
93
1.05M
            + (2*ecc->p.size * (mp_size_t) j << c)),
94
1.05M
            2*ecc->p.size, 1<<c, bits);
95
96
1.05M
    ecc->add_hh (ecc, r, r, tp, scratch_out);
97
1.05M
  }
98
425k
    }
99
17.6k
#undef tp
100
17.6k
#undef scratch_out
101
17.6k
}