Update the imported curve448 code to use OpenSSL copyright headers
[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 (
22     gf_s *__restrict__ y,
23     const gf x,
24     int n
25 ) {
26     gf tmp;
27     assert(n>0);
28     if (n&1) {
29         gf_sqr(y,x);
30         n--;
31     } else {
32         gf_sqr(tmp,x);
33         gf_sqr(y,tmp);
34         n-=2;
35     }
36     for (; n; n-=2) {
37         gf_sqr(tmp,y);
38         gf_sqr(y,tmp);
39     }
40 }
41
42 #define gf_add_nr gf_add_RAW
43
44 /** Subtract mod p.  Bias by 2 and don't reduce  */
45 static ossl_inline void gf_sub_nr ( gf c, const gf a, const gf b ) {
46     gf_sub_RAW(c,a,b);
47     gf_bias(c, 2);
48     if (GF_HEADROOM < 3) gf_weak_reduce(c);
49 }
50
51 /** Subtract mod p. Bias by amt but don't reduce.  */
52 static ossl_inline void gf_subx_nr ( gf c, const gf a, const gf b, int amt ) {
53     gf_sub_RAW(c,a,b);
54     gf_bias(c, amt);
55     if (GF_HEADROOM < amt+1) gf_weak_reduce(c);
56 }
57
58 /** Mul by signed int.  Not constant-time WRT the sign of that int. */
59 static ossl_inline void gf_mulw(gf c, const gf a, int32_t w) {
60     if (w>0) {
61         gf_mulw_unsigned(c, a, w);
62     } else {
63         gf_mulw_unsigned(c, a, -w);
64         gf_sub(c,ZERO,c);
65     }
66 }
67
68 /** Constant time, x = is_z ? z : y */
69 static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z) {
70     constant_time_select(x,y,z,sizeof(gf),is_z,0);
71 }
72
73 /** Constant time, if (neg) x=-x; */
74 static ossl_inline void gf_cond_neg(gf x, mask_t neg) {
75     gf y;
76     gf_sub(y,ZERO,x);
77     gf_cond_sel(x,x,y,neg);
78 }
79
80 /** Constant time, if (swap) (x,y) = (y,x); */
81 static ossl_inline void
82 gf_cond_swap(gf x, gf_s *__restrict__ y, mask_t swap) {
83     constant_time_cond_swap(x,y,sizeof(gf_s),swap);
84 }
85
86 static ossl_inline void gf_mul_qnr(gf_s *__restrict__ out, const gf x) {
87     gf_sub(out,ZERO,x);
88 }
89
90 static ossl_inline void gf_div_qnr(gf_s *__restrict__ out, const gf x) {
91     gf_sub(out,ZERO,x);
92 }
93
94
95 #endif /* __GF_H__ */