From 1d3140fffb06e5b85034486ed52ebb947303273e Mon Sep 17 00:00:00 2001 From: Iluvmagick Date: Mon, 30 Sep 2024 17:28:28 +0400 Subject: [PATCH] Removed examples as there is no hope to fix them. --- .../bacs/bacs_examples.hpp | 173 -------------- .../tbcs/tbcs_examples.hpp | 133 ----------- .../r1cs/r1cs_examples.hpp | 225 ------------------ .../uscs/uscs_examples.hpp | 203 ---------------- crypto3/libs/zk/examples/simple_example.hpp | 78 ------ 5 files changed, 812 deletions(-) delete mode 100644 crypto3/libs/zk/examples/relations/circuit_satisfaction_problems/bacs/bacs_examples.hpp delete mode 100644 crypto3/libs/zk/examples/relations/circuit_satisfaction_problems/tbcs/tbcs_examples.hpp delete mode 100644 crypto3/libs/zk/examples/relations/constraint_satisfaction_problems/r1cs/r1cs_examples.hpp delete mode 100644 crypto3/libs/zk/examples/relations/constraint_satisfaction_problems/uscs/uscs_examples.hpp delete mode 100644 crypto3/libs/zk/examples/simple_example.hpp diff --git a/crypto3/libs/zk/examples/relations/circuit_satisfaction_problems/bacs/bacs_examples.hpp b/crypto3/libs/zk/examples/relations/circuit_satisfaction_problems/bacs/bacs_examples.hpp deleted file mode 100644 index da08799e71..0000000000 --- a/crypto3/libs/zk/examples/relations/circuit_satisfaction_problems/bacs/bacs_examples.hpp +++ /dev/null @@ -1,173 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2021 Mikhail Komarov -// Copyright (c) 2020-2021 Nikita Kaskov -// -// 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_BACS_EXAMPLES_HPP -#define CRYPTO3_BACS_EXAMPLES_HPP - -#include - -#include - -namespace nil { - namespace crypto3 { - namespace zk { - namespace snark { - - /** - * A BACS example comprises a BACS circuit, BACS primary input, and BACS auxiliary input. - */ - template - struct bacs_example { - - bacs_circuit circuit; - bacs_primary_input primary_input; - bacs_auxiliary_input auxiliary_input; - - bacs_example() = default; - bacs_example(const bacs_example &other) = default; - bacs_example(const bacs_circuit &circuit, - const bacs_primary_input &primary_input, - const bacs_auxiliary_input &auxiliary_input) : - circuit(circuit), - primary_input(primary_input), auxiliary_input(auxiliary_input) { - } - - bacs_example(bacs_circuit &&circuit, - bacs_primary_input &&primary_input, - bacs_auxiliary_input &&auxiliary_input) : - circuit(std::move(circuit)), - primary_input(std::move(primary_input)), auxiliary_input(std::move(auxiliary_input)) { - } - }; - - /** - * Generate a BACS example such that: - * - the primary input has size primary_input_size; - * - the auxiliary input has size auxiliary_input_size; - * - the circuit has num_gates gates; - * - the circuit has num_outputs (<= num_gates) output gates. - * - * This is done by first selecting primary and auxiliary inputs uniformly at random, and then for each - * gate: - * - selecting random left and right wires from primary inputs, auxiliary inputs, and outputs of - * previous gates, - * - selecting random linear combinations for left and right wires, consisting of 1, 2, 3 or 4 terms - * each, with random coefficients, - * - if the gate is an output gate, then adding a random non-output wire to either left or right linear - * combination, with appropriate coefficient, so that the linear combination evaluates to 0. - */ - template - bacs_example generate_bacs_example(std::size_t primary_input_size, - std::size_t auxiliary_input_size, - std::size_t num_gates, - std::size_t num_outputs); - - template - linear_combination random_linear_combination(const std::size_t num_variables) { - - using policy_type = FieldType; - using field_value_type = policy_type::value_type; - - const std::size_t terms = 1 + (std::rand() % 3); - linear_combination result; - - for (std::size_t i = 0; i < terms; ++i) { - const field_value_type coeff = algebra::random_element(); - result = result + coeff * variable(std::rand() % (num_variables + 1)); - } - - return result; - } - - template - bacs_example generate_bacs_example(std::size_t primary_input_size, - std::size_t auxiliary_input_size, - std::size_t num_gates, - std::size_t num_outputs) { - - using policy_type = FieldType; - using field_value_type = policy_type::value_type; - - bacs_example example; - for (std::size_t i = 0; i < primary_input_size; ++i) { - example.primary_input.emplace_back(algebra::random_element()); - } - - for (std::size_t i = 0; i < auxiliary_input_size; ++i) { - example.auxiliary_input.emplace_back(algebra::random_element()); - } - - example.circuit.primary_input_size = primary_input_size; - example.circuit.auxiliary_input_size = auxiliary_input_size; - - bacs_variable_assignment all_vals; - all_vals.insert(all_vals.end(), example.primary_input.begin(), example.primary_input.end()); - all_vals.insert(all_vals.end(), example.auxiliary_input.begin(), example.auxiliary_input.end()); - - for (std::size_t i = 0; i < num_gates; ++i) { - const std::size_t num_variables = primary_input_size + auxiliary_input_size + i; - bacs_gate gate; - gate.lhs = random_linear_combination(num_variables); - gate.rhs = random_linear_combination(num_variables); - gate.output = variable(num_variables + 1); - - if (i >= num_gates - num_outputs) { - /* make gate a circuit output and fix */ - gate.is_circuit_output = true; - const typename variable::index_type var_idx = - std::rand() % (1 + primary_input_size + std::min(num_gates - num_outputs, i)); - const field_value_type var_val = - (var_idx == 0 ? field_value_type::one() : all_vals[var_idx - 1]); - - if (std::rand() % 2 == 0) { - const field_value_type lhs_val = gate.lhs.evaluate(all_vals); - const field_value_type coeff = -(lhs_val * var_val.inversed()); - gate.lhs = gate.lhs + coeff * variable(var_idx); - } else { - const field_value_type rhs_val = gate.rhs.evaluate(all_vals); - const field_value_type coeff = -(rhs_val * var_val.inversed()); - gate.rhs = gate.rhs + coeff * variable(var_idx); - } - - assert(gate.evaluate(all_vals).is_zero()); - } else { - gate.is_circuit_output = false; - } - - example.circuit.add_gate(gate); - all_vals.emplace_back(gate.evaluate(all_vals)); - } - - assert(example.circuit.is_satisfied(example.primary_input, example.auxiliary_input)); - - return example; - } - - } // namespace snark - } // namespace zk - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_BACS_EXAMPLES_HPP diff --git a/crypto3/libs/zk/examples/relations/circuit_satisfaction_problems/tbcs/tbcs_examples.hpp b/crypto3/libs/zk/examples/relations/circuit_satisfaction_problems/tbcs/tbcs_examples.hpp deleted file mode 100644 index 5a075d4774..0000000000 --- a/crypto3/libs/zk/examples/relations/circuit_satisfaction_problems/tbcs/tbcs_examples.hpp +++ /dev/null @@ -1,133 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2021 Mikhail Komarov -// Copyright (c) 2020-2021 Nikita Kaskov -// -// 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_ZK_TBCS_EXAMPLES_HPP -#define CRYPTO3_ZK_ED25519SIG_HPP - -#include - -namespace nil { - namespace crypto3 { - namespace zk { - namespace snark { - - /** - * A TBCS example comprises a TBCS circuit, TBCS primary input, and TBCS auxiliary input. - */ - struct tbcs_example { - - tbcs_circuit circuit; - tbcs_primary_input primary_input; - tbcs_auxiliary_input auxiliary_input; - - tbcs_example() = default; - tbcs_example(const tbcs_example &other) = default; - tbcs_example(const tbcs_circuit &circuit, - const tbcs_primary_input &primary_input, - const tbcs_auxiliary_input &auxiliary_input) : - circuit(circuit), - primary_input(primary_input), auxiliary_input(auxiliary_input) { - } - - tbcs_example(tbcs_circuit &&circuit, - tbcs_primary_input &&primary_input, - tbcs_auxiliary_input &&auxiliary_input) : - circuit(std::move(circuit)), - primary_input(std::move(primary_input)), auxiliary_input(std::move(auxiliary_input)) { - } - }; - - /** - * Generate a TBCS example such that: - * - the primary input has size primary_input_size; - * - the auxiliary input has size auxiliary_input_size; - * - the circuit has num_gates gates; - * - the circuit has num_outputs (<= num_gates) output gates. - * - * This is done by first selecting primary and auxiliary inputs uniformly at random, and then for each - * gate: - * - selecting random left and right wires from primary inputs, auxiliary inputs, and outputs of - * previous gates, - * - selecting a gate type at random (subject to the constraint "output = 0" if this is an output gate). - */ - tbcs_example generate_tbcs_example(const std::size_t primary_input_size, - const std::size_t auxiliary_input_size, - const std::size_t num_gates, - const std::size_t num_outputs); - - tbcs_example generate_tbcs_example(const std::size_t primary_input_size, - const std::size_t auxiliary_input_size, - const std::size_t num_gates, - const std::size_t num_outputs) { - tbcs_example example; - for (std::size_t i = 0; i < primary_input_size; ++i) { - example.primary_input.push_back(std::rand() % 2 == 0 ? false : true); - } - - for (std::size_t i = 0; i < auxiliary_input_size; ++i) { - example.auxiliary_input.push_back(std::rand() % 2 == 0 ? false : true); - } - - example.circuit.primary_input_size = primary_input_size; - example.circuit.auxiliary_input_size = auxiliary_input_size; - - tbcs_variable_assignment all_vals; - all_vals.insert(all_vals.end(), example.primary_input.begin(), example.primary_input.end()); - all_vals.insert(all_vals.end(), example.auxiliary_input.begin(), example.auxiliary_input.end()); - - for (std::size_t i = 0; i < num_gates; ++i) { - const std::size_t num_variables = primary_input_size + auxiliary_input_size + i; - tbcs_gate gate; - gate.left_wire = std::rand() % (num_variables + 1); - gate.right_wire = std::rand() % (num_variables + 1); - gate.output = num_variables + 1; - - if (i >= num_gates - num_outputs) { - /* make gate a circuit output and fix */ - do { - gate.type = (tbcs_gate_type)(std::rand() % num_tbcs_gate_types); - } while (gate.evaluate(all_vals)); - - gate.is_circuit_output = true; - } else { - gate.type = (tbcs_gate_type)(std::rand() % num_tbcs_gate_types); - gate.is_circuit_output = false; - } - - example.circuit.add_gate(gate); - all_vals.push_back(gate.evaluate(all_vals)); - } - - assert(example.circuit.is_satisfied(example.primary_input, example.auxiliary_input)); - - return example; - } - - } // namespace snark - } // namespace zk - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_ZK_ED25519SIG_HPP diff --git a/crypto3/libs/zk/examples/relations/constraint_satisfaction_problems/r1cs/r1cs_examples.hpp b/crypto3/libs/zk/examples/relations/constraint_satisfaction_problems/r1cs/r1cs_examples.hpp deleted file mode 100644 index 121683d74e..0000000000 --- a/crypto3/libs/zk/examples/relations/constraint_satisfaction_problems/r1cs/r1cs_examples.hpp +++ /dev/null @@ -1,225 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2021 Mikhail Komarov -// Copyright (c) 2020-2021 Nikita Kaskov -// -// 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_R1CS_EXAMPLES_HPP -#define CRYPTO3_R1CS_EXAMPLES_HPP - -#include - -#include - -namespace nil { - namespace crypto3 { - namespace zk { - namespace snark { - - using nil::crypto3::algebra; - using nil::crypto3::algebra::fields; - - /** - * A R1CS example comprises a R1CS constraint system, R1CS input, and R1CS witness. - */ - template - struct r1cs_example { - r1cs_constraint_system constraint_system; - r1cs_primary_input primary_input; - r1cs_auxiliary_input auxiliary_input; - - r1cs_example() = default; - r1cs_example(const r1cs_example &other) = default; - r1cs_example(const r1cs_constraint_system &constraint_system, - const r1cs_primary_input &primary_input, - const r1cs_auxiliary_input &auxiliary_input) : - constraint_system(constraint_system), - primary_input(primary_input), auxiliary_input(auxiliary_input) {}; - r1cs_example(r1cs_constraint_system &&constraint_system, - r1cs_primary_input &&primary_input, - r1cs_auxiliary_input &&auxiliary_input) : - constraint_system(std::move(constraint_system)), - primary_input(std::move(primary_input)), auxiliary_input(std::move(auxiliary_input)) {}; - }; - - /** - * Generate a R1CS example such that: - * - the number of constraints of the R1CS constraint system is num_constraints; - * - the number of variables of the R1CS constraint system is (approximately) num_constraints; - * - the number of inputs of the R1CS constraint system is num_inputs; - * - the R1CS input consists of ``full'' field elements (typically require the whole log|Field| bits to - * represent). - */ - template - r1cs_example generate_r1cs_example_with_field_input(const std::size_t num_constraints, - const std::size_t num_inputs); - - /** - * Generate a R1CS example such that: - * - the number of constraints of the R1CS constraint system is num_constraints; - * - the number of variables of the R1CS constraint system is (approximately) num_constraints; - * - the number of inputs of the R1CS constraint system is num_inputs; - * - the R1CS input consists of binary values (as opposed to ``full'' field elements). - */ - template - r1cs_example generate_r1cs_example_with_binary_input(const std::size_t num_constraints, - const std::size_t num_inputs); - - template - r1cs_example generate_r1cs_example_with_field_input(const std::size_t num_constraints, - const std::size_t num_inputs) { - - assert(num_inputs <= num_constraints + 2); - - r1cs_constraint_system cs; - cs.primary_input_size = num_inputs; - cs.auxiliary_input_size = 2 + num_constraints - num_inputs; // TODO: explain this - - r1cs_variable_assignment full_variable_assignment; - field_value_type a = algebra::random_element(); - field_value_type b = algebra::random_element(); - full_variable_assignment.push_back(a); - full_variable_assignment.push_back(b); - - for (std::size_t i = 0; i < num_constraints - 1; ++i) { - linear_combination A, B, C; - - if (i % 2) { - // a * b = c - A.add_term(i + 1, 1); - B.add_term(i + 2, 1); - C.add_term(i + 3, 1); - field_value_type tmp = a * b; - full_variable_assignment.push_back(tmp); - a = b; - b = tmp; - } else { - // a + b = c - B.add_term(0, 1); - A.add_term(i + 1, 1); - A.add_term(i + 2, 1); - C.add_term(i + 3, 1); - field_value_type tmp = a + b; - full_variable_assignment.push_back(tmp); - a = b; - b = tmp; - } - - cs.add_constraint(r1cs_constraint(A, B, C)); - } - - linear_combination A, B, C; - field_value_type fin = field_value_type::zero(); - for (std::size_t i = 1; i < cs.num_variables(); ++i) { - A.add_term(i, 1); - B.add_term(i, 1); - fin = fin + full_variable_assignment[i - 1]; - } - C.add_term(cs.num_variables(), 1); - cs.add_constraint(r1cs_constraint(A, B, C)); - full_variable_assignment.push_back(fin.squared()); - - /* split variable assignment */ - r1cs_primary_input primary_input(full_variable_assignment.begin(), - full_variable_assignment.begin() + num_inputs); - r1cs_primary_input auxiliary_input(full_variable_assignment.begin() + num_inputs, - full_variable_assignment.end()); - - /* sanity checks */ - assert(cs.num_variables() == full_variable_assignment.size()); - assert(cs.num_variables() >= num_inputs); - assert(cs.num_inputs() == num_inputs); - assert(cs.num_constraints() == num_constraints); - assert(cs.is_satisfied(primary_input, auxiliary_input)); - - return r1cs_example(std::move(cs), std::move(primary_input), std::move(auxiliary_input)); - } - - template - r1cs_example generate_r1cs_example_with_binary_input(const std::size_t num_constraints, - const std::size_t num_inputs) { - - using policy_type = FieldType; - using field_value_type = policy_type::value_type; - - algebra: - - assert(num_inputs >= 1); - - r1cs_constraint_system cs; - cs.primary_input_size = num_inputs; - cs.auxiliary_input_size = num_constraints; /* we will add one auxiliary variable per constraint */ - - r1cs_variable_assignment full_variable_assignment; - for (std::size_t i = 0; i < num_inputs; ++i) { - full_variable_assignment.push_back(field_value_type(std::rand() % 2)); - } - - std::size_t lastvar = num_inputs - 1; - for (std::size_t i = 0; i < num_constraints; ++i) { - ++lastvar; - const std::size_t u = (i == 0 ? std::rand() % num_inputs : std::rand() % i); - const std::size_t v = (i == 0 ? std::rand() % num_inputs : std::rand() % i); - - /* chose two random bits and XOR them together: - res = u + v - 2 * u * v - 2 * u * v = u + v - res - */ - linear_combination A, B, C; - A.add_term(u + 1, 2); - B.add_term(v + 1, 1); - if (u == v) { - C.add_term(u + 1, 2); - } else { - C.add_term(u + 1, 1); - C.add_term(v + 1, 1); - } - C.add_term(lastvar + 1, -field_value_type::one()); - - cs.add_constraint(r1cs_constraint(A, B, C)); - full_variable_assignment.push_back(full_variable_assignment[u] + full_variable_assignment[v] - - full_variable_assignment[u] * full_variable_assignment[v] - - full_variable_assignment[u] * full_variable_assignment[v]); - } - - /* split variable assignment */ - r1cs_primary_input primary_input(full_variable_assignment.begin(), - full_variable_assignment.begin() + num_inputs); - r1cs_primary_input auxiliary_input(full_variable_assignment.begin() + num_inputs, - full_variable_assignment.end()); - - /* sanity checks */ - assert(cs.num_variables() == full_variable_assignment.size()); - assert(cs.num_variables() >= num_inputs); - assert(cs.num_inputs() == num_inputs); - assert(cs.num_constraints() == num_constraints); - assert(cs.is_satisfied(primary_input, auxiliary_input)); - - return r1cs_example(std::move(cs), std::move(primary_input), std::move(auxiliary_input)); - } - - } // namespace snark - } // namespace zk - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_R1CS_EXAMPLES_HPP diff --git a/crypto3/libs/zk/examples/relations/constraint_satisfaction_problems/uscs/uscs_examples.hpp b/crypto3/libs/zk/examples/relations/constraint_satisfaction_problems/uscs/uscs_examples.hpp deleted file mode 100644 index 111f59446f..0000000000 --- a/crypto3/libs/zk/examples/relations/constraint_satisfaction_problems/uscs/uscs_examples.hpp +++ /dev/null @@ -1,203 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2021 Mikhail Komarov -// Copyright (c) 2020-2021 Nikita Kaskov -// -// 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_USCS_EXAMPLES_HPP -#define CRYPTO3_USCS_EXAMPLES_HPP - -#include - -#include - -namespace nil { - namespace crypto3 { - namespace zk { - namespace snark { - - /** - * A USCS example comprises a USCS constraint system, USCS input, and USCS witness. - */ - template - struct uscs_example { - uscs_constraint_system constraint_system; - uscs_primary_input primary_input; - uscs_auxiliary_input auxiliary_input; - - uscs_example() = default; - uscs_example(const uscs_example &other) = default; - uscs_example(const uscs_constraint_system &constraint_system, - const uscs_primary_input &primary_input, - const uscs_auxiliary_input &auxiliary_input) : - constraint_system(constraint_system), - primary_input(primary_input), auxiliary_input(auxiliary_input) {}; - uscs_example(uscs_constraint_system &&constraint_system, - uscs_primary_input &&primary_input, - uscs_auxiliary_input &&auxiliary_input) : - constraint_system(std::move(constraint_system)), - primary_input(std::move(primary_input)), auxiliary_input(std::move(auxiliary_input)) {}; - }; - - /** - * Generate a USCS example such that: - * - the number of constraints of the USCS constraint system is num_constraints; - * - the number of variables of the USCS constraint system is (approximately) num_constraints; - * - the number of inputs of the USCS constraint system is num_inputs; - * - the USCS input consists of ``full'' field elements (typically require the whole log|Field| bits to - * represent). - */ - template - uscs_example generate_uscs_example_with_field_input(const std::size_t num_constraints, - const std::size_t num_inputs); - - /** - * Generate a USCS example such that: - * - the number of constraints of the USCS constraint system is num_constraints; - * - the number of variables of the USCS constraint system is (approximately) num_constraints; - * - the number of inputs of the USCS constraint system is num_inputs; - * - the USCS input consists of binary values (as opposed to ``full'' field elements). - */ - template - uscs_example generate_uscs_example_with_binary_input(const std::size_t num_constraints, - const std::size_t num_inputs); - - template - uscs_example generate_uscs_example_with_field_input(const std::size_t num_constraints, - const std::size_t num_inputs) { - - using policy_type = FieldType; - using field_value_type = policy_type::value_type; - - std::cout << "Call to generate_uscs_example_with_field_input" << std::endl; - - assert(num_inputs >= 1); - assert(num_constraints >= num_inputs); - - uscs_constraint_system cs; - cs.primary_input_size = num_inputs; - cs.auxiliary_input_size = num_constraints - num_inputs; - - uscs_variable_assignment full_variable_assignment; - for (std::size_t i = 0; i < num_constraints; ++i) { - full_variable_assignment.emplace_back(FieldType(std::rand())); - } - - for (std::size_t i = 0; i < num_constraints; ++i) { - std::size_t x, y, z; - - do { - x = std::rand() % num_constraints; - y = std::rand() % num_constraints; - z = std::rand() % num_constraints; - } while (x == z || y == z); - - const field_value_type x_coeff = algebra::random_element(); - const field_value_type y_coeff = algebra::random_element(); - const field_value_type val = - (std::rand() % 2 == 0 ? field_value_type::one() : -field_value_type::one()); - const field_value_type z_coeff = - (val - x_coeff * full_variable_assignment[x] - y_coeff * full_variable_assignment[y]) * - full_variable_assignment[z].inversed(); - - uscs_constraint constr; - constr.add_term(x + 1, x_coeff); - constr.add_term(y + 1, y_coeff); - constr.add_term(z + 1, z_coeff); - - cs.add_constraint(constr); - } - - /* split variable assignment */ - uscs_primary_input primary_input(full_variable_assignment.begin(), - full_variable_assignment.begin() + num_inputs); - uscs_primary_input auxiliary_input(full_variable_assignment.begin() + num_inputs, - full_variable_assignment.end()); - - /* sanity checks */ - assert(cs.num_variables() == full_variable_assignment.size()); - assert(cs.num_variables() >= num_inputs); - assert(cs.num_inputs() == num_inputs); - assert(cs.num_constraints() == num_constraints); - assert(cs.is_satisfied(primary_input, auxiliary_input)); - - return uscs_example(std::move(cs), std::move(primary_input), std::move(auxiliary_input)); - } - - template - uscs_example generate_uscs_example_with_binary_input(const std::size_t num_constraints, - const std::size_t num_inputs) { - std::cout << "Call to generate_uscs_example_with_binary_input" << std::endl; - - assert(num_inputs >= 1); - - uscs_constraint_system cs; - cs.primary_input_size = num_inputs; - cs.auxiliary_input_size = num_constraints; - - uscs_variable_assignment full_variable_assignment; - for (std::size_t i = 0; i < num_inputs; ++i) { - full_variable_assignment.push_back(FieldType(std::rand() % 2)); - } - - std::size_t lastvar = num_inputs - 1; - for (std::size_t i = 0; i < num_constraints; ++i) { - ++lastvar; - - /* chose two random bits and XOR them together */ - const std::size_t u = (i == 0 ? std::rand() % num_inputs : std::rand() % i); - const std::size_t v = (i == 0 ? std::rand() % num_inputs : std::rand() % i); - - uscs_constraint constr; - constr.add_term(u + 1, 1); - constr.add_term(v + 1, 1); - constr.add_term(lastvar + 1, 1); - constr.add_term(0, -FieldType::value_type::one()); // shift constant term (which is 0) by 1 - - cs.add_constraint(constr); - full_variable_assignment.push_back(full_variable_assignment[u] + full_variable_assignment[v] - - full_variable_assignment[u] * full_variable_assignment[v] - - full_variable_assignment[u] * full_variable_assignment[v]); - } - - /* split variable assignment */ - uscs_primary_input primary_input(full_variable_assignment.begin(), - full_variable_assignment.begin() + num_inputs); - uscs_primary_input auxiliary_input(full_variable_assignment.begin() + num_inputs, - full_variable_assignment.end()); - - /* sanity checks */ - assert(cs.num_variables() == full_variable_assignment.size()); - assert(cs.num_variables() >= num_inputs); - assert(cs.num_inputs() == num_inputs); - assert(cs.num_constraints() == num_constraints); - assert(cs.is_satisfied(primary_input, auxiliary_input)); - - return uscs_example(std::move(cs), std::move(primary_input), std::move(auxiliary_input)); - } - - } // namespace snark - } // namespace zk - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_USCS_EXAMPLES_HPP diff --git a/crypto3/libs/zk/examples/simple_example.hpp b/crypto3/libs/zk/examples/simple_example.hpp deleted file mode 100644 index 9450da21bd..0000000000 --- a/crypto3/libs/zk/examples/simple_example.hpp +++ /dev/null @@ -1,78 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2018-2021 Mikhail Komarov -// Copyright (c) 2020-2021 Nikita Kaskov -// -// 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_SIMPLE_EXAMPLE_HPP -#define CRYPTO3_SIMPLE_EXAMPLE_HPP - -#include - -namespace nil { - namespace crypto3 { - namespace zk { - namespace snark { - - template - r1cs_example gen_r1cs_example_from_blueprint(const std::size_t num_constraints, - const std::size_t num_inputs); - - /* NOTE: all examples here actually generate one constraint less to account for soundness constraint in - * QAP */ - - template - r1cs_example gen_r1cs_example_from_blueprint(const std::size_t num_constraints) { - const std::size_t new_num_constraints = num_constraints - 1; - - /* construct dummy example: inner products of two vectors */ - blueprint bp; - blueprint_variable_vector A; - blueprint_variable_vector B; - variable res; - - // the variables on the blueprint are (ONE (constant 1 term), res, A[0], ..., A[num_constraints-1], - // B[0], ..., B[num_constraints-1]) - res.allocate(bp); - A.allocate(bp, new_num_constraints); - B.allocate(bp, new_num_constraints); - - inner_product_component compute_inner_product(bp, A, B, res, "compute_inner_product"); - compute_inner_product.generate_r1cs_constraints(); - - /* fill in random example */ - for (std::size_t i = 0; i < new_num_constraints; ++i) { - bp.val(A[i]) = algebra::random_element(); - bp.val(B[i]) = algebra::random_element(); - } - - compute_inner_product.generate_r1cs_witness(); - return r1cs_example( - bp.get_constraint_system(), bp.primary_input(), bp.auxiliary_input()); - } - - } // namespace snark - } // namespace zk - } // namespace crypto3 -} // namespace nil - -#endif // CRYPTO3_SIMPLE_EXAMPLE_HPP