Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
fp.h File Reference
#include <stdint.h>
#include "rng.h"
#include "parameters.h"
Include dependency graph for fp.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef uint64_t fp_t[FIELD_64BITS_WORDS]

Functions

void fp_add (uint64_t *output, const uint64_t *input_a, const uint64_t *input_b)
void fp_sub (uint64_t *output, const uint64_t *input_a, const uint64_t *input_b)
void fp_mul (uint64_t *output, const uint64_t *input_a, const uint64_t *input_b)
void fp_sqr (uint64_t *output, const uint64_t *input_a)
void fp_neg (fp_t output, const fp_t input)
void fp_set_to_one (fp_t output)
void fp_set_to_zero (fp_t output)
void fp_copy (fp_t output, const fp_t input)
void fp_cset (fp_t output, const fp_t input, uint64_t input_mask)
void fp_cswap (fp_t input_a, fp_t input_b, uint64_t input)
void fp_sample (fp_t output)
void fp_inv (fp_t output, const fp_t input)
void fp_half (fp_t output, const fp_t input)
void fp_to_mont (fp_t output, const fp_t input)
void fp_from_mont (fp_t output, const fp_t input)
int64_t fp_is_zero (const fp_t input)
uint8_t fp_is_equal (const fp_t input_a, const fp_t input_b)
uint8_t fp_is_smaller (const fp_t input1, const fp_t input2)
uint8_t fp_is_square (const fp_t input)
void fp_sqrt (fp_t output, const fp_t input)
void fp_curt (fp_t output, const fp_t input)

Typedef Documentation

◆ fp_t

typedef uint64_t fp_t[FIELD_64BITS_WORDS]

Definition at line 12 of file fp.h.

Function Documentation

◆ fp_add()

void fp_add ( uint64_t * output,
const uint64_t * input_a,
const uint64_t * input_b )
extern

◆ fp_copy()

void fp_copy ( fp_t output,
const fp_t input )

Definition at line 24 of file fp.c.

24 {
25 memcpy(output, input, sizeof(fp_t));
26}
uint64_t fp_t[FIELD_64BITS_WORDS]
Definition fp.h:12

◆ fp_cset()

void fp_cset ( fp_t output,
const fp_t input,
uint64_t input_mask )

Definition at line 28 of file fp.c.

28 {
29 // If mask = 0 then output is not modified, else if input = 0xFF...FF then output <- input
30 uint64_t temp;
31 unsigned int i;
32
33 for (i = 0; i < FIELD_64BITS_WORDS; i++) {
34 temp = input_mask & (output[i] ^ input[i]);
35 output[i] = temp ^ output[i];
36 }
37}
#define FIELD_64BITS_WORDS
Definition p254.h:9
for i

References FIELD_64BITS_WORDS, and i.

Referenced by isogeny_walks_3_fp(), and isogeny_walks_get_points_3_fp().

Here is the caller graph for this function:

◆ fp_cswap()

void fp_cswap ( fp_t input_a,
fp_t input_b,
uint64_t input )

Definition at line 39 of file fp.c.

39 {
40 // If input = 0 then a <- a and b <- b, else if input = 0xFF...FF then a <- b and b <- a
41 uint64_t temp;
42 unsigned int i;
43
44 for (i = 0; i < FIELD_64BITS_WORDS; i++) {
45 temp = input & (input_a[i] ^ input_b[i]);
46 input_a[i] = temp ^ input_a[i];
47 input_b[i] = temp ^ input_b[i];
48 }
49}

References FIELD_64BITS_WORDS, and i.

◆ fp_curt()

void fp_curt ( fp_t output,
const fp_t input )

Definition at line 189 of file fp.c.

189 {
190 fp_t temp, input_1;
191 uint64_t flag;
192 int i, j;
193 // input_1 <- input ^ -([p - 2] / 3)
194 fp_set_to_one(input_1);
195 fp_copy(temp, input);
196 for (i = 0; i < FIELD_64BITS_WORDS; i++) {
197 flag = 1;
198 for (j = 0; j < 64; j++) {
199 if ((flag & CUBE_ROOT_EXPONENT_213[i]) != 0)
200 fp_mul(input_1, temp, input_1);
201 fp_sqr(temp, temp);
202 flag <<= 1;
203 }
204 }
205 fp_copy(output, input_1);
206}
#define fp_sqr
Definition fp-gmp.h:73
#define fp_mul
Definition fp-gmp.h:70
#define fp_copy
Definition fp-gmp.h:79
void fp_set_to_one(fp_t input_output)
Definition fp.c:14
for j

References FIELD_64BITS_WORDS, fp_copy, fp_mul, fp_set_to_one(), fp_sqr, i, and j.

Referenced by isogeny_walks_get_points_3_fp(), isogeny_walks_get_points_3_fp(), isogeny_walks_to_montgomery_model_3_fp(), and isogeny_walks_to_montgomery_model_3_fp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fp_from_mont()

