forked from flintlib/flint
/
unity_zp_pow_2k.c
168 lines (138 loc) · 4.49 KB
/
unity_zp_pow_2k.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*=============================================================================
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
=============================================================================*/
/******************************************************************************
Copyright (C) 2015 Vladimir Glazachev
******************************************************************************/
#include "aprcl.h"
/*
returns smallest integer k satisfies:
log(n) < (k * (k + 1) * 2^(2 * k)) / (2^(k + 1) - k - 2) + 1
*/
ulong
_unity_zp_pow_2k_find_k(const fmpz_t n)
{
ulong bits;
bits = fmpz_bits(n);
if (bits <= 8) return 1;
if (bits <= 24) return 2;
if (bits <= 69) return 3;
if (bits <= 196) return 4;
if (bits <= 538) return 5;
if (bits <= 1433) return 6;
if (bits <= 3714) return 7;
if (bits <= 9399) return 8;
if (bits <= 23290) return 9;
if (bits <= 56651) return 10;
return 11;
}
void
unity_zp_pow_2k_fmpz(unity_zp f, const unity_zp g, const fmpz_t pow)
{
ulong j, k, pow2k;
slong i, e;
fmpz_t digit;
unity_zp temp;
unity_zp *g_powers;
fmpz_init(digit);
unity_zp_init(temp, f->p, f->exp, f->n);
/* g_sqr = g * g */
unity_zp_sqr(temp, g);
/* selects optimal k value for n */
k = _unity_zp_pow_2k_find_k(pow);
/* selects e such that 2^(ek) < n < 2^((e + 1) * k) */
e = (fmpz_bits(pow) - 1) / k;
/* computes 2^k */
pow2k = 1 << k;
/*
g_powers store odd powers of g up to 2^k - 1;
g_powers[(i + 1) / 2] = g^i
*/
pow2k = 1 << (k - 1);
g_powers = (unity_zp*) flint_malloc(sizeof(unity_zp) * (pow2k + 1));
/* sets g_powers[0] = 1 */
unity_zp_init(g_powers[0], f->p, f->exp, f->n);
unity_zp_coeff_set_ui(g_powers[0], 0, 1);
/* sets g_powers[1] = g */
unity_zp_init(g_powers[1], f->p, f->exp, f->n);
unity_zp_copy(g_powers[1], g);
/* sets g_powers[i] = g^2 * g_powers[i - 1] */
for (i = 2; i <= pow2k; i++)
{
unity_zp_init(g_powers[i], f->p, f->exp, f->n);
unity_zp_mul(g_powers[i], g_powers[i - 1], temp);
}
/* for all digits[i] */
for (i = e; i >= 0; i--)
{
/*
digit contains i-th digit of pow in k-ary base;
k <= 11 so digit < 2^11 and fit into ulong
*/
fmpz_fdiv_q_2exp(digit, pow, i * k);
fmpz_fdiv_r_2exp(digit, digit, k);
/* if digit == 0 set f = f^(2^k) */
if (*digit == 0)
{
for (j = 0; j < k; j++)
{
/* sets f = f^2 */
unity_zp_sqr(temp, f);
unity_zp_swap(temp, f);
}
}
else
{
ulong t, b;
/* digit = 2^t * b and b is odd */
t = p_power_in_q(*digit, 2);
b = *digit / (1 << t);
if (i == e)
{
unity_zp_copy(f, g_powers[(b + 1) / 2]);
}
else
{
/* sets f = f^(2^(k - t)) */
for (j = 0; j < k - t; j++)
{
unity_zp_sqr(temp, f);
unity_zp_swap(temp, f);
}
/* sets f = f * g^b */
unity_zp_mul(temp, f, g_powers[(b + 1) / 2]);
unity_zp_swap(temp, f);
}
/* sets f = f^(2^t) */
for (j = 0; j < t; j++)
{
unity_zp_sqr(temp, f);
unity_zp_swap(temp, f);
}
}
}
for (i = 0; i <= pow2k; i++)
unity_zp_clear(g_powers[i]);
flint_free(g_powers);
fmpz_clear(digit);
unity_zp_clear(temp);
}
void
unity_zp_pow_2k_ui(unity_zp f, const unity_zp g, ulong pow)
{
fmpz_t p;
fmpz_init_set_ui(p, pow);
unity_zp_pow_2k_fmpz(f, g, p);
fmpz_clear(p);
}