1 /*############################################################################
2 # Copyright 2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 ############################################################################*/
16 /// Definition of EFq math
17 /*! \file */
18 
19 #ifndef EPID_MEMBER_TINY_MATH_EFQ_H_
20 #define EPID_MEMBER_TINY_MATH_EFQ_H_
21 #include <stddef.h>
22 #include "epid/common/bitsupplier.h"
23 #include "epid/common/types.h"
24 
25 /// \cond
26 typedef struct EccPointFq EccPointFq;
27 typedef struct EccPointJacobiFq EccPointJacobiFq;
28 typedef struct FpElem FpElem;
29 typedef struct FqElem FqElem;
30 /// \endcond
31 
32 /// Multiply two points in EFq.
33 /*!
34 This function is mitigated against software side-channel
35 attacks.
36 
37 \param[out] result of multiplying left and right.
38 \param[in] base The first operand to be multiplied.
39 \param[in] exp The second operand to be multiplied.
40 */
41 void EFqMulSSCM(EccPointJacobiFq* result, EccPointJacobiFq const* base,
42                 FpElem const* exp);
43 
44 /// Exponentiate a point in EFq by an element of Fp.
45 /*!
46 \param[out] result target.
47 \param[in] base the base.
48 \param[in] exp the exponent.
49 \returns A value different from zero (i.e., true) if on success.
50          Zero (i.e., false) otherwise.
51 
52 \returns 1 on success, 0 on failure
53 */
54 int EFqAffineExp(EccPointFq* result, EccPointFq const* base, FpElem const* exp);
55 
56 /// Sum the results of exponentiating two points in EFq by elements of Fp.
57 /*!
58 \param[out] result target.
59 \param[in] base0 the first base.
60 \param[in] exp0 the first exponent.
61 \param[in] base1 the second base.
62 \param[in] exp1 the second exponent.
63 \returns A value different from zero (i.e., true) if on success.
64          Zero (i.e., false) otherwise.
65 */
66 int EFqAffineMultiExp(EccPointFq* result, EccPointFq const* base0,
67                       FpElem const* exp0, EccPointFq const* base1,
68                       FpElem const* exp1);
69 
70 /// Sum the results of exponentiating two points in EFq by elements of Fp.
71 /*!
72 \param[out] result target.
73 \param[in] base0 the first base.
74 \param[in] exp0 the first exponent.
75 \param[in] base1 the second base.
76 \param[in] exp1 the second exponent.
77 \returns 1 on success, 0 on failure
78 */
79 void EFqMultiExp(EccPointJacobiFq* result, EccPointJacobiFq const* base0,
80                  FpElem const* exp0, EccPointJacobiFq const* base1,
81                  FpElem const* exp1);
82 
83 /// Add two points in EFq.
84 /*!
85 \param[out] result of adding left and right.
86 \param[in] left The first operand to be added.
87 \param[in] right The second operand to be added.
88 \returns A value different from zero (i.e., true) if on success.
89          Zero (i.e., false) otherwise.
90 */
91 int EFqAffineAdd(EccPointFq* result, EccPointFq const* left,
92                  EccPointFq const* right);
93 
94 /// Double a point in EFq.
95 /*!
96 \param[out] result target.
97 \param[in] in the value to double.
98 \returns A value different from zero (i.e., true) if on success.
99          Zero (i.e., false) otherwise.
100 */
101 int EFqAffineDbl(EccPointFq* result, EccPointFq const* in);
102 
103 /// Double a point in EFq.
104 /*!
105 \param[out] result target.
106 \param[in] in the value to double.
107 */
108 void EFqDbl(EccPointJacobiFq* result, EccPointJacobiFq const* in);
109 
110 /// Add two points in EFq.
111 /*!
112 \param[out] result of adding left and right.
113 \param[in] left The first operand to be added.
114 \param[in] right The second operand to be added.
115 */
116 void EFqAdd(EccPointJacobiFq* result, EccPointJacobiFq const* left,
117             EccPointJacobiFq const* right);
118 
119 /// Generate a random point in EFq.
120 /*!
121 \param[in] result the random value.
122 \param[in] rnd_func Random number generator.
123 \param[in] rnd_param Pass through context data for rnd_func.
124 \returns A value different from zero (i.e., true) if on success.
125          Zero (i.e., false) otherwise.
126 */
127 int EFqRand(EccPointFq* result, BitSupplier rnd_func, void* rnd_param);
128 
129 /// Set a point's value.
130 /*!
131 \param[out] result target.
132 \param[in] x value to set.
133 \param[in] y value to set.
134 */
135 void EFqSet(EccPointJacobiFq* result, FqElem const* x, FqElem const* y);
136 
137 /// Test if a point is infinity.
138 /*!
139 \param[in] in the point to test.
140 \returns A value different from zero (i.e., true) indeed
141          the value is infinity. Zero (i.e., false) otherwise.
142 */
143 int EFqIsInf(EccPointJacobiFq const* in);
144 
145 /// Convert a point from Affine to Jacobi representation.
146 /*!
147 \param[out] result target.
148 \param[in] in value to set.
149 */
150 void EFqFromAffine(EccPointJacobiFq* result, EccPointFq const* in);
151 
152 /// Convert a point from Jacobi to Affine representation.
153 /*!
154 \param[out] result target.
155 \param[in] in value to set.
156 \returns A value different from zero (i.e., true) if on success.
157          Zero (i.e., false) otherwise.
158 */
159 int EFqToAffine(EccPointFq* result, EccPointJacobiFq const* in);
160 
161 /// Negate a point on EFq.
162 /*!
163 \param[out] result the negative of the element.
164 \param[in] in the element to negate.
165 */
166 void EFqNeg(EccPointJacobiFq* result, EccPointJacobiFq const* in);
167 
168 /// Test if two points on EFq are equal
169 /*!
170 \param[in] left The first operand to be tested.
171 \param[in] right The second operand to be tested.
172 \returns A value different from zero (i.e., true) if indeed
173          the values are equal. Zero (i.e., false) otherwise.
174 */
175 int EFqEq(EccPointJacobiFq const* left, EccPointJacobiFq const* right);
176 
177 /// Hashes an arbitrary message to a point on EFq.
178 /*!
179 \param[out] result target.
180 \param[in] msg buffer to reinterpret.
181 \param[in] len length of msg in bytes.
182 \param[in] hashalg hash algorithm to use.
183 \returns A value different from zero (i.e., true) if on success.
184          Zero (i.e., false) otherwise.
185 */
186 int EFqHash(EccPointFq* result, unsigned char const* msg, size_t len,
187             HashAlg hashalg);
188 
189 /// Copy a point's value
190 /*!
191 \param[out] result copy target.
192 \param[in] in copy source.
193 */
194 void EFqCp(EccPointFq* result, EccPointFq const* in);
195 
196 /// Test if two points on EFq are equal
197 /*!
198 \param[in] left The first operand to be tested.
199 \param[in] right The second operand to be tested.
200 \returns A value different from zero (i.e., true) if indeed
201          the values are equal. Zero (i.e., false) otherwise.
202 */
203 int EFqEqAffine(EccPointFq const* left, EccPointFq const* right);
204 
205 /// Conditionally Set a point's value to one of two values.
206 /*!
207 \param[out] result target.
208 \param[in] true_val value to set if condition is true.
209 \param[in] false_val value to set if condition is false.
210 \param[in] truth_val value of condition.
211 */
212 void EFqCondSet(EccPointJacobiFq* result, EccPointJacobiFq const* true_val,
213                 EccPointJacobiFq const* false_val, int truth_val);
214 
215 /// Copy a point's value
216 /*!
217 \param[out] result copy target.
218 \param[in] in copy source.
219 */
220 void EFqJCp(EccPointJacobiFq* result, EccPointJacobiFq const* in);
221 
222 /// Set an element's value to infinity.
223 /*!
224 \param[out] result element to set.
225 */
226 void EFqInf(EccPointJacobiFq* result);
227 
228 /// Test if a point is on EFq.
229 /*!
230 \param[in] in the point to test.
231 \returns A value different from zero (i.e., true) indeed
232          the point is on the curve. Zero (i.e., false) otherwise.
233 */
234 int EFqOnCurve(EccPointFq const* in);
235 
236 /// Test if a point is on EFq.
237 /*!
238 \param[in] in the point to test.
239 \returns A value different from zero (i.e., true) indeed
240          the point is on the curve. Zero (i.e., false) otherwise.
241 */
242 int EFqJOnCurve(EccPointJacobiFq const* in);
243 
244 /// Generate a random point in EFq.
245 /*!
246 \param[in] result the random value.
247 \param[in] rnd_func Random number generator.
248 \param[in] rnd_param Pass through context data for rnd_func.
249 \returns A value different from zero (i.e., true) if on success.
250          Zero (i.e., false) otherwise.
251 */
252 int EFqJRand(EccPointJacobiFq* result, BitSupplier rnd_func, void* rnd_param);
253 
254 #endif  // EPID_MEMBER_TINY_MATH_EFQ_H_
255