Coverage Report

Created: 2023-09-25 06:34

/src/nettle-with-libgmp/ecc-mul-g.c
Line
Count
Source
1
/* ecc-mul-g.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 <assert.h>
39
40
#include "ecc.h"
41
#include "ecc-internal.h"
42
43
void
44
ecc_mul_g (const struct ecc_curve *ecc, mp_limb_t *r,
45
     const mp_limb_t *np, mp_limb_t *scratch)
46
2.48k
{
47
  /* Scratch need determined by the ecc_add_jja call. Current total is
48
     8 * ecc->p.size, at most 576 bytes. */
49
667k
#define tp scratch
50
133k
#define scratch_out (scratch + 3*ecc->p.size)
51
52
2.48k
  unsigned k, c;
53
2.48k
  unsigned i, j;
54
2.48k
  unsigned bit_rows;
55
56
2.48k
  int is_zero;
57
58
2.48k
  k = ecc->pippenger_k;
59
2.48k
  c = ecc->pippenger_c;
60
61
2.48k
  bit_rows = (ecc->p.bit_size + k - 1) / k;
62
  
63
2.48k
  mpn_zero (r, 3*ecc->p.size);
64
  
65
58.6k
  for (i = k, is_zero = 1; i-- > 0; )
66
56.1k
    {
67
56.1k
      ecc_dup_jj (ecc, r, r, scratch);
68
189k
      for (j = 0; j * c < bit_rows; j++)
69
133k
  {
70
133k
    unsigned bits;
71
    /* Avoid the mp_bitcnt_t type for compatibility with older GMP
72
       versions. */
73
133k
    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
948k
    for (bits = 0, bit_index = i + k*(c*j+c); bit_index > i + k*c*j; )
78
814k
      {
79
814k
        mp_size_t limb_index;
80
814k
        unsigned shift;
81
        
82
814k
        bit_index -= k;
83
84
814k
        limb_index = bit_index / GMP_NUMB_BITS;
85
814k
        if (limb_index >= ecc->p.size)
86
6.00k
    continue;
87
88
808k
        shift = bit_index % GMP_NUMB_BITS;
89
808k
        bits = (bits << 1) | ((np[limb_index] >> shift) & 1);
90
808k
      }
91
133k
    mpn_sec_tabselect (tp,
92
133k
           (ecc->pippenger_table
93
133k
            + (2*ecc->p.size * (mp_size_t) j << c)),
94
133k
            2*ecc->p.size, 1<<c, bits);
95
133k
    cnd_copy (is_zero, r, tp, 2*ecc->p.size);
96
133k
    cnd_copy (is_zero, r + 2*ecc->p.size, ecc->unit, ecc->p.size);
97
    
98
133k
    ecc_add_jja (ecc, tp, r, tp, scratch_out);
99
    /* Use the sum when valid. ecc_add_jja produced garbage if
100
       is_zero != 0 or bits == 0, . */    
101
133k
    cnd_copy (bits & (is_zero - 1), r, tp, 3*ecc->p.size);
102
133k
    is_zero &= (bits == 0);
103
133k
  }
104
56.1k
    }
105
2.48k
#undef tp
106
2.48k
#undef scratch_out
107
2.48k
}