Convert to C90 from C99
[openssl.git] / crypto / ec / curve448 / field.h
1 /**
2  * @file field.h
3  * @brief Generic gf header.
4  * @copyright
5  *   Copyright (c) 2014 Cryptography Research, Inc.  \n
6  *   Released under the MIT License.  See LICENSE.txt for license information.
7  * @author Mike Hamburg
8  */
9
10 #ifndef __GF_H__
11 #define __GF_H__
12
13 #include "constant_time.h"
14 #include "f_field.h"
15 #include <string.h>
16     
17 /** Square x, n times. */
18 static ossl_inline void gf_sqrn (
19     gf_s *__restrict__ y,
20     const gf x,
21     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     gf_sub_RAW(c,a,b);
44     gf_bias(c, 2);
45     if (GF_HEADROOM < 3) gf_weak_reduce(c);
46 }
47
48 /** Subtract mod p. Bias by amt but don't reduce.  */
49 static ossl_inline void gf_subx_nr ( gf c, const gf a, const gf b, int amt ) {
50     gf_sub_RAW(c,a,b);
51     gf_bias(c, amt);
52     if (GF_HEADROOM < amt+1) gf_weak_reduce(c);
53 }
54
55 /** Mul by signed int.  Not constant-time WRT the sign of that int. */
56 static ossl_inline void gf_mulw(gf c, const gf a, int32_t w) {
57     if (w>0) {
58         gf_mulw_unsigned(c, a, w);
59     } else {
60         gf_mulw_unsigned(c, a, -w);
61         gf_sub(c,ZERO,c);
62     }
63 }
64
65 /** Constant time, x = is_z ? z : y */
66 static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z) {
67     constant_time_select(x,y,z,sizeof(gf),is_z,0);
68 }
69
70 /** Constant time, if (neg) x=-x; */
71 static ossl_inline void gf_cond_neg(gf x, mask_t neg) {
72     gf y;
73     gf_sub(y,ZERO,x);
74     gf_cond_sel(x,x,y,neg);
75 }
76
77 /** Constant time, if (swap) (x,y) = (y,x); */
78 static ossl_inline void
79 gf_cond_swap(gf x, gf_s *__restrict__ y, mask_t swap) {
80     constant_time_cond_swap(x,y,sizeof(gf_s),swap);
81 }
82
83 static ossl_inline void gf_mul_qnr(gf_s *__restrict__ out, const gf x) {
84     gf_sub(out,ZERO,x);
85 }
86
87 static ossl_inline void gf_div_qnr(gf_s *__restrict__ out, const gf x) {
88     gf_sub(out,ZERO,x);
89 }
90
91
92 #endif /* __GF_H__ */