Path: blob/main/Modules/_decimal/libmpdec/numbertheory.c
12 views
/*1* Copyright (c) 2008-2020 Stefan Krah. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/262728#include "mpdecimal.h"2930#include <assert.h>31#include <stdlib.h>3233#include "bits.h"34#include "numbertheory.h"35#include "umodarith.h"363738/* Bignum: Initialize the Number Theoretic Transform. */394041/*42* Return the nth root of unity in F(p). This corresponds to e**((2*pi*i)/n)43* in the Fourier transform. We have w**n == 1 (mod p).44* n := transform length.45* sign := -1 for forward transform, 1 for backward transform.46* modnum := one of {P1, P2, P3}.47*/48mpd_uint_t49_mpd_getkernel(mpd_uint_t n, int sign, int modnum)50{51mpd_uint_t umod, p, r, xi;52#ifdef PPRO53double dmod;54uint32_t dinvmod[3];55#endif5657SETMODULUS(modnum);58r = mpd_roots[modnum]; /* primitive root of F(p) */59p = umod;60xi = (p-1) / n;6162if (sign == -1)63return POWMOD(r, (p-1-xi));64else65return POWMOD(r, xi);66}6768/*69* Initialize and return transform parameters.70* n := transform length.71* sign := -1 for forward transform, 1 for backward transform.72* modnum := one of {P1, P2, P3}.73*/74struct fnt_params *75_mpd_init_fnt_params(mpd_size_t n, int sign, int modnum)76{77struct fnt_params *tparams;78mpd_uint_t umod;79#ifdef PPRO80double dmod;81uint32_t dinvmod[3];82#endif83mpd_uint_t kernel, w;84mpd_uint_t i;85mpd_size_t nhalf;8687assert(ispower2(n));88assert(sign == -1 || sign == 1);89assert(P1 <= modnum && modnum <= P3);9091nhalf = n/2;92tparams = mpd_sh_alloc(sizeof *tparams, nhalf, sizeof (mpd_uint_t));93if (tparams == NULL) {94return NULL;95}9697SETMODULUS(modnum);98kernel = _mpd_getkernel(n, sign, modnum);99100tparams->modnum = modnum;101tparams->modulus = umod;102tparams->kernel = kernel;103104/* wtable[] := w**0, w**1, ..., w**(nhalf-1) */105w = 1;106for (i = 0; i < nhalf; i++) {107tparams->wtable[i] = w;108w = MULMOD(w, kernel);109}110111return tparams;112}113114/* Initialize wtable of size three. */115void116_mpd_init_w3table(mpd_uint_t w3table[3], int sign, int modnum)117{118mpd_uint_t umod;119#ifdef PPRO120double dmod;121uint32_t dinvmod[3];122#endif123mpd_uint_t kernel;124125SETMODULUS(modnum);126kernel = _mpd_getkernel(3, sign, modnum);127128w3table[0] = 1;129w3table[1] = kernel;130w3table[2] = POWMOD(kernel, 2);131}132133134