aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/s2n/pq-crypto/sike_r3/sikep434r3_fp.c
blob: 867ac0f6c16ae8314093dcf3055b41409a818a4a (plain) (blame)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/********************************************************************************************
* Supersingular Isogeny Key Encapsulation Library
*
* Abstract: modular arithmetic for P434
*********************************************************************************************/

#include "sikep434r3.h"
#include "pq-crypto/s2n_pq.h"
#include "sikep434r3_fp.h"
#include "sikep434r3_fpx.h"
#include "sikep434r3_fp_x64_asm.h"

/* Multiprecision subtraction with correction with 2*p, c = a-b+2p. */
void mp_sub434_p2(const digit_t* a, const digit_t* b, digit_t* c)
{
#if defined(S2N_SIKE_P434_R3_ASM)
    if (s2n_sikep434r3_asm_is_enabled()) {
        mp_sub434_p2_asm(a, b, c);
        return;
    }
#endif

    unsigned int i, borrow = 0;

    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_SUBC(borrow, a[i], b[i], borrow, c[i]);
    }

    borrow = 0;
    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_ADDC(borrow, c[i], ((const digit_t*)p434x2)[i], borrow, c[i]);
    }
}

/* Multiprecision subtraction with correction with 4*p, c = a-b+4p. */
void mp_sub434_p4(const digit_t* a, const digit_t* b, digit_t* c)
{
#if defined(S2N_SIKE_P434_R3_ASM)
    if (s2n_sikep434r3_asm_is_enabled()) {
        mp_sub434_p4_asm(a, b, c);
        return;
    }
#endif

    unsigned int i, borrow = 0;

    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_SUBC(borrow, a[i], b[i], borrow, c[i]);
    }

    borrow = 0;
    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_ADDC(borrow, c[i], ((const digit_t*)p434x4)[i], borrow, c[i]);
    }
}

/* Modular addition, c = a+b mod p434.
 * Inputs: a, b in [0, 2*p434-1]
 * Output: c in [0, 2*p434-1] */
void fpadd434(const digit_t* a, const digit_t* b, digit_t* c)
{
#if defined(S2N_SIKE_P434_R3_ASM)
    if (s2n_sikep434r3_asm_is_enabled()) {
        fpadd434_asm(a, b, c);
        return;
    }
#endif
    unsigned int i, carry = 0;
    digit_t mask;

    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_ADDC(carry, a[i], b[i], carry, c[i]);
    }

    carry = 0;
    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_SUBC(carry, c[i], ((const digit_t*)p434x2)[i], carry, c[i]);
    }
    mask = 0 - (digit_t)carry;

    carry = 0;
    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_ADDC(carry, c[i], ((const digit_t*)p434x2)[i] & mask, carry, c[i]);
    }
}

/* Modular subtraction, c = a-b mod p434.
 * Inputs: a, b in [0, 2*p434-1]
 * Output: c in [0, 2*p434-1] */
void fpsub434(const digit_t* a, const digit_t* b, digit_t* c)
{
#if defined(S2N_SIKE_P434_R3_ASM)
    if (s2n_sikep434r3_asm_is_enabled()) {
        fpsub434_asm(a, b, c);
        return;
    }
#endif

    unsigned int i, borrow = 0;
    digit_t mask;

    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_SUBC(borrow, a[i], b[i], borrow, c[i]);
    }
    mask = 0 - (digit_t)borrow;

    borrow = 0;
    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_ADDC(borrow, c[i], ((const digit_t*)p434x2)[i] & mask, borrow, c[i]);
    }
}

/* Modular negation, a = -a mod p434.
 * Input/output: a in [0, 2*p434-1]  */
void fpneg434(digit_t* a)
{
    unsigned int i, borrow = 0;

    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_SUBC(borrow, ((const digit_t*)p434x2)[i], a[i], borrow, a[i]);
    }
}

/* Modular division by two, c = a/2 mod p434.
 * Input : a in [0, 2*p434-1]
 * Output: c in [0, 2*p434-1] */
void fpdiv2_434(const digit_t* a, digit_t* c)
{
    unsigned int i, carry = 0;
    digit_t mask;

    mask = 0 - (digit_t)(a[0] & 1); /* If a is odd compute a+p434 */
    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_ADDC(carry, a[i], ((const digit_t*)p434)[i] & mask, carry, c[i]);
    }

    mp_shiftr1(c, S2N_SIKE_P434_R3_NWORDS_FIELD);
}

/* Modular correction to reduce field element a in [0, 2*p434-1] to [0, p434-1]. */
void fpcorrection434(digit_t* a)
{
    unsigned int i, borrow = 0;
    digit_t mask;

    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_SUBC(borrow, a[i], ((const digit_t*)p434)[i], borrow, a[i]);
    }
    mask = 0 - (digit_t)borrow;

    borrow = 0;
    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        S2N_SIKE_P434_R3_ADDC(borrow, a[i], ((const digit_t*)p434)[i] & mask, borrow, a[i]);
    }
}