void fp_from_mont ( fp_t output,
const fp_t input )

Definition at line 104 of file fp.c.

104 {
105 fp_t one = {0};
106 one[0] = 1;
107 fp_mul(output, input, one);
108}

References fp_mul.

Referenced by fp2_from_mont().

Here is the caller graph for this function:

◆ fp_half()

void fp_half ( fp_t output,
const fp_t input )

Definition at line 84 of file fp.c.

84 {
85 uint8_t carry = 0;
86 int i;
87 uint64_t mask;
88
89 mask = 0 - (uint64_t) (input[0] & 1);
90 // If a is odd compute a + p
91 for (i = 0; i < FIELD_64BITS_WORDS; i++) {
92 addition_with_carry_u64(output[i], carry, carry, input[i], FIELD_CHARACTERISTIC[i] & mask);
93 }
94
96}
void multiprecision_shift_to_right(uint64_t *input_a_output_shifted_a, uint64_t input_words_length)
Definition utilities.c:80
#define addition_with_carry_u64(output, output_carry, input_carry, input_a, input_b)
Definition utilities.h:34

References addition_with_carry_u64, FIELD_64BITS_WORDS, i, and multiprecision_shift_to_right().

Referenced by fp2_half().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fp_inv()

void fp_inv ( fp_t output,
const fp_t input )

Definition at line 62 of file fp.c.

62 {
63
64 fp_t temp, out;
65 fp_set_to_one(out);
66 fp_copy(temp, input);
67
68 int i, j, k;
69 uint64_t flag;
70
71 for (i = 0, k = 0; i < FIELD_64BITS_WORDS && k < FIELD_BITS; i++, k++) {
72 flag = 1;
73 for (j = 0; j < 64; j++, k++) {
74 if ((flag & FIELD_INVERSION_EXPONENT[i]) != 0)
75 fp_mul(out, temp, out);
76 fp_sqr(temp, temp);
77 flag <<= 1;
78 }
79 }
80 fp_copy(output, out);
81}
#define FIELD_BITS
Definition p254.h:7

References FIELD_64BITS_WORDS, FIELD_BITS, fp_copy, fp_mul, fp_set_to_one(), fp_sqr, i, and j.

Here is the call graph for this function:

◆ fp_is_equal()

uint8_t fp_is_equal ( const fp_t input_a,
const fp_t input_b )

Definition at line 122 of file fp.c.

122 {
123 int i;
124 uint8_t r = 0;
125 uint64_t t;
126
127 for (i = 0; i < FIELD_64BITS_WORDS; i++) {
128 t = 0;
129 uint8_t *ta = (uint8_t *) &input_a[i];
130 uint8_t *tb = (uint8_t *) &input_b[i];
131 t = (ta[0] ^ tb[0]) | (ta[1] ^ tb[1]) | (ta[2] ^ tb[2]) | (ta[3] ^ tb[3]) |
132 (ta[4] ^ tb[4]) | (ta[5] ^ tb[5]) | (ta[6] ^ tb[6]) | (ta[7] ^ tb[7]);
133
134 t = (-t);
135 t = t >> 63;
136 r |= t;
137 }
138
139 return (uint8_t) (1 - r);
140}

References FIELD_64BITS_WORDS, and i.

Referenced by fp2_is_equal(), fp2_sqrt_fast(), fp2_sqrt_slow(), fp_is_square(), and isogeny_walks_get_points_3_fp().

Here is the caller graph for this function:

◆ fp_is_smaller()

uint8_t fp_is_smaller ( const fp_t input1,
const fp_t input2 )

Definition at line 143 of file fp.c.

143 {
144 return multiprecision_is_smaller(input1, input2, FIELD_64BITS_WORDS);
145}
uint8_t multiprecision_is_smaller(const uint64_t *input_a, const uint64_t *input_b, uint64_t input_length)
Definition utilities.c:111

References FIELD_64BITS_WORDS, and multiprecision_is_smaller().

Here is the call graph for this function:

◆ fp_is_square()

uint8_t fp_is_square ( const fp_t input)

Definition at line 148 of file fp.c.

148 {
149 fp_t temp, input_1;
150 uint64_t flag;
151 int i, j;
152 // input_1 <- input ^ ([p - 1] / 2)
153 fp_set_to_one(input_1);
154 fp_copy(temp, input);
155 for (i = 0; i < FIELD_64BITS_WORDS; i++) {
156 flag = 1;
157 for (j = 0; j < 64; j++) {
158 if ((flag & SQUARE_ROOT_EXPONENT_12[i]) != 0)
159 fp_mul(input_1, temp, input_1);
160 fp_sqr(temp, temp);
161 flag <<= 1;
162 }
163 }
164
165 return fp_is_equal(input_1, MONTGOMERY_CONSTANT_ONE);
166}
uint8_t fp_is_equal(const fp_t input_a, const fp_t input_b)
Definition fp.c:122

