Skip to content

Commit

Permalink
partial proof function added #13
Browse files Browse the repository at this point in the history
  • Loading branch information
vo-nil committed Sep 17, 2024
1 parent c3ea5d2 commit 67470e1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ namespace nil {
bool generate_to_file(
boost::filesystem::path proof_file_,
boost::filesystem::path json_file_,
std::optional<boost::filesystem::path> challenge_file_,
std::optional<boost::filesystem::path> theta_power_file,
bool partial_proof,
bool skip_verification) {
if (!nil::proof_generator::can_write_to_file(proof_file_.string())) {
BOOST_LOG_TRIVIAL(error) << "Can't write to file " << proof_file_;
Expand All @@ -191,17 +188,16 @@ namespace nil {
BOOST_ASSERT(lpc_scheme_);

BOOST_LOG_TRIVIAL(info) << "Generating proof...";
auto prover = nil::crypto3::zk::snark::placeholder_prover<BlueprintField, PlaceholderParams>(
*public_preprocessed_data_,
*private_preprocessed_data_,
*table_description_,
*constraint_system_,
*lpc_scheme_,
partial_proof);
Proof proof = prover.process();
Proof proof = nil::crypto3::zk::snark::placeholder_prover<BlueprintField, PlaceholderParams>::process(
*public_preprocessed_data_,
*private_preprocessed_data_,
*table_description_,
*constraint_system_,
*lpc_scheme_
);
BOOST_LOG_TRIVIAL(info) << "Proof generated";

if (skip_verification || partial_proof) {
if (skip_verification) {
BOOST_LOG_TRIVIAL(info) << "Skipping proof verification";
} else {
if (!verify(proof)) {
Expand Down Expand Up @@ -239,50 +235,90 @@ namespace nil {
.generate_input(*public_inputs_, proof, constraint_system_->public_input_sizes());
output_file->close();

if (partial_proof) {
if (!challenge_file_) {
BOOST_LOG_TRIVIAL(error) << "Challenge output file is not set.";
return false;
}
if (!theta_power_file) {
BOOST_LOG_TRIVIAL(error) << "Theta power file is not set.";
return false;
}
BOOST_LOG_TRIVIAL(info) << "Writing challenge";
using challenge_marshalling_type =
nil::crypto3::marshalling::types::field_element<
TTypeBase, typename BlueprintField::value_type>;

challenge_marshalling_type marshalled_challenge(proof.eval_proof.challenge);

bool res =
detail::encode_marshalling_to_file<challenge_marshalling_type>(
*challenge_file_,
marshalled_challenge);
if (res) {
BOOST_LOG_TRIVIAL(info) << "Challenge written.";
} else {
BOOST_LOG_TRIVIAL(error) << "Failed to write challenge to file.";
}
return res;
}

auto commitment_scheme = prover.get_commitment_scheme();
// The caller must call the preprocessor or load the preprocessed data before calling this function.
bool generate_partial_proof_to_file(
boost::filesystem::path proof_file_,
std::optional<boost::filesystem::path> challenge_file_,
std::optional<boost::filesystem::path> theta_power_file) {
if (!nil::proof_generator::can_write_to_file(proof_file_.string())) {
BOOST_LOG_TRIVIAL(error) << "Can't write to file " << proof_file_;
return false;
}

commitment_scheme.state_commited(crypto3::zk::snark::FIXED_VALUES_BATCH);
commitment_scheme.state_commited(crypto3::zk::snark::VARIABLE_VALUES_BATCH);
commitment_scheme.state_commited(crypto3::zk::snark::PERMUTATION_BATCH);
commitment_scheme.state_commited(crypto3::zk::snark::QUOTIENT_BATCH);
commitment_scheme.state_commited(crypto3::zk::snark::LOOKUP_BATCH);
commitment_scheme.mark_batch_as_fixed(crypto3::zk::snark::FIXED_VALUES_BATCH);
BOOST_ASSERT(public_preprocessed_data_);
BOOST_ASSERT(private_preprocessed_data_);
BOOST_ASSERT(table_description_);
BOOST_ASSERT(constraint_system_);
BOOST_ASSERT(lpc_scheme_);

commitment_scheme.set_fixed_polys_values(common_data_->commitment_scheme_data);
BOOST_LOG_TRIVIAL(info) << "Generating proof...";
auto prover = nil::crypto3::zk::snark::placeholder_prover<BlueprintField, PlaceholderParams>(
*public_preprocessed_data_,
*private_preprocessed_data_,
*table_description_,
*constraint_system_,
*lpc_scheme_,
true);
Proof proof = prover.process();
BOOST_LOG_TRIVIAL(info) << "Proof generated";

BOOST_LOG_TRIVIAL(info) << "Writing proof to " << proof_file_;
auto filled_placeholder_proof =
nil::crypto3::marshalling::types::fill_placeholder_proof<Endianness, Proof>(proof, lpc_scheme_->get_fri_params());
bool res = nil::proof_generator::detail::encode_marshalling_to_file(
proof_file_,
filled_placeholder_proof,
true
);
if (res) {
BOOST_LOG_TRIVIAL(info) << "Proof written.";
} else {
BOOST_LOG_TRIVIAL(error) << "Failed to write proof to file.";
}

if (!challenge_file_) {
BOOST_LOG_TRIVIAL(error) << "Challenge output file is not set.";
return false;
}
if (!theta_power_file) {
BOOST_LOG_TRIVIAL(error) << "Theta power file is not set.";
return false;
}
BOOST_LOG_TRIVIAL(info) << "Writing challenge";
using challenge_marshalling_type =
nil::crypto3::marshalling::types::field_element<
TTypeBase, typename BlueprintField::value_type>;

std::size_t theta_power = commitment_scheme.compute_theta_power_for_combined_Q();
challenge_marshalling_type marshalled_challenge(proof.eval_proof.challenge);

auto output_file = open_file<std::ofstream>(theta_power_file->string(), std::ios_base::out);
(*output_file) << theta_power << std::endl;
output_file->close();
res = detail::encode_marshalling_to_file<challenge_marshalling_type>(
*challenge_file_, marshalled_challenge);
if (res) {
BOOST_LOG_TRIVIAL(info) << "Challenge written.";
} else {
BOOST_LOG_TRIVIAL(error) << "Failed to write challenge to file.";
}

auto commitment_scheme = prover.get_commitment_scheme();

commitment_scheme.state_commited(crypto3::zk::snark::FIXED_VALUES_BATCH);
commitment_scheme.state_commited(crypto3::zk::snark::VARIABLE_VALUES_BATCH);
commitment_scheme.state_commited(crypto3::zk::snark::PERMUTATION_BATCH);
commitment_scheme.state_commited(crypto3::zk::snark::QUOTIENT_BATCH);
commitment_scheme.state_commited(crypto3::zk::snark::LOOKUP_BATCH);
commitment_scheme.mark_batch_as_fixed(crypto3::zk::snark::FIXED_VALUES_BATCH);

commitment_scheme.set_fixed_polys_values(common_data_->commitment_scheme_data);

std::size_t theta_power = commitment_scheme.compute_theta_power_for_combined_Q();

auto output_file = open_file<std::ofstream>(theta_power_file->string(), std::ios_base::out);
(*output_file) << theta_power << std::endl;
output_file->close();

return res;
}

Expand Down
13 changes: 2 additions & 11 deletions proof-producer/bin/proof-producer/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ int run_prover(const nil::proof_generator::ProverOptions& prover_options) {
prover.generate_to_file(
prover_options.proof_file_path,
prover_options.json_file_path,
std::nullopt,
std::nullopt,
false, /* partial proof */
false/*don't skip verification*/) &&
prover.save_preprocessed_common_data_to_file(prover_options.preprocessed_common_data_path) &&
prover.save_public_preprocessed_data_to_file(prover_options.preprocessed_public_data_path) &&
Expand All @@ -79,9 +76,6 @@ int run_prover(const nil::proof_generator::ProverOptions& prover_options) {
prover.generate_to_file(
prover_options.proof_file_path,
prover_options.json_file_path,
std::nullopt,
std::nullopt,
false, /*partial proof*/
true/*skip verification*/);
break;
case nil::proof_generator::detail::ProverStage::PARTIAL_PROVE:
Expand All @@ -93,13 +87,10 @@ int run_prover(const nil::proof_generator::ProverOptions& prover_options) {
prover.read_preprocessed_common_data_from_file(prover_options.preprocessed_common_data_path) &&
prover.read_commitment_scheme_from_file(prover_options.commitment_scheme_state_path) &&
prover.preprocess_private_data() &&
prover.generate_to_file(
prover.generate_partial_proof_to_file(
prover_options.proof_file_path,
prover_options.json_file_path,
prover_options.challenge_file_path,
prover_options.theta_power_file_path,
true, /* partial proof */
true/*skip verification*/);
prover_options.theta_power_file_path);
break;
case nil::proof_generator::detail::ProverStage::VERIFY:
prover_result =
Expand Down

0 comments on commit 67470e1

Please sign in to comment.