Skip to content

Commit

Permalink
Merge pull request #107 from clearmatics/103-plonk-simple-circuit-all…
Browse files Browse the repository at this point in the history
…-curves

Test simple Plonk circuit on all curves
  • Loading branch information
dtebbs authored Feb 3, 2023
2 parents 26cfe45 + 3603f95 commit cf7910d
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,7 @@ bls12_381_test_vector_transcript_hasher::
};
}

void bls12_381_test_vector_transcript_hasher::buffer_clear()
{
this->buffer.clear();
}

size_t bls12_381_test_vector_transcript_hasher::buffer_size()
{
return this->buffer.size();
}
void bls12_381_test_vector_transcript_hasher::reset() { this->buffer.clear(); }

void bls12_381_test_vector_transcript_hasher::add_element(
const libff::Fr<libff::bls12_381_pp> &element)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ class bls12_381_test_vector_transcript_hasher
libff::Fr<libff::bls12_381_pp> get_hash();

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

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

} // namespace libsnark
Expand Down
56 changes: 56 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,56 @@
/** @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 bytes 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
// bytes 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 reset();
};

} // namespace libsnark

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

#endif // LIBSNARK_ZK_PROOF_SYSTEMS_PLONK_DUMMY_TRANSCRIPT_HASHER_HPP_
94 changes: 94 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,94 @@
/** @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_TCC_
#define LIBSNARK_ZK_PROOF_SYSTEMS_PLONK_TESTS_DUMMY_TRANSCRIPT_HASHER_TCC_

#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>::reset()
{
this->buffer.clear();
}

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_TCC_
Loading

0 comments on commit cf7910d

Please sign in to comment.