forked from relic-toolkit/relic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelic_fbx.h
214 lines (188 loc) · 6.56 KB
/
relic_fbx.h
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* RELIC is an Efficient LIbrary for Cryptography
* Copyright (C) 2007-2015 RELIC Authors
*
* This file is part of RELIC. RELIC is legal property of its developers,
* whose names are not listed here. Please refer to the COPYRIGHT file
* for contact information.
*
* RELIC is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* RELIC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with RELIC. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @defgroup fbx Extensions of binary fields
*/
/**
* @file
*
* Interface of the module for extension field arithmetic over binary fields.
*
* @ingroup fbx
*/
#ifndef RELIC_FBX_H
#define RELIC_FBX_H
#include "relic_fb.h"
#include "relic_types.h"
/*============================================================================*/
/* Type definitions */
/*============================================================================*/
/**
* Represents a quadratic extension binary field element.
*
* This extension field is constructed with the basis {1, s}, where s is a
* quadratic non-residue in the binary field.
*/
typedef fb_t fb2_t[2];
/*============================================================================*/
/* Macro definitions */
/*============================================================================*/
/**
* Initializes a quadratic extension binary field with a null value.
*
* @param[out] A - the quadratic extension element to initialize.
*/
#define fb2_null(A) \
fb_null(A[0]); fb_null(A[1]); \
/**
* Calls a function to allocate a quadratic extension binary field element.
*
* @param[out] A - the new quadratic extension field element.
*/
#define fb2_new(A) \
fb_new(A[0]); fb_new(A[1]); \
/**
* Calls a function to free a quadratic extension binary field element.
*
* @param[out] A - the quadratic extension field element to free.
*/
#define fb2_free(A) \
fb_free(A[0]); fb_free(A[1]); \
/**
* Copies the second argument to the first argument.
*
* @param[out] C - the result.
* @param[in] A - the quadratic extension field element to copy.
*/
#define fb2_copy(C, A) \
fb_copy(C[0], A[0]); fb_copy(C[1], A[1]); \
/**
* Negates a quadratic extension field element.
*
* f@param[out] C - the result.
* @param[out] A - the quadratic extension field element to negate.
*/
#define fb2_neg(C, A) \
fb_neg(C[0], A[0]); fb_neg(C[1], A[1]); \
/**
* Assigns zero to a quadratic extension field element.
*
* @param[out] A - the quadratic extension field element to zero.
*/
#define fb2_zero(A) \
fb_zero(A[0]); fb_zero(A[1]); \
/**
* Tests if a quadratic extension field element is zero or not.
*
* @param[in] A - the quadratic extension field element to test.
* @return 1 if the argument is zero, 0 otherwise.
*/
#define fb2_is_zero(A) \
(fb_is_zero(A[0]) && fb_is_zero(A[1])) \
/**
* Assigns a random value to a quadratic extension field element.
*
* @param[out] A - the quadratic extension field element to assign.
*/
#define fb2_rand(A) \
fb_rand(A[0]); fb_rand(A[1]); \
/**
* Prints a quadratic extension field element to standard output.
*
* @param[in] A - the quadratic extension field element to print.
*/
#define fb2_print(A) \
fb_print(A[0]); fb_print(A[1]); \
/**
* Returns the result of a comparison between two quadratic extension field
* elements
*
* @param[in] A - the first quadratic extension field element.
* @param[in] B - the second quadratic extension field element.
* @return CMP_NE if a != b, CMP_EQ if a == b.
*/
#define fb2_cmp(A, B) \
((fb_cmp(A[0], B[0]) == CMP_EQ) && (fb_cmp(A[1], B[1]) == CMP_EQ) \
? CMP_EQ : CMP_NE) \
/**
* Adds two quadratic extension field elements. Computes c = a + b.
*
* @param[out] C - the result.
* @param[in] A - the first quadratic extension field element.
* @param[in] B - the second quadratic extension field element.
*/
#define fb2_add(C, A, B) \
fb_add(C[0], A[0], B[0]); fb_add(C[1], A[1], B[1]); \
/**
* Subtracts a quadratic extension field element from another. Computes
* c = a - b.
*
* @param[out] C - the result.
* @param[in] A - the quadratic extension binary field element.
* @param[in] B - the quadratic extension binary field element.
*/
#define fb2_sub(C, A, B) \
fb_sub(C[0], A[0], B[0]); fb_sub(C[1], A[1], B[1]); \
/*============================================================================*/
/* Function prototypes */
/*============================================================================*/
/**
* Multiples two quadratic extension field elements. Computes c = a * b.
*
* @param[out] c - the result.
* @param[in] a - the quadratic extension binary field element.
* @param[in] b - the quadratic extension binary field element.
*/
void fb2_mul(fb2_t c, fb2_t a, fb2_t b);
/**
* Multiples a quadratic extension field element by a quadratic non-residue.
* Computes c = a * s.
*
* @param[out] c - the result.
* @param[in] a - the quadratic extension binary field element.
* @param[in] b - the quadratic extension binary field element.
*/
void fb2_mul_nor(fb2_t c, fb2_t a);
/**
* Computes the square of a quadratic extension field element. Computes
* c = a * a.
*
* @param[out] c - the result.
* @param[in] a - the binary field element to square.
*/
void fb2_sqr(fb2_t c, fb2_t a);
/**
* Solves a quadratic equation for c, Tr(a) = 0. Computes c such that
* c^2 + c = a.
*
* @param[out] c - the result.
* @param[in] a - the quadratic extension field element.
*/
void fb2_slv(fb2_t c, fb2_t a);
/**
* Inverts a quadratic extension field element. Computes c = a^{-1}.
*
* @param[out] c - the result.
* @param[in] a - the quadratic extension field element to invert.
*/
void fb2_inv(fb2_t c, fb2_t a);
#endif /* !RELIC_FBX_H */