/* Digit multiplication, digit * digit -> 2-digit result */
void digit_x_digit(const digit_t a, const digit_t b, digit_t* c)
{
    register digit_t al, ah, bl, bh, temp;
    digit_t albl, albh, ahbl, ahbh, res1, res2, res3, carry;
    digit_t mask_low = (digit_t)(-1) >> (sizeof(digit_t)*4), mask_high = (digit_t)(-1) << (sizeof(digit_t)*4);

    al = a & mask_low;                        /* Low part */
    ah = a >> (sizeof(digit_t) * 4);          /* High part */
    bl = b & mask_low;
    bh = b >> (sizeof(digit_t) * 4);

    albl = al*bl;
    albh = al*bh;
    ahbl = ah*bl;
    ahbh = ah*bh;
    c[0] = albl & mask_low;                   /* C00 */

    res1 = albl >> (sizeof(digit_t) * 4);
    res2 = ahbl & mask_low;
    res3 = albh & mask_low;  
    temp = res1 + res2 + res3;
    carry = temp >> (sizeof(digit_t) * 4);
    c[0] ^= temp << (sizeof(digit_t) * 4);    /* C01 */

    res1 = ahbl >> (sizeof(digit_t) * 4);
    res2 = albh >> (sizeof(digit_t) * 4);
    res3 = ahbh & mask_low;
    temp = res1 + res2 + res3 + carry;
    c[1] = temp & mask_low;                   /* C10 */
    carry = temp & mask_high; 
    c[1] ^= (ahbh & mask_high) + carry;       /* C11 */
}

/* Multiprecision comba multiply, c = a*b, where lng(a) = lng(b) = nwords. */
void mp_mul(const digit_t* a, const digit_t* b, digit_t* c, const unsigned int nwords)
{
#if defined(S2N_SIKE_P434_R3_ASM)
    if (s2n_sikep434r3_asm_is_enabled()) {
        S2N_SIKE_P434_R3_UNREFERENCED_PARAMETER(nwords);
        mul434_asm(a, b, c);
        return;
    }
#endif

    unsigned int i, j;
    digit_t t = 0, u = 0, v = 0, UV[2];
    unsigned int carry;
    
    for (i = 0; i < nwords; i++) {
        for (j = 0; j <= i; j++) {
            S2N_SIKE_P434_R3_MUL(a[j], b[i-j], UV+1, UV[0]);
            S2N_SIKE_P434_R3_ADDC(0, UV[0], v, carry, v);
            S2N_SIKE_P434_R3_ADDC(carry, UV[1], u, carry, u);
            t += carry;
        }
        c[i] = v;
        v = u; 
        u = t;
        t = 0;
    }

    for (i = nwords; i < 2*nwords-1; i++) {
        for (j = i-nwords+1; j < nwords; j++) {
            S2N_SIKE_P434_R3_MUL(a[j], b[i-j], UV+1, UV[0]);
            S2N_SIKE_P434_R3_ADDC(0, UV[0], v, carry, v);
            S2N_SIKE_P434_R3_ADDC(carry, UV[1], u, carry, u);
            t += carry;
        }
        c[i] = v;
        v = u; 
        u = t;
        t = 0;
    }
    c[2*nwords-1] = v; 
}

/* Efficient Montgomery reduction using comba and exploiting the special form of the prime p434.
 * mc = ma*R^-1 mod p434x2, where R = 2^448.
 * If ma < 2^448*p434, the output mc is in the range [0, 2*p434-1].
 * ma is assumed to be in Montgomery representation. */
void rdc_mont(digit_t* ma, digit_t* mc)
{
#if defined(S2N_SIKE_P434_R3_ASM)
    if (s2n_sikep434r3_asm_is_enabled()) {
        rdc434_asm(ma, mc);
        return;
    }
#endif

    unsigned int i, j, carry, count = S2N_SIKE_P434_R3_ZERO_WORDS;
    digit_t UV[2], t = 0, u = 0, v = 0;

    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        mc[i] = 0;
    }

    for (i = 0; i < S2N_SIKE_P434_R3_NWORDS_FIELD; i++) {
        for (j = 0; j < i; j++) {
            if (j < (i-S2N_SIKE_P434_R3_ZERO_WORDS+1)) {
                S2N_SIKE_P434_R3_MUL(mc[j], ((const digit_t*)p434p1)[i-j], UV+1, UV[0]);
                S2N_SIKE_P434_R3_ADDC(0, UV[0], v, carry, v);
                S2N_SIKE_P434_R3_ADDC(carry, UV[1], u, carry, u);
                t += carry; 
            }
        }
        S2N_SIKE_P434_R3_ADDC(0, v, ma[i], carry, v);
        S2N_SIKE_P434_R3_ADDC(carry, u, 0, carry, u);
        t += carry; 
        mc[i] = v;
        v = u;
        u = t;
        t = 0;
    }    

    for (i = S2N_SIKE_P434_R3_NWORDS_FIELD; i < 2*S2N_SIKE_P434_R3_NWORDS_FIELD-1; i++) {
        if (count > 0) {
            count -= 1;
        }
        for (j = i-S2N_SIKE_P434_R3_NWORDS_FIELD+1; j < S2N_SIKE_P434_R3_NWORDS_FIELD; j++) {
            if (j < (S2N_SIKE_P434_R3_NWORDS_FIELD-count)) {
                S2N_SIKE_P434_R3_MUL(mc[j], ((const digit_t*)p434p1)[i-j], UV+1, UV[0]);
                S2N_SIKE_P434_R3_ADDC(0, UV[0], v, carry, v);
                S2N_SIKE_P434_R3_ADDC(carry, UV[1], u, carry, u);
                t += carry;
            }
        }
        S2N_SIKE_P434_R3_ADDC(0, v, ma[i], carry, v);
        S2N_SIKE_P434_R3_ADDC(carry, u, 0, carry, u);
        t += carry; 
        mc[i-S2N_SIKE_P434_R3_NWORDS_FIELD] = v;
        v = u;
        u = t;
        t = 0;
    }

    /* `carry` isn't read after this, but it's still a necessary argument to the macro */
    /* cppcheck-suppress unreadVariable */
    S2N_SIKE_P434_R3_ADDC(0, v, ma[2*S2N_SIKE_P434_R3_NWORDS_FIELD-1], carry, v);
    mc[S2N_SIKE_P434_R3_NWORDS_FIELD-1] = v;
}