aboutsummaryrefslogtreecommitdiff
path: root/epid/member/tiny/math/fq.h
blob: a813c41f491e50fb3a666901b2e45168a26c9965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*############################################################################
# Copyright 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
############################################################################*/
/// Definition of Fq math
/*! \file */

#ifndef EPID_MEMBER_TINY_MATH_FQ_H_
#define EPID_MEMBER_TINY_MATH_FQ_H_

#include <stddef.h>
#include <stdint.h>
#include "epid/common/bitsupplier.h"

/// \cond
typedef struct FqElem FqElem;
typedef struct VeryLargeInt VeryLargeInt;
/// \endcond

/// Test if an element is in Fq.
/*!
\param[in] in the element to test.
\returns A value different from zero (i.e., true) indeed
         the value is in the field. Zero (i.e., false) otherwise.
*/
int FqInField(FqElem const* in);

/// Add two elements of Fq.
/*!
\param[out] result of adding left and right.
\param[in] left The first operand to be added.
\param[in] right The second operand to be added.
*/
void FqAdd(FqElem* result, FqElem const* left, FqElem const* right);

/// Subtract two elements of Fq.
/*!
\param[out] result of subtracting left from right.
\param[in] left The operand to be subtracted from.
\param[in] right The operand to subtract.
*/
void FqSub(FqElem* result, FqElem const* left, FqElem const* right);

/// Multiply two elements of Fq.
/*!
\param[out] result of multiplying left and right.
\param[in] left The first operand to be multiplied.
\param[in] right The second operand to be multiplied.
*/
void FqMul(FqElem* result, FqElem const* left, FqElem const* right);

/// Exponentiate an element of Fq by a large integer.
/*!
\param[out] result target.
\param[in] base the base.
\param[in] exp the exponent.
*/
void FqExp(FqElem* result, FqElem const* base, VeryLargeInt const* exp);

/// Copy an element's value
/*!
\param[out] result copy target.
\param[in] in copy source.
*/
void FqCp(FqElem* result, FqElem const* in);

/// Test if an element is zero.
/*!
\param[in] value the element to test.
\returns A value different from zero (i.e., true) if indeed
         the value is zero. Zero (i.e., false) otherwise.
*/
int FqIsZero(FqElem const* value);

/// Invert an element of Fq.
/*!
\param[out] result the inverse of the element.
\param[in] in the element to invert.
*/
void FqInv(FqElem* result, FqElem const* in);

/// Negate an element of Fq.
/*!
This function was formerly called as FqConst.

\param[out] result the negative of the element.
\param[in] in the element to negate.
*/
void FqNeg(FqElem* result, FqElem const* in);

/// Square an element of Fq.
/*!
\param[out] result the square of the element.
\param[in] in the element to square.
*/
void FqSquare(FqElem* result, FqElem const* in);

/// Clear an element's value.
/*!
\param[out] result element to clear.
*/
void FqClear(FqElem* result);

/// Set an element's value.
/*!
\param[out] result target.
\param[in] in value to set.
*/
void FqSet(FqElem* result, uint32_t in);

/// Test if two elements in Fq are equal
/*!
\param[in] left The first operand to be tested.
\param[in] right The second operand to be tested.
\returns A value different from zero (i.e., true) if indeed
         the values are equal. Zero (i.e., false) otherwise.
*/
int FqEq(FqElem const* left, FqElem const* right);

/// Conditionally Set an element's value to one of two values.
/*!
\param[out] result target.
\param[in] true_val value to set if condition is true.
\param[in] false_val value to set if condition is false.
\param[in] truth_val value of condition.
*/
void FqCondSet(FqElem* result, FqElem const* true_val, FqElem const* false_val,
               int truth_val);

/// Compute the Square root of an element of Fq.
/*!
\param[out] result the square root of the element.
\param[in] in the element to find the square root of.
\returns A value different from zero (i.e., true) if the square root
         exists.  Zero (i.e., false) otherwise.
*/
int FqSqrt(FqElem* result, FqElem const* in);

/// Generate a random element of Fq.
/*!
\param[in] result the random value.
\param[in] rnd_func Random number generator.
\param[in] rnd_param Pass through context data for rnd_func.
\returns A value different from zero (i.e., true) if on success.
         Zero (i.e., false) otherwise.
*/
int FqRand(FqElem* result, BitSupplier rnd_func, void* rnd_param);

/// Reinterpret a buffer as an element of Fq
/*!
\param[out] result target.
\param[in] hash buffer to reinterpret.
\param[in] len length of hash in bytes.
*/
void FqFromHash(FqElem* result, unsigned char const* hash, size_t len);

#endif  // EPID_MEMBER_TINY_MATH_FQ_H_