Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
fp-karatsuba.c
Go to the documentation of this file.
1#include "fp-karatsuba.h"
2#include "../fp-counters.h"
3// #include "framework.h"
4#include "../../primes.h"
5
6#include <gmp.h>
7
8//#define USE_GMP_SEC_FUNCTIONS
9//#define MONTGOMERY
10
11// #if defined P2047m1l226
12
13#define pbits 2047
14#define itch_size 128
15
16// #endif
17
18// const fp fp_0 = {0x0};
19
20void fp_pow(fp b, const fp e, const fp a)
21{
22 (void) a;
23 (void) b;
24 (void) e;
25}
26
27/*
28 see Algorithm 14.36 "Montgomery multiplication"
29 https://cacr.uwaterloo.ca/hac/about/chap14.pdf
30*/
31void fp_mul(fp c, const fp a, const fp b)
32{
33// #if defined(P2047m1l226)
34 uint64_t result[64] = {0};
35 fp_mult_32x32(result, a, b);
36 fp_word_redc(c, result);
38}
39
40void fp_add(fp c, const fp a, const fp b)
41{
42
43 fp_add_s(c, a, b);
45}
46
47void fp_sub(fp c, const fp a, const fp b)
48{
49
50 fp_sub_s(c, a, b);
52}
53
54
55void fp_sqr(fp b, const fp a)
56{
57
58 uint64_t result[64];
59 fp_squaring(result, a, a);
60 fp_word_redc(b, result);
61
63}
64
65
66#if defined(P5119m46l244) || defined(P6143m59l262) || defined(P8191m78l338) || defined(P9215m85l389)
67
68void fp_mont_it_redc(fp a, const uint64_t b[2 * NUMBER_OF_WORDS])
69{
70 uint64_t r0[2 * NUMBER_OF_WORDS] = {0};
71 uint64_t r1[2 * NUMBER_OF_WORDS] = {0};
72
73 mult_redc(r0, b, redc_alpha);
74
75 add_redc(r0, &b[E2], r0);
76
77 mult_redc(r1, r0, redc_alpha);
78
79 add_redc_final(a, &r0[E2], r1);
80}
81
82#else
83
84/*
85 see Algorithm 14.32 "Montgomery reduction" mult_redc(r0, b, redc_alpha);
86
87 // // r0 = (a− q0)/2^e2 + q0 × alpha
88 add_redc(r0, &b[78], r0);
89
90 // // // q0
91 // // memcpy(q1, r0, 78 * sizeof(uint64_t));
92
93 mult_redc(r1, r0, redc_alpha);
94
95
96 add_redc_final(a, &r0[78], r1);
97
98 https://cacr.uwaterloo.ca/hac/about/chap14.pdf
99*/
100void fp_mont_redc(fp a, const uint64_t b[2 * NUMBER_OF_WORDS])
101{
102
103 static __thread uint64_t tp[itch_size];
104 uint64_t A[2 * NUMBER_OF_WORDS + 1] = {0x0};
105 // uint64_t a_i[1] = {0x0};
106 uint64_t tmp_1[NUMBER_OF_WORDS + 1] = {0x0};
107
108 // 1. A = T
109 mpn_copyd(A, b, 2 * NUMBER_OF_WORDS);
110
111 for (int i = 0; i < NUMBER_OF_WORDS; i++)
112 {
113 // 2.1 u_i = a_i * m' mod b
114 // since montgomery friendly m' = 1
115 // a_i[0] = A[i];
116
117 // 2.2 tmp_1 = u_i * m
118 mpn_sec_mul(tmp_1, p, NUMBER_OF_WORDS, &A[i], 1, tp);
119
120 // 2.2 A = A + u_i * m * b^i
121 mpn_add(A + i, A + i, 2 * NUMBER_OF_WORDS + 1 - i, tmp_1, NUMBER_OF_WORDS + 1);
122
123 }
124
125 // 3. A = A/b^n
126 mpn_copyd(a, A + NUMBER_OF_WORDS, NUMBER_OF_WORDS);
127
128 // 4. If A > m then A = A - m
129 mpn_cnd_sub_n(mpn_cmp(a, p, NUMBER_OF_WORDS) > 0, a, a, p, NUMBER_OF_WORDS);
130}
131
132
133
134
135#endif
136
137#if defined(P2047m1l226)
138
139// void fp_mont_2k(fp a, const uint64_t b[2 * NUMBER_OF_WORDS])
140// {
141
142// static __thread uint64_t tp[itch_size];
143// uint64_t A[2 * NUMBER_OF_WORDS + 1] = {0x0};
144// // uint64_t a_i[1] = {0x0};
145// uint64_t tmp_1[NUMBER_OF_WORDS + 1] = {0x0};
146
147// // 1. A = T
148// mpn_copyd(A, b, 2 * NUMBER_OF_WORDS);
149
150// for (int i = 0; i < NUMBER_OF_WORDS; i++)
151// {
152
153// memset(tmp_1, 0, sizeof(tmp_1));
154// // 2.2 tmp_1 = u_i * m
155// mpn_sec_mul(tmp_1, redc_alpha, NUMBER_OF_WORDS - 1, A, 1, tp);
156
157// // mpn_sec_mul(tmp_1, p, NUMBER_OF_WORDS, &A[i], 1, tp);
158
159// // mpn_sub(A, A, 1, A, 1);
160
161// // mpn_copyd(A, A + 1, 2 * NUMBER_OF_WORDS - i);
162
163// // 2.2 A = A + u_i * m * b^i
164// mpn_add(A, A + 1, 2 * NUMBER_OF_WORDS - i, tmp_1, NUMBER_OF_WORDS);
165
166// }
167
168// // 3. A = A/b^n
169// mpn_copyd(a, A, NUMBER_OF_WORDS);
170
171// // 4. If A > m then A = A - m
172// mpn_cnd_sub_n(mpn_cmp(a, p, NUMBER_OF_WORDS) > 0, a, a, p, NUMBER_OF_WORDS);
173// }
174
175#endif
176
177#if defined(P4095m27l262)
178
179void fp_mont_4k(fp a, const uint64_t b[2 * NUMBER_OF_WORDS])
180{
181
182 static __thread uint64_t tp[itch_size];
183 uint64_t A[2 * NUMBER_OF_WORDS + 1] = {0x0};
184 // uint64_t a_i[1] = {0x0};
185 uint64_t tmp_1[NUMBER_OF_WORDS + 1] = {0x0};
186
187 // 1. A = T
188 mpn_copyd(A, b, 2 * NUMBER_OF_WORDS);
189
190 for (int i = 0; i < NUMBER_OF_WORDS; i++)
191 {
192 // 2.1 u_i = a_i * m' mod b
193 // since montgomery friendly m' = 1
194 // a_i[0] = A[i];
195
196 memset(tmp_1, 0, sizeof(tmp_1));
197 // 2.2 tmp_1 = u_i * m
198 mpn_sec_mul(tmp_1, redc_alpha, 38, A, 1, tp);
199
200 // mpn_sec_mul(tmp_1, p, NUMBER_OF_WORDS, &A[i], 1, tp);
201
202 mpn_sub(A, A, 1, A, 1);
203
204 mpn_copyd(A, A + 1, NUMBER_OF_WORDS - i);
205
206 // 2.2 A = A + u_i * m * b^i
207 mpn_add(A, A, 2 * NUMBER_OF_WORDS + 1 - i, tmp_1, 38);
208
209 }
210
211 // 3. A = A/b^n
212 mpn_copyd(a, A, NUMBER_OF_WORDS);
213
214 // 4. If A > m then A = A - m
215 mpn_cnd_sub_n(mpn_cmp(a, p, NUMBER_OF_WORDS) > 0, a, a, p, NUMBER_OF_WORDS);
216}
217
218#endif
#define CNT_FP_ADD_INC()
Definition fp-counters.h:29
#define CNT_FP_MUL_INC()
Definition fp-counters.h:30
#define CNT_FP_SQR_INC()
Definition fp-counters.h:32
#define p
Definition fp-gmp.h:44
#define fp_sqr
Definition fp-gmp.h:73
uint64_t fp[NUMBER_OF_WORDS]
Definition fp-gmp.h:22
#define fp_pow
Definition fp-gmp.h:61
#define fp_sub
Definition fp-gmp.h:67
#define fp_mul
Definition fp-gmp.h:70
#define fp_add
Definition fp-gmp.h:64
#define itch_size
void fp_mont_redc(fp a, const uint64_t b[2 *NUMBER_OF_WORDS])
#define fp_squaring
#define fp_add_s
#define redc_alpha
#define fp_sub_s
A
Definition tests.py:29
for i
f a
Definition to_model.m:12