2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2014 Cryptography Research, Inc.
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
10 * Originally written by Mike Hamburg
16 # include "constant_time.h"
21 # define NLIMBS (64/sizeof(word_t))
22 # define X_SER_BYTES 56
26 } __attribute__ ((aligned(32))) gf_s, gf[1];
28 /* RFC 7748 support */
29 # define X_PUBLIC_BYTES X_SER_BYTES
30 # define X_PRIVATE_BYTES X_PUBLIC_BYTES
31 # define X_PRIVATE_BITS 448
33 # define INLINE_UNUSED __inline__ __attribute__((unused,always_inline))
35 static INLINE_UNUSED void gf_copy(gf out, const gf a)
40 static INLINE_UNUSED void gf_add_RAW(gf out, const gf a, const gf b);
41 static INLINE_UNUSED void gf_sub_RAW(gf out, const gf a, const gf b);
42 static INLINE_UNUSED void gf_bias(gf inout, int amount);
43 static INLINE_UNUSED void gf_weak_reduce(gf inout);
45 void gf_strong_reduce(gf inout);
46 void gf_add(gf out, const gf a, const gf b);
47 void gf_sub(gf out, const gf a, const gf b);
48 void gf_mul(gf_s * __restrict__ out, const gf a, const gf b);
49 void gf_mulw_unsigned(gf_s * __restrict__ out, const gf a, uint32_t b);
50 void gf_sqr(gf_s * __restrict__ out, const gf a);
51 mask_t gf_isr(gf a, const gf x); /** a^2 x = 1, QNR, or 0 if x=0. Return true if successful */
52 mask_t gf_eq(const gf x, const gf y);
53 mask_t gf_lobit(const gf x);
54 mask_t gf_hibit(const gf x);
56 void gf_serialize(uint8_t *serial, const gf x, int with_highbit);
57 mask_t gf_deserialize(gf x, const uint8_t serial[SER_BYTES], int with_hibit,
60 # include "f_impl.h" /* Bring in the inline implementations */
63 # define LIMBPERM(i) (i)
65 # define LIMB_MASK(i) (((1)<<LIMB_PLACE_VALUE(i))-1)
67 static const gf ZERO = {{{0}}}, ONE = {{{1}}};
69 /* Square x, n times. */
70 static ossl_inline void gf_sqrn(gf_s * __restrict__ y, const gf x, int n)
88 # define gf_add_nr gf_add_RAW
90 /* Subtract mod p. Bias by 2 and don't reduce */
91 static ossl_inline void gf_sub_nr(gf c, const gf a, const gf b)
99 /* Subtract mod p. Bias by amt but don't reduce. */
100 static ossl_inline void gf_subx_nr(gf c, const gf a, const gf b, int amt)
104 if (GF_HEADROOM < amt + 1)
108 /* Mul by signed int. Not constant-time WRT the sign of that int. */
109 static ossl_inline void gf_mulw(gf c, const gf a, int32_t w)
112 gf_mulw_unsigned(c, a, w);
114 gf_mulw_unsigned(c, a, -w);
119 /* Constant time, x = is_z ? z : y */
120 static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z)
122 constant_time_select(x, y, z, sizeof(gf), is_z, 0);
125 /* Constant time, if (neg) x=-x; */
126 static ossl_inline void gf_cond_neg(gf x, mask_t neg)
130 gf_cond_sel(x, x, y, neg);
133 /* Constant time, if (swap) (x,y) = (y,x); */
134 static ossl_inline void gf_cond_swap(gf x, gf_s * __restrict__ y, mask_t swap)
136 constant_time_cond_swap(x, y, sizeof(gf_s), swap);
139 #endif /* __GF_H__ */