Skip to content

Commit

Permalink
plonk: added dummy transcript hasher specialized for all curves; adde…
Browse files Browse the repository at this point in the history
…d unit tests for the simple quadratic residue circuit for all curves (addresses issue #103)
  • Loading branch information
Vesselin Velichkov committed Jan 19, 2023
1 parent f2a21f2 commit 3fbb2a0
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 21 deletions.
59 changes: 59 additions & 0 deletions libsnark/zk_proof_systems/plonk/tests/dummy_transcript_hasher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/** @file
*****************************************************************************
* @author This file is part of libff, developed by Clearmatics Ltd
* (originally developed by SCIPR Lab) and contributors
* (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#ifndef LIBSNARK_ZK_PROOF_SYSTEMS_PLONK_TESTS_DUMMY_TRANSCRIPT_HASHER_HPP_
#define LIBSNARK_ZK_PROOF_SYSTEMS_PLONK_TESTS_DUMMY_TRANSCRIPT_HASHER_HPP_

#include "libsnark/zk_proof_systems/plonk/utils.hpp"

#include <array>

namespace libsnark
{

/// Implementation of a dummy transcript hasher interface (see
/// transcript_hasher.hpp). It returns the number of the elemnts in
/// the hash buffer as an Fr element. Specialized over the curve
/// field. See also class bls12_381_test_vector_transcript_hasher,
/// which is specific to the BLS12_381 curve.
template<typename ppT> class dummy_transcript_hasher
{
private:
// buffer accumulating data to be hashed
std::vector<uint8_t> buffer;

public:
dummy_transcript_hasher();

// Add an Fr element to the transcript buffer for hashing.
void add_element(const libff::Fr<ppT> &element);
// Add the coordinates of a G1 curve point to the transcript buffer for
// hashing.
void add_element(const libff::G1<ppT> &element);
// Add the coordinates of a G2 curve point to the transcript buffer for
// hashing.
void add_element(const libff::G2<ppT> &element);

// Dummy implementation of get_hash that simply returns the number
// of elements in the buffer as an Fr value for the purposes of
// unit testing. TODO: to be replaced by a call to a proper hash
// function e.g. SHA2, BLAKE, etc.
libff::Fr<ppT> get_hash();

// clear the buffer (for now only for testing)
void buffer_clear();

// get buffer size
size_t buffer_size();
};

} // namespace libsnark

#include "libsnark/zk_proof_systems/plonk/tests/dummy_transcript_hasher.tcc"

#endif // LIBSNARK_ZK_PROOF_SYSTEMS_PLONK_DUMMY_TRANSCRIPT_HASHER_HPP_
100 changes: 100 additions & 0 deletions libsnark/zk_proof_systems/plonk/tests/dummy_transcript_hasher.tcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/** @file
*****************************************************************************
* @author This file is part of libff, developed by Clearmatics Ltd
* (originally developed by SCIPR Lab) and contributors
* (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#ifndef LIBSNARK_ZK_PROOF_SYSTEMS_PLONK_TESTS_DUMMY_TRANSCRIPT_HASHER_CPP_
#define LIBSNARK_ZK_PROOF_SYSTEMS_PLONK_TESTS_DUMMY_TRANSCRIPT_HASHER_CPP_

#include "libsnark/zk_proof_systems/plonk/tests/dummy_transcript_hasher.hpp"

// Implementation of the dummy transcript hasher interface. See
// dummy_transcript_hasher.hpp.
namespace libsnark
{

template<typename ppT> dummy_transcript_hasher<ppT>::dummy_transcript_hasher()
{

}

template<typename ppT> void dummy_transcript_hasher<ppT>::buffer_clear()
{
this->buffer.clear();
}

template<typename ppT> size_t dummy_transcript_hasher<ppT>::buffer_size()
{
return this->buffer.size();
}

template<typename ppT>
void dummy_transcript_hasher<ppT>::add_element(const libff::Fr<ppT> &element)
{
// convert the Fr element into a string
std::string str;
{
std::ostringstream ss;
libff::field_write<libff::encoding_binary, libff::form_plain>(
element, ss);
str = ss.str();
}
// copy the string as a sequence of uint8_t elements at the end of
// the buffer
std::copy(str.begin(), str.end(), std::back_inserter(this->buffer));
}

template<typename ppT>
void dummy_transcript_hasher<ppT>::add_element(const libff::G1<ppT> &element)
{
libff::G1<ppT> element_aff(element);
element_aff.to_affine_coordinates();

// convert the affine coordinates of the curve point into a string
std::string str;
{
std::ostringstream ss;
libff::group_write<
libff::encoding_binary,
libff::form_plain,
libff::compression_off>(element_aff, ss);
str = ss.str();
}
// copy the string as a sequence of uint8_t elements at the end of
// the buffer
std::copy(str.begin(), str.end(), std::back_inserter(this->buffer));
}

template<typename ppT>
void dummy_transcript_hasher<ppT>::add_element(const libff::G2<ppT> &element)
{
libff::G2<ppT> element_aff(element);
element_aff.to_affine_coordinates();

// convert the affine coordinates of the curve point into a string
std::string str;
{
std::ostringstream ss;
libff::group_write<
libff::encoding_binary,
libff::form_plain,
libff::compression_off>(element_aff, ss);
str = ss.str();
}
// copy the string as a sequence of uint8_t elements at the end of
// the buffer
std::copy(str.begin(), str.end(), std::back_inserter(this->buffer));
}

template<typename ppT> libff::Fr<ppT> dummy_transcript_hasher<ppT>::get_hash()
{
libff::Fr<ppT> buffer_len = libff::Fr<ppT>(this->buffer.size());
return buffer_len;
}

} // namespace libsnark

#endif // LIBSNARK_ZK_PROOF_SYSTEMS_PLONK_TESTS_DUMMY_TRANSCRIPT_HASHER_CPP_
60 changes: 39 additions & 21 deletions libsnark/zk_proof_systems/plonk/tests/test_plonk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "libsnark/zk_proof_systems/plonk/prover.hpp"
#include "libsnark/zk_proof_systems/plonk/tests/bls12_381_test_vector_transcript_hasher.hpp"
#include "libsnark/zk_proof_systems/plonk/tests/dummy_transcript_hasher.hpp"
#include "libsnark/zk_proof_systems/plonk/verifier.hpp"

#include <algorithm>
Expand Down Expand Up @@ -1148,9 +1149,10 @@ template<typename ppT> void test_plonk_gates_matrix_transpose()
// ( x y / y / x)
// (a1 a2 b1 b2 c1 c2) -> (1 2 3 4 5 6)
// (c2 b2 b1 a2 c1 a1) -> (6 4 3 2 5 1)
template<typename ppT, class transcript_hasher>
void test_plonk_prepare_gates_matrix()
template<typename ppT, class transcript_hasher> void test_plonk_simple_circuit()
{
ppT::init_public_params();

using Field = libff::Fr<ppT>;

// 0 Arithmetization of test circuit y^2 = x mod r
Expand Down Expand Up @@ -1337,57 +1339,70 @@ void test_plonk_constants_k1_k2_bls12_381()
}
}

TEST(TestPlonkConstantsK1K2, Edwards)
TEST(TestPlonk, Edwards)
{
test_plonk_constants_k1_k2<libff::edwards_pp>();
test_plonk_random_constants_k1_k2<libff::edwards_pp>();
// TODO add test_plonk_simple_circuit
}

TEST(TestPlonkConstantsK1K2, Mnt4)
TEST(TestPlonk, BN128)
{
test_plonk_constants_k1_k2<libff::bn128_pp>();
test_plonk_random_constants_k1_k2<libff::bn128_pp>();
// TODO add test_plonk_simple_circuit
}

TEST(TestPlonk, Mnt4)
{
test_plonk_constants_k1_k2<libff::mnt4_pp>();
test_plonk_random_constants_k1_k2<libff::mnt4_pp>();
test_plonk_simple_circuit<
libff::mnt4_pp,
dummy_transcript_hasher<libff::mnt4_pp>>();
}

TEST(TestPlonkConstantsK1K2, Mnt6)
TEST(TestPlonk, Mnt6)
{
test_plonk_constants_k1_k2<libff::mnt6_pp>();
test_plonk_random_constants_k1_k2<libff::mnt6_pp>();
test_plonk_simple_circuit<
libff::mnt6_pp,
dummy_transcript_hasher<libff::mnt6_pp>>();
}

TEST(TestPlonkConstantsK1K2, BW6_761)
TEST(TestPlonk, BW6_761)
{
test_plonk_constants_k1_k2<libff::bw6_761_pp>();
test_plonk_random_constants_k1_k2<libff::bw6_761_pp>();
test_plonk_simple_circuit<
libff::bw6_761_pp,
dummy_transcript_hasher<libff::bw6_761_pp>>();
}

TEST(TestPlonkConstantsK1K2, BN128)
{
test_plonk_constants_k1_k2<libff::bn128_pp>();
test_plonk_random_constants_k1_k2<libff::bn128_pp>();
}

TEST(TestPlonkConstantsK1K2, ALT_BN128)
TEST(TestPlonk, ALT_BN128)
{
test_plonk_constants_k1_k2<libff::alt_bn128_pp>();
test_plonk_random_constants_k1_k2<libff::alt_bn128_pp>();
test_plonk_simple_circuit<
libff::alt_bn128_pp,
dummy_transcript_hasher<libff::alt_bn128_pp>>();
}

TEST(TestPlonkConstantsK1K2, BLS12_377)
TEST(TestPlonk, BLS12_377)
{
test_plonk_constants_k1_k2<libff::bls12_377_pp>();
test_plonk_random_constants_k1_k2<libff::bls12_377_pp>();
test_plonk_simple_circuit<
libff::bls12_377_pp,
dummy_transcript_hasher<libff::bls12_377_pp>>();
}

TEST(TestPlonkConstantsK1K2, BLS12_381)
TEST(TestPlonk, BLS12_381)
{
test_plonk_constants_k1_k2<libff::bls12_381_pp>();
test_plonk_random_constants_k1_k2<libff::bls12_381_pp>();
test_plonk_constants_k1_k2_bls12_381();
}

TEST(TestPlonk, BLS12_381)
{
test_plonk_srs<libff::bls12_381_pp>();
test_plonk_prover_rounds<
libff::bls12_381_pp,
Expand All @@ -1402,10 +1417,13 @@ TEST(TestPlonk, BLS12_381)
libff::bls12_381_pp,
bls12_381_test_vector_transcript_hasher>();
test_plonk_gates_matrix_transpose<libff::bls12_381_pp>();
test_plonk_prepare_gates_matrix<
test_plonk_prepare_gates_matrix<libff::bls12_381_pp>();
test_plonk_simple_circuit<
libff::bls12_381_pp,
bls12_381_test_vector_transcript_hasher>();
test_plonk_prepare_gates_matrix<libff::bls12_381_pp>();
test_plonk_simple_circuit<
libff::bls12_381_pp,
dummy_transcript_hasher<libff::bls12_381_pp>>();
}

} // namespace libsnark

0 comments on commit 3fbb2a0

Please sign in to comment.