Skip to content

Commit

Permalink
Redshift generator and prover inited.
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaskov committed Oct 30, 2021
1 parent a10e968 commit 84b0593
Show file tree
Hide file tree
Showing 14 changed files with 721 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ZK_PLONK_PARAMS_HPP
#define CRYPTO3_ZK_PLONK_PARAMS_HPP
#ifndef CRYPTO3_ZK_PLONK_BATCHED_KATE_PARAMS_HPP
#define CRYPTO3_ZK_PLONK_BATCHED_KATE_PARAMS_HPP

#include <memory>

Expand Down Expand Up @@ -95,4 +95,4 @@ namespace nil {
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_R1CS_GG_PPZKSNARK_BASIC_PROVER_HPP
#endif // CRYPTO3_ZK_PLONK_BATCHED_KATE_PARAMS_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ZK_PLONK_PROOF_HPP
#define CRYPTO3_ZK_PLONK_PROOF_HPP
#ifndef CRYPTO3_ZK_PLONK_BATCHED_KATE_PROOF_HPP
#define CRYPTO3_ZK_PLONK_BATCHED_KATE_PROOF_HPP

#include <nil/crypto3/zk/snark/commitments/fri_commitment.hpp>
#include <nil/crypto3/zk/snark/commitments/kate_commitment.hpp>
#include <nil/crypto3/zk/snark/commitments/batched_kate_commitment.hpp>

namespace nil {
namespace crypto3 {
Expand All @@ -39,12 +38,12 @@ namespace nil {
class plonk_proof;

template<typename TCurve>
struct plonk_proof<TCurve, kate_commitment> {
struct plonk_proof<TCurve, batched_kate_commitment_scheme<...>> {
std::vector<std::uint8_t> data;
};
} // namespace snark
} // namespace zk
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_PLONK_PROOF_HPP
#endif // CRYPTO3_ZK_PLONK_BATCHED_KATE_PROOF_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ZK_PLONK_PROVER_HPP
#define CRYPTO3_ZK_PLONK_PROVER_HPP
#ifndef CRYPTO3_ZK_PLONK_BATCHED_KATE_PROVER_HPP
#define CRYPTO3_ZK_PLONK_BATCHED_KATE_PROVER_HPP

#include <nil/crypto3/zk/snark/commitments/fri_commitment.hpp>
#include <nil/crypto3/zk/snark/commitments/batched_kate_commitment.hpp>
#include <nil/crypto3/zk/snark/relations/constraint_satisfaction_problems/r1cs.hpp>

namespace nil {
Expand All @@ -35,15 +35,13 @@ namespace nil {
namespace snark {

template<typename TCurve,
typename TConstraintSystem,
typename TCommitment>
class plonk_prover;

template<typename TCurve,
typename TConstraintSystem>
class plonk_prover<TCurve, TConstraintSystem, kate_commitment> {

using commitment_scheme = kate_commitment;
template<typename TCurve>
class plonk_prover<TCurve, batched_kate_commitment_scheme<...>> {
using commitment_scheme_type = batched_kate_commitment_scheme<...>;
using constraint_system_type = plonk_constraint_system<typename TCurve::scalar_field_type>;

size_t n;

Expand All @@ -55,9 +53,9 @@ namespace nil {
std::vector<std::unique_ptr<widget::TransitionWidgetBase<typename TCurve::scalar_field_type>>> transition_widgets;
transcript::StandardTranscript transcript;

std::shared_ptr<plonk_proving_key<TCurve, TConstraintSystem>> key;
std::shared_ptr<plonk_proving_key<TCurve, commitment_scheme_type>> key;
std::shared_ptr<program_witness> witness;
std::unique_ptr<CommitmentScheme> commitment_scheme;
std::unique_ptr<commitment_scheme_type> commitment;

work_queue queue;
bool uses_quotient_mid;
Expand All @@ -66,7 +64,7 @@ namespace nil {

public:

plonk_prover(std::shared_ptr<plonk_proving_key<TCurve, TConstraintSystem>> input_key,
plonk_prover(std::shared_ptr<plonk_proving_key<TCurve, commitment_scheme_type>> input_key,
std::shared_ptr<program_witness> input_witness,
const transcript::Manifest& input_manifest)
: n(input_key == nullptr ? 0 : input_key->n)
Expand Down Expand Up @@ -94,7 +92,7 @@ namespace nil {
transcript = other.transcript;
key = std::move(other.key);
witness = std::move(other.witness);
commitment_scheme = std::move(other.commitment_scheme);
commitment = std::move(other.commitment);

queue = work_queue(key.get(), witness.get(), &transcript);
return *this;
Expand All @@ -105,7 +103,7 @@ namespace nil {
, transcript(other.transcript)
, key(std::move(other.key))
, witness(std::move(other.witness))
, commitment_scheme(std::move(other.commitment_scheme))
, commitment(std::move(other.commitment))
, queue(key.get(), witness.get(), &transcript)
{
for (size_t i = 0; i < other.random_widgets.size(); ++i) {
Expand All @@ -122,7 +120,7 @@ namespace nil {
std::string commit_tag = "W_" + std::to_string(i + 1);
typename TCurve::scalar_field_type::value_type* coefficients =
witness->wires.at(wire_tag).get_coefficients();
commitment_scheme->commit(coefficients, commit_tag,
commitment->commit(coefficients, commit_tag,
typename TCurve::scalar_field_type::value_type::zero(), queue);
}

Expand Down Expand Up @@ -178,7 +176,7 @@ namespace nil {
const size_t offset = n * i;
typename TCurve::scalar_field_type::value_type* coefficients = &key->quotient_large.get_coefficients()[offset];
std::string quotient_tag = "T_" + std::to_string(i + 1);
commitment_scheme->commit(coefficients, quotient_tag,
commitment->commit(coefficients, quotient_tag,
typename TCurve::scalar_field_type::value_type::zero(), queue);
}

Expand All @@ -187,7 +185,7 @@ namespace nil {
std::string quotient_tag = "T_" + std::to_string(program_width);
typename TCurve::scalar_field_type::value_type program_flag =
program_width == 3 ? typename TCurve::scalar_field_type::value_type::one() : typename TCurve::scalar_field_type::value_type::zero();
commitment_scheme->commit(coefficients, quotient_tag, program_flag, queue);
commitment->commit(coefficients, quotient_tag, program_flag, queue);
}

void execute_preamble_round() {
Expand Down Expand Up @@ -330,7 +328,7 @@ namespace nil {
queue.flush_queue();
transcript.apply_fiat_shamir("nu");

commitment_scheme->batch_open(transcript, queue, key, witness);
commitment->batch_open(transcript, queue, key, witness);
}

typename TCurve::scalar_field_type::value_type compute_linearisation_coefficients() {
Expand All @@ -340,7 +338,7 @@ namespace nil {

math::polynomial& r = key->linear_poly;

commitment_scheme->add_opening_evaluations_to_transcript(transcript, key, witness, false);
commitment->add_opening_evaluations_to_transcript(transcript, key, witness, false);
typename TCurve::scalar_field_type::value_type t_eval = key->quotient_large.evaluate(zeta, 4 * n);

if constexpr (use_linearisation) {
Expand Down Expand Up @@ -391,4 +389,4 @@ namespace nil {
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_PLONK_PROVER_HPP
#endif // CRYPTO3_ZK_PLONK_BATCHED_KATE_PROVER_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_PLONK_PROVING_KEY_HPP
#define CRYPTO3_PLONK_PROVING_KEY_HPP
#ifndef CRYPTO3_PLONK_BATCHED_KATE_PROVING_KEY_HPP
#define CRYPTO3_PLONK_BATCHED_KATE_PROVING_KEY_HPP

#include <nil/crypto3/zk/snark/commitments/fri_commitment.hpp>
#include <nil/crypto3/zk/snark/commitments/batched_kate_commitment.hpp>
#include <nil/crypto3/zk/snark/relations/constraint_satisfaction_problems/r1cs.hpp>

namespace nil {
namespace crypto3 {
namespace zk {
namespace snark {

template<typename TCurve
typename TConstraintSystem = plonk_constraint_system<typename TCurve::scalar_field_type>>
struct plonk_proving_key_data {
template<typename TCurve, typename TCommitment>
struct plonk_proving_key_data;

template<typename TCurve>
struct plonk_proving_key_data<TCurve, batched_kate_commitment_scheme<...>> {
std::uint32_t n;
std::uint32_t num_public_inputs;
bool contains_recursive_proof;
Expand All @@ -60,9 +62,11 @@ namespace nil {
lhs.recursive_proof_public_input_indices == rhs.recursive_proof_public_input_indices;
}

template<typename TCurve,
typename TConstraintSystem = plonk_constraint_system<typename TCurve::scalar_field_type>>
class plonk_proving_key {
template<typename TCurve, typename TCommitment>
class plonk_proving_key;

template<typename TCurve>
class plonk_proving_key<TCurve, batched_kate_commitment_scheme<...>> {
constexpr static const std::size_t scalar_bytes = TCurve::scalar_field_type::value_bits/BYTE_BITS;

std::size_t n;
Expand Down Expand Up @@ -307,4 +311,4 @@ namespace nil {
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_PLONK_PROVING_KEY_HPP
#endif // CRYPTO3_PLONK_BATCHED_KATE_PROVING_KEY_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_PLONK_VERIFICATION_KEY_HPP
#define CRYPTO3_PLONK_VERIFICATION_KEY_HPP
#ifndef CRYPTO3_PLONK_BATCHED_KATE_VERIFICATION_KEY_HPP
#define CRYPTO3_PLONK_BATCHED_KATE_VERIFICATION_KEY_HPP

#include <nil/crypto3/zk/snark/commitments/fri_commitment.hpp>
#include <nil/crypto3/zk/snark/commitments/batched_kate_commitment.hpp>
#include <nil/crypto3/zk/snark/relations/constraint_satisfaction_problems/r1cs.hpp>

namespace nil {
namespace crypto3 {
namespace zk {
namespace snark {

template<typename TCurve
typename TConstraintSystem = plonk_constraint_system<typename TCurve::scalar_field_type>>
struct plonk_verification_key_data {
template<typename TCurve, typename TCommitment>
struct plonk_verification_key_data;

template<typename TCurve>
struct plonk_verification_key_data<TCurve, batched_kate_commitment_scheme<...>> {
std::uint32_t n;
std::uint32_t num_public_inputs;
std::map<std::string, typename TCurve::template g1_type<>::value_type> constraint_selectors;
Expand All @@ -52,9 +54,13 @@ namespace nil {
lhs.permutation_selectors == rhs.permutation_selectors;
}

template<typename TCurve,
typename TConstraintSystem = plonk_constraint_system<typename TCurve::scalar_field_type>>
class plonk_verification_key {
template<typename TCurve, typename TCommitment>
class plonk_verification_key;

template<typename TCurve>
class plonk_verification_key<TCurve, batched_kate_commitment_scheme<...>> {
using commitment_scheme_type = batched_kate_commitment_scheme<...>;

constexpr static const std::size_t scalar_bytes = TCurve::scalar_field_type::value_bits/BYTE_BITS;

std::size_t n;
Expand Down Expand Up @@ -88,7 +94,7 @@ namespace nil {
, reference_string(crs)
{}

plonk_verification_key(plonk_verification_key_data<TCurve, TConstraintSystem>&& data,
plonk_verification_key(plonk_verification_key_data<TCurve, commitment_scheme_type>&& data,
std::shared_ptr<VerifierReferenceString> const& crs)
: n(data.n)
, num_public_inputs(data.num_public_inputs)
Expand Down Expand Up @@ -155,4 +161,4 @@ namespace nil {
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_PLONK_VERIFICATION_KEY_HPP
#endif // CRYPTO3_PLONK_BATCHED_KATE_VERIFICATION_KEY_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ZK_PLONK_VERIFIER_HPP
#define CRYPTO3_ZK_PLONK_VERIFIER_HPP
#ifndef CRYPTO3_ZK_PLONK_BATCHED_KATE_VERIFIER_HPP
#define CRYPTO3_ZK_PLONK_BATCHED_KATE_VERIFIER_HPP

#include <nil/crypto3/zk/snark/commitments/fri_commitment.hpp>
#include <nil/crypto3/zk/snark/commitments/kate_commitment.hpp>
#include <nil/crypto3/zk/snark/relations/constraint_satisfaction_problems/r1cs.hpp>

Expand All @@ -36,23 +35,24 @@ namespace nil {
namespace snark {

template<typename TCurve,
typename TConstraintSystem,
typename TCommitment>
class plonk_verifier;

template<typename TCurve,
typename TConstraintSystem>
class plonk_verifier<TCurve, TConstraintSystem, kate_commitment> {
template<typename TCurve>
class plonk_verifier<TCurve, batched_kate_commitment_scheme<...>> {
using commitment_scheme_type = batched_kate_commitment_scheme<...>;
using constraint_system_type = plonk_constraint_system<typename TCurve::scalar_field_type>;

transcript::Manifest manifest;

std::shared_ptr<plonk_verification_key<TCurve, TConstraintSystem>> key;
std::shared_ptr<plonk_verification_key<TCurve, commitment_scheme_type>> key;
std::map<std::string, typename TCurve::g1_type<affine>::value_type> kate_g1_elements;
std::map<std::string, typename TCurve::scalar_field_type::value_type> kate_fr_elements;
std::unique_ptr<CommitmentScheme> commitment_scheme;
std::unique_ptr<commitment_scheme_type> commitment;

public:

plonk_verifier(std::shared_ptr<plonk_verification_key<TCurve, TConstraintSystem>> verifier_key,
plonk_verifier(std::shared_ptr<plonk_verification_key<TCurve, commitment_scheme_type>> verifier_key,
const transcript::Manifest& input_manifest)
: manifest(input_manifest)
, key(verifier_key)
Expand All @@ -61,7 +61,7 @@ namespace nil {
plonk_verifier& operator=(plonk_verifier&& other) {
key = other.key;
manifest = other.manifest;
commitment_scheme = (std::move(other.commitment_scheme));
commitment = (std::move(other.commitment));
kate_g1_elements.clear();
kate_fr_elements.clear();
return *this;
Expand All @@ -70,7 +70,7 @@ namespace nil {
plonk_verifier(plonk_verifier &&other)
: manifest(other.manifest)
, key(other.key)
, commitment_scheme(std::move(other.commitment_scheme))
, commitment(std::move(other.commitment))
{}

bool validate_commitments() {
Expand Down Expand Up @@ -167,7 +167,7 @@ namespace nil {
// Note that we do not actually compute the scalar multiplications but just accumulate the scalars
// and the group elements in different vectors.
//
commitment_scheme->batch_verify(transcript, kate_g1_elements, kate_fr_elements, key);
commitment->batch_verify(transcript, kate_g1_elements, kate_fr_elements, key);

// Step 9: Compute partial opening batch commitment [D]_1:
// [D]_1 = (a_eval.b_eval.[qM]_1 + a_eval.[qL]_1 + b_eval.[qR]_1 + c_eval.[qO]_1 + [qC]_1) * nu_{linear} * α
Expand Down Expand Up @@ -279,4 +279,4 @@ namespace nil {
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_PLONK_VERIFIER_HPP
#endif // CRYPTO3_ZK_PLONK_BATCHED_KATE_VERIFIER_HPP
Loading

1 comment on commit 84b0593

@nkaskov
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#20

Please sign in to comment.