]> pd.if.org Git - zpackage/blob - crypto/libeddsa/lib/fld.h
add package signing code
[zpackage] / crypto / libeddsa / lib / fld.h
1 #ifndef FLD_H
2 #define FLD_H
3
4 #include <stdint.h>
5
6 #include "bitness.h"
7 #include "compat.h"
8 #include "limb.h"
9
10
11 #ifdef USE_64BIT
12
13 /*
14  * in 64bit mode we use 5 limbs each 51 bits long
15  */
16
17 #define FLD_LIMB_NUM            5
18 #define FLD_LIMB_BITS           51
19
20 #define FLD_LIMB_MASK           ((1L << FLD_LIMB_BITS)-1)
21
22 #else
23
24 /*
25  * in 32bit mode fld_t consists of 10 limbs alternating in size
26  * between 26 and 25 bits.
27  * this approach is inspired from djb's curve25519-paper, where it
28  * is explained in more detail.
29  */
30
31 #define FLD_LIMB_NUM            10
32
33 /* macros for alternating limb sizes: use with d=1,0,1,0,... */
34 #define FLD_LIMB_BITS(d)        (25+(d))
35 #define FLD_LIMB_MASK(d)        ((1 << FLD_LIMB_BITS(d))-1)
36
37 #endif
38
39
40 /*
41  * fld_t is our datatype for all operations modulo 2^255-19.
42  *
43  * since we typedef an array here, all parameters of type fld_t get
44  * a call-by-reference semantic!
45  */
46
47 typedef limb_t fld_t[FLD_LIMB_NUM];
48
49
50
51 /*
52  * exported constants
53  */
54 extern const fld_t con_d;
55 extern const fld_t con_2d;
56 extern const fld_t con_m2d;
57 extern const fld_t con_j;
58
59
60 /*
61  * prototypes for 32bit/64bit specific functions 
62  */
63 void    fld_reduce(fld_t dst, const fld_t x);
64 void    fld_import(fld_t dst, const uint8_t src[32]);
65 void    fld_export(uint8_t dst[32], const fld_t src);
66 void    fld_mul(fld_t res, const fld_t a, const fld_t b);
67 void    fld_scale(fld_t dst, const fld_t src, limb_t x);
68 void    fld_sq(fld_t res, const fld_t a);
69
70
71 /*
72  * prototypes for common code
73  */
74 int     fld_eq(const fld_t a, const fld_t b);
75 void    fld_inv(fld_t res, const fld_t z);
76 void    fld_pow2523(fld_t res, const fld_t z);
77
78
79
80 /*
81  * simple inline functions
82  */
83
84 static INLINE void
85 fld_set0(fld_t res, limb_t x0)
86 {
87         int i;
88         res[0] = x0;
89         for (i = 1; i < FLD_LIMB_NUM; i++)
90                 res[i] = 0;
91 }
92
93
94 static INLINE void
95 fld_add(fld_t res, const fld_t a, const fld_t b)
96 {
97         int i;
98         for (i = 0; i < FLD_LIMB_NUM; i++)
99                 res[i] = a[i] + b[i];
100 }
101
102 static INLINE void
103 fld_sub(fld_t res, const fld_t a, const fld_t b)
104 {
105         int i;
106         for (i = 0; i < FLD_LIMB_NUM; i++)
107                 res[i] = a[i] - b[i];
108 }
109
110 /*
111  * fld_tinyscale scales an element a without reducing it. this could
112  * be used for conditionally change sign of an element.
113  */
114 static INLINE void
115 fld_tinyscale(fld_t res, const fld_t a, limb_t x)
116 {
117         int i;
118         for (i = 0; i < FLD_LIMB_NUM; i++)
119                 res[i] = x * a[i];
120 }
121
122 /*
123  * fld_scale2 is a special case of fld_tinyscale with x = 2.
124  */
125 static INLINE void
126 fld_scale2(fld_t res, const fld_t a)
127 {
128         int i;
129         for (i = 0; i < FLD_LIMB_NUM; i++)
130                 res[i] = a[i] << 1;
131 }
132
133 /*
134  * fld_neg is a special case of fld_tinyscale with x = -1.
135  */
136 static INLINE void
137 fld_neg(fld_t res, const fld_t a)
138 {
139         int i;
140         for (i = 0; i < FLD_LIMB_NUM; i++)
141                 res[i] = -a[i];
142 }
143
144 #endif