Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_fields.cpp File Reference
Include dependency graph for test_fields.cpp:

Go to the source code of this file.

Functions

template<typename FieldT >
void test_field ()
 
template<typename FieldT >
void test_sqrt ()
 
template<typename FieldT >
void test_two_squarings ()
 
template<typename FieldT >
void test_Frobenius ()
 
template<typename FieldT >
void test_unitary_inverse ()
 
template<typename FieldT >
void test_cyclotomic_squaring ()
 
template<>
void test_cyclotomic_squaring< Fqk< edwards_pp > > ()
 
template<>
void test_cyclotomic_squaring< Fqk< mnt4_pp > > ()
 
template<>
void test_cyclotomic_squaring< Fqk< mnt6_pp > > ()
 
template<typename ppT >
void test_all_fields ()
 
template<typename Fp4T >
void test_Fp4_tom_cook ()
 
int main ()
 

Function Documentation

◆ main()

int main ( void )

Definition at line 220 of file test_fields.cpp.

221{
225
231
235
240
241#ifdef CURVE_BN128 // BN128 has fancy dependencies so it may be disabled
245#endif
246}
static void init_public_params()
static void init_public_params()
Definition bn128_pp.cpp:13
static void init_public_params()
static void init_public_params()
Definition mnt4_pp.cpp:18
static void init_public_params()
Definition mnt6_pp.cpp:18
void test_Fp4_tom_cook()
void test_field()
void test_all_fields()
void test_Frobenius()
void test_two_squarings()
void test_cyclotomic_squaring()
Here is the call graph for this function:

◆ test_all_fields()

template<typename ppT >
void test_all_fields ( )

Definition at line 139 of file test_fields.cpp.

140{
145
149
152
154}
void test_unitary_inverse()
void test_sqrt()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_cyclotomic_squaring()

template<typename FieldT >
void test_cyclotomic_squaring ( )
Here is the caller graph for this function:

◆ test_cyclotomic_squaring< Fqk< edwards_pp > >()

template<>
void test_cyclotomic_squaring< Fqk< edwards_pp > > ( )

Definition at line 103 of file test_fields.cpp.

104{
105 typedef Fqk<edwards_pp> FieldT;
106 assert(FieldT::extension_degree() % 2 == 0);
107 FieldT a = FieldT::random_element();
108 FieldT a_unitary = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse();
109 // beta = a^((q^(k/2)-1)*(q+1))
110 FieldT beta = a_unitary.Frobenius_map(1) * a_unitary;
111 assert(beta.cyclotomic_squared() == beta.squared());
112}
typename EC_ppT::Fqk_type Fqk
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181

◆ test_cyclotomic_squaring< Fqk< mnt4_pp > >()

template<>
void test_cyclotomic_squaring< Fqk< mnt4_pp > > ( )

Definition at line 115 of file test_fields.cpp.

116{
117 typedef Fqk<mnt4_pp> FieldT;
118 assert(FieldT::extension_degree() % 2 == 0);
119 FieldT a = FieldT::random_element();
120 FieldT a_unitary = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse();
121 // beta = a^(q^(k/2)-1)
122 FieldT beta = a_unitary;
123 assert(beta.cyclotomic_squared() == beta.squared());
124}

◆ test_cyclotomic_squaring< Fqk< mnt6_pp > >()

template<>
void test_cyclotomic_squaring< Fqk< mnt6_pp > > ( )

Definition at line 127 of file test_fields.cpp.

128{
129 typedef Fqk<mnt6_pp> FieldT;
130 assert(FieldT::extension_degree() % 2 == 0);
131 FieldT a = FieldT::random_element();
132 FieldT a_unitary = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse();
133 // beta = a^((q^(k/2)-1)*(q+1))
134 FieldT beta = a_unitary.Frobenius_map(1) * a_unitary;
135 assert(beta.cyclotomic_squared() == beta.squared());
136}

◆ test_field()

template<typename FieldT >
void test_field ( )

Definition at line 22 of file test_fields.cpp.

