1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*============================================================================
Copyright 2006 William Hart
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
============================================================================*/
// -------------------------------------------------------
//
// TonelliShanks.cpp
//
// Provides Tonelli-Shanks square root mod p, and mod p^k
//
// -------------------------------------------------------
#include <gmp.h>
#include "TonelliShanks.h"
#include "ModuloArith.h" //need multiplication mod p
mpz_t two; //variables for sqrtmod
mpz_t p1;
mpz_t b;
mpz_t g;
mpz_t xsq;
mpz_t mk;
mpz_t bpow;
mpz_t gpow;
mpz_t inv; //variables for sqrtmodpow
mpz_t tempsqpow;
mpz_t pk; //variable for sqrtmodpk
void TonelliInit(void)
{
mpz_init(two);
mpz_init(p1);
mpz_init(b);
mpz_init(g);
mpz_init(xsq);
mpz_init(mk);
mpz_init(bpow);
mpz_init(gpow);
mpz_init(inv);
mpz_init(tempsqpow);
mpz_init(pk);
return;
}
int32_t sqrtmod(mpz_t asqrt, mpz_t a, mpz_t p)
{
int32_t r,k,m;
if (mpz_kronecker(a,p)!=1)
{
mpz_set_ui(asqrt,0);
return 0; //return 0 if a is not a square mod p
}
mpz_set_ui(two,2);
mpz_sub_ui(p1,p,1);
r = mpz_remove(p1,p1,two);
mpz_powm(b,a,p1,p);
for (k=2; ;k++)
{
if (mpz_ui_kronecker(k,p) == -1) break;
}
mpz_set_ui(mk,k);
mpz_powm(g,mk,p1,p);
mpz_add_ui(p1,p1,1);
mpz_divexact_ui(p1,p1,2);
mpz_powm(xsq,a,p1,p);
if (!mpz_cmp_ui(b,1))
{
mpz_set(asqrt,xsq);
return 1;
}
while (mpz_cmp_ui(b,1))
{
mpz_set(bpow,b);
for (m=1; (m<=r-1) && (mpz_cmp_ui(bpow,1));m++)
{
mpz_powm_ui(bpow,bpow,2,p);
}
mpz_set(gpow,g);
for (int32_t i = 1;i<= r-m-1;i++)
{
mpz_powm_ui(gpow,gpow,2,p);
};
modmul(xsq,xsq,gpow,p);
mpz_powm_ui(gpow,gpow,2,p);
modmul(b,b,gpow,p);
mpz_set(gpow,g);
r = m;
}
mpz_set(asqrt,xsq);
return 1;
}
inline void sqrtmodpow(mpz_t res, mpz_t z, mpz_t a, mpz_t pk)
{
mpz_mul_ui(inv,z,2);
mpz_invert(inv,inv,pk);
mpz_set(tempsqpow,a);
mpz_submul(tempsqpow,z,z);
mpz_fdiv_r(tempsqpow,tempsqpow,pk);
modmul(tempsqpow,tempsqpow,inv,pk);
mpz_add(tempsqpow,tempsqpow,z);
mpz_set(res,tempsqpow);
return;
}
void sqrtmodpk(mpz_t res, mpz_t z, mpz_t a, mpz_t p, int32_t k)
{
mpz_set(res,z);
mpz_set(pk,p);
for (int32_t i = 2;i<=k;i++)
{
mpz_mul(pk,pk,p);
sqrtmodpow(res,res,a,pk);
}
return;
}
|