Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
test_fp.c
Go to the documentation of this file.
1#include <fp.h>
2#include "test_declarations.h"
3#include "test_utils.h"
4#include <stdio.h>
5
6static MunitResult sample(MUNIT_UNUSED const MunitParameter params[], MUNIT_UNUSED void *user_data_or_fixture) {
7 fp_t seed, rnd, aux, one;
8 uint8_t value_equality, expected_value;
9 uint32_t num_checks = 10000;
11 fp_set_to_zero(rnd);
12 fp_set_to_zero(aux);
13 fp_set_to_one(one);
14
15 assert_memory_not_equal(FIELD_BYTES, seed, rnd); // Random value different from zero
16
17 fp_copy(aux, FIELD_CHARACTERISTIC);
18 assert_memory_equal(FIELD_BYTES, aux, FIELD_CHARACTERISTIC);
19
20 aux[FIELD_64BITS_WORDS - 1]++;
21 value_equality = fp_is_smaller(aux, FIELD_CHARACTERISTIC);
22 expected_value = 0x00;
23 assert_memory_equal(sizeof(uint8_t), &expected_value, &value_equality);
24
25 aux[FIELD_64BITS_WORDS - 1] -= 2;
26 value_equality = fp_is_smaller(aux, FIELD_CHARACTERISTIC);
27 expected_value = 0x01;
28 assert_memory_equal(sizeof(uint8_t), &expected_value, &value_equality);
29
30 for (uint32_t i = 0; i < num_checks; i++) {
31 fp_sample(rnd);
32 value_equality = fp_is_smaller(rnd, FIELD_CHARACTERISTIC);
33 assert_memory_equal(sizeof(uint8_t), &expected_value, &value_equality);
34 }
35
36
37 return MUNIT_OK;
38}
39
40
41static MunitResult is_zero(MUNIT_UNUSED const MunitParameter params[], MUNIT_UNUSED void *user_data_or_fixture) {
42
43 fp_t a;
45 assert_true(fp_is_zero(a));
47 assert_false(fp_is_zero(a));
48
49 return MUNIT_OK;
50}
51
52
53static MunitResult add_and_sub(MUNIT_UNUSED const MunitParameter params[],
54 MUNIT_UNUSED void *user_data_or_fixture) {
55 fp_t a, b, c, d, e, f;
56
61
62 assert_memory_not_equal(FIELD_64BITS_WORDS, a, d); // Random value different from zero
63
64 fp_add(c, a, b);
65 assert_memory_equal(FIELD_64BITS_WORDS, a, c); // a + 0 = a
67 fp_add(c, b, a);
68 assert_memory_equal(FIELD_64BITS_WORDS, a, c); // 0 + a = a
69
70 fp_neg(b, a);
71 assert_memory_not_equal(FIELD_64BITS_WORDS, a, b); // -a != a
72 fp_add(c, a, b);
73 assert_memory_equal(FIELD_64BITS_WORDS, c, d); // a - a = 0
74 fp_add(c, b, a);
75 assert_memory_equal(FIELD_64BITS_WORDS, c, d); // (-a) + a = 0
76
77 fp_add(b, a, a);
78 fp_half(c, b);
79 assert_memory_equal(FIELD_64BITS_WORDS, c, a); // (2a)/2 = a
80
82 assert_memory_not_equal(FIELD_64BITS_WORDS, b, d); // Random value different from zero
83
84 fp_add(c, a, b);
85 fp_sub(d, a, b);
86 assert_memory_not_equal(FIELD_64BITS_WORDS, b, d); // a + b != a - b
87
88 fp_sub(e, c, b);
89 assert_memory_equal(FIELD_64BITS_WORDS, e, a); // (a + b) - b = a
90
91 fp_add(f, d, b);
92 assert_memory_equal(FIELD_64BITS_WORDS, f, a); // (a - b) + b = a
93
94 fp_add(e, c, d);
95 fp_half(e, e);
96 assert_memory_equal(FIELD_64BITS_WORDS, e, a); // ([a + b] + [a - b])/2 = a
97
98 fp_sub(f, c, d);
99 fp_half(f, f);
100 assert_memory_equal(FIELD_64BITS_WORDS, f, b); // ([a + b] - [a - b])/2 = b
101
102 fp_sub(c, b, a);
103 fp_neg(c, c);
104 assert_memory_equal(FIELD_64BITS_WORDS, c, d); // (a - b) = -(b - a)
105
106 return MUNIT_OK;
107}
108
109
110static MunitResult mul_and_sqr(MUNIT_UNUSED const MunitParameter params[], MUNIT_UNUSED void *user_data_or_fixture) {
111 fp_t a, b, c, d;
112
117
118 a[0] = 1;
119 fp_copy(b, a);
120
121 fp_to_mont(b, b);
122 assert_memory_equal(FIELD_64BITS_WORDS, b, MONTGOMERY_CONSTANT_ONE); // 1 -> R
123
124 fp_from_mont(b, b);
125 assert_memory_equal(FIELD_64BITS_WORDS, b, a); // R -> 1
126
128 fp_mul(b, a, MONTGOMERY_CONSTANT_ONE);
129 assert_memory_equal(FIELD_64BITS_WORDS, b, a); // a * 1 = a
130
131 fp_mul(b, MONTGOMERY_CONSTANT_ONE, a);
132 assert_memory_equal(FIELD_64BITS_WORDS, b, a); // 1 * a = a
133
135 fp_mul(b, a, c);
136 assert_memory_equal(FIELD_64BITS_WORDS, b, c); // a * 0 = 0
137
138 fp_mul(b, c, a);
139 assert_memory_equal(FIELD_64BITS_WORDS, b, c); // 0 * a = 0
140
141 c[0] = 2;
142 fp_to_mont(c, c);
143 fp_add(b, a, a);
144 fp_mul(d, a, c);
145 assert_memory_equal(FIELD_64BITS_WORDS, b, d); // 2 * a = a + a
146
149 fp_sqr(b, b);
150 assert_memory_equal(FIELD_64BITS_WORDS, b, c); // 0^2 = 0
151
152 fp_set_to_one(b);
153 fp_sqr(b, b);
154 assert_memory_equal(FIELD_64BITS_WORDS, b, MONTGOMERY_CONSTANT_ONE); // 1^2 = 1
155
156 fp_sqr(b, a);
157 fp_mul(c, a, a);
158 assert_memory_equal(FIELD_64BITS_WORDS, b, c); // a * a = a^2
159
160 return MUNIT_OK;
161}
162
163
164static MunitResult inv(MUNIT_UNUSED const MunitParameter params[], MUNIT_UNUSED void *user_data_or_fixture) {
165
166 fp_t a, b, c;
167
171
172 assert_memory_not_equal(FIELD_64BITS_WORDS, a, c); // Random value different from zero
173
175 assert_memory_not_equal(FIELD_64BITS_WORDS, a, b);
176
177 fp_inv(b, a);
178 fp_mul(c, a, b);
179 assert_memory_equal(FIELD_64BITS_WORDS, c, MONTGOMERY_CONSTANT_ONE); // a * a⁻¹ = 1
180
181 fp_inv(b, b);
182 assert_memory_equal(FIELD_64BITS_WORDS, a, b); // (a⁻¹)⁻¹ = a
183
185 fp_inv(b, a);
186 assert_memory_equal(FIELD_64BITS_WORDS, a, b); // (1⁻¹) = 1
187
188 fp_mul(c, a, b);
189 assert_memory_equal(FIELD_64BITS_WORDS, c, MONTGOMERY_CONSTANT_ONE); // 1 * 1 = 1
190
191 return MUNIT_OK;
192}
193
194static MunitResult square_root(MUNIT_UNUSED const MunitParameter params[], MUNIT_UNUSED void *user_data_or_fixture) {
195
196 fp_t a, b, c, d;
197
202
203 assert_memory_not_equal(FIELD_64BITS_WORDS, a, c); // Random value different from zero
204 assert_memory_not_equal(FIELD_64BITS_WORDS, a, b);
205
206 // b = a^2
207 fp_sqr(b, a);
208
209 // c = sqrt(b)
210 fp_sqrt(c, b);
211
212 // d = c^2
213 fp_sqr(d, c);
214 assert_memory_equal(FIELD_64BITS_WORDS, b, d);
215
216 return MUNIT_OK;
217}
218
219static MunitResult cube_root(MUNIT_UNUSED const MunitParameter params[], MUNIT_UNUSED void *user_data_or_fixture) {
220
221 fp_t a, b, c, d;
222
223 if(FIELD_BITS == 381 || FIELD_BITS == 575 || FIELD_BITS == 765){
224 return MUNIT_SKIP;
225 }
226
231
232 assert_memory_not_equal(FIELD_64BITS_WORDS, a, c); // Random value different from zero
233 assert_memory_not_equal(FIELD_64BITS_WORDS, a, b);
234
235 // b = a^3
236 fp_sqr(b, a);
237 fp_mul(b, b, a);
238
239 // c = sqrt(b)
240 fp_curt(c, b);
241
242 // d = c^2
243 fp_sqr(d, c);
244 fp_mul(d, d, c);
245 assert_memory_equal(FIELD_64BITS_WORDS, b, d);
246
247 return MUNIT_OK;
248}
249
250/*
251 * Register test cases
252 */
253
255 TEST_CASE(sample),
256 TEST_CASE(is_zero),
257 TEST_CASE(add_and_sub),
258 TEST_CASE(mul_and_sqr),
259 TEST_CASE(inv),
260 TEST_CASE(square_root),
261 TEST_CASE(cube_root),
263};
264
#define fp_sqr
Definition fp-gmp.h:73
#define fp_sub
Definition fp-gmp.h:67
#define fp_mul
Definition fp-gmp.h:70
#define fp_inv
Definition fp-gmp.h:88
#define fp_add
Definition fp-gmp.h:64
#define fp_copy
Definition fp-gmp.h:79
int64_t fp_is_zero(const fp_t input)
Definition fp.c:112
void fp_sample(fp_t output)
Definition fp.c:51
void fp_curt(fp_t output, const fp_t input)
Definition fp.c:189
void fp_to_mont(fp_t output, const fp_t input)
Definition fp.c:99
void fp_neg(fp_t output, const fp_t input)
Definition fp.c:9
void fp_set_to_zero(fp_t input_output)
Definition fp.c:19
void fp_half(fp_t output, const fp_t input)
Definition fp.c:84
void fp_from_mont(fp_t output, const fp_t input)
Definition fp.c:104
void fp_set_to_one(fp_t input_output)
Definition fp.c:14
void fp_sqrt(fp_t output, const fp_t input)
Definition fp.c:168
uint8_t fp_is_smaller(const fp_t input1, const fp_t input2)
Definition fp.c:143
uint64_t fp_t[FIELD_64BITS_WORDS]
Definition fp.h:12
#define MUNIT_UNUSED
Definition munit.h:136
MunitResult
Definition munit.h:415
@ MUNIT_SKIP
Definition munit.h:421
@ MUNIT_OK
Definition munit.h:417
#define FIELD_BYTES
Definition p254.h:8
#define FIELD_64BITS_WORDS
Definition p254.h:9
#define FIELD_BITS
Definition p254.h:7
for i
MunitTest test_fp[]
Definition test_fp.c:254
#define TEST_END
Definition test_utils.h:11
#define TEST_CASE(test_func)
Definition test_utils.h:19
#define RANDOM_FP_ELEMENT(out)
Definition test_utils.h:38
f a
Definition to_model.m:12