-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalar.c
205 lines (181 loc) · 3.26 KB
/
scalar.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* scalar.c
*
* Copyright 2006 Johan de Jong
*
* This file is part of Frobenius
*
* Frobenius 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.
*
* Frobenius 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 Frobenius; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* */
#include <stdio.h>
#include <stdlib.h>
#include "data.h"
#include "scalar.h"
mpz_t modulus;
mpz_t prime;
mpz_t temp;
#ifdef KIJKEN
void empty_function(void)
{
printf("This just for breakpoints when debugging.\n");
}
#endif
/* Only called once. */
void setup_scalars(void)
{
mpz_init_set_ui(prime, (unsigned long) p);
mpz_init(modulus);
mpz_init(temp);
mpz_ui_pow_ui(modulus, (unsigned long) p, (unsigned long) r);
}
void close_scalars(void )
{
mpz_clear(temp);
mpz_clear(modulus);
mpz_clear(prime);
}
void printmscalar(mscalar a)
{
mpz_cdiv_q_ui(temp, modulus, 2);
if (mpz_cmp(a, temp)>0) {
mpz_sub(temp, a, modulus);
mpz_out_str(stdout, (int) 10, temp);
} else {
mpz_out_str(stdout, (int) 10, a);
}
}
#ifdef PROFILER
void make_scalar(mscalar a)
{
mpz_init(a);
}
void free_scalar(mscalar a)
{
mpz_clear(a);
}
#endif
/*
unsigned long int valuation(mscalar x)
{
return(mpz_remove(temp,x,prime));
}
*/
/*
void sc_add(mscalar a, mscalar b, mscalar c)
{
mpz_add(c,a,b);
mpz_mod(c,c,modulus);
}
*/
/*
void sc_mult(mscalar a, mscalar b, mscalar c)
{
mpz_mul(c,a,b);
mpz_mod(c,c,modulus);
}
*/
/*
void sc_imult(int a, mscalar b, mscalar c)
{
mpz_mul_si(c,b,(long) a);
mpz_mod(c,c,modulus);
}
*/
/*
void sc_inv(mscalar a, mscalar b)
{
mpz_invert(b,a,modulus);
}
*/
/* Divides a by b. If b is not a unit then this assumes *
* valuation(a) >= valuation(b), and the result is lifted *
* to an integer mod p^r. */
/* Does not destroy a and b. */
void sc_div(mscalar a, mscalar b, mscalar c)
{
unsigned long e;
e = mpz_remove(temp, b, prime);
mpz_invert(temp, temp, modulus);
mpz_mul(temp, temp, a);
mpz_mod(c, temp, modulus);
while (e) {
mpz_divexact_ui(c, c, (unsigned long) p);
e--;
}
}
/* Divides a by p. The assumption is that this can be done. */
/*
void div_p(mscalar a)
{
mpz_divexact_ui(a,a,(unsigned long) p);
}
*/
/*
void sc_add_replace(mscalar a, mscalar b)
{
sc_add(a,b,b);
}
*/
/*
void sc_mult_replace(mscalar a, mscalar b)
{
sc_mult(a,b,b);
}
*/
/*
void sc_imult_replace(int a, mscalar b)
{
sc_imult(a,b,b);
}
*/
/*
void sc_zero(mscalar a)
{
mpz_set_ui(a,(unsigned long) 0);
}
*/
/*
void sc_one(mscalar a)
{
mpz_set_ui(a,(unsigned long) 1);
}
*/
/*
void sc_copy(mscalar a, mscalar b)
{
mpz_set(b,a);
}
*/
/*
void sc_negate(mscalar a)
{
mpz_neg(a,a);
mpz_mod(a,a,modulus);
}
*/
/*
void ito_sc(int a, mscalar b)
{
mpz_set_si(b,(long) a);
mpz_mod(b,b,modulus);
}
*/
/*
int sc_is_zero(mscalar a)
{
return(mpz_divisible_p(a,modulus));
}
*/