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

Commit a250988

Browse files
committed
Added a proper private input extractor.
1 parent 85edb97 commit a250988

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

.github/workflows/run_tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ jobs:
6969
blueprint_verifiers_placeholder_f3_loop_test,
7070
blueprint_verifiers_placeholder_gate_component_test,
7171
blueprint_proxy_test
72+
blueprint_private_input_extractor_test
7273
] # Tests to execute
7374
steps:
7475
- uses: cachix/install-nix-action@v23
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2023 Dmitrii Tabalin <[email protected]>
3+
//
4+
// MIT License
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//---------------------------------------------------------------------------//
24+
25+
#ifndef CRYPTO3_BLUEPRINT_PRIVATE_INPUT_EXTRACTOR_HPP
26+
#define CRYPTO3_BLUEPRINT_PRIVATE_INPUT_EXTRACTOR_HPP
27+
28+
#include <unordered_map>
29+
#include <unordered_set>
30+
31+
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
32+
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
33+
#include <nil/crypto3/zk/snark/arithmetization/plonk/variable.hpp>
34+
35+
namespace nil {
36+
namespace blueprint {
37+
// So you want to figure out where a variable you pass into the component actually gets assigned to?
38+
// Congratulations! You've come to the right place. You can do that in just 5 easy steps!
39+
// Note that step 2 and 5 are IMPORTANT, and you SHOULD read them!
40+
// Step 1: Instantiate your component.
41+
// Step 2: Fill the input variables you want to assign to with distinct variables.
42+
// IMPORTANT: the variables you want to assign to MUST be distinct from all others!
43+
// They also MUST be outside of the component!
44+
// ENSURE that they are not the same as the default variable (var(0, 0, false, witness)),
45+
// (or fill all of the input values)
46+
// E.g. you could make the variable to have very large witness column number.
47+
// The variable CANNOT be with the legacy private input index: copy constraints
48+
// would not generate for it and it would not be found.
49+
// Step 3: Call input_assignment_variables with the component, the input and the row at which the component
50+
// is going to be based.
51+
// Step 4: The map you got in return maps the variables from instance_input to the variables from the
52+
// component.
53+
// Step 5: IMPORTANT! Be sure to check that the variable is in the map before assinging to it.
54+
// In rare situations the variable might turn out to be unused.
55+
// The map would not contain the variable in this case.
56+
template<typename BlueprintFieldType, typename ArithmetizationParams, typename ComponentType>
57+
std::unordered_map<
58+
crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>,
59+
crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>> input_assignment_variables(
60+
const ComponentType &component,
61+
const typename ComponentType::input_type &instance_input,
62+
const std::size_t start_row_index) {
63+
64+
using constraint_system =
65+
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
66+
using var = crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>;
67+
68+
circuit<constraint_system> bp;
69+
// As assignment does not change, we can keep it static to save time
70+
// Because templates generate code for each instantiation, different ArithmetizationParams
71+
// will have different static assignment
72+
static assignment<constraint_system> assignment;
73+
generate_copy_constraints(component, bp, assignment, instance_input, start_row_index);
74+
auto input_vars = instance_input.all_vars();
75+
std::unordered_set<var> input_vars_set(input_vars.begin(), input_vars.end());
76+
77+
std::unordered_map<var, var> result;
78+
for (const auto &copy_constraint : bp.copy_constraints()) {
79+
auto &var1 = copy_constraint.first,
80+
&var2 = copy_constraint.second;
81+
if (input_vars_set.find(var1) != input_vars_set.end()) {
82+
result[var1] = var2;
83+
}
84+
if (input_vars_set.find(var2) != input_vars_set.end()) {
85+
result[var2] = var1;
86+
}
87+
}
88+
return result;
89+
}
90+
} // namespace blueprint
91+
} // namespace nil
92+
93+
#endif // CRYPTO3_BLUEPRINT_PRIVATE_INPUT_EXTRACTOR_HPP

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ set(COMMON_TEST_FILES
5656
"gate_id"
5757
"utils/connectedness_check"
5858
"private_input"
59+
"private_input_extractor"
5960
"proxy"
6061
)
6162

test/private_input_extractor.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2023 Dmitrii Tabalin <[email protected]>
3+
//
4+
// MIT License
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//---------------------------------------------------------------------------//
24+
25+
#define BOOST_TEST_MODULE blueprint_private_input_extractor_test
26+
27+
#include <boost/test/unit_test.hpp>
28+
29+
#include <nil/crypto3/algebra/fields/arithmetic_params/pallas.hpp>
30+
31+
#include <nil/crypto3/hash/keccak.hpp>
32+
#include <nil/crypto3/random/algebraic_engine.hpp>
33+
34+
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
35+
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
36+
#include <nil/blueprint/components/algebra/fields/plonk/addition.hpp>
37+
38+
#include <nil/blueprint/private_input_extractor.hpp>
39+
40+
#include "test_plonk_component.hpp"
41+
42+
using namespace nil;
43+
44+
45+
BOOST_AUTO_TEST_CASE(blueprint_plonk_private_input_test) {
46+
using field_type = typename crypto3::algebra::curves::pallas::base_field_type;
47+
using value_type = typename field_type::value_type;
48+
49+
constexpr std::size_t WitnessColumns = 3;
50+
constexpr std::size_t PublicInputColumns = 1;
51+
constexpr std::size_t ConstantColumns = 0;
52+
constexpr std::size_t SelectorColumns = 1;
53+
using ArithmetizationParams =
54+
crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
55+
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<field_type, ArithmetizationParams>;
56+
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
57+
constexpr std::size_t Lambda = 40;
58+
using AssignmentType = nil::blueprint::assignment<ArithmetizationType>;
59+
60+
using var = crypto3::zk::snark::plonk_variable<typename field_type::value_type>;
61+
62+
using component_type = blueprint::components::addition<ArithmetizationType, field_type, nil::blueprint::basic_non_native_policy<field_type>>;
63+
64+
typename component_type::input_type instance_input = {
65+
var(0, 0, false, var::column_type::public_input),
66+
var(0, 1, false, var::column_type::public_input)
67+
};
68+
69+
component_type component_instance({0, 1, 2}, {}, {});
70+
71+
auto unopt = nil::blueprint::input_assignment_variables<field_type, ArithmetizationParams, component_type>(
72+
component_instance, instance_input, 3);
73+
BOOST_ASSERT(unopt.size() == 2);
74+
BOOST_ASSERT(unopt.count(instance_input.x) == 1);
75+
BOOST_ASSERT(unopt.count(instance_input.y) == 1);
76+
BOOST_ASSERT(unopt[instance_input.x] == var(0, 3, false, var::column_type::witness));
77+
BOOST_ASSERT(unopt[instance_input.y] == var(1, 3, false, var::column_type::witness));
78+
}

0 commit comments

Comments
 (0)