31 #ifndef CRYPTO3_ZK_BLUEPRINT_BENES_ROUTING_COMPONENT_HPP
32 #define CRYPTO3_ZK_BLUEPRINT_BENES_ROUTING_COMPONENT_HPP
42 namespace components {
44 template<
typename FieldType>
58 std::vector<std::vector<blueprint_variable_vector<FieldType>>> routed_packets;
59 std::vector<multipacking_component<FieldType>> pack_inputs, unpack_outputs;
69 std::vector<blueprint_variable_vector<FieldType>> benes_switch_bits;
100 for (std::size_t column_idx = 0; column_idx <=
num_columns; ++column_idx) {
102 for (std::size_t packet_idx = 0; packet_idx <
num_packets; ++packet_idx) {
110 for (std::size_t packet_idx = 0; packet_idx <
num_packets; ++packet_idx) {
115 routed_packets[0][packet_idx],
116 FieldType::capacity()));
123 FieldType::capacity()));
129 for (std::size_t column_idx = 0; column_idx <
num_columns; ++column_idx) {
137 for (std::size_t packet_idx = 0; packet_idx <
num_packets; ++packet_idx) {
138 pack_inputs[packet_idx].generate_r1cs_constraints(
false);
140 unpack_outputs[packet_idx].generate_r1cs_constraints(
true);
142 for (std::size_t subpacket_idx = 0; subpacket_idx <
num_subpackets; ++subpacket_idx) {
144 1, routed_packets[0][packet_idx][subpacket_idx],
145 routed_packets[
num_columns][packet_idx][subpacket_idx]));
151 for (std::size_t column_idx = 0; column_idx <
num_columns; ++column_idx) {
152 for (std::size_t packet_idx = 0; packet_idx <
num_packets; ++packet_idx) {
153 const std::size_t straight_edge = neighbors[column_idx][packet_idx].first;
154 const std::size_t cross_edge = neighbors[column_idx][packet_idx].second;
159 routed_packets[column_idx][packet_idx][0] -
160 routed_packets[column_idx + 1][straight_edge][0],
161 routed_packets[column_idx][packet_idx][0] -
162 routed_packets[column_idx + 1][cross_edge][0],
166 generate_boolean_r1cs_constraint<FieldType>(
167 this->
bp, benes_switch_bits[column_idx][packet_idx]);
170 for (std::size_t subpacket_idx = 0; subpacket_idx <
num_subpackets;
177 benes_switch_bits[column_idx][packet_idx],
178 routed_packets[column_idx + 1][cross_edge][subpacket_idx] -
179 routed_packets[column_idx + 1][straight_edge][subpacket_idx],
180 routed_packets[column_idx][packet_idx][subpacket_idx] -
181 routed_packets[column_idx + 1][straight_edge][subpacket_idx]));
190 for (std::size_t packet_idx = 0; packet_idx <
num_packets; ++packet_idx) {
191 pack_inputs[packet_idx].generate_r1cs_witness_from_bits();
197 for (std::size_t column_idx = 0; column_idx <
num_columns; ++column_idx) {
198 for (std::size_t packet_idx = 0; packet_idx <
num_packets; ++packet_idx) {
199 const std::size_t straight_edge = neighbors[column_idx][packet_idx].first;
200 const std::size_t cross_edge = neighbors[column_idx][packet_idx].second;
203 this->
bp.val(benes_switch_bits[column_idx][packet_idx]) =
204 typename FieldType::value_type(routing[column_idx][packet_idx] ? 1 : 0);
207 for (std::size_t subpacket_idx = 0; subpacket_idx <
num_subpackets; ++subpacket_idx) {
208 this->
bp.val(routing[column_idx][packet_idx] ?
209 routed_packets[column_idx + 1][cross_edge][subpacket_idx] :
210 routed_packets[column_idx + 1][straight_edge][subpacket_idx]) =
211 this->
bp.val(routed_packets[column_idx][packet_idx][subpacket_idx]);
217 for (std::size_t packet_idx = 0; packet_idx <
lines_to_unpack; ++packet_idx) {
218 unpack_outputs[packet_idx].generate_r1cs_witness_from_packed();
Definition: benes_components.hpp:45
void generate_r1cs_constraints()
Definition: benes_components.hpp:135
std::size_t lines_to_unpack
Definition: benes_components.hpp:78
const std::size_t num_subpackets
Definition: benes_components.hpp:80
const std::vector< blueprint_variable_vector< FieldType > > routing_output_bits
Definition: benes_components.hpp:77
const std::size_t num_packets
Definition: benes_components.hpp:73
void generate_r1cs_witness(const integer_permutation &permutation)
Definition: benes_components.hpp:188
const std::vector< blueprint_variable_vector< FieldType > > routing_input_bits
Definition: benes_components.hpp:76
const std::size_t packet_size
Definition: benes_components.hpp:80
const std::size_t num_columns
Definition: benes_components.hpp:74
benes_routing_component(blueprint< FieldType > &bp, const std::size_t num_packets, const std::vector< blueprint_variable_vector< FieldType >> &routing_input_bits, const std::vector< blueprint_variable_vector< FieldType >> &routing_output_bits, const std::size_t lines_to_unpack)
Definition: benes_components.hpp:82
Definition: blueprint_variable.hpp:57
Definition: blueprint.hpp:46
Definition: component.hpp:37
blueprint< FieldType > & bp
Definition: component.hpp:39
Definition: packing.hpp:100
vector(T, U...) -> vector< std::enable_if_t<(std::is_same_v< T, U > &&...), T >, 1+sizeof...(U)>
deduction guide for uniform initialization
benes_routing get_benes_routing(const integer_permutation &permutation)
Definition: benes.hpp:344
std::vector< std::vector< std::pair< std::size_t, std::size_t > > > benes_topology
Definition: benes.hpp:67
std::size_t benes_num_columns(std::size_t num_packets)
Definition: benes.hpp:203
std::vector< std::vector< bool > > benes_routing
Definition: benes.hpp:77
benes_topology generate_benes_topology(std::size_t num_packets)
Definition: benes.hpp:210