Merge f_arithmetic.c into f_generic.c
[openssl.git] / crypto / ec / curve448 / f_field.h
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2015-2016 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 __P448_F_FIELD_H__
14 # define __P448_F_FIELD_H__ 1
15
16 # include "constant_time.h"
17 # include <string.h>
18 # include <assert.h>
19
20 # include "word.h"
21
22 # define NLIMBS (64/sizeof(word_t))
23 # define X_SER_BYTES 56
24 # define SER_BYTES 56
25 typedef struct gf_s {
26     word_t limb[NLIMBS];
27 } __attribute__ ((aligned(32))) gf_s, gf[1];
28
29 /* RFC 7748 support */
30 # define X_PUBLIC_BYTES  X_SER_BYTES
31 # define X_PRIVATE_BYTES X_PUBLIC_BYTES
32 # define X_PRIVATE_BITS  448
33
34 # define INLINE_UNUSED __inline__ __attribute__((unused,always_inline))
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 /* Defined below in f_impl.h */
41 static INLINE_UNUSED void gf_copy(gf out, const gf a)
42 {
43     *out = *a;
44 }
45
46 static INLINE_UNUSED void gf_add_RAW(gf out, const gf a, const gf b);
47 static INLINE_UNUSED void gf_sub_RAW(gf out, const gf a, const gf b);
48 static INLINE_UNUSED void gf_bias(gf inout, int amount);
49 static INLINE_UNUSED void gf_weak_reduce(gf inout);
50
51 void gf_strong_reduce(gf inout);
52 void gf_add(gf out, const gf a, const gf b);
53 void gf_sub(gf out, const gf a, const gf b);
54 void gf_mul(gf_s * __restrict__ out, const gf a, const gf b);
55 void gf_mulw_unsigned(gf_s * __restrict__ out, const gf a, uint32_t b);
56 void gf_sqr(gf_s * __restrict__ out, const gf a);
57 mask_t gf_isr(gf a, const gf x); /** a^2 x = 1, QNR, or 0 if x=0.  Return true if successful */
58 mask_t gf_eq(const gf x, const gf y);
59 mask_t gf_lobit(const gf x);
60 mask_t gf_hibit(const gf x);
61
62 void gf_serialize(uint8_t *serial, const gf x, int with_highbit);
63 mask_t gf_deserialize(gf x, const uint8_t serial[SER_BYTES], int with_hibit,
64                       uint8_t hi_nmask);
65
66
67 #ifdef __cplusplus
68 } /* extern "C" */
69 #endif
70
71 # include "f_impl.h"            /* Bring in the inline implementations */
72
73 # ifndef LIMBPERM
74 #  define LIMBPERM(i) (i)
75 # endif
76 # define LIMB_MASK(i) (((1)<<LIMB_PLACE_VALUE(i))-1)
77
78 static const gf ZERO = {{{0}}}, ONE = {{{1}}};
79
80 #endif                          /* __P448_F_FIELD_H__ */