23{
24 bigint<1> rand1 = bigint<1>("76749407");
25 bigint<1> rand2 = bigint<1>("44410867");
26 bigint<1> randsum = bigint<1>("121160274");
27
28 FieldT zero = FieldT::zero();
29 FieldT one = FieldT::one();
30 FieldT a = FieldT::random_element();
31 FieldT a_ser;
32 a_ser = reserialize<FieldT>(a);
33 assert(a_ser == a);
34
35 FieldT b = FieldT::random_element();
36 FieldT c = FieldT::random_element();
37 FieldT d = FieldT::random_element();
38
39 assert(a != zero);
40 assert(a != one);
41
42 assert(a * a == a.squared());
43 assert((a + b).squared() == a.squared() + a*b + b*a + b.squared());
44 assert((a + b)*(c + d) == a*c + a*d + b*c + b*d);
45 assert(a - b == a + (-b));
46 assert(a - b == (-b) + a);
47
48 assert((a ^ rand1) * (a ^ rand2) == (a^randsum));
49
50 assert(a * a.inverse() == one);
51 assert((a + b) * c.inverse() == a * c.inverse() + (b.inverse() * c).inverse());
52
53}
std::string one()
T reserialize(const T &obj)
CK_ULONG d
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_Fp4_tom_cook()

template<typename Fp4T >
void test_Fp4_tom_cook ( )

Definition at line 157 of file test_fields.cpp.

158{
159 typedef typename Fp4T::my_Fp FieldT;
160 for (size_t i = 0; i < 100; ++i)
161 {
162 const Fp4T a = Fp4T::random_element();
163 const Fp4T b = Fp4T::random_element();
164 const Fp4T correct_res = a * b;
165
166 Fp4T res;
167
168 const FieldT
169 &a0 = a.c0.c0,
170 &a1 = a.c1.c0,
171 &a2 = a.c0.c1,
172 &a3 = a.c1.c1;
173
174 const FieldT
175 &b0 = b.c0.c0,
176 &b1 = b.c1.c0,
177 &b2 = b.c0.c1,
178 &b3 = b.c1.c1;
179
180 FieldT
181 &c0 = res.c0.c0,
182 &c1 = res.c1.c0,
183 &c2 = res.c0.c1,
184 &c3 = res.c1.c1;
185
186 const FieldT v0 = a0 * b0;
187 const FieldT v1 = (a0 + a1 + a2 + a3) * (b0 + b1 + b2 + b3);
188 const FieldT v2 = (a0 - a1 + a2 - a3) * (b0 - b1 + b2 - b3);
189 const FieldT v3 = (a0 + FieldT(2)*a1 + FieldT(4)*a2 + FieldT(8)*a3) * (b0 + FieldT(2)*b1 + FieldT(4)*b2 + FieldT(8)*b3);
190 const FieldT v4 = (a0 - FieldT(2)*a1 + FieldT(4)*a2 - FieldT(8)*a3) * (b0 - FieldT(2)*b1 + FieldT(4)*b2 - FieldT(8)*b3);
191 const FieldT v5 = (a0 + FieldT(3)*a1 + FieldT(9)*a2 + FieldT(27)*a3) * (b0 + FieldT(3)*b1 + FieldT(9)*b2 + FieldT(27)*b3);
192 const FieldT v6 = a3 * b3;
193
194 const FieldT beta = Fp4T::non_residue;
195
196 c0 = v0 + beta*(FieldT(4).inverse()*v0 - FieldT(6).inverse()*(v1 + v2) + FieldT(24).inverse() * (v3 + v4) - FieldT(5) * v6);
197 c1 = - FieldT(3).inverse()*v0 + v1 - FieldT(2).inverse()*v2 - FieldT(4).inverse()*v3 + FieldT(20).inverse() * v4 + FieldT(30).inverse() * v5 - FieldT(12) * v6 + beta * ( - FieldT(12).inverse() * (v0 - v1) + FieldT(24).inverse()*(v2 - v3) - FieldT(120).inverse() * (v4 - v5) - FieldT(3) * v6);
198 c2 = - (FieldT(5)*(FieldT(4).inverse()))* v0 + (FieldT(2)*(FieldT(3).inverse()))*(v1 + v2) - FieldT(24).inverse()*(v3 + v4) + FieldT(4)*v6 + beta*v6;
199 c3 = FieldT(12).inverse() * (FieldT(5)*v0 - FieldT(7)*v1) - FieldT(24).inverse()*(v2 - FieldT(7)*v3 + v4 + v5) + FieldT(15)*v6;
200
201 assert(res == correct_res);
202
203 // {v0, v3, v4, v5}
204 const FieldT u = (FieldT::one() - beta).inverse();
205 assert(v0 == u * c0 + beta * u * c2 - beta * u * FieldT(2).inverse() * v1 - beta * u * FieldT(2).inverse() * v2 + beta * v6);
206 assert(v3 == - FieldT(15) * u * c0 - FieldT(30) * u * c1 - FieldT(3) * (FieldT(4) + beta) * u * c2 - FieldT(6) * (FieldT(4) + beta) * u * c3 + (FieldT(24) - FieldT(3) * beta * FieldT(2).inverse()) * u * v1 + (-FieldT(8) + beta * FieldT(2).inverse()) * u * v2
207 - FieldT(3) * (-FieldT(16) + beta) * v6);
208 assert(v4 == - FieldT(15) * u * c0 + FieldT(30) * u * c1 - FieldT(3) * (FieldT(4) + beta) * u * c2 + FieldT(6) * (FieldT(4) + beta) * u * c3 + (FieldT(24) - FieldT(3) * beta * FieldT(2).inverse()) * u * v2 + (-FieldT(8) + beta * FieldT(2).inverse()) * u * v1
209 - FieldT(3) * (-FieldT(16) + beta) * v6);
210 assert(v5 == - FieldT(80) * u * c0 - FieldT(240) * u * c1 - FieldT(8) * (FieldT(9) + beta) * u * c2 - FieldT(24) * (FieldT(9) + beta) * u * c3 - FieldT(2) * (-FieldT(81) + beta) * u * v1 + (-FieldT(81) + beta) * u * v2
211 - FieldT(8) * (-FieldT(81) + beta) * v6);
212
213 // c0 + beta c2 - (beta v1)/2 - (beta v2)/ 2 - (-1 + beta) beta v6,
214 // -15 c0 - 30 c1 - 3 (4 + beta) c2 - 6 (4 + beta) c3 + (24 - (3 beta)/2) v1 + (-8 + beta/2) v2 + 3 (-16 + beta) (-1 + beta) v6,
215 // -15 c0 + 30 c1 - 3 (4 + beta) c2 + 6 (4 + beta) c3 + (-8 + beta/2) v1 + (24 - (3 beta)/2) v2 + 3 (-16 + beta) (-1 + beta) v6,
216 // -80 c0 - 240 c1 - 8 (9 + beta) c2 - 24 (9 + beta) c3 - 2 (-81 + beta) v1 + (-81 + beta) v2 + 8 (-81 + beta) (-1 + beta) v6
217 }
218}
Here is the caller graph for this function:

◆ test_Frobenius()

template<typename FieldT >
void test_Frobenius ( )

Definition at line 76 of file test_fields.cpp.

77{
78 FieldT a = FieldT::random_element();
79 assert(a.Frobenius_map(0) == a);
80 FieldT a_q = a ^ FieldT::base_field_char();
81 for (size_t power = 1; power < 10; ++power)
82 {
83 const FieldT a_qi = a.Frobenius_map(power);
84 assert(a_qi == a_q);
85
86 a_q = a_q ^ FieldT::base_field_char();
87 }
88}
FieldT power(const FieldT &base, const bigint< m > &exponent)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_sqrt()

template<typename FieldT >
void test_sqrt ( )

Definition at line 56 of file test_fields.cpp.

57{
58 for (size_t i = 0; i < 100; ++i)
59 {
60 FieldT a = FieldT::random_element();
61 FieldT asq = a.squared();
62 assert(asq.sqrt() == a || asq.sqrt() == -a);
63 }
64}
Here is the caller graph for this function:

◆ test_two_squarings()

template<typename FieldT >
void test_two_squarings ( )

Definition at line 67 of file test_fields.cpp.

68{
69 FieldT a = FieldT::random_element();
70 assert(a.squared() == a * a);
71 assert(a.squared() == a.squared_complex());
72 assert(a.squared() == a.squared_karatsuba());
73}
Here is the caller graph for this function:

◆ test_unitary_inverse()

template<typename FieldT >
void test_unitary_inverse ( )

Definition at line 91 of file test_fields.cpp.

92{
93 assert(FieldT::extension_degree() % 2 == 0);
94 FieldT a = FieldT::random_element();
95 FieldT aqcubed_minus1 = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse();
96 assert(aqcubed_minus1.inverse() == aqcubed_minus1.unitary_inverse());
97}
Here is the caller graph for this function: