8100fe638a8f92829db15bcd121a34e55fcc960a
[openssl.git] / crypto / ec / curve448 / field.h
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2014 Cryptography Research, Inc.
4  *
5  * Licensed under the OpenSSL license (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  *
10  * Originally written by Mike Hamburg
11  */
12
13 #ifndef __GF_H__
14 # define __GF_H__
15
16 # include "constant_time.h"
17 # include "f_field.h"
18 # include <string.h>
19
20 /** Square x, n times. */
21 static ossl_inline void gf_sqrn(gf_s * __restrict__ y, const gf x, int n)
22 {
23     gf tmp;
24     assert(n > 0);
25     if (n & 1) {
26         gf_sqr(y, x);
27         n--;
28     } else {
29         gf_sqr(tmp, x);
30         gf_sqr(y, tmp);
31         n -= 2;
32     }
33     for (; n; n -= 2) {
34         gf_sqr(tmp, y);
35         gf_sqr(y, tmp);
36     }
37 }
38
39 # define gf_add_nr gf_add_RAW
40
41 /** Subtract mod p.  Bias by 2 and don't reduce  */
42 static ossl_inline void gf_sub_nr(gf c, const gf a, const gf b)
43 {
44     gf_sub_RAW(c, a, b);
45     gf_bias(c, 2);
46     if (GF_HEADROOM < 3)
47         gf_weak_reduce(c);
48 }
49
50 /** Subtract mod p. Bias by amt but don't reduce.  */
51 static ossl_inline void gf_subx_nr(gf c, const gf a, const gf b, int amt)
52 {
53     gf_sub_RAW(c, a, b);
54     gf_bias(c, amt);
55     if (GF_HEADROOM < amt + 1)
56         gf_weak_reduce(c);
57 }
58
59 /** Mul by signed int.  Not constant-time WRT the sign of that int. */
60 static ossl_inline void gf_mulw(gf c, const gf a, int32_t w)
61 {
62     if (w > 0) {
63         gf_mulw_unsigned(c, a, w);
64     } else {
65         gf_mulw_unsigned(c, a, -w);
66         gf_sub(c, ZERO, c);
67     }
68 }
69
70 /** Constant time, x = is_z ? z : y */
71 static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z)
72 {
73     constant_time_select(x, y, z, sizeof(gf), is_z, 0);
74 }
75
76 /** Constant time, if (neg) x=-x; */
77 static ossl_inline void gf_cond_neg(gf x, mask_t neg)
78 {
79     gf y;
80     gf_sub(y, ZERO, x);
81     gf_cond_sel(x, x, y, neg);
82 }
83
84 /** Constant time, if (swap) (x,y) = (y,x); */
85 static ossl_inline void gf_cond_swap(gf x, gf_s * __restrict__ y, mask_t swap)
86 {
87     constant_time_cond_swap(x, y, sizeof(gf_s), swap);
88 }
89
90 static ossl_inline void gf_mul_qnr(gf_s * __restrict__ out, const gf x)
91 {
92     gf_sub(out, ZERO, x);
93 }
94
95 static ossl_inline void gf_div_qnr(gf_s * __restrict__ out, const gf x)
96 {
97     gf_sub(out, ZERO, x);
98 }
99
100 #endif                          /* __GF_H__ */