References FIELD_64BITS_WORDS, fp_copy, fp_is_equal(), fp_mul, fp_set_to_one(), fp_sqr, i, and j.

Referenced by fp2_is_square(), and isogeny_walks_get_points_3_fp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fp_is_zero()

int64_t fp_is_zero ( const fp_t input)

Definition at line 112 of file fp.c.

112 {
113 int i;
114 uint64_t output = 0;
115 for (i = FIELD_64BITS_WORDS - 1; i >= 0; i--)
116 output |= input[i];
117
118 return ~(((int64_t)output >> 63) | (-(int64_t)output >> 63));
119}

References FIELD_64BITS_WORDS, and i.

Referenced by fp2_is_zero(), and fp2_sqrt_slow().

Here is the caller graph for this function:

◆ fp_mul()

void fp_mul ( uint64_t * output,
const uint64_t * input_a,
const uint64_t * input_b )
extern

◆ fp_neg()

void fp_neg ( fp_t output,
const fp_t input )

Definition at line 9 of file fp.c.

9 {
10 fp_t zero = {0};
11 fp_sub(output, zero, input);
12}
#define fp_sub
Definition fp-gmp.h:67

References fp_sub.

Referenced by fp2_conj(), fp2_neg(), fp2_sqrt_fast(), isogeny_walks_get_points_3(), and isogeny_walks_get_points_3_fp().

Here is the caller graph for this function:

◆ fp_sample()

void fp_sample ( fp_t output)

Definition at line 51 of file fp.c.

51 {
52 randombytes((uint8_t *) output, FIELD_BYTES);
54 while (!multiprecision_is_smaller(output, FIELD_CHARACTERISTIC, FIELD_64BITS_WORDS)) {
55 randombytes((uint8_t *) output, FIELD_BYTES);
57 }
58}
void randombytes(void *x, size_t l)
Definition rng.c:8
#define FIELD_BYTES
Definition p254.h:8
#define MASK_FIELD_ELEMENT
Definition p254.h:11

References FIELD_64BITS_WORDS, FIELD_BYTES, MASK_FIELD_ELEMENT, multiprecision_is_smaller(), and randombytes().

Here is the call graph for this function:

◆ fp_set_to_one()

void fp_set_to_one ( fp_t output)

Definition at line 14 of file fp.c.

14 {
15 fp_copy(input_output, MONTGOMERY_CONSTANT_ONE);
16}

References fp_copy.

Referenced by fp2_set_to_one(), fp2_sqrt_fast(), fp2_sqrt_slow(), fp_curt(), fp_inv(), fp_is_square(), fp_sqrt(), isogeny_walks_3_fp(), and isogeny_walks_get_points_3().

Here is the caller graph for this function:

◆ fp_set_to_zero()

void fp_set_to_zero ( fp_t output)

Definition at line 19 of file fp.c.

19 {
20 memset(input_output, 0, sizeof(fp_t));
21}

Referenced by fp2_set_to_one(), fp2_set_to_zero(), and fp2_sqrt_fast().

Here is the caller graph for this function:

◆ fp_sqr()

void fp_sqr ( uint64_t * output,
const uint64_t * input_a )
extern

◆ fp_sqrt()

void fp_sqrt ( fp_t output,
const fp_t input )

Definition at line 168 of file fp.c.

168 {
169 fp_t temp, input_1;
170 uint64_t flag;
171 int i, j;
172 // input_1 <- input ^ ([p - 3] / 4)
173 fp_set_to_one(input_1);
174 fp_copy(temp, input);
175 for (i = 0; i < FIELD_64BITS_WORDS; i++) {
176 flag = 1;
177 for (j = 0; j < 64; j++) {
178 if ((flag & SQUARE_ROOT_EXPONENT_34[i]) != 0)
179 fp_mul(input_1, temp, input_1);
180 fp_sqr(temp, temp);
181 flag <<= 1;
182 }
183 }
184 fp_mul(output, input_1, input);
185}

References FIELD_64BITS_WORDS, fp_copy, fp_mul, fp_set_to_one(), fp_sqr, i, and j.

Referenced by elligator(), isogeny_walks_get_points_3_fp(), and isogeny_walks_get_points_3_fp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fp_sub()

void fp_sub ( uint64_t * output,
const uint64_t * input_a,
const uint64_t * input_b )
extern

◆ fp_to_mont()

void fp_to_mont ( fp_t output,
const fp_t input )

Definition at line 99 of file fp.c.

99 {
100 fp_mul(output, input, (uint64_t *) &MONTGOMERY_CONSTANT_R_SQUARED);
101}

References fp_mul.

Referenced by fp2_to_mont().

Here is the caller graph for this function: