Migrate libsnark test code to Google Test

This commit is contained in:
Jack Grigg 2017-10-10 15:58:35 +01:00
parent 24d98cece0
commit 054ae60645
No known key found for this signature in database
GPG Key ID: 665DBCD284F7DAFF
8 changed files with 139 additions and 135 deletions

View File

@ -13,6 +13,8 @@
#include "algebra/curves/mnt/mnt4/mnt4_pp.hpp" #include "algebra/curves/mnt/mnt4/mnt4_pp.hpp"
#include "algebra/curves/mnt/mnt6/mnt6_pp.hpp" #include "algebra/curves/mnt/mnt6/mnt6_pp.hpp"
#include <gtest/gtest.h>
using namespace libsnark; using namespace libsnark;
template<typename ppT> template<typename ppT>
@ -46,11 +48,11 @@ void pairing_test()
ans1.print(); ans1.print();
ans2.print(); ans2.print();
ans3.print(); ans3.print();
assert(ans1 == ans2); EXPECT_EQ(ans1, ans2);
assert(ans2 == ans3); EXPECT_EQ(ans2, ans3);
assert(ans1 != GT_one); EXPECT_NE(ans1, GT_one);
assert((ans1^Fr<ppT>::field_char()) == GT_one); EXPECT_EQ((ans1^Fr<ppT>::field_char()), GT_one);
printf("\n\n"); printf("\n\n");
} }
@ -70,7 +72,7 @@ void double_miller_loop_test()
const Fqk<ppT> ans_1 = ppT::miller_loop(prec_P1, prec_Q1); const Fqk<ppT> ans_1 = ppT::miller_loop(prec_P1, prec_Q1);
const Fqk<ppT> ans_2 = ppT::miller_loop(prec_P2, prec_Q2); const Fqk<ppT> ans_2 = ppT::miller_loop(prec_P2, prec_Q2);
const Fqk<ppT> ans_12 = ppT::double_miller_loop(prec_P1, prec_Q1, prec_P2, prec_Q2); const Fqk<ppT> ans_12 = ppT::double_miller_loop(prec_P1, prec_Q1, prec_P2, prec_Q2);
assert(ans_1 * ans_2 == ans_12); EXPECT_EQ(ans_1 * ans_2, ans_12);
} }
template<typename ppT> template<typename ppT>
@ -99,15 +101,15 @@ void affine_pairing_test()
ans1.print(); ans1.print();
ans2.print(); ans2.print();
ans3.print(); ans3.print();
assert(ans1 == ans2); EXPECT_EQ(ans1, ans2);
assert(ans2 == ans3); EXPECT_EQ(ans2, ans3);
assert(ans1 != GT_one); EXPECT_NE(ans1, GT_one);
assert((ans1^Fr<ppT>::field_char()) == GT_one); EXPECT_EQ((ans1^Fr<ppT>::field_char()), GT_one);
printf("\n\n"); printf("\n\n");
} }
int main(void) TEST(algebra, bilinearity)
{ {
start_profiling(); start_profiling();
edwards_pp::init_public_params(); edwards_pp::init_public_params();

View File

@ -14,6 +14,8 @@
#include "algebra/curves/alt_bn128/alt_bn128_pp.hpp" #include "algebra/curves/alt_bn128/alt_bn128_pp.hpp"
#include <sstream> #include <sstream>
#include <gtest/gtest.h>
using namespace libsnark; using namespace libsnark;
template<typename GroupT> template<typename GroupT>
@ -25,31 +27,31 @@ void test_mixed_add()
el = GroupT::zero(); el = GroupT::zero();
el.to_special(); el.to_special();
result = base.mixed_add(el); result = base.mixed_add(el);
assert(result == base + el); EXPECT_EQ(result, base + el);
base = GroupT::zero(); base = GroupT::zero();
el = GroupT::random_element(); el = GroupT::random_element();
el.to_special(); el.to_special();
result = base.mixed_add(el); result = base.mixed_add(el);
assert(result == base + el); EXPECT_EQ(result, base + el);
base = GroupT::random_element(); base = GroupT::random_element();
el = GroupT::zero(); el = GroupT::zero();
el.to_special(); el.to_special();
result = base.mixed_add(el); result = base.mixed_add(el);
assert(result == base + el); EXPECT_EQ(result, base + el);
base = GroupT::random_element(); base = GroupT::random_element();
el = GroupT::random_element(); el = GroupT::random_element();
el.to_special(); el.to_special();
result = base.mixed_add(el); result = base.mixed_add(el);
assert(result == base + el); EXPECT_EQ(result, base + el);
base = GroupT::random_element(); base = GroupT::random_element();
el = base; el = base;
el.to_special(); el.to_special();
result = base.mixed_add(el); result = base.mixed_add(el);
assert(result == base.dbl()); EXPECT_EQ(result, base.dbl());
} }
template<typename GroupT> template<typename GroupT>
@ -60,53 +62,53 @@ void test_group()
bigint<1> randsum = bigint<1>("121160274"); bigint<1> randsum = bigint<1>("121160274");
GroupT zero = GroupT::zero(); GroupT zero = GroupT::zero();
assert(zero == zero); EXPECT_EQ(zero, zero);
GroupT one = GroupT::one(); GroupT one = GroupT::one();
assert(one == one); EXPECT_EQ(one, one);
GroupT two = bigint<1>(2l) * GroupT::one(); GroupT two = bigint<1>(2l) * GroupT::one();
assert(two == two); EXPECT_EQ(two, two);
GroupT five = bigint<1>(5l) * GroupT::one(); GroupT five = bigint<1>(5l) * GroupT::one();
GroupT three = bigint<1>(3l) * GroupT::one(); GroupT three = bigint<1>(3l) * GroupT::one();
GroupT four = bigint<1>(4l) * GroupT::one(); GroupT four = bigint<1>(4l) * GroupT::one();
assert(two+five == three+four); EXPECT_EQ(two+five, three+four);
GroupT a = GroupT::random_element(); GroupT a = GroupT::random_element();
GroupT b = GroupT::random_element(); GroupT b = GroupT::random_element();
assert(one != zero); EXPECT_NE(one, zero);
assert(a != zero); EXPECT_NE(a, zero);
assert(a != one); EXPECT_NE(a, one);
assert(b != zero); EXPECT_NE(b, zero);
assert(b != one); EXPECT_NE(b, one);
assert(a.dbl() == a + a); EXPECT_EQ(a.dbl(), a + a);
assert(b.dbl() == b + b); EXPECT_EQ(b.dbl(), b + b);
assert(one.add(two) == three); EXPECT_EQ(one.add(two), three);
assert(two.add(one) == three); EXPECT_EQ(two.add(one), three);
assert(a + b == b + a); EXPECT_EQ(a + b, b + a);
assert(a - a == zero); EXPECT_EQ(a - a, zero);
assert(a - b == a + (-b)); EXPECT_EQ(a - b, a + (-b));
assert(a - b == (-b) + a); EXPECT_EQ(a - b, (-b) + a);
// handle special cases // handle special cases
assert(zero + (-a) == -a); EXPECT_EQ(zero + (-a), -a);
assert(zero - a == -a); EXPECT_EQ(zero - a, -a);
assert(a - zero == a); EXPECT_EQ(a - zero, a);
assert(a + zero == a); EXPECT_EQ(a + zero, a);
assert(zero + a == a); EXPECT_EQ(zero + a, a);
assert((a + b).dbl() == (a + b) + (b + a)); EXPECT_EQ((a + b).dbl(), (a + b) + (b + a));
assert(bigint<1>("2") * (a + b) == (a + b) + (b + a)); EXPECT_EQ(bigint<1>("2") * (a + b), (a + b) + (b + a));
assert((rand1 * a) + (rand2 * a) == (randsum * a)); EXPECT_EQ((rand1 * a) + (rand2 * a), (randsum * a));
assert(GroupT::order() * a == zero); EXPECT_EQ(GroupT::order() * a, zero);
assert(GroupT::order() * one == zero); EXPECT_EQ(GroupT::order() * one, zero);
assert((GroupT::order() * a) - a != zero); EXPECT_NE((GroupT::order() * a) - a, zero);
assert((GroupT::order() * one) - one != zero); EXPECT_NE((GroupT::order() * one) - one, zero);
test_mixed_add<GroupT>(); test_mixed_add<GroupT>();
} }
@ -115,7 +117,7 @@ template<typename GroupT>
void test_mul_by_q() void test_mul_by_q()
{ {
GroupT a = GroupT::random_element(); GroupT a = GroupT::random_element();
assert((GroupT::base_field_char()*a) == a.mul_by_q()); EXPECT_EQ((GroupT::base_field_char()*a), a.mul_by_q());
} }
template<typename GroupT> template<typename GroupT>
@ -129,13 +131,13 @@ void test_output()
ss << g; ss << g;
GroupT gg; GroupT gg;
ss >> gg; ss >> gg;
assert(g == gg); EXPECT_EQ(g, gg);
/* use a random point in next iteration */ /* use a random point in next iteration */
g = GroupT::random_element(); g = GroupT::random_element();
} }
} }
int main(void) TEST(algebra, groups)
{ {
edwards_pp::init_public_params(); edwards_pp::init_public_params();
test_group<G1<edwards_pp> >(); test_group<G1<edwards_pp> >();

View File

@ -7,9 +7,11 @@
#include "algebra/fields/bigint.hpp" #include "algebra/fields/bigint.hpp"
#include <gtest/gtest.h>
using namespace libsnark; using namespace libsnark;
void test_bigint() TEST(algebra, bigint)
{ {
static_assert(ULONG_MAX == 0xFFFFFFFFFFFFFFFFul, "unsigned long not 64-bit"); static_assert(ULONG_MAX == 0xFFFFFFFFFFFFFFFFul, "unsigned long not 64-bit");
static_assert(GMP_NUMB_BITS == 64, "GMP limb not 64-bit"); static_assert(GMP_NUMB_BITS == 64, "GMP limb not 64-bit");
@ -24,84 +26,72 @@ void test_bigint()
bigint<1> b1 = bigint<1>(b1_decimal); bigint<1> b1 = bigint<1>(b1_decimal);
bigint<2> b2 = bigint<2>(b2_decimal); bigint<2> b2 = bigint<2>(b2_decimal);
assert(b0.as_ulong() == 0ul); EXPECT_EQ(b0.as_ulong(), 0ul);
assert(b0.is_zero()); EXPECT_TRUE(b0.is_zero());
assert(b1.as_ulong() == 76749407ul); EXPECT_EQ(b1.as_ulong(), 76749407ul);
assert(!(b1.is_zero())); EXPECT_FALSE(b1.is_zero());
assert(b2.as_ulong() == 15747124762497195938ul); EXPECT_EQ(b2.as_ulong(), 15747124762497195938ul);
assert(!(b2.is_zero())); EXPECT_FALSE(b2.is_zero());
assert(b0 != b1); EXPECT_NE(b0, b1);
assert(!(b0 == b1)); EXPECT_FALSE(b0 == b1);
assert(b2.max_bits() == 128); EXPECT_EQ(b2.max_bits(), 128);
assert(b2.num_bits() == 99); EXPECT_EQ(b2.num_bits(), 99);
for (size_t i = 0; i < 128; i++) { for (size_t i = 0; i < 128; i++) {
assert(b2.test_bit(i) == (b2_binary[127-i] == '1')); EXPECT_EQ(b2.test_bit(i), (b2_binary[127-i] == '1'));
} }
bigint<3> b3 = b2 * b1; bigint<3> b3 = b2 * b1;
assert(b3 == bigint<3>(b3_decimal)); EXPECT_EQ(b3, bigint<3>(b3_decimal));
assert(!(b3.is_zero())); EXPECT_FALSE(b3.is_zero());
bigint<3> b3a { b3 }; bigint<3> b3a { b3 };
assert(b3a == bigint<3>(b3_decimal)); EXPECT_EQ(b3a, bigint<3>(b3_decimal));
assert(b3a == b3); EXPECT_EQ(b3a, b3);
assert(!(b3a.is_zero())); EXPECT_FALSE(b3a.is_zero());
mpz_t m3; mpz_t m3;
mpz_init(m3); mpz_init(m3);
b3.to_mpz(m3); b3.to_mpz(m3);
bigint<3> b3b { m3 }; bigint<3> b3b { m3 };
assert(b3b == b3); EXPECT_EQ(b3b, b3);
bigint<2> quotient; bigint<2> quotient;
bigint<2> remainder; bigint<2> remainder;
bigint<3>::div_qr(quotient, remainder, b3, b2); bigint<3>::div_qr(quotient, remainder, b3, b2);
assert(quotient.num_bits() < GMP_NUMB_BITS); EXPECT_LT(quotient.num_bits(), GMP_NUMB_BITS);
assert(quotient.as_ulong() == b1.as_ulong()); EXPECT_EQ(quotient.as_ulong(), b1.as_ulong());
bigint<1> b1inc = bigint<1>("76749408"); bigint<1> b1inc = bigint<1>("76749408");
bigint<1> b1a = quotient.shorten(b1inc, "test"); bigint<1> b1a = quotient.shorten(b1inc, "test");
assert(b1a == b1); EXPECT_EQ(b1a, b1);
assert(remainder.is_zero()); EXPECT_TRUE(remainder.is_zero());
remainder.limit(b2, "test"); remainder.limit(b2, "test");
try { EXPECT_THROW((void)(quotient.shorten(b1, "test")), std::domain_error);
(void)(quotient.shorten(b1, "test")); EXPECT_THROW(remainder.limit(remainder, "test"), std::domain_error);
assert(false);
} catch (std::domain_error) {}
try {
remainder.limit(remainder, "test");
assert(false);
} catch (std::domain_error) {}
bigint<1> br = bigint<1>("42"); bigint<1> br = bigint<1>("42");
b3 += br; b3 += br;
assert(b3 != b3a); EXPECT_NE(b3, b3a);
assert(b3 > b3a); EXPECT_GT(b3, b3a);
assert(!(b3a > b3)); EXPECT_FALSE(b3a > b3);
bigint<3>::div_qr(quotient, remainder, b3, b2); bigint<3>::div_qr(quotient, remainder, b3, b2);
assert(quotient.num_bits() < GMP_NUMB_BITS); EXPECT_LT(quotient.num_bits(), GMP_NUMB_BITS);
assert(quotient.as_ulong() == b1.as_ulong()); EXPECT_EQ(quotient.as_ulong(), b1.as_ulong());
assert(remainder.num_bits() < GMP_NUMB_BITS); EXPECT_LT(remainder.num_bits(), GMP_NUMB_BITS);
assert(remainder.as_ulong() == 42); EXPECT_EQ(remainder.as_ulong(), 42);
b3a.clear(); b3a.clear();
assert(b3a.is_zero()); EXPECT_TRUE(b3a.is_zero());
assert(b3a.num_bits() == 0); EXPECT_EQ(b3a.num_bits(), 0);
assert(!(b3.is_zero())); EXPECT_FALSE(b3.is_zero());
bigint<4> bx = bigint<4>().randomize(); bigint<4> bx = bigint<4>().randomize();
bigint<4> by = bigint<4>().randomize(); bigint<4> by = bigint<4>().randomize();
assert(!(bx == by)); EXPECT_FALSE(bx == by);
// TODO: test serialization // TODO: test serialization
} }
int main(void)
{
test_bigint();
return 0;
}

View File

@ -15,6 +15,8 @@
#include "algebra/fields/fp6_3over2.hpp" #include "algebra/fields/fp6_3over2.hpp"
#include "algebra/fields/fp12_2over3over2.hpp" #include "algebra/fields/fp12_2over3over2.hpp"
#include <gtest/gtest.h>
using namespace libsnark; using namespace libsnark;
template<typename FieldT> template<typename FieldT>
@ -29,25 +31,25 @@ void test_field()
FieldT a = FieldT::random_element(); FieldT a = FieldT::random_element();
FieldT a_ser; FieldT a_ser;
a_ser = reserialize<FieldT>(a); a_ser = reserialize<FieldT>(a);
assert(a_ser == a); EXPECT_EQ(a_ser, a);
FieldT b = FieldT::random_element(); FieldT b = FieldT::random_element();
FieldT c = FieldT::random_element(); FieldT c = FieldT::random_element();
FieldT d = FieldT::random_element(); FieldT d = FieldT::random_element();
assert(a != zero); EXPECT_NE(a, zero);
assert(a != one); EXPECT_NE(a, one);
assert(a * a == a.squared()); EXPECT_EQ(a * a, a.squared());
assert((a + b).squared() == a.squared() + a*b + b*a + b.squared()); EXPECT_EQ((a + b).squared(), a.squared() + a*b + b*a + b.squared());
assert((a + b)*(c + d) == a*c + a*d + b*c + b*d); EXPECT_EQ((a + b)*(c + d), a*c + a*d + b*c + b*d);
assert(a - b == a + (-b)); EXPECT_EQ(a - b, a + (-b));
assert(a - b == (-b) + a); EXPECT_EQ(a - b, (-b) + a);
assert((a ^ rand1) * (a ^ rand2) == (a^randsum)); EXPECT_EQ((a ^ rand1) * (a ^ rand2), (a^randsum));
assert(a * a.inverse() == one); EXPECT_EQ(a * a.inverse(), one);
assert((a + b) * c.inverse() == a * c.inverse() + (b.inverse() * c).inverse()); EXPECT_EQ((a + b) * c.inverse(), a * c.inverse() + (b.inverse() * c).inverse());
} }
@ -58,7 +60,7 @@ void test_sqrt()
{ {
FieldT a = FieldT::random_element(); FieldT a = FieldT::random_element();
FieldT asq = a.squared(); FieldT asq = a.squared();
assert(asq.sqrt() == a || asq.sqrt() == -a); EXPECT_TRUE(asq.sqrt() == a || asq.sqrt() == -a);
} }
} }
@ -66,21 +68,21 @@ template<typename FieldT>
void test_two_squarings() void test_two_squarings()
{ {
FieldT a = FieldT::random_element(); FieldT a = FieldT::random_element();
assert(a.squared() == a * a); EXPECT_EQ(a.squared(), a * a);
assert(a.squared() == a.squared_complex()); EXPECT_EQ(a.squared(), a.squared_complex());
assert(a.squared() == a.squared_karatsuba()); EXPECT_EQ(a.squared(), a.squared_karatsuba());
} }
template<typename FieldT> template<typename FieldT>
void test_Frobenius() void test_Frobenius()
{ {
FieldT a = FieldT::random_element(); FieldT a = FieldT::random_element();
assert(a.Frobenius_map(0) == a); EXPECT_EQ(a.Frobenius_map(0), a);
FieldT a_q = a ^ FieldT::base_field_char(); FieldT a_q = a ^ FieldT::base_field_char();
for (size_t power = 1; power < 10; ++power) for (size_t power = 1; power < 10; ++power)
{ {
const FieldT a_qi = a.Frobenius_map(power); const FieldT a_qi = a.Frobenius_map(power);
assert(a_qi == a_q); EXPECT_EQ(a_qi, a_q);
a_q = a_q ^ FieldT::base_field_char(); a_q = a_q ^ FieldT::base_field_char();
} }
@ -89,10 +91,10 @@ void test_Frobenius()
template<typename FieldT> template<typename FieldT>
void test_unitary_inverse() void test_unitary_inverse()
{ {
assert(FieldT::extension_degree() % 2 == 0); EXPECT_EQ(FieldT::extension_degree() % 2, 0);
FieldT a = FieldT::random_element(); FieldT a = FieldT::random_element();
FieldT aqcubed_minus1 = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse(); FieldT aqcubed_minus1 = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse();
assert(aqcubed_minus1.inverse() == aqcubed_minus1.unitary_inverse()); EXPECT_EQ(aqcubed_minus1.inverse(), aqcubed_minus1.unitary_inverse());
} }
template<typename FieldT> template<typename FieldT>
@ -102,36 +104,36 @@ template<>
void test_cyclotomic_squaring<Fqk<edwards_pp> >() void test_cyclotomic_squaring<Fqk<edwards_pp> >()
{ {
typedef Fqk<edwards_pp> FieldT; typedef Fqk<edwards_pp> FieldT;
assert(FieldT::extension_degree() % 2 == 0); EXPECT_EQ(FieldT::extension_degree() % 2, 0);
FieldT a = FieldT::random_element(); FieldT a = FieldT::random_element();
FieldT a_unitary = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse(); FieldT a_unitary = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse();
// beta = a^((q^(k/2)-1)*(q+1)) // beta = a^((q^(k/2)-1)*(q+1))
FieldT beta = a_unitary.Frobenius_map(1) * a_unitary; FieldT beta = a_unitary.Frobenius_map(1) * a_unitary;
assert(beta.cyclotomic_squared() == beta.squared()); EXPECT_EQ(beta.cyclotomic_squared(), beta.squared());
} }
template<> template<>
void test_cyclotomic_squaring<Fqk<mnt4_pp> >() void test_cyclotomic_squaring<Fqk<mnt4_pp> >()
{ {
typedef Fqk<mnt4_pp> FieldT; typedef Fqk<mnt4_pp> FieldT;
assert(FieldT::extension_degree() % 2 == 0); EXPECT_EQ(FieldT::extension_degree() % 2, 0);
FieldT a = FieldT::random_element(); FieldT a = FieldT::random_element();
FieldT a_unitary = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse(); FieldT a_unitary = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse();
// beta = a^(q^(k/2)-1) // beta = a^(q^(k/2)-1)
FieldT beta = a_unitary; FieldT beta = a_unitary;
assert(beta.cyclotomic_squared() == beta.squared()); EXPECT_EQ(beta.cyclotomic_squared(), beta.squared());
} }
template<> template<>
void test_cyclotomic_squaring<Fqk<mnt6_pp> >() void test_cyclotomic_squaring<Fqk<mnt6_pp> >()
{ {
typedef Fqk<mnt6_pp> FieldT; typedef Fqk<mnt6_pp> FieldT;
assert(FieldT::extension_degree() % 2 == 0); EXPECT_EQ(FieldT::extension_degree() % 2, 0);
FieldT a = FieldT::random_element(); FieldT a = FieldT::random_element();
FieldT a_unitary = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse(); FieldT a_unitary = a.Frobenius_map(FieldT::extension_degree()/2) * a.inverse();
// beta = a^((q^(k/2)-1)*(q+1)) // beta = a^((q^(k/2)-1)*(q+1))
FieldT beta = a_unitary.Frobenius_map(1) * a_unitary; FieldT beta = a_unitary.Frobenius_map(1) * a_unitary;
assert(beta.cyclotomic_squared() == beta.squared()); EXPECT_EQ(beta.cyclotomic_squared(), beta.squared());
} }
template<typename ppT> template<typename ppT>
@ -197,16 +199,16 @@ void test_Fp4_tom_cook()
c2 = - (FieldT(5)*(FieldT(4).inverse()))* v0 + (FieldT(2)*(FieldT(3).inverse()))*(v1 + v2) - FieldT(24).inverse()*(v3 + v4) + FieldT(4)*v6 + beta*v6; c2 = - (FieldT(5)*(FieldT(4).inverse()))* v0 + (FieldT(2)*(FieldT(3).inverse()))*(v1 + v2) - FieldT(24).inverse()*(v3 + v4) + FieldT(4)*v6 + beta*v6;
c3 = FieldT(12).inverse() * (FieldT(5)*v0 - FieldT(7)*v1) - FieldT(24).inverse()*(v2 - FieldT(7)*v3 + v4 + v5) + FieldT(15)*v6; c3 = FieldT(12).inverse() * (FieldT(5)*v0 - FieldT(7)*v1) - FieldT(24).inverse()*(v2 - FieldT(7)*v3 + v4 + v5) + FieldT(15)*v6;
assert(res == correct_res); EXPECT_EQ(res, correct_res);
// {v0, v3, v4, v5} // {v0, v3, v4, v5}
const FieldT u = (FieldT::one() - beta).inverse(); const FieldT u = (FieldT::one() - beta).inverse();
assert(v0 == u * c0 + beta * u * c2 - beta * u * FieldT(2).inverse() * v1 - beta * u * FieldT(2).inverse() * v2 + beta * v6); EXPECT_EQ(v0, u * c0 + beta * u * c2 - beta * u * FieldT(2).inverse() * v1 - beta * u * FieldT(2).inverse() * v2 + beta * v6);
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 EXPECT_EQ(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
- FieldT(3) * (-FieldT(16) + beta) * v6); - FieldT(3) * (-FieldT(16) + beta) * v6);
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 EXPECT_EQ(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
- FieldT(3) * (-FieldT(16) + beta) * v6); - FieldT(3) * (-FieldT(16) + beta) * v6);
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 EXPECT_EQ(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
- FieldT(8) * (-FieldT(81) + beta) * v6); - FieldT(8) * (-FieldT(81) + beta) * v6);
// c0 + beta c2 - (beta v1)/2 - (beta v2)/ 2 - (-1 + beta) beta v6, // c0 + beta c2 - (beta v1)/2 - (beta v2)/ 2 - (-1 + beta) beta v6,
@ -216,7 +218,7 @@ void test_Fp4_tom_cook()
} }
} }
int main(void) TEST(algebra, fields)
{ {
edwards_pp::init_public_params(); edwards_pp::init_public_params();
test_all_fields<edwards_pp>(); test_all_fields<edwards_pp>();

View File

@ -10,6 +10,8 @@
#include "common/profiling.hpp" #include "common/profiling.hpp"
#include "gadgetlib1/gadgets/hashes/sha256/sha256_gadget.hpp" #include "gadgetlib1/gadgets/hashes/sha256/sha256_gadget.hpp"
#include <gtest/gtest.h>
using namespace libsnark; using namespace libsnark;
template<typename FieldT> template<typename FieldT>
@ -35,10 +37,10 @@ void test_two_to_one()
f.generate_r1cs_witness(); f.generate_r1cs_witness();
output.generate_r1cs_witness(hash_bv); output.generate_r1cs_witness(hash_bv);
assert(pb.is_satisfied()); EXPECT_TRUE(pb.is_satisfied());
} }
int main(void) TEST(gadgetlib1, sha256)
{ {
start_profiling(); start_profiling();
default_ec_pp::init_public_params(); default_ec_pp::init_public_params();

View File

@ -15,6 +15,8 @@
#include "gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.hpp" #include "gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.hpp"
#include "gadgetlib1/gadgets/hashes/sha256/sha256_gadget.hpp" #include "gadgetlib1/gadgets/hashes/sha256/sha256_gadget.hpp"
#include <gtest/gtest.h>
using namespace libsnark; using namespace libsnark;
template<typename ppT> template<typename ppT>
@ -28,7 +30,7 @@ void test_all_merkle_tree_gadgets()
test_merkle_tree_check_update_gadget<FieldT, sha256_two_to_one_hash_gadget<FieldT> >(); test_merkle_tree_check_update_gadget<FieldT, sha256_two_to_one_hash_gadget<FieldT> >();
} }
int main(void) TEST(gadgetlib1, merkle_tree)
{ {
start_profiling(); start_profiling();

View File

@ -17,6 +17,8 @@
#include "reductions/r1cs_to_qap/r1cs_to_qap.hpp" #include "reductions/r1cs_to_qap/r1cs_to_qap.hpp"
#include "relations/constraint_satisfaction_problems/r1cs/examples/r1cs_examples.hpp" #include "relations/constraint_satisfaction_problems/r1cs/examples/r1cs_examples.hpp"
#include <gtest/gtest.h>
using namespace libsnark; using namespace libsnark;
template<typename FieldT> template<typename FieldT>
@ -28,7 +30,7 @@ void test_qap(const size_t qap_degree, const size_t num_inputs, const bool binar
See the transformation from R1CS to QAP for why this is the case. See the transformation from R1CS to QAP for why this is the case.
So we need that qap_degree >= num_inputs + 1. So we need that qap_degree >= num_inputs + 1.
*/ */
assert(num_inputs + 1 <= qap_degree); ASSERT_LE(num_inputs + 1, qap_degree);
enter_block("Call to test_qap"); enter_block("Call to test_qap");
const size_t num_constraints = qap_degree - num_inputs - 1; const size_t num_constraints = qap_degree - num_inputs - 1;
@ -51,7 +53,7 @@ void test_qap(const size_t qap_degree, const size_t num_inputs, const bool binar
leave_block("Generate constraint system and assignment"); leave_block("Generate constraint system and assignment");
enter_block("Check satisfiability of constraint system"); enter_block("Check satisfiability of constraint system");
assert(example.constraint_system.is_satisfied(example.primary_input, example.auxiliary_input)); EXPECT_TRUE(example.constraint_system.is_satisfied(example.primary_input, example.auxiliary_input));
leave_block("Check satisfiability of constraint system"); leave_block("Check satisfiability of constraint system");
const FieldT t = FieldT::random_element(), const FieldT t = FieldT::random_element(),
@ -72,17 +74,17 @@ void test_qap(const size_t qap_degree, const size_t num_inputs, const bool binar
leave_block("Compute QAP witness"); leave_block("Compute QAP witness");
enter_block("Check satisfiability of QAP instance 1"); enter_block("Check satisfiability of QAP instance 1");
assert(qap_inst_1.is_satisfied(qap_wit)); EXPECT_TRUE(qap_inst_1.is_satisfied(qap_wit));
leave_block("Check satisfiability of QAP instance 1"); leave_block("Check satisfiability of QAP instance 1");
enter_block("Check satisfiability of QAP instance 2"); enter_block("Check satisfiability of QAP instance 2");
assert(qap_inst_2.is_satisfied(qap_wit)); EXPECT_TRUE(qap_inst_2.is_satisfied(qap_wit));
leave_block("Check satisfiability of QAP instance 2"); leave_block("Check satisfiability of QAP instance 2");
leave_block("Call to test_qap"); leave_block("Call to test_qap");
} }
int main() TEST(relations, qap)
{ {
start_profiling(); start_profiling();

View File

@ -17,6 +17,8 @@
#include "relations/constraint_satisfaction_problems/r1cs/examples/r1cs_examples.hpp" #include "relations/constraint_satisfaction_problems/r1cs/examples/r1cs_examples.hpp"
#include "zk_proof_systems/ppzksnark/r1cs_ppzksnark/examples/run_r1cs_ppzksnark.hpp" #include "zk_proof_systems/ppzksnark/r1cs_ppzksnark/examples/run_r1cs_ppzksnark.hpp"
#include <gtest/gtest.h>
using namespace libsnark; using namespace libsnark;
template<typename ppT> template<typename ppT>
@ -28,12 +30,12 @@ void test_r1cs_ppzksnark(size_t num_constraints,
const bool test_serialization = true; const bool test_serialization = true;
r1cs_example<Fr<ppT> > example = generate_r1cs_example_with_binary_input<Fr<ppT> >(num_constraints, input_size); r1cs_example<Fr<ppT> > example = generate_r1cs_example_with_binary_input<Fr<ppT> >(num_constraints, input_size);
const bool bit = run_r1cs_ppzksnark<ppT>(example, test_serialization); const bool bit = run_r1cs_ppzksnark<ppT>(example, test_serialization);
assert(bit); EXPECT_TRUE(bit);
print_header("(leave) Test R1CS ppzkSNARK"); print_header("(leave) Test R1CS ppzkSNARK");
} }
int main() TEST(zk_proof_systems, r1cs_ppzksnark)
{ {
default_r1cs_ppzksnark_pp::init_public_params(); default_r1cs_ppzksnark_pp::init_public_params();
start_profiling(); start_profiling();