Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 9dc6995

Browse files
committed
Public input gate added#180
1 parent a8eaa51 commit 9dc6995

File tree

4 files changed

+154
-5
lines changed

4 files changed

+154
-5
lines changed

include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/gates_argument.hpp>
4646
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/params.hpp>
4747
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp>
48+
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/public_input.hpp>
4849

4950
namespace nil {
5051
namespace crypto3 {
@@ -92,7 +93,7 @@ namespace nil {
9293
constexpr static const std::size_t gate_parts = 1;
9394
constexpr static const std::size_t permutation_parts = 3;
9495
constexpr static const std::size_t lookup_parts = 6;
95-
constexpr static const std::size_t f_parts = 8;
96+
constexpr static const std::size_t f_parts = 9;
9697

9798
public:
9899

@@ -184,6 +185,12 @@ namespace nil {
184185
transcript
185186
)[0];
186187

188+
_F_dfs[8] = public_input_processor<ParamsType>::prove(
189+
constraint_system.public_input_gate(),
190+
preprocessed_public_data.common_data,
191+
_polynomial_table, transcript
192+
);
193+
187194
/////TEST
188195
#ifdef ZK_PLACEHOLDER_DEBUG_ENABLED
189196
placeholder_debug_output();
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2021 Mikhail Komarov <[email protected]>
3+
// Copyright (c) 2021 Nikita Kaskov <[email protected]>
4+
// Copyright (c) 2022 Ilia Shirobokov <[email protected]>
5+
// Copyright (c) 2022 Alisa Cherniaeva <[email protected]>
6+
// Copyright (c) 2023 Elena Tatuzova <[email protected]>
7+
//
8+
// MIT License
9+
//
10+
// Permission is hereby granted, free of charge, to any person obtaining a copy
11+
// of this software and associated documentation files (the "Software"), to deal
12+
// in the Software without restriction, including without limitation the rights
13+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
// copies of the Software, and to permit persons to whom the Software is
15+
// furnished to do so, subject to the following conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be included in all
18+
// copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
// SOFTWARE.
27+
//---------------------------------------------------------------------------//
28+
29+
#ifndef CRYPTO3_ZK_PLONK_PLACEHOLDER_PUBLIC_INPUT_HPP
30+
#define CRYPTO3_ZK_PLONK_PLACEHOLDER_PUBLIC_INPUT_HPP
31+
32+
#include <nil/crypto3/math/polynomial/polynomial.hpp>
33+
#include <nil/crypto3/math/polynomial/shift.hpp>
34+
#include <nil/crypto3/math/domains/evaluation_domain.hpp>
35+
#include <nil/crypto3/math/algorithms/make_evaluation_domain.hpp>
36+
37+
#include <nil/crypto3/hash/sha2.hpp>
38+
39+
#include <nil/crypto3/container/merkle/tree.hpp>
40+
41+
#include <nil/crypto3/zk/transcript/fiat_shamir.hpp>
42+
#include <nil/crypto3/zk/snark/arithmetization/plonk/lookup_constraint.hpp>
43+
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/params.hpp>
44+
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/detail/placeholder_policy.hpp>
45+
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp>
46+
47+
namespace nil {
48+
namespace crypto3 {
49+
namespace zk {
50+
namespace snark {
51+
template<typename PlaceholderParams>
52+
struct public_input_processor{
53+
using field_type = typename PlaceholderParams::field_type;
54+
using public_input_gate_type = typename PlaceholderParams::constraint_system_type::public_input_gate_type;
55+
using common_data_type = const typename placeholder_public_preprocessor<field_type, PlaceholderParams>::preprocessed_data_type::common_data_type;
56+
using policy_type = detail::placeholder_policy<field_type, PlaceholderParams>;
57+
using assignment_type = plonk_polynomial_dfs_table<field_type, typename PlaceholderParams::arithmetization_params>;
58+
using transcript_type = typename transcript::fiat_shamir_heuristic_sequential<typename PlaceholderParams::transcript_hash_type>;
59+
using variable_type = plonk_variable<typename field_type::value_type>;
60+
using proof_type = placeholder_proof<field_type, PlaceholderParams>;
61+
62+
static inline math::polynomial_dfs<typename field_type::value_type> prove(
63+
const public_input_gate_type &public_input_gate,
64+
const common_data_type &common_data,
65+
const assignment_type &assignments,
66+
transcript_type &transcript
67+
){
68+
math::polynomial_dfs<typename field_type::value_type> result;
69+
if(public_input_gate.size() == 0){
70+
return result;
71+
}
72+
auto alpha = transcript.template challenge<field_type>();
73+
for(std::size_t i = 0; i < public_input_gate.size(); i++){
74+
const auto &var = public_input_gate[i];
75+
math::polynomial_dfs<typename field_type::value_type> l;
76+
77+
if(var.type == variable_type::witness){
78+
l = assignments.witness(var.index);
79+
} else if (var.type == variable_type::public_input){
80+
l = assignments.public_input(var.index);
81+
} else if (var.type == variable_type::constant){
82+
l = assignments.constant(var.index);
83+
} else if (var.type == variable_type::selector){
84+
l = assignments.selector(var.index);
85+
} else {
86+
}
87+
l -= typename field_type::value_type(l[0]);
88+
l *= math::polynomial_shift(common_data.lagrange_0, var.rotation, common_data.basic_domain->m);
89+
result *= alpha;
90+
result += l;
91+
}
92+
return result;
93+
}
94+
95+
static inline typename field_type::value_type verify(
96+
const std::vector<typename field_type::value_type> &public_input,
97+
typename policy_type::evaluation_map &columns_at_y,
98+
typename field_type::value_type challenge,
99+
const public_input_gate_type &public_input_gate,
100+
const common_data_type &common_data,
101+
transcript_type &transcript
102+
){
103+
if(public_input_gate.size() == 0){
104+
return field_type::value_type::zero();
105+
}
106+
BOOST_ASSERT(public_input_gate.size() == public_input.size());
107+
108+
typename field_type::value_type result;
109+
auto alpha = transcript.template challenge<field_type>();
110+
111+
for(std::size_t i = 0; i < public_input_gate.size(); i++){
112+
const auto &var = public_input_gate[i];
113+
auto key = std::tuple(var.index, var.rotation, var.type);
114+
auto value = columns_at_y[key] - public_input[i];
115+
value *= math::polynomial_shift(common_data.lagrange_0, var.rotation, common_data.basic_domain->m).evaluate(challenge);
116+
result *= alpha;
117+
result += value;
118+
}
119+
return result;
120+
}
121+
};
122+
} // namespace snark
123+
} // namespace zk
124+
} // namespace crypto3
125+
} // namespace nil
126+
127+
#endif // #ifndef CRYPTO3_ZK_PLONK_PLACEHOLDER_PUBLIC_INPUT_ARGUMENT_HPP

include/nil/crypto3/zk/snark/systems/plonk/placeholder/verifier.hpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp>
3737
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/params.hpp>
3838
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp>
39+
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/public_input.hpp>
3940

4041
namespace nil {
4142
namespace crypto3 {
@@ -59,7 +60,7 @@ namespace nil {
5960
constexpr static const std::size_t gate_parts = 1;
6061
constexpr static const std::size_t permutation_parts = 3;
6162
constexpr static const std::size_t lookup_parts = 4;
62-
constexpr static const std::size_t f_parts = 8;
63+
constexpr static const std::size_t f_parts = 9;
6364

6465
public:
6566
static void generate_evaluation_points(
@@ -138,6 +139,8 @@ namespace nil {
138139
commitment_scheme_type commitment_scheme,
139140
const std::array<std::vector<typename FieldType::value_type>, ParamsType::arithmetization_params::public_input_columns> public_input
140141
){
142+
BOOST_ASSERT(constraint_system.public_input_gate().size() == 0);
143+
141144
// TODO: process rotations for public input.
142145
auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1);
143146
auto challenge = proof.eval_proof.challenge;
@@ -157,12 +160,13 @@ namespace nil {
157160
}
158161
return process(preprocessed_public_data, proof, constraint_system, commitment_scheme);
159162
}
160-
163+
161164
static inline bool process(
162165
const typename public_preprocessor_type::preprocessed_data_type &preprocessed_public_data,
163166
const placeholder_proof<FieldType, ParamsType> &proof,
164167
const plonk_constraint_system<FieldType, typename ParamsType::arithmetization_params> &constraint_system,
165-
commitment_scheme_type commitment_scheme
168+
commitment_scheme_type commitment_scheme,
169+
const std::vector<typename FieldType::value_type> public_input = {}
166170
) {
167171
// 1. Add circuit definition to transcript
168172
// transcript(short_description);
@@ -283,6 +287,13 @@ namespace nil {
283287
placeholder_gates_argument<FieldType, ParamsType>::verify_eval(
284288
constraint_system.gates(), columns_at_y, proof.eval_proof.challenge, transcript);
285289

290+
// 7. public input argument
291+
typename FieldType::value_type public_input_argument = public_input_processor<ParamsType>::verify(
292+
public_input, columns_at_y,
293+
proof.eval_proof.challenge, constraint_system.public_input_gate(),
294+
preprocessed_public_data.common_data, transcript
295+
);
296+
286297
std::array<typename FieldType::value_type, f_parts> alphas =
287298
transcript.template challenges<FieldType, f_parts>();
288299

@@ -318,6 +329,7 @@ namespace nil {
318329
F[5] = lookup_argument[2];
319330
F[6] = lookup_argument[3];
320331
F[7] = gate_argument[0];
332+
F[8] = public_input_argument;
321333

322334
typename FieldType::value_type F_consolidated = FieldType::value_type::zero();
323335
for (std::size_t i = 0; i < f_parts; i++) {

test/systems/plonk/placeholder/placeholder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,11 @@ BOOST_FIXTURE_TEST_CASE(prover_test, test_initializer) {
707707
preprocessed_public_data, preprocessed_private_data, desc, constraint_system, assignments, lpc_scheme);
708708

709709
bool verifier_res = placeholder_verifier<field_type, lpc_placeholder_params_type>::process(
710-
preprocessed_public_data, proof, constraint_system, lpc_scheme);
710+
preprocessed_public_data, proof, constraint_system, lpc_scheme, {1,0}); // check with correct public input for public_input gate
711711
BOOST_CHECK(verifier_res);
712+
verifier_res = placeholder_verifier<field_type, lpc_placeholder_params_type>::process(
713+
preprocessed_public_data, proof, constraint_system, lpc_scheme, {0,1}); // check with incorrect public input for public_input gate
714+
BOOST_CHECK(!verifier_res);
712715
}
713716

714717
BOOST_AUTO_TEST_CASE(lookup_test) {

0 commit comments

Comments
 (0)