Coverage Report

Created: 2024-11-21 07:03

/src/mpdecimal-4.0.0/libmpdec/numbertheory.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2008-2024 Stefan Krah. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions
6
 * are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
28
#include <assert.h>
29
#include <stdlib.h>
30
31
#include "bits.h"
32
#include "numbertheory.h"
33
#include "mpdecimal.h"
34
#include "umodarith.h"
35
36
37
/* Bignum: Initialize the Number Theoretic Transform. */
38
39
/*
40
 * Return the nth root of unity in F(p). This corresponds to e**((2*pi*i)/n)
41
 * in the Fourier transform. We have w**n == 1 (mod p).
42
 *    n := transform length.
43
 *    sign := -1 for forward transform, 1 for backward transform.
44
 *    modnum := one of {P1, P2, P3}.
45
 */
46
mpd_uint_t
47
_mpd_getkernel(mpd_uint_t n, int sign, int modnum)
48
0
{
49
0
    mpd_uint_t umod, p, r, xi;
50
#ifdef PPRO
51
    double dmod;
52
    uint32_t dinvmod[3];
53
#endif
54
55
0
    SETMODULUS(modnum);
56
0
    r = mpd_roots[modnum]; /* primitive root of F(p) */
57
0
    p = umod;
58
0
    xi = (p-1) / n;
59
60
0
    if (sign == -1)
61
0
        return POWMOD(r, (p-1-xi));
62
0
    else
63
0
        return POWMOD(r, xi);
64
0
}
65
66
/*
67
 * Initialize and return transform parameters.
68
 *    n := transform length.
69
 *    sign := -1 for forward transform, 1 for backward transform.
70
 *    modnum := one of {P1, P2, P3}.
71
 */
72
struct fnt_params *
73
_mpd_init_fnt_params(mpd_size_t n, int sign, int modnum)
74
0
{
75
0
    struct fnt_params *tparams;
76
0
    mpd_uint_t umod;
77
#ifdef PPRO
78
    double dmod;
79
    uint32_t dinvmod[3];
80
#endif
81
0
    mpd_uint_t kernel, w;
82
0
    mpd_uint_t i;
83
0
    mpd_size_t nhalf;
84
85
0
    assert(ispower2(n));
86
0
    assert(sign == -1 || sign == 1);
87
0
    assert(P1 <= modnum && modnum <= P3);
88
89
0
    nhalf = n/2;
90
0
    tparams = mpd_sh_alloc(sizeof *tparams, nhalf, sizeof (mpd_uint_t));
91
0
    if (tparams == NULL) {
92
0
        return NULL;
93
0
    }
94
95
0
    SETMODULUS(modnum);
96
0
    kernel = _mpd_getkernel(n, sign, modnum);
97
98
0
    tparams->modnum = modnum;
99
0
    tparams->modulus = umod;
100
0
    tparams->kernel = kernel;
101
102
    /* wtable[] := w**0, w**1, ..., w**(nhalf-1) */
103
0
    w = 1;
104
0
    for (i = 0; i < nhalf; i++) {
105
0
        tparams->wtable[i] = w;
106
0
        w = MULMOD(w, kernel);
107
0
    }
108
109
0
    return tparams;
110
0
}
111
112
/* Initialize wtable of size three. */
113
void
114
_mpd_init_w3table(mpd_uint_t w3table[3], int sign, int modnum)
115
0
{
116
0
    mpd_uint_t umod;
117
#ifdef PPRO
118
    double dmod;
119
    uint32_t dinvmod[3];
120
#endif
121
0
    mpd_uint_t kernel;
122
123
0
    SETMODULUS(modnum);
124
0
    kernel = _mpd_getkernel(3, sign, modnum);
125
126
0
    w3table[0] = 1;
127
0
    w3table[1] = kernel;
128
0
    w3table[2] = POWMOD(kernel, 2);
129
0
}