Original HUSH source code based on ZEC 1.0.8 . For historical purposes only!
https://hush.is
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.6 KiB
121 lines
3.6 KiB
/**
|
|
*****************************************************************************
|
|
* @author This file is part of libsnark, developed by SCIPR Lab
|
|
* and contributors (see AUTHORS).
|
|
* @copyright MIT license (see LICENSE file)
|
|
*****************************************************************************/
|
|
#include "common/profiling.hpp"
|
|
#ifdef CURVE_BN128
|
|
#include "algebra/curves/bn128/bn128_pp.hpp"
|
|
#endif
|
|
#include "algebra/curves/alt_bn128/alt_bn128_pp.hpp"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
using namespace libsnark;
|
|
|
|
template<typename ppT>
|
|
void pairing_test()
|
|
{
|
|
GT<ppT> GT_one = GT<ppT>::one();
|
|
|
|
printf("Running bilinearity tests:\n");
|
|
G1<ppT> P = (Fr<ppT>::random_element()) * G1<ppT>::one();
|
|
//G1<ppT> P = Fr<ppT>("2") * G1<ppT>::one();
|
|
G2<ppT> Q = (Fr<ppT>::random_element()) * G2<ppT>::one();
|
|
//G2<ppT> Q = Fr<ppT>("3") * G2<ppT>::one();
|
|
|
|
printf("P:\n");
|
|
P.print();
|
|
P.print_coordinates();
|
|
printf("Q:\n");
|
|
Q.print();
|
|
Q.print_coordinates();
|
|
printf("\n\n");
|
|
|
|
Fr<ppT> s = Fr<ppT>::random_element();
|
|
//Fr<ppT> s = Fr<ppT>("2");
|
|
G1<ppT> sP = s * P;
|
|
G2<ppT> sQ = s * Q;
|
|
|
|
printf("Pairing bilinearity tests (three must match):\n");
|
|
GT<ppT> ans1 = ppT::reduced_pairing(sP, Q);
|
|
GT<ppT> ans2 = ppT::reduced_pairing(P, sQ);
|
|
GT<ppT> ans3 = ppT::reduced_pairing(P, Q)^s;
|
|
ans1.print();
|
|
ans2.print();
|
|
ans3.print();
|
|
EXPECT_EQ(ans1, ans2);
|
|
EXPECT_EQ(ans2, ans3);
|
|
|
|
EXPECT_NE(ans1, GT_one);
|
|
EXPECT_EQ((ans1^Fr<ppT>::field_char()), GT_one);
|
|
printf("\n\n");
|
|
}
|
|
|
|
template<typename ppT>
|
|
void double_miller_loop_test()
|
|
{
|
|
const G1<ppT> P1 = (Fr<ppT>::random_element()) * G1<ppT>::one();
|
|
const G1<ppT> P2 = (Fr<ppT>::random_element()) * G1<ppT>::one();
|
|
const G2<ppT> Q1 = (Fr<ppT>::random_element()) * G2<ppT>::one();
|
|
const G2<ppT> Q2 = (Fr<ppT>::random_element()) * G2<ppT>::one();
|
|
|
|
const G1_precomp<ppT> prec_P1 = ppT::precompute_G1(P1);
|
|
const G1_precomp<ppT> prec_P2 = ppT::precompute_G1(P2);
|
|
const G2_precomp<ppT> prec_Q1 = ppT::precompute_G2(Q1);
|
|
const G2_precomp<ppT> prec_Q2 = ppT::precompute_G2(Q2);
|
|
|
|
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_12 = ppT::double_miller_loop(prec_P1, prec_Q1, prec_P2, prec_Q2);
|
|
EXPECT_EQ(ans_1 * ans_2, ans_12);
|
|
}
|
|
|
|
template<typename ppT>
|
|
void affine_pairing_test()
|
|
{
|
|
GT<ppT> GT_one = GT<ppT>::one();
|
|
|
|
printf("Running bilinearity tests:\n");
|
|
G1<ppT> P = (Fr<ppT>::random_element()) * G1<ppT>::one();
|
|
G2<ppT> Q = (Fr<ppT>::random_element()) * G2<ppT>::one();
|
|
|
|
printf("P:\n");
|
|
P.print();
|
|
printf("Q:\n");
|
|
Q.print();
|
|
printf("\n\n");
|
|
|
|
Fr<ppT> s = Fr<ppT>::random_element();
|
|
G1<ppT> sP = s * P;
|
|
G2<ppT> sQ = s * Q;
|
|
|
|
printf("Pairing bilinearity tests (three must match):\n");
|
|
GT<ppT> ans1 = ppT::affine_reduced_pairing(sP, Q);
|
|
GT<ppT> ans2 = ppT::affine_reduced_pairing(P, sQ);
|
|
GT<ppT> ans3 = ppT::affine_reduced_pairing(P, Q)^s;
|
|
ans1.print();
|
|
ans2.print();
|
|
ans3.print();
|
|
EXPECT_EQ(ans1, ans2);
|
|
EXPECT_EQ(ans2, ans3);
|
|
|
|
EXPECT_NE(ans1, GT_one);
|
|
EXPECT_EQ((ans1^Fr<ppT>::field_char()), GT_one);
|
|
printf("\n\n");
|
|
}
|
|
|
|
TEST(algebra, bilinearity)
|
|
{
|
|
start_profiling();
|
|
alt_bn128_pp::init_public_params();
|
|
pairing_test<alt_bn128_pp>();
|
|
double_miller_loop_test<alt_bn128_pp>();
|
|
|
|
#ifdef CURVE_BN128 // BN128 has fancy dependencies so it may be disabled
|
|
bn128_pp::init_public_params();
|
|
pairing_test<bn128_pp>();
|
|
double_miller_loop_test<bn128_pp>();
|
|
#endif
|
|
}
|
|
|