arb/dlog/power.c

38 lines
881 B
C
Raw Permalink Normal View History

2016-09-06 15:43:12 +02:00
/*
2016-03-07 09:55:19 +01:00
Copyright (C) 2016 Pascal Molin
2016-09-06 15:43:12 +02:00
This file is part of Arb.
Arb is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version. See <http://www.gnu.org/licenses/>.
*/
2016-03-07 09:55:19 +01:00
#include "dlog.h"
ulong
dlog_power(const dlog_power_t t, ulong b)
{
int k;
ulong x, pk[30]; /* 3^30*2+1, 2^30*3+1 are primes */
2016-03-07 09:55:19 +01:00
pk[0] = 1;
2016-03-29 12:52:26 +01:00
2016-03-07 09:55:19 +01:00
for (k = 1; k < t->e; k++)
pk[k] = pk[k-1] * t->p;
2016-03-29 12:52:26 +01:00
2016-03-07 09:55:19 +01:00
x = 0;
2016-03-29 12:52:26 +01:00
2016-03-07 09:55:19 +01:00
for(k = 0; k < t->e; k++)
{
ulong bk, xk;
bk = nmod_pow_ui(b, pk[t->e-1-k], t->mod);
2016-03-07 09:55:19 +01:00
xk = dlog_precomp(t->pre, bk);
b = nmod_mul(b, nmod_pow_ui(t->apk[k], xk, t->mod), t->mod);
2016-03-07 09:55:19 +01:00
x += xk * pk[k]; /* cannot overflow */
}
2016-03-29 12:52:26 +01:00
2016-03-07 09:55:19 +01:00
return x;
}