-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fixed bool constant loading * added tests for bitwise operations
- Loading branch information
Showing
33 changed files
with
501 additions
and
25 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
mlir-assigner/include/mlir-assigner/components/boolean/and.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//---------------------------------------------------------------------------// | ||
// Copyright (c) 2023 Nikita Kaskov <[email protected]> | ||
// | ||
// MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
//---------------------------------------------------------------------------// | ||
|
||
#ifndef CRYPTO3_ASSIGNER_AND_HPP | ||
#define CRYPTO3_ASSIGNER_AND_HPP | ||
|
||
#include <mlir/Dialect/Arith/IR/Arith.h> | ||
|
||
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp> | ||
|
||
#include <nil/blueprint/components/algebra/fields/plonk/non_native/lookup_logic_ops.hpp> | ||
#include <nil/blueprint/component.hpp> | ||
#include <nil/blueprint/basic_non_native_policy.hpp> | ||
#include <nil/blueprint/components/algebra/fixedpoint/lookup_tables/tester.hpp> // TODO: check if there is a new mechanism for this in nil upstream | ||
|
||
#include <mlir-assigner/helper/asserts.hpp> | ||
#include <mlir-assigner/memory/stack_frame.hpp> | ||
#include <mlir-assigner/components/handle_component.hpp> | ||
|
||
namespace nil { | ||
namespace blueprint { | ||
template<typename BlueprintFieldType, typename ArithmetizationParams> | ||
void handle_logic_and( | ||
mlir::arith::AndIOp &operation, | ||
stack_frame<crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>> &frame, | ||
circuit_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp, | ||
assignment_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> | ||
&assignment, | ||
std::uint32_t start_row) { | ||
using component_type = components::lookup_logic_and< | ||
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>; | ||
|
||
auto input = PREPARE_INPUT(mlir::arith::AndIOp); | ||
const auto p = detail::PolicyManager::get_parameters( | ||
detail::ManifestReader<component_type, ArithmetizationParams>::get_witness(0)); | ||
|
||
component_type component(p.witness); | ||
fill_trace(component, input, operation, frame, bp, assignment, start_row); | ||
} | ||
|
||
template<typename BlueprintFieldType, typename ArithmetizationParams> | ||
void handle_logic_or( | ||
mlir::arith::OrIOp &operation, | ||
stack_frame<crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>> &frame, | ||
circuit_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp, | ||
assignment_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> | ||
&assignment, | ||
std::uint32_t start_row) { | ||
//FIXME logic_or is commented out. As soon as it is enabled, remove add the liens above and it SHOULD work | ||
UNREACHABLE("LogicOR not enabled in blueprint"); | ||
// using component_type = components::lookup_logic_or< | ||
// crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>; | ||
// | ||
// auto input = PREPARE_INPUT(mlir::arith::OrIOp); | ||
// const auto p = detail::PolicyManager::get_parameters( | ||
// detail::ManifestReader<component_type, ArithmetizationParams>::get_witness(0)); | ||
// | ||
// component_type component(p.witness); | ||
// fill_trace(component, input, operation, frame, bp, assignment, start_row); | ||
} | ||
|
||
template<typename BlueprintFieldType, typename ArithmetizationParams> | ||
void handle_logic_xor( | ||
mlir::arith::XOrIOp &operation, | ||
stack_frame<crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>> &frame, | ||
circuit_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp, | ||
assignment_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> | ||
&assignment, | ||
std::uint32_t start_row) { | ||
using component_type = components::lookup_logic_xor< | ||
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>>; | ||
|
||
auto input = PREPARE_INPUT(mlir::arith::XOrIOp); | ||
const auto p = detail::PolicyManager::get_parameters( | ||
detail::ManifestReader<component_type, ArithmetizationParams>::get_witness(0)); | ||
|
||
component_type component(p.witness); | ||
fill_trace(component, input, operation, frame, bp, assignment, start_row); | ||
} | ||
|
||
} // namespace blueprint | ||
} // namespace nil | ||
|
||
#endif // CRYPTO3_ASSIGNER_LOGIC_OPS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
mlir-assigner/include/mlir-assigner/components/handle_component.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
|
||
//---------------------------------------------------------------------------// | ||
// Copyright (c) 2023 Nikita Kaskov <[email protected]> | ||
// | ||
// MIT License | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
//---------------------------------------------------------------------------// | ||
|
||
#ifndef CRYPTO3_ASSIGNER_HANDLE_COMPONENT_HPP | ||
#define CRYPTO3_ASSIGNER_HANDLE_COMPONENT_HPP | ||
|
||
#define PREPARE_INPUT(OP) \ | ||
prepare_binary_operation_input<BlueprintFieldType, ArithmetizationParams, OP, \ | ||
typename component_type::input_type>(operation, frame, bp, assignment); | ||
|
||
#include <mlir-assigner/memory/stack_frame.hpp> | ||
namespace nil { | ||
namespace blueprint { | ||
template<typename BlueprintFieldType, typename ArithmetizationParams, typename BinOp, typename input_type> | ||
input_type prepare_binary_operation_input( | ||
BinOp &operation, | ||
stack_frame<crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>> &frame, | ||
circuit_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp, | ||
assignment_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> | ||
&assignment) { | ||
|
||
auto lhs = frame.locals.find(mlir::hash_value(operation.getLhs())); | ||
ASSERT(lhs != frame.locals.end()); | ||
auto rhs = frame.locals.find(mlir::hash_value(operation.getRhs())); | ||
ASSERT(rhs != frame.locals.end()); | ||
|
||
auto x = lhs->second; | ||
auto y = rhs->second; | ||
|
||
input_type instance_input; | ||
instance_input.input[0] = x; | ||
instance_input.input[1] = y; | ||
return instance_input; | ||
} | ||
|
||
template<typename BlueprintFieldType, typename ArithmetizationParams, typename ComponentType> | ||
void handle_component_input( | ||
assignment_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> | ||
&assignment, | ||
typename ComponentType::input_type &instance_input) { | ||
|
||
using var = crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>; | ||
|
||
std::vector<var> all_vars = instance_input.all_vars(); | ||
std::vector<std::reference_wrapper<var>> input(all_vars.begin(), all_vars.end()); | ||
const auto &used_rows = assignment.get_used_rows(); | ||
|
||
for (auto &v : input) { | ||
bool found = (used_rows.find(v.get().rotation) != used_rows.end()); | ||
if (!found && | ||
(v.get().type == var::column_type::witness || v.get().type == var::column_type::constant)) { | ||
const auto new_v = save_shared_var(assignment, v); | ||
v.get().index = new_v.index; | ||
v.get().rotation = new_v.rotation; | ||
v.get().relative = new_v.relative; | ||
v.get().type = new_v.type; | ||
} | ||
} | ||
} | ||
|
||
template<typename BlueprintFieldType, typename ArithmetizationParams, typename component_type, typename BinOp> | ||
void fill_trace( | ||
component_type &component, | ||
typename component_type::input_type &input, | ||
BinOp &mlir_op, | ||
stack_frame<crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>> &frame, | ||
circuit_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp, | ||
assignment_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> | ||
&assignment, | ||
std::uint32_t start_row) { | ||
|
||
if constexpr (nil::blueprint::use_custom_lookup_tables<component_type>()) { | ||
auto lookup_tables = component.component_custom_lookup_tables(); | ||
for (auto &t : lookup_tables) { | ||
bp.register_lookup_table( | ||
std::shared_ptr<nil::crypto3::zk::snark::lookup_table_definition<BlueprintFieldType>>(t)); | ||
} | ||
}; | ||
|
||
if constexpr (nil::blueprint::use_lookups<component_type>()) { | ||
auto lookup_tables = component.component_lookup_tables(); | ||
for (auto &[k, v] : lookup_tables) { | ||
bp.reserve_table(k); | ||
} | ||
}; | ||
|
||
handle_component_input<BlueprintFieldType, ArithmetizationParams, component_type>(assignment, input); | ||
|
||
components::generate_circuit(component, bp, assignment, input, start_row); | ||
auto result = components::generate_assignments(component, assignment, input, start_row); | ||
frame.locals[mlir::hash_value(mlir_op.getResult())] = result.output; | ||
} | ||
} // namespace blueprint | ||
} // namespace nil | ||
#endif // CRYPTO3_ASSIGNER_HANDLE_COMPONENT_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.