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

Commit cffaa2d

Browse files
tshchelovekIluvmagick
authored andcommitted
some logic #308
1 parent 5d13667 commit cffaa2d

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

include/nil/blueprint/components/algebra/fields/plonk/logic_and_flag.hpp

+43
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,13 @@ namespace nil {
102102
std::size_t lookup_column_amount) {
103103
return rows_amount_internal(witness_amount);
104104
}
105+
constexpr static std::size_t get_empty_rows_amount() {
106+
return 1;
107+
}
105108

106109
constexpr static const std::size_t gates_amount = 1;
107110
const std::size_t rows_amount = rows_amount_internal(component_type::witness_amount());
111+
const std::size_t empty_rows_amount = get_empty_rows_amount();
108112

109113
struct input_type {
110114
var x;
@@ -126,6 +130,12 @@ namespace nil {
126130
var(component.W(component.witness_amount() - 1),
127131
start_row_index + component.rows_amount - 1, false);
128132
}
133+
result_type(const logic_and_flag<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType,
134+
ArithmetizationParams>
135+
> &component,
136+
std::uint32_t start_row_index, bool skip) {
137+
output = var(component.W(0), start_row_index, false);
138+
}
129139

130140
std::vector<var> all_vars() const {
131141
return {output};
@@ -148,6 +158,19 @@ namespace nil {
148158
std::initializer_list<typename component_type::public_input_container_type::value_type>
149159
public_inputs) :
150160
component_type(witnesses, constants, public_inputs, get_manifest()) {};
161+
162+
static typename BlueprintFieldType::value_type calculate(typename BlueprintFieldType::value_type x,
163+
typename BlueprintFieldType::value_type y) {
164+
165+
std::array<typename BlueprintFieldType::value_type, 5> t;
166+
t[0] = x;
167+
t[1] = y;
168+
t[2] = t[0] * t[1]; // p
169+
t[3] = t[2].is_zero() ? t[2] : t[2].inversed(); // v
170+
t[4] = t[3] * t[2]; // f
171+
172+
return t[4];
173+
}
151174
};
152175

153176
template<typename BlueprintFieldType, typename ArithmetizationParams>
@@ -192,6 +215,26 @@ namespace nil {
192215
(component, start_row_index);
193216
}
194217

218+
template<typename BlueprintFieldType, typename ArithmetizationParams>
219+
typename plonk_logic_and_flag_component<BlueprintFieldType, ArithmetizationParams>::result_type
220+
generate_empty_assignments(
221+
const plonk_logic_and_flag_component<BlueprintFieldType, ArithmetizationParams>
222+
&component,
223+
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>
224+
&assignment,
225+
const typename plonk_logic_and_flag_component<BlueprintFieldType,
226+
ArithmetizationParams>::input_type &instance_input,
227+
const std::uint32_t start_row_index) {
228+
using component_type = plonk_logic_and_flag_component<BlueprintFieldType, ArithmetizationParams>;
229+
230+
assignment.witness(component.W(0), start_row_index) = component_type::calculate(var_value(assignment, instance_input.x),
231+
var_value(assignment, instance_input.y));
232+
233+
return
234+
typename plonk_logic_and_flag_component<BlueprintFieldType, ArithmetizationParams>::result_type
235+
(component, start_row_index, true);
236+
}
237+
195238
template<typename BlueprintFieldType, typename ArithmetizationParams>
196239
std::size_t generate_gates(
197240
const plonk_logic_and_flag_component<BlueprintFieldType, ArithmetizationParams>

include/nil/blueprint/components/algebra/fields/plonk/logic_or_flag.hpp

+45
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,13 @@ namespace nil {
110110
std::size_t lookup_column_amount) {
111111
return rows_amount_internal(witness_amount);
112112
}
113+
constexpr static std::size_t get_empty_rows_amount() {
114+
return 1;
115+
}
113116

114117
const std::size_t gates_amount = gates_amount_internal(this->witness_amount());
115118
const std::size_t rows_amount = get_rows_amount(this->witness_amount(), 0);
119+
const std::size_t empty_rows_amount = get_empty_rows_amount();
116120

117121
struct input_type {
118122
var x;
@@ -134,6 +138,12 @@ namespace nil {
134138
var(component.W(component.witness_amount() - 1),
135139
start_row_index + component.rows_amount - 1, false);
136140
}
141+
result_type(const logic_or_flag<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType,
142+
ArithmetizationParams>
143+
> &component,
144+
std::uint32_t start_row_index, bool skip) {
145+
output = var(component.W(0), start_row_index, false);
146+
}
137147

138148
std::vector<var> all_vars() const {
139149
return {output};
@@ -156,6 +166,21 @@ namespace nil {
156166
std::initializer_list<typename component_type::public_input_container_type::value_type>
157167
public_inputs) :
158168
component_type(witnesses, constants, public_inputs, get_manifest()) {};
169+
170+
static typename BlueprintFieldType::value_type calculate(typename BlueprintFieldType::value_type x,
171+
typename BlueprintFieldType::value_type y) {
172+
std::array<typename BlueprintFieldType::value_type, 7> t;
173+
174+
t[0] = x;
175+
t[1] = y;
176+
t[2] = t[0].is_zero() ? t[0] : t[0].inversed();
177+
t[3] = t[1].is_zero() ? t[1] : t[1].inversed();
178+
t[4] = t[0] * t[2];
179+
t[5] = t[1] * t[3];
180+
t[6] = t[4] + t[5] - t[4] * t[5];
181+
182+
return t[6];
183+
}
159184
};
160185

161186
template<typename BlueprintFieldType, typename ArithmetizationParams>
@@ -200,6 +225,26 @@ namespace nil {
200225
(component, start_row_index);
201226
}
202227

228+
template<typename BlueprintFieldType, typename ArithmetizationParams>
229+
typename plonk_logic_or_flag_component<BlueprintFieldType, ArithmetizationParams>::result_type
230+
generate_empty_assignments(
231+
const plonk_logic_or_flag_component<BlueprintFieldType, ArithmetizationParams>
232+
&component,
233+
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>
234+
&assignment,
235+
const typename plonk_logic_or_flag_component<BlueprintFieldType, ArithmetizationParams>::input_type
236+
&instance_input,
237+
const std::uint32_t start_row_index) {
238+
using component_type = plonk_logic_or_flag_component<BlueprintFieldType, ArithmetizationParams>;
239+
240+
assignment.witness(component.W(0), start_row_index) = component_type::calculate(
241+
var_value(assignment, instance_input.x),
242+
var_value(assignment, instance_input.y));
243+
244+
return typename plonk_logic_or_flag_component<BlueprintFieldType, ArithmetizationParams>::result_type
245+
(component, start_row_index, true);
246+
}
247+
203248
template<typename BlueprintFieldType, typename ArithmetizationParams>
204249
std::vector<std::size_t> generate_gates(
205250
const plonk_logic_or_flag_component<BlueprintFieldType, ArithmetizationParams>

test/algebra/fields/plonk/logic_and_flag.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ auto test_logic_and_flag(std::vector<typename BlueprintFieldType::value_type> pu
8484

8585
nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
8686
component_instance, public_input, result_check, instance_input);
87+
nil::crypto3::test_empty_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
88+
component_instance, public_input, result_check, instance_input);
8789
}
8890

8991
template<typename FieldType, std::size_t RandomTestsAmount, std::uint32_t WitnessesAmount>

test/algebra/fields/plonk/logic_or_flag.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ auto test_logic_or_flag(std::vector<typename BlueprintFieldType::value_type> pub
9090

9191
nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
9292
component_instance, public_input, result_check, instance_input);
93+
nil::crypto3::test_empty_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
94+
component_instance, public_input, result_check, instance_input);
9395
}
9496

9597
template<typename FieldType, std::size_t RandomTestsAmount, std::size_t WitnessColumns>

0 commit comments

Comments
 (0)