├── .clang-format ├── .envrc ├── .github └── workflows │ ├── run_tests.yml │ └── set_version.yml ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE_1_0.txt ├── README.md ├── VERSION ├── cmake └── Config.cmake.in ├── docs ├── concepts.md ├── extension.md ├── index.md ├── manual.md └── usage.md ├── example ├── CMakeLists.txt ├── curves.cpp ├── plonk │ ├── addition_component.cpp │ ├── addition_component.hpp │ └── component_template.hpp ├── simple_example.hpp ├── test_component.cpp └── test_component.hpp ├── flake.lock ├── flake.nix ├── include └── nil │ ├── blueprint │ ├── algorithms │ │ ├── allocate.hpp │ │ └── generate_circuit.hpp │ ├── assert.hpp │ ├── basic_non_native_policy.hpp │ ├── benchmarks │ │ └── circuit_generator.hpp │ ├── blueprint │ │ ├── plonk │ │ │ ├── assignment.hpp │ │ │ ├── assignment_proxy.hpp │ │ │ ├── circuit.hpp │ │ │ └── circuit_proxy.hpp │ │ └── r1cs │ │ │ ├── circuit.hpp │ │ │ └── detail │ │ │ └── r1cs │ │ │ ├── blueprint_linear_combination.hpp │ │ │ └── blueprint_variable.hpp │ ├── chips │ │ └── plonk │ │ │ ├── bit_check.hpp │ │ │ └── incomplete_addition.hpp │ ├── component.hpp │ ├── component_batch.hpp │ ├── component_stretcher.hpp │ ├── components │ │ ├── algebra │ │ │ ├── curves │ │ │ │ ├── detail │ │ │ │ │ └── r1cs │ │ │ │ │ │ ├── element_g1_affine.hpp │ │ │ │ │ │ ├── element_ops.hpp │ │ │ │ │ │ ├── fixed_base_mul_zcash.hpp │ │ │ │ │ │ ├── mnt4.hpp │ │ │ │ │ │ ├── mnt6.hpp │ │ │ │ │ │ └── scalar_mul.hpp │ │ │ │ ├── edwards │ │ │ │ │ ├── plonk │ │ │ │ │ │ └── non_native │ │ │ │ │ │ │ ├── bool_scalar_multiplication.hpp │ │ │ │ │ │ │ ├── complete_addition.hpp │ │ │ │ │ │ │ ├── doubling.hpp │ │ │ │ │ │ │ ├── ec_point.hpp │ │ │ │ │ │ │ ├── fixed_base_multiplication.hpp │ │ │ │ │ │ │ ├── scalar_non_native_range.hpp │ │ │ │ │ │ │ ├── variable_base_multiplication.hpp │ │ │ │ │ │ │ └── variable_base_multiplication_per_bit.hpp │ │ │ │ │ └── r1cs │ │ │ │ │ │ └── element_g1.hpp │ │ │ │ ├── montgomery │ │ │ │ │ └── element_g1.hpp │ │ │ │ ├── pasta │ │ │ │ │ └── plonk │ │ │ │ │ │ ├── addition.hpp │ │ │ │ │ │ ├── doubling.hpp │ │ │ │ │ │ ├── endo_scalar.hpp │ │ │ │ │ │ ├── fixed_base_scalar_mul_15_wires.hpp │ │ │ │ │ │ ├── fixed_base_scalar_mul_5_wires.hpp │ │ │ │ │ │ ├── fixed_base_scalar_mul_9_wires.hpp │ │ │ │ │ │ ├── multi_scalar_mul_15_wires.hpp │ │ │ │ │ │ ├── tripling.hpp │ │ │ │ │ │ ├── types.hpp │ │ │ │ │ │ ├── unified_addition.hpp │ │ │ │ │ │ ├── variable_base_endo_scalar_mul_15_wires.hpp │ │ │ │ │ │ └── variable_base_scalar_mul.hpp │ │ │ │ ├── twisted_edwards │ │ │ │ │ └── element_g1.hpp │ │ │ │ └── weierstrass │ │ │ │ │ └── r1cs │ │ │ │ │ ├── element_g1.hpp │ │ │ │ │ └── element_g2.hpp │ │ │ ├── fields │ │ │ │ ├── plonk │ │ │ │ │ ├── addition.hpp │ │ │ │ │ ├── bit_shift_constant.hpp │ │ │ │ │ ├── combined_inner_product.hpp │ │ │ │ │ ├── division.hpp │ │ │ │ │ ├── division_or_zero.hpp │ │ │ │ │ ├── element_powers.hpp │ │ │ │ │ ├── exponentiation.hpp │ │ │ │ │ ├── linear_interpolation.hpp │ │ │ │ │ ├── logic_and_flag.hpp │ │ │ │ │ ├── logic_or_flag.hpp │ │ │ │ │ ├── multiplication.hpp │ │ │ │ │ ├── multiplication_by_constant.hpp │ │ │ │ │ ├── non_native │ │ │ │ │ │ ├── addition.hpp │ │ │ │ │ │ ├── bit_composition.hpp │ │ │ │ │ │ ├── bit_decomposition.hpp │ │ │ │ │ │ ├── comparison_checked.hpp │ │ │ │ │ │ ├── comparison_flag.hpp │ │ │ │ │ │ ├── comparison_mode.hpp │ │ │ │ │ │ ├── comparison_unchecked.hpp │ │ │ │ │ │ ├── detail │ │ │ │ │ │ │ ├── bit_builder_component.hpp │ │ │ │ │ │ │ ├── boolean_lookup_op_component.hpp │ │ │ │ │ │ │ └── boolean_op_component.hpp │ │ │ │ │ │ ├── division_remainder.hpp │ │ │ │ │ │ ├── equality_flag.hpp │ │ │ │ │ │ ├── logic_ops.hpp │ │ │ │ │ │ ├── lookup_logic_ops.hpp │ │ │ │ │ │ ├── multiplication.hpp │ │ │ │ │ │ ├── range.hpp │ │ │ │ │ │ ├── reduction.hpp │ │ │ │ │ │ └── subtraction.hpp │ │ │ │ │ ├── quadratic_interpolation.hpp │ │ │ │ │ ├── range_check.hpp │ │ │ │ │ ├── sqrt.hpp │ │ │ │ │ └── subtraction.hpp │ │ │ │ └── r1cs │ │ │ │ │ ├── element_fp.hpp │ │ │ │ │ ├── element_fp2.hpp │ │ │ │ │ ├── element_fp3.hpp │ │ │ │ │ ├── element_fp4.hpp │ │ │ │ │ ├── element_fp6_2over3.hpp │ │ │ │ │ ├── exponentiation.hpp │ │ │ │ │ └── field_to_bits.hpp │ │ │ └── pairing │ │ │ │ ├── detail │ │ │ │ └── r1cs │ │ │ │ │ ├── mnt4.hpp │ │ │ │ │ └── mnt6.hpp │ │ │ │ └── weierstrass │ │ │ │ └── r1cs │ │ │ │ ├── final_exponentiation.hpp │ │ │ │ ├── miller_loop.hpp │ │ │ │ ├── pairing_checks.hpp │ │ │ │ └── precomputation.hpp │ │ ├── boolean │ │ │ └── r1cs │ │ │ │ ├── comparison.hpp │ │ │ │ ├── conjunction.hpp │ │ │ │ ├── disjunction.hpp │ │ │ │ └── inner_product.hpp │ │ ├── component_from_r1cs.hpp │ │ ├── detail │ │ │ └── r1cs │ │ │ │ ├── lookup_1bit.hpp │ │ │ │ ├── lookup_signed_3bit.hpp │ │ │ │ ├── loose_multiplexing.hpp │ │ │ │ └── packing.hpp │ │ ├── hashes │ │ │ ├── digest_selector_component.hpp │ │ │ ├── hash_io.hpp │ │ │ ├── hmac_component.hpp │ │ │ ├── knapsack │ │ │ │ └── r1cs │ │ │ │ │ └── knapsack.hpp │ │ │ ├── pedersen.hpp │ │ │ ├── poseidon │ │ │ │ └── plonk │ │ │ │ │ ├── poseidon.hpp │ │ │ │ │ └── poseidon_constants.hpp │ │ │ └── sha2 │ │ │ │ ├── plonk │ │ │ │ ├── decomposition.hpp │ │ │ │ ├── detail │ │ │ │ │ ├── 8_split_4.txt │ │ │ │ │ ├── 8_split_7.txt │ │ │ │ │ ├── sha_table_generators.hpp │ │ │ │ │ └── split_functions.hpp │ │ │ │ ├── sha256.hpp │ │ │ │ ├── sha256_process.hpp │ │ │ │ ├── sha512.hpp │ │ │ │ └── sha512_process.hpp │ │ │ │ └── r1cs │ │ │ │ ├── sha256_aux.hpp │ │ │ │ ├── sha256_component.hpp │ │ │ │ └── sha256_construction.hpp │ │ ├── merkle_tree │ │ │ ├── plonk │ │ │ │ └── merkle_tree.hpp │ │ │ └── r1cs │ │ │ │ ├── authentication_path.hpp │ │ │ │ ├── check_read.hpp │ │ │ │ ├── check_update.hpp │ │ │ │ ├── prove.hpp │ │ │ │ └── validate.hpp │ │ ├── mock │ │ │ ├── mocked_component_base.hpp │ │ │ └── mocked_components.hpp │ │ ├── pubkey │ │ │ └── eddsa │ │ │ │ └── plonk │ │ │ │ └── non_native │ │ │ │ ├── batched_verification.hpp │ │ │ │ └── verification.hpp │ │ ├── routing │ │ │ └── r1cs │ │ │ │ ├── as_waksman.hpp │ │ │ │ └── benes.hpp │ │ ├── systems │ │ │ ├── set_commitment │ │ │ │ ├── set_commitment_component.hpp │ │ │ │ └── set_membership_proof_variable.hpp │ │ │ └── snark │ │ │ │ ├── pcd │ │ │ │ └── r1cs_pcd │ │ │ │ │ ├── compliance_predicate │ │ │ │ │ └── cp_handler.hpp │ │ │ │ │ └── r1cs_mp_ppzkpcd │ │ │ │ │ └── mp_pcd_circuits.hpp │ │ │ │ ├── plonk │ │ │ │ ├── flexible │ │ │ │ │ ├── additions.hpp │ │ │ │ │ ├── colinear_checks.hpp │ │ │ │ │ ├── constant_pow.hpp │ │ │ │ │ ├── multiplications.hpp │ │ │ │ │ ├── poseidon.hpp │ │ │ │ │ ├── pow_factor.hpp │ │ │ │ │ ├── swap.hpp │ │ │ │ │ └── x_index.hpp │ │ │ │ ├── kimchi │ │ │ │ │ ├── base_details │ │ │ │ │ │ └── batch_dlog_accumulator_check_base.hpp │ │ │ │ │ ├── batch_verify_base_field.hpp │ │ │ │ │ ├── batch_verify_scalar_field.hpp │ │ │ │ │ ├── detail │ │ │ │ │ │ ├── batch_scalar │ │ │ │ │ │ │ ├── prepare_scalars.hpp │ │ │ │ │ │ │ └── random.hpp │ │ │ │ │ │ ├── binding.hpp │ │ │ │ │ │ ├── compare.hpp │ │ │ │ │ │ ├── constraints │ │ │ │ │ │ │ ├── generic_scalars.hpp │ │ │ │ │ │ │ ├── index_terms_scalars.hpp │ │ │ │ │ │ │ ├── perm_scalars.hpp │ │ │ │ │ │ │ ├── rpn_expression.hpp │ │ │ │ │ │ │ ├── rpn_string_literal.hpp │ │ │ │ │ │ │ ├── unnormalized_lagrange_basis.hpp │ │ │ │ │ │ │ └── vanishes_on_last_4_rows.hpp │ │ │ │ │ │ ├── inner_constants.hpp │ │ │ │ │ │ ├── limbs.hpp │ │ │ │ │ │ ├── map_fq.hpp │ │ │ │ │ │ ├── map_fr.hpp │ │ │ │ │ │ ├── oracles_scalar │ │ │ │ │ │ │ ├── b_poly.hpp │ │ │ │ │ │ │ ├── b_poly_coefficients.hpp │ │ │ │ │ │ │ ├── combine_proof_evals.hpp │ │ │ │ │ │ │ ├── element_powers.hpp │ │ │ │ │ │ │ ├── ft_eval.hpp │ │ │ │ │ │ │ ├── lagrange_denominators.hpp │ │ │ │ │ │ │ ├── oracles_cip.hpp │ │ │ │ │ │ │ ├── prev_chal_evals.hpp │ │ │ │ │ │ │ └── public_evaluations.hpp │ │ │ │ │ │ ├── sponge.hpp │ │ │ │ │ │ ├── table_commitment.hpp │ │ │ │ │ │ ├── to_group.hpp │ │ │ │ │ │ ├── transcript_fq.hpp │ │ │ │ │ │ ├── transcript_fr.hpp │ │ │ │ │ │ ├── zk_w3.hpp │ │ │ │ │ │ └── zkpm_evaluate.hpp │ │ │ │ │ ├── oracles_scalar.hpp │ │ │ │ │ ├── prepare_batch_scalar.hpp │ │ │ │ │ ├── proof_system │ │ │ │ │ │ ├── circuit_description.hpp │ │ │ │ │ │ ├── kimchi_commitment_params.hpp │ │ │ │ │ │ └── kimchi_params.hpp │ │ │ │ │ ├── scalar_details │ │ │ │ │ │ ├── batch_dlog_accumulator_check_scalar.hpp │ │ │ │ │ │ ├── derive_plonk.hpp │ │ │ │ │ │ ├── evals_of_split_evals.hpp │ │ │ │ │ │ ├── plonk_map_fields.hpp │ │ │ │ │ │ └── prepare_scalars_inversion.hpp │ │ │ │ │ ├── types │ │ │ │ │ │ ├── alpha_argument_type.hpp │ │ │ │ │ │ ├── binding.hpp │ │ │ │ │ │ ├── column_type.hpp │ │ │ │ │ │ ├── commitment.hpp │ │ │ │ │ │ ├── evaluation_proof.hpp │ │ │ │ │ │ ├── index_term_type.hpp │ │ │ │ │ │ ├── proof.hpp │ │ │ │ │ │ └── verifier_index.hpp │ │ │ │ │ ├── verifier_base_field.hpp │ │ │ │ │ ├── verify_heterogenous_base.hpp │ │ │ │ │ ├── verify_heterogenous_scalar.hpp │ │ │ │ │ └── verify_scalar.hpp │ │ │ │ ├── placeholder │ │ │ │ │ ├── detail │ │ │ │ │ │ ├── expression_evaluation_component.hpp │ │ │ │ │ │ ├── f1_loop.hpp │ │ │ │ │ │ ├── f3_loop.hpp │ │ │ │ │ │ └── gate_component.hpp │ │ │ │ │ ├── fri_array_swap.hpp │ │ │ │ │ ├── fri_cosets.hpp │ │ │ │ │ ├── fri_lin_inter.hpp │ │ │ │ │ ├── gate_argument_verifier.hpp │ │ │ │ │ ├── lookup_argument_verifier.hpp │ │ │ │ │ └── permutation_argument_verifier.hpp │ │ │ │ └── verifier │ │ │ │ │ ├── final_polynomial_check.hpp │ │ │ │ │ ├── proof_input_type.hpp │ │ │ │ │ ├── proof_wrapper.hpp │ │ │ │ │ └── verifier.hpp │ │ │ │ ├── r1cs_pp_zksnark │ │ │ │ └── verifier.hpp │ │ │ │ └── set_commitment.hpp │ │ └── voting │ │ │ └── r1cs │ │ │ └── encrypted_input_voting.hpp │ ├── detail │ │ ├── huang_lu.hpp │ │ ├── lookup_table_loaders.hpp │ │ └── lookup_table_precomputes.hpp │ ├── gate_id.hpp │ ├── lookup_library.hpp │ ├── manifest.hpp │ ├── utils │ │ ├── connectedness_check.hpp │ │ ├── gate_mover.hpp │ │ └── satisfiability_check.hpp │ └── zkevm │ │ ├── bytecode.hpp │ │ ├── operations │ │ ├── add_sub.hpp │ │ ├── div.hpp │ │ ├── iszero.hpp │ │ └── mul.hpp │ │ ├── stack.hpp │ │ ├── state.hpp │ │ ├── state_selector.hpp │ │ ├── zkevm_circuit.hpp │ │ ├── zkevm_machine_interface.hpp │ │ ├── zkevm_opcodes.hpp │ │ ├── zkevm_operation.hpp │ │ └── zkevm_word.hpp │ └── detail │ └── static_pow.hpp ├── run_tests.sh ├── test ├── CMakeLists.txt ├── algebra │ ├── curves │ │ ├── plonk │ │ │ ├── endo_scalar.cpp │ │ │ ├── unified_addition.cpp │ │ │ ├── variable_base_endo_scalar_mul.cpp │ │ │ └── variable_base_scalar_mul.cpp │ │ └── r1cs │ │ │ ├── fixed_base_mul_zcash.cpp │ │ │ ├── montgomery.cpp │ │ │ ├── test_utils.hpp │ │ │ └── twisted_edwards.cpp │ ├── fields │ │ ├── plonk │ │ │ ├── combined_inner_product.cpp │ │ │ ├── element_powers.cpp │ │ │ ├── exponentiation.cpp │ │ │ ├── field_operations.cpp │ │ │ ├── interpolation.cpp │ │ │ ├── logic_and_flag.cpp │ │ │ ├── logic_or_flag.cpp │ │ │ ├── non_native │ │ │ │ ├── addition.cpp │ │ │ │ ├── bit_composition.cpp │ │ │ │ ├── bit_decomposition.cpp │ │ │ │ ├── bit_shift_constant.cpp │ │ │ │ ├── chop_and_glue_non_native.hpp │ │ │ │ ├── comparison_checked.cpp │ │ │ │ ├── comparison_flag.cpp │ │ │ │ ├── comparison_unchecked.cpp │ │ │ │ ├── division_remainder.cpp │ │ │ │ ├── equality_flag.cpp │ │ │ │ ├── logic_ops.cpp │ │ │ │ ├── lookup_logic_ops.cpp │ │ │ │ ├── multiplication.cpp │ │ │ │ ├── range.cpp │ │ │ │ ├── reduction.cpp │ │ │ │ └── subtraction.cpp │ │ │ ├── range_check.cpp │ │ │ └── sqrt.cpp │ │ └── r1cs │ │ │ ├── arithmetic.hpp │ │ │ ├── exponentiation.cpp │ │ │ ├── fp2.cpp │ │ │ ├── fp2_verification.cpp │ │ │ ├── fp3.cpp │ │ │ ├── fp3_verification.cpp │ │ │ ├── fp4.cpp │ │ │ ├── fp4_verification.cpp │ │ │ ├── fp6_2over3.cpp │ │ │ └── fp6_2over3_verification.cpp │ └── pairing │ │ └── weierstrass │ │ └── r1cs │ │ ├── miller_loop.cpp │ │ ├── miller_loop.hpp │ │ └── precomputation.cpp ├── basic_components.cpp ├── basic_components_r1cs_gg_ppzksnark.cpp ├── component_batch.cpp ├── detail │ └── huang_lu.cpp ├── gate_id.cpp ├── hashes │ ├── plonk │ │ ├── decomposition.cpp │ │ ├── detail │ │ │ ├── sha_table_generators_base4.cpp │ │ │ └── sha_table_generators_base7.cpp │ │ ├── poseidon.cpp │ │ ├── sha256.cpp │ │ ├── sha256_process.cpp │ │ ├── sha512.cpp │ │ └── sha512_process.cpp │ └── r1cs │ │ ├── knapsack.cpp │ │ ├── knapsack.hpp │ │ ├── knapsack_verification.cpp │ │ ├── pedersen.cpp │ │ ├── sha256.cpp │ │ ├── sha256.hpp │ │ └── sha256_verification.cpp ├── manifest.cpp ├── merkle_tree_components.cpp ├── mock │ └── mocked_components.cpp ├── non_native │ └── plonk │ │ ├── add_mul_zkllvm_compatible.cpp │ │ ├── bool_scalar_multiplication.cpp │ │ └── scalar_non_native_range.cpp ├── private_input.cpp ├── proxy.cpp ├── r1cs_examples.hpp ├── routing │ └── r1cs │ │ ├── as_waksman.cpp │ │ └── benes.cpp ├── routing_algorithms │ ├── routing_algorithms.cpp │ └── test_routing_algorithms.py ├── set_commitment_component.cpp ├── test_plonk_component.hpp ├── utils │ └── connectedness_check.cpp ├── verifiers │ ├── flexible │ │ ├── additions.cpp │ │ ├── constant_pow.cpp │ │ ├── multiplications.cpp │ │ ├── poseidon.cpp │ │ ├── pow_factor.cpp │ │ └── swap.cpp │ ├── kimchi │ │ ├── base_field.cpp │ │ ├── basic_verifier.cpp │ │ ├── basic_verifier_types.hpp │ │ ├── batch_verify_base_field.cpp │ │ ├── batch_verify_scalar_field.cpp │ │ ├── demo_verifier.cpp │ │ ├── detail │ │ │ ├── b_poly.cpp │ │ │ ├── b_poly_coefficients.cpp │ │ │ ├── combine_proof_evals.cpp │ │ │ ├── constraints │ │ │ │ ├── generic_scalars.cpp │ │ │ │ ├── index_terms_scalars.cpp │ │ │ │ ├── perm_scalars.cpp │ │ │ │ ├── rpn_expression.cpp │ │ │ │ ├── unnormalized_lagrange_basis.cpp │ │ │ │ └── vanishes_on_last_4_rows.cpp │ │ │ ├── ft_eval.cpp │ │ │ ├── lagrange_denominators.cpp │ │ │ ├── oracles_cip.cpp │ │ │ ├── prepare_scalars.cpp │ │ │ ├── prev_chal_evals.cpp │ │ │ ├── public_evaluations.cpp │ │ │ ├── to_group.cpp │ │ │ ├── zk_w3.cpp │ │ │ └── zkpm_evaluate.cpp │ │ ├── index_terms_instances │ │ │ ├── chacha_test.hpp │ │ │ ├── ec_index_terms.hpp │ │ │ ├── ec_index_terms_cip.hpp │ │ │ ├── lookup_test.hpp │ │ │ └── recursion_test.hpp │ │ ├── oracles_scalar.cpp │ │ ├── prepare_batch_scalar.cpp │ │ ├── proof_data.hpp │ │ ├── sponge │ │ │ ├── aux_sponge.hpp │ │ │ ├── aux_transcript_fq.hpp │ │ │ ├── aux_transcript_fr.hpp │ │ │ ├── compare.cpp │ │ │ ├── oracles.cpp │ │ │ ├── sponge.cpp │ │ │ ├── transcript_fq.cpp │ │ │ └── transcript_fr.cpp │ │ ├── table_commitment.cpp │ │ └── verify_scalar.cpp │ ├── pickles │ │ ├── scalar_details │ │ │ └── evals_of_split_evals.cpp │ │ ├── verify_heterogenous_base.cpp │ │ └── verify_heterogenous_scalar.cpp │ ├── placeholder │ │ ├── data │ │ │ └── merkle_tree_poseidon │ │ │ │ ├── circuit.crct │ │ │ │ ├── common.dat │ │ │ │ └── proof.bin │ │ ├── expression_evaluation_component.cpp │ │ ├── f1_loop.cpp │ │ ├── f3_loop.cpp │ │ ├── final_polynomial_check.cpp │ │ ├── fri_array_swap.cpp │ │ ├── fri_cosets.cpp │ │ ├── fri_lin_inter.cpp │ │ ├── gate_argument_verifier.cpp │ │ ├── gate_component.cpp │ │ ├── lookup_argument_verifier.cpp │ │ ├── permutation_argument_verifier.cpp │ │ └── verifier.cpp │ └── r1cs_ppzksnark.cpp ├── verify_r1cs_scheme.hpp ├── voting │ └── r1cs │ │ └── encrypted_input_voting.cpp └── zkevm │ ├── bytecode.cpp │ ├── opcode_tester.hpp │ ├── opcodes │ ├── add_sub.cpp │ ├── div.cpp │ ├── iszero.cpp │ └── mul.cpp │ ├── state_selector.cpp │ ├── state_transition.cpp │ └── zkevm_word.cpp └── zkllvm-blueprint.nix /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | BasedOnStyle: WebKit 3 | AlignAfterOpenBracket: Align 4 | AlignConsecutiveAssignments: false 5 | AlignConsecutiveDeclarations: false 6 | AlignEscapedNewlines: Left 7 | AlignOperands: true 8 | AlignTrailingComments: true 9 | AllowAllParametersOfDeclarationOnNextLine: false 10 | AllowShortBlocksOnASingleLine: false 11 | AllowShortCaseLabelsOnASingleLine: false 12 | AllowShortFunctionsOnASingleLine: None 13 | AllowShortIfStatementsOnASingleLine: Never 14 | AllowShortLoopsOnASingleLine: false 15 | AlwaysBreakAfterDefinitionReturnType: None 16 | AlwaysBreakAfterReturnType: None 17 | AlwaysBreakBeforeMultilineStrings: true 18 | AlwaysBreakTemplateDeclarations: Yes 19 | BinPackArguments: true 20 | BinPackParameters: true 21 | BreakAfterJavaFieldAnnotations: true 22 | BreakBeforeBinaryOperators: None 23 | BreakBeforeBraces: Custom 24 | BraceWrapping: 25 | AfterClass: false 26 | AfterControlStatement: false 27 | AfterEnum: false 28 | AfterFunction: false 29 | AfterNamespace: false 30 | AfterObjCDeclaration: false 31 | AfterStruct: false 32 | AfterUnion: false 33 | AfterExternBlock: false 34 | BeforeCatch: false 35 | BeforeElse: false 36 | IndentBraces: false 37 | SplitEmptyFunction: false 38 | SplitEmptyRecord: false 39 | SplitEmptyNamespace: false 40 | BreakBeforeInheritanceComma: false 41 | BreakBeforeTernaryOperators: false 42 | BreakConstructorInitializers: AfterColon 43 | BreakStringLiterals: true 44 | ColumnLimit: 120 45 | CompactNamespaces: false 46 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 47 | ConstructorInitializerIndentWidth: 4 48 | ContinuationIndentWidth: 4 49 | Cpp11BracedListStyle: true 50 | DerivePointerAlignment: false 51 | DisableFormat: false 52 | ExperimentalAutoDetectBinPacking: true 53 | FixNamespaceComments: true 54 | ForEachMacros: [ 'BOOST_FOREACH' ] 55 | IncludeBlocks: Regroup 56 | IncludeCategories: 57 | - Regex: '^"(<)/' 58 | Priority: 1 59 | - Regex: '^(<(boost)/)' 60 | Priority: 2 61 | - Regex: '^(<(nil\/algebra)/)' 62 | Priority: 3 63 | - Regex: '.*' 64 | Priority: 4 65 | IndentCaseLabels: true 66 | IndentPPDirectives: None 67 | IndentWidth: 4 68 | IndentWrappedFunctionNames: true 69 | KeepEmptyLinesAtTheStartOfBlocks: true 70 | Language: Cpp 71 | NamespaceIndentation: All 72 | ObjCBlockIndentWidth: 4 73 | ObjCSpaceAfterProperty: true 74 | ObjCSpaceBeforeProtocolList: true 75 | PointerAlignment: Right 76 | ReflowComments: true 77 | SortIncludes: false 78 | SortUsingDeclarations: true 79 | SpaceAfterCStyleCast: false 80 | SpaceAfterTemplateKeyword: false 81 | SpaceBeforeAssignmentOperators: true 82 | SpaceBeforeParens: ControlStatements 83 | SpaceInEmptyParentheses: false 84 | SpacesBeforeTrailingComments: 4 85 | SpacesInAngles: false 86 | SpacesInCStyleCastParentheses: false 87 | SpacesInContainerLiterals: true 88 | SpacesInParentheses: false 89 | SpacesInSquareBrackets: false 90 | Standard: Cpp11 91 | TabWidth: 4 92 | UseTab: Never 93 | 94 | ... 95 | -------------------------------------------------------------------------------- /.envrc: -------------------------------------------------------------------------------- 1 | use flake 2 | -------------------------------------------------------------------------------- /.github/workflows/run_tests.yml: -------------------------------------------------------------------------------- 1 | name: Run tests 2 | 3 | on: 4 | # Triggers the workflow on pull request events but only for the master branch 5 | pull_request: 6 | branches: [ master ] 7 | push: 8 | branches: [ master ] 9 | 10 | # Allows you to run this workflow manually from the Actions tab 11 | workflow_dispatch: 12 | 13 | env: 14 | SUITE_REPO: "NilFoundation/crypto3" 15 | LIB_NAME: "blueprint" 16 | CACHE_NAME: "blueprint-job-cache" 17 | 18 | jobs: 19 | handle-syncwith: 20 | if: github.event_name == 'pull_request' 21 | name: Call Reusable SyncWith Handler 22 | uses: NilFoundation/ci-cd/.github/workflows/reusable-handle-syncwith.yml@v1 23 | with: 24 | ci-cd-ref: 'v1' 25 | secrets: inherit 26 | 27 | build-and-test: 28 | needs: [ handle-syncwith ] 29 | runs-on: ["self-hosted", "aws_autoscaling"] 30 | strategy: 31 | fail-fast: false 32 | steps: 33 | # https://github.com/actions/checkout/issues/1552 34 | - name: Clean up after previous checkout 35 | run: chmod +w -R ${GITHUB_WORKSPACE}; rm -rf ${GITHUB_WORKSPACE}/*; 36 | 37 | - name: Checkout Blueprint 38 | uses: actions/checkout@v4 39 | with: 40 | fetch-depth: 0 41 | submodules: recursive 42 | 43 | - name: Checkout submodules to specified refs 44 | if: inputs.submodules-refs != '' 45 | uses: NilFoundation/ci-cd/actions/recursive-checkout@v1.2.1 46 | with: 47 | refs: ${{ inputs.submodules-refs }} 48 | paths: | 49 | ${{ github.workspace }}/** 50 | !${{ github.workspace }}/ 51 | !${{ github.workspace }}/**/.git/** 52 | 53 | # nix is taken from the cloud-init template, no need to install it. 54 | - name: Build and run tests 55 | env: 56 | NIX_CONFIG: | 57 | cores = 8 58 | max-jobs = 4 59 | run: | 60 | nix build -L .?submodules=1#checks.x86_64-linux.default 61 | -------------------------------------------------------------------------------- /.github/workflows/set_version.yml: -------------------------------------------------------------------------------- 1 | name: Set version 2 | 3 | on: 4 | #Disabled because the workflow does not actually work with current runner permissions 5 | #push: 6 | # branches: [ master ] 7 | workflow_dispatch: 8 | 9 | jobs: 10 | set_version: 11 | name: Set and tag version 12 | runs-on: [ubuntu-latest] 13 | env: 14 | VERSION_FILE_NAME: VERSION 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v3 18 | 19 | - name: Set version 20 | id: set_version 21 | run: | 22 | version=$(cat ${{ env.VERSION_FILE_NAME }} | tr -d '\r').$GITHUB_RUN_NUMBER 23 | echo "VERSION=$version" >> $GITHUB_ENV 24 | 25 | - name: Tag new version 26 | run: git tag v${{ env.VERSION }} 27 | 28 | - name: Push tags 29 | uses: ad-m/github-push-action@master 30 | with: 31 | tags: true 32 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NilFoundation/zkllvm-blueprint/faa6439e00bfcdb72fc4314f42f29240a716ae91/.gitmodules -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.21.4) 2 | 3 | project(crypto3_blueprint VERSION 0.1.0 LANGUAGES C CXX) 4 | 5 | option(CMAKE_ENABLE_TESTS "Enable tests" FALSE) # used by CMTest module 6 | option(BUILD_EXAMPLES "Build examples" FALSE) 7 | 8 | find_package(CM) 9 | find_package(crypto3 REQUIRED) 10 | find_package(Boost REQUIRED COMPONENTS container random filesystem log log_setup program_options thread system) 11 | 12 | 13 | add_library(blueprint INTERFACE) 14 | add_library(crypto3::blueprint ALIAS blueprint) 15 | 16 | include(GNUInstallDirs) 17 | target_include_directories(blueprint INTERFACE 18 | "$" 19 | "$" 20 | ) 21 | 22 | target_link_libraries(blueprint INTERFACE 23 | ${Boost_LIBRARIES} 24 | crypto3::all 25 | ) 26 | 27 | include(CMTest) 28 | cm_add_test_subdirectory(test) 29 | 30 | if(BUILD_EXAMPLES) 31 | add_subdirectory(example) 32 | endif() 33 | 34 | # Install phase 35 | 36 | set(EXPORT_NAME crypto3_blueprint-targets) 37 | # Install target 38 | install(TARGETS blueprint EXPORT ${EXPORT_NAME}) 39 | # Install headers 40 | install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) 41 | 42 | # Generate and install package config files 43 | 44 | include(CMakePackageConfigHelpers) 45 | set(CONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/crypto3_blueprint) 46 | set(PACKAGE_CONFIG_NAME crypto3_blueprint-config) 47 | 48 | write_basic_config_version_file( 49 | ${PACKAGE_CONFIG_NAME}-version.cmake 50 | VERSION ${crypto3_blueprint_VERSION} 51 | COMPATIBILITY SameMajorVersion 52 | ) 53 | 54 | install( 55 | FILES 56 | ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_CONFIG_NAME}.cmake 57 | ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_CONFIG_NAME}-version.cmake 58 | DESTINATION ${CONFIG_DIR} 59 | ) 60 | 61 | install(EXPORT ${EXPORT_NAME} 62 | NAMESPACE crypto3:: 63 | DESTINATION ${CONFIG_DIR}) 64 | -------------------------------------------------------------------------------- /LICENSE_1_0.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020-2021 Mikhail Komarov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Circuit Definition Library for =nil; Foundation's Cryptography Suite 2 | 3 | [![Run tests](https://github.com/NilFoundation/zkllvm-blueprint/actions/workflows/run_tests.yml/badge.svg)](https://github.com/NilFoundation/zkllvm-blueprint/actions/workflows/run_tests.yml) 4 | 5 | ## Dependencies 6 | 7 | - [Boost](https://boost.org) (>= 1.76) 8 | - [cmake](https://cmake.org/) (>=3.21.4) 9 | - Following dependencies must be built and installed from sources: 10 | - [CMake Modules](https://github.com/BoostCMake/cmake_modules.git) 11 | - [crypto3](https://github.com/nilfoundation/crypto3.git) 12 | 13 | ## Building and installation 14 | 15 | ```bash 16 | cmake -B build -DCMAKE_INSTALL_PREFIX=/path/to/install 17 | make -C build install 18 | ``` 19 | 20 | > Note: if you got an error on `find_package` during cmake configuration, make sure that you provided paths to the installed dependencies (for example, via `CMAKE_PREFIX_PATH` environment variable) 21 | 22 | ## Nix support 23 | This repository provides Nix flake, so once you have installed Nix with flake support, you can use single command to fetch all the dependencies and build: 24 | 25 | ```bash 26 | nix build ?submodules=1# 27 | ``` 28 | 29 | To activate Nix development environment: 30 | 31 | ```bash 32 | nix develop 33 | ``` 34 | 35 | To run all tests: 36 | 37 | ```bash 38 | nix flake check -L ?submodules=1# 39 | ``` 40 | 41 | To build/develop/test with local crypto3 version, add an argument `--override-input nil-crypto3 /path/to/local/crypto3` to any of the above commands. 42 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.0 2 | -------------------------------------------------------------------------------- /cmake/Config.cmake.in: -------------------------------------------------------------------------------- 1 | @PACKAGE_INIT@ 2 | 3 | include(CMakeFindDependencyMacro) 4 | find_dependency(Boost COMPONENTS REQUIRED 5 | container json filesystem log log_setup program_options random thread system unit_test_framework) 6 | find_dependency(crypto3 REQUIRED) 7 | 8 | include("${CMAKE_CURRENT_LIST_DIR}/crypto3_blueprint-targets.cmake") 9 | -------------------------------------------------------------------------------- /docs/concepts.md: -------------------------------------------------------------------------------- 1 | # Concepts # {#component_concepts} 2 | 3 | A ```circuit``` is defined by ```Blueprint``` and ```Blueprint assignment table``` (contains ```Blueprint public assignment table``` and ```Blueprint private assignment table```) instances. 4 | It consist of one or multiple components putted on these two. 5 | While ```Blueprint``` holds information about the circuit itself, its gates, constraints and other fixed expressions, ```Blueprint assignment table``` contains public and private assignments needed by zk-SNARK system. 6 | 7 | ## Blueprint 8 | 9 | ## PLONK component concept ## {##plonk_component_concepts} 10 | 11 | ### PLONK Component interface ### 12 | 13 | A ```Component``` ```X``` is a state-less type with following static functions to operate with it: 14 | 15 | * ```X::allocate_rows``` - allocates required amount of rows in the given ```Arithmetization table```. The amount of required rows amount is constexpr for the particular component; 16 | * ```X::generate_circuit``` - generates gate expressions, copy constraints and lookup constraints and puts them on the given ```Blueprint```; 17 | * ```X::generate_assignments``` - evaluates assignments values and puts them on the given ```Blueprint assignment table```; 18 | 19 | Note that ```generate_circuit``` can modify of the ```Blueprint public assignment table``` setting ```Constant``` or ```Selector``` columns, but they don't use or set data of the ```Blueprint private assignment table```. The only function managing ```Blueprint private assignment table``` is ```generate_assignments``` - which, by the way, also can modify the ```Blueprint public assignment table```. In short, it looks like that: 20 | 21 | |Function |Required Input |Can modify | 22 | |-----------------------------|------------------------|-----------------------| 23 | |```X::allocate_rows``` |```Blueprint``` |```Blueprint```| 24 | |```X::generate_circuit``` |```Blueprint```, ```Component params```, ```Allocated data (auxiliary data for the component re-use)```, ```component start row``` |```Blueprint```, ```Allocated data```| 25 | |```X::generate_assignments``` |```Blueprint assignment table```, ```Component params```, ```component start row``` |```Blueprint assignment table```| 26 | 27 | The process of adding a component is following: 28 | 29 | 1. (Optional) Get ```component``` start row by calling ```allocate_rows```. If the ```component``` is used as part of other ```component``` logic, it's not necessary to call the function, because needed rows are allocated by the master ```component```. 30 | 2. (Optional) Allocate public input on the ```Blueprint assignment table``` via ```Blueprint assignment table::allocate_public_input```. 31 | 3. Set all the gates and constraints on the ```Blueprint``` by calling ```generate_circuit```. ```Allocated data``` is being modified in process of the function working. 32 | 4. Set all the assignments on the ```Blueprint assignment table``` table by calling ```generate_assignments```. -------------------------------------------------------------------------------- /docs/extension.md: -------------------------------------------------------------------------------- 1 | # Extension {#blueprint_extension_manual} 2 | 3 | @tableofcontents 4 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # Blueprint (Circuit Definition Library) {#blueprint_index} 2 | 3 | @subpage blueprint_manual 4 | -------------------------------------------------------------------------------- /docs/manual.md: -------------------------------------------------------------------------------- 1 | # Manual {#blueprint_manual} 2 | 3 | @subpage blueprint_usage_manual 4 | @subpage blueprint_extension_manual 5 | -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #---------------------------------------------------------------------------# 2 | # Copyright (c) 2018-2021 Mikhail Komarov 3 | # Copyright (c) 2020-2021 Nikita Kaskov 4 | # 5 | # Distributed under the Boost Software License, Version 1.0 6 | # See accompanying file LICENSE_1_0.txt or copy at 7 | # http://www.boost.org/LICENSE_1_0.txt 8 | #---------------------------------------------------------------------------# 9 | 10 | macro(define_blueprint_example name) 11 | add_executable(blueprint_${name}_example ${name}.cpp) 12 | target_link_libraries(blueprint_${name}_example PRIVATE 13 | ${CMAKE_WORKSPACE_NAME}_blueprint 14 | 15 | ${CMAKE_WORKSPACE_NAME}::algebra 16 | ${CMAKE_WORKSPACE_NAME}::math 17 | ${CMAKE_WORKSPACE_NAME}::block 18 | ${CMAKE_WORKSPACE_NAME}::hash 19 | ${CMAKE_WORKSPACE_NAME}::multiprecision 20 | ${CMAKE_WORKSPACE_NAME}::zk 21 | marshalling::crypto3_zk 22 | 23 | ${Boost_LIBRARIES}) 24 | set_target_properties(blueprint_${name}_example PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED TRUE) 25 | endmacro() 26 | 27 | 28 | 29 | set(EXAMPLES_NAMES 30 | "curves" 31 | "test_component" 32 | "plonk/addition_component") 33 | 34 | foreach(EXAMPLE_NAME ${EXAMPLES_NAMES}) 35 | define_blueprint_example(${EXAMPLE_NAME}) 36 | endforeach() -------------------------------------------------------------------------------- /example/plonk/addition_component.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2021-2022 Mikhail Komarov 3 | // Copyright (c) 2021-2022 Nikita Kaskov 4 | // Copyright (c) 2022 Ilia Shirobokov 5 | // 6 | // MIT License 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in all 16 | // copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | // SOFTWARE. 25 | //---------------------------------------------------------------------------// 26 | 27 | #define BOOST_TEST_MODULE blueprint_plonk_addition_example_test 28 | 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | #include 40 | 41 | #include 42 | #include 43 | 44 | #include "addition_component.hpp" 45 | #include "../test/test_plonk_component.hpp" 46 | 47 | using namespace nil::crypto3; 48 | 49 | BOOST_AUTO_TEST_SUITE(blueprint_plonk_addition_example_suite) 50 | 51 | BOOST_AUTO_TEST_CASE(blueprint_plonk_addition_example) { 52 | using curve_type = algebra::curves::pallas; 53 | using BlueprintFieldType = typename curve_type::base_field_type; 54 | constexpr std::size_t WitnessColumns = 3; 55 | constexpr std::size_t PublicInputColumns = 1; 56 | constexpr std::size_t ConstantColumns = 0; 57 | constexpr std::size_t SelectorColumns = 1; 58 | using ArithmetizationParams = 59 | zk::snark::plonk_arithmetization_params; 60 | using ArithmetizationType = zk::snark::plonk_constraint_system; 61 | using hash_type = nil::crypto3::hashes::keccak_1600<256>; 62 | constexpr std::size_t Lambda = 40; 63 | 64 | using var = zk::snark::plonk_variable; 65 | 66 | using component_type = zk::components::addition; 67 | 68 | // test_component set public input to the first rows of the public_input columns 69 | typename component_type::params_type params = { 70 | var(0, 0, false, var::column_type::public_input), var(0, 1, false, var::column_type::public_input), 71 | var(0, 2, false, var::column_type::public_input)}; 72 | 73 | typename BlueprintFieldType::value_type x = 1; 74 | typename BlueprintFieldType::value_type y = 3; 75 | typename BlueprintFieldType::value_type sum = 4; 76 | 77 | std::vector public_input = {x, y, sum}; 78 | 79 | test_component(params, public_input); 80 | } 81 | 82 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /example/simple_example.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #ifndef CRYPTO3_SIMPLE_EXAMPLE_HPP 27 | #define CRYPTO3_SIMPLE_EXAMPLE_HPP 28 | 29 | #include "relations/constraint_satisfaction_problems/r1cs/examples/r1cs_examples.hpp" 30 | 31 | #include 32 | 33 | namespace nil { 34 | namespace crypto3 { 35 | namespace blueprint { 36 | namespace components { 37 | 38 | template 39 | r1cs_example gen_r1cs_example_from_blueprint(const std::size_t num_constraints, 40 | const std::size_t num_inputs); 41 | 42 | /* NOTE: all examples here actually generate one constraint less to account for soundness constraint in 43 | * QAP */ 44 | 45 | template 46 | r1cs_example gen_r1cs_example_from_blueprint(const std::size_t num_constraints) { 47 | const std::size_t new_num_constraints = num_constraints - 1; 48 | 49 | /* construct dummy example: inner products of two vectors */ 50 | blueprint bp; 51 | blueprint_variable_vector A; 52 | blueprint_variable_vector B; 53 | blueprint_variable res; 54 | 55 | // the variables on the blueprint are (ONE (constant 1 term), res, A[0], ..., A[num_constraints-1], 56 | // B[0], ..., B[num_constraints-1]) 57 | res.allocate(bp); 58 | A.allocate(bp, new_num_constraints); 59 | B.allocate(bp, new_num_constraints); 60 | 61 | inner_product compute_inner_product(bp, A, B, res, "compute_inner_product"); 62 | compute_inner_product.generate_gates(); 63 | 64 | /* fill in random example */ 65 | for (std::size_t i = 0; i < new_num_constraints; ++i) { 66 | bp.val(A[i]) = algebra::random_element(); 67 | bp.val(B[i]) = algebra::random_element(); 68 | } 69 | 70 | compute_inner_product.generate_assignments(); 71 | return r1cs_example( 72 | bp.get_constraint_system(), bp.primary_input(), bp.auxiliary_input()); 73 | } 74 | 75 | } // namespace components 76 | } // namespace blueprint 77 | } // namespace crypto3 78 | } // namespace nil 79 | 80 | #endif // CRYPTO3_SIMPLE_EXAMPLE_HPP 81 | -------------------------------------------------------------------------------- /example/test_component.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | 21 | #include "test_component.hpp" 22 | 23 | using namespace nil::crypto3::zk; 24 | using namespace nil::crypto3::algebra; 25 | 26 | int main(){ 27 | 28 | using curve_type = curves::bls12<381>; 29 | using field_type = typename curve_type::scalar_field_type; 30 | 31 | // Create blueprint 32 | 33 | blueprint bp; 34 | blueprint::value_type out; 35 | blueprint::value_type x; 36 | 37 | // Allocate variables 38 | 39 | out.allocate(bp); 40 | x.allocate(bp); 41 | 42 | // This sets up the blueprint variables 43 | // so that the first one (out) represents the public 44 | // input and the rest is private input 45 | 46 | bp.set_input_sizes(1); 47 | 48 | // Initialize component 49 | 50 | test_component g(bp, out, x); 51 | g.generate_gates(); 52 | 53 | // Add witness values 54 | 55 | bp.val(out) = 35; 56 | bp.val(x) = 3; 57 | 58 | g.generate_assignments(); 59 | 60 | assert(bp.is_satisfied()); 61 | 62 | const snark::r1cs_constraint_system constraint_system = bp.get_constraint_system(); 63 | 64 | const typename snark::r1cs_gg_ppzksnark::keypair_type keypair = snark::generate>(constraint_system); 65 | 66 | const typename snark::r1cs_gg_ppzksnark::proof_type proof = snark::prove>(keypair.first, bp.primary_input(), bp.auxiliary_input()); 67 | 68 | bool verified = snark::verify>(keypair.second, bp.primary_input(), proof); 69 | 70 | std::cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << std::endl; 71 | std::cout << "Verification status: " << verified << std::endl; 72 | 73 | const typename snark::r1cs_gg_ppzksnark::verification_key_type vk = keypair.second; 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /example/test_component.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #ifndef CRYPTO3_BLUEPRINT_EXAMPLE_TEST_COMPONENT_HPP 27 | #define CRYPTO3_BLUEPRINT_EXAMPLE_TEST_COMPONENT_HPP 28 | 29 | #include 30 | 31 | #include 32 | 33 | #include 34 | 35 | using namespace nil::crypto3::zk; 36 | using namespace nil::crypto3::algebra; 37 | 38 | template 39 | class test_component : public components::component { 40 | using field_type = FieldType; 41 | components::blueprint_variable sym_1; 42 | components::blueprint_variable y; 43 | components::blueprint_variable sym_2; 44 | public: 45 | const components::blueprint_variable out; 46 | const components::blueprint_variable x; 47 | 48 | test_component(blueprint &bp, 49 | const components::blueprint_variable &out, 50 | const components::blueprint_variable &x) : 51 | components::component(bp), out(out), x(x) { 52 | 53 | // Allocate variables to blueprint 54 | 55 | sym_1.allocate(this->bp); 56 | y.allocate(this->bp); 57 | sym_2.allocate(this->bp); 58 | } 59 | 60 | void generate_gates() { 61 | // x*x = sym_1 62 | this->bp.add_r1cs_constraint(snark::r1cs_constraint(x, x, sym_1)); 63 | 64 | // sym_1 * x = y 65 | this->bp.add_r1cs_constraint(snark::r1cs_constraint(sym_1, x, y)); 66 | 67 | // y + x = sym_2 68 | this->bp.add_r1cs_constraint(snark::r1cs_constraint(y + x, 1, sym_2)); 69 | 70 | // sym_2 + 5 = ~out 71 | this->bp.add_r1cs_constraint(snark::r1cs_constraint(sym_2 + 5, 1, out)); 72 | } 73 | 74 | void generate_assignments() { 75 | this->bp.val(sym_1) = this->bp.val(x) * this->bp.val(x); 76 | this->bp.val(y) = this->bp.val(sym_1) * this->bp.val(x); 77 | this->bp.val(sym_2) = this->bp.val(y) + this->bp.val(x); 78 | } 79 | }; 80 | 81 | #endif // CRYPTO3_BLUEPRINT_EXAMPLE_TEST_COMPONENT_HPP 82 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "Nix flake for zkllvm-blueprint"; 3 | 4 | inputs = { 5 | nixpkgs.url = "github:NixOS/nixpkgs"; 6 | flake-utils.url = "github:numtide/flake-utils"; 7 | nix-3rdparty = { 8 | url = "github:NilFoundation/nix-3rdparty"; 9 | inputs = { 10 | nixpkgs.follows = "nixpkgs"; 11 | flake-utils.follows = "flake-utils"; 12 | }; 13 | }; 14 | nil-crypto3 = { 15 | url = "https://github.com/NilFoundation/crypto3"; 16 | type = "git"; 17 | inputs = { 18 | nixpkgs.follows = "nixpkgs"; 19 | }; 20 | }; 21 | }; 22 | 23 | outputs = { self, nixpkgs, nil-crypto3, flake-utils, nix-3rdparty }: 24 | (flake-utils.lib.eachDefaultSystem (system: 25 | let 26 | pkgs = import nixpkgs { inherit system; }; 27 | stdenv = pkgs.llvmPackages_16.stdenv; 28 | crypto3 = nil-crypto3.packages.${system}.crypto3; 29 | cmake_modules = nix-3rdparty.packages.${system}.cmake_modules; 30 | in { 31 | packages = rec { 32 | zkllvm-blueprint = (pkgs.callPackage ./zkllvm-blueprint.nix { 33 | src_repo = self; 34 | crypto3 = crypto3; 35 | cmake_modules = cmake_modules; 36 | }); 37 | zkllvm-blueprint-debug = (pkgs.callPackage ./zkllvm-blueprint.nix { 38 | src_repo = self; 39 | crypto3 = crypto3; 40 | cmake_modules = cmake_modules; 41 | enableDebug = true; 42 | }); 43 | zkllvm-blueprint-debug-tests = (pkgs.callPackage ./zkllvm-blueprint.nix { 44 | src_repo = self; 45 | crypto3 = crypto3; 46 | cmake_modules = cmake_modules; 47 | enableDebug = true; 48 | runTests = true; 49 | }); 50 | default = zkllvm-blueprint-debug-tests; 51 | }; 52 | checks = rec { 53 | gcc = (pkgs.callPackage ./zkllvm-blueprint.nix { 54 | src_repo = self; 55 | crypto3 = crypto3; 56 | cmake_modules = cmake_modules; 57 | runTests = true; 58 | }); 59 | clang = (pkgs.callPackage ./zkllvm-blueprint.nix { 60 | stdenv = pkgs.llvmPackages_18.stdenv; 61 | src_repo = self; 62 | crypto3 = crypto3; 63 | cmake_modules = cmake_modules; 64 | runTests = true; 65 | }); 66 | all = pkgs.symlinkJoin { 67 | name = "all"; 68 | paths = [ gcc clang ]; 69 | }; 70 | default = all; 71 | }; 72 | })); 73 | } 74 | 75 | # `nix flake -L check` to run all tests (-L to output build logs) 76 | # `nix flake show` to show derivations tree 77 | # If build fails due to OOM, run `export NIX_CONFIG="cores = 2"` to set desired parallel level 78 | -------------------------------------------------------------------------------- /include/nil/blueprint/algorithms/allocate.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2022 Mikhail Komarov 3 | // Copyright (c) 2022 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #ifndef CRYPTO3_ZK_COMPONENTS_ALGORITHMS_ALLOCATE_HPP 27 | #define CRYPTO3_ZK_COMPONENTS_ALGORITHMS_ALLOCATE_HPP 28 | 29 | #include 30 | 31 | namespace nil { 32 | namespace crypto3 { 33 | namespace blueprint { 34 | namespace components { 35 | 36 | template 37 | std::uint32_t allocate( 38 | ComponentType component_instance, 39 | BlueprintType &bp, 40 | const std::uint32_t components_amount = 1) { 41 | 42 | return bp.allocate_rows(component_instance.rows_amount() * components_amount); 43 | } 44 | 45 | } // namespace components 46 | } // namespace blueprint 47 | } // namespace crypto3 48 | } // namespace nil 49 | 50 | #endif // CRYPTO3_ZK_COMPONENTS_ALGORITHMS_ALLOCATE_HPP 51 | -------------------------------------------------------------------------------- /include/nil/blueprint/assert.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2022 Mikhail Komarov 3 | // Copyright (c) 2022 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_ASSERT_HPP 27 | #define CRYPTO3_BLUEPRINT_COMPONENTS_ASSERT_HPP 28 | 29 | #include 30 | #include 31 | 32 | namespace nil { 33 | namespace blueprint { 34 | namespace detail { 35 | template 36 | static void blueprint_assert(T1 line, T2 file, T3 expr){ 37 | std::stringstream errMsg; 38 | errMsg << "Assertion " << expr << " failed on line " << line << " in file " << file; 39 | throw std::runtime_error(errMsg.str().c_str()); 40 | } 41 | } // namespace detail 42 | } // namespace blueprint 43 | } // namespace nil 44 | 45 | #define BLUEPRINT_RELEASE_ASSERT( expr ) \ 46 | ( (expr) ? (void)0 : nil::blueprint::detail::blueprint_assert( __LINE__, __FILE__, #expr)) 47 | 48 | #ifdef BLUEPRINT_DEBUG_ENABLED 49 | #define BLUEPRINT_ASSERT( expr ) \ 50 | BLUEPRINT_RELEASE_ASSERT( expr ) 51 | #else 52 | #define BLUEPRINT_ASSERT( expr ) ((void)0) 53 | #endif 54 | 55 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_ASSERT_HPP 56 | -------------------------------------------------------------------------------- /include/nil/blueprint/chips/plonk/bit_check.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2021 Mikhail Komarov 3 | // Copyright (c) 2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | // @file Declaration of interfaces for PLONK unified addition component. 26 | //---------------------------------------------------------------------------// 27 | 28 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_BIT_CHECK_CHIP_HPP 29 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_BIT_CHECK_CHIP_HPP 30 | 31 | #include 32 | 33 | #include 34 | #include 35 | 36 | namespace nil { 37 | namespace crypto3 { 38 | namespace blueprint { 39 | namespace chips { 40 | 41 | template 42 | class bit_check; 43 | 44 | template 46 | class bit_check< 47 | snark::plonk_constraint_system>{ 49 | 50 | typedef snark::plonk_constraint_system ArithmetizationType; 52 | 53 | using var = snark::plonk_variable; 54 | public: 55 | constexpr static const std::size_t rows_amount = 0; 56 | 57 | static snark::plonk_constraint generate( 58 | blueprint &bp, 59 | const var& X1) { 60 | 61 | return bp.add_constraint(X1 * (X1 - 1)); 62 | } 63 | }; 64 | } // namespace chips 65 | } // namespace blueprint 66 | } // namespace crypto3 67 | } // namespace nil 68 | 69 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_BIT_CHECK_CHIP_HPP 70 | -------------------------------------------------------------------------------- /include/nil/blueprint/chips/plonk/incomplete_addition.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2021 Mikhail Komarov 3 | // Copyright (c) 2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | // @file Declaration of interfaces for PLONK unified addition component. 26 | //---------------------------------------------------------------------------// 27 | 28 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_INCOMPLETE_ADDITION_CHIP_HPP 29 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_INCOMPLETE_ADDITION_CHIP_HPP 30 | 31 | #include 32 | 33 | #include 34 | #include 35 | 36 | namespace nil { 37 | namespace crypto3 { 38 | namespace blueprint { 39 | namespace chips { 40 | 41 | template 43 | class incomplete_addition; 44 | 45 | template 48 | class incomplete_addition< 49 | snark::plonk_constraint_system, 51 | CurveType>{ 52 | 53 | typedef snark::plonk_constraint_system ArithmetizationType; 55 | 56 | using var = snark::plonk_variable; 57 | public: 58 | constexpr static const std::size_t rows_amount = 0; 59 | 60 | static snark::plonk_constraint generate( 61 | blueprint &bp, 62 | blueprint_public_assignment_table &public_assignment, 63 | const var& X1, const var& Y1, const var& X2, const var& Y2, 64 | const var& X3, const var& Y3) { 65 | 66 | return bp.add_constraint(); 67 | } 68 | }; 69 | } // namespace chips 70 | } // namespace blueprint 71 | } // namespace crypto3 72 | } // namespace nil 73 | 74 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_INCOMPLETE_ADDITION_CHIP_HPP 75 | -------------------------------------------------------------------------------- /include/nil/blueprint/components/algebra/curves/detail/r1cs/element_ops.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2021 Mikhail Komarov 3 | // Copyright (c) 2021 Nikita Kaskov 4 | // Copyright (c) 2021 Ilias Khairullin 5 | // 6 | // MIT License 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in all 16 | // copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | // SOFTWARE. 25 | //---------------------------------------------------------------------------// 26 | // @file Declaration of available operation components over curve group elements. 27 | //---------------------------------------------------------------------------// 28 | 29 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_ELEMENT_OPS_COMPONENT_HPP 30 | #define CRYPTO3_BLUEPRINT_COMPONENTS_ELEMENT_OPS_COMPONENT_HPP 31 | 32 | namespace nil { 33 | namespace crypto3 { 34 | namespace blueprint { 35 | namespace components { 36 | template 37 | struct element_g1_is_well_formed { }; 38 | 39 | template 40 | struct element_g1_addition { }; 41 | 42 | template 43 | struct element_g1_conditional_addition { }; 44 | 45 | template 46 | struct element_g1_to_twisted_edwards { }; 47 | 48 | template 49 | struct element_g1_to_bits { }; 50 | } // namespace components 51 | } // namespace blueprint 52 | } // namespace crypto3 53 | } // namespace nil 54 | 55 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_ELEMENT_OPS_COMPONENT_HPP 56 | -------------------------------------------------------------------------------- /include/nil/blueprint/components/algebra/curves/detail/r1cs/mnt4.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | // @file Declaration of specializations of basic_curve_component_policy to 26 | // - basic_curve_component_policy. 27 | // 28 | // See pairing_params.hpp . 29 | //---------------------------------------------------------------------------// 30 | 31 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_MNT4_BASIC_CURVE_COMPONENT_POLICY_HPP 32 | #define CRYPTO3_BLUEPRINT_COMPONENTS_MNT4_BASIC_CURVE_COMPONENT_POLICY_HPP 33 | 34 | #include 35 | 36 | #include 37 | #include 38 | 39 | namespace nil { 40 | namespace crypto3 { 41 | namespace blueprint { 42 | namespace components { 43 | 44 | using namespace nil::crypto3::algebra; 45 | 46 | template 47 | class basic_curve_component_policy; 48 | 49 | /** 50 | * Specialization for MNT4. 51 | */ 52 | template 53 | class basic_curve_component_policy> { 54 | using curve_type = typename curves::mnt4; 55 | 56 | typedef typename curve_type::chained_on_curve_type chained_on_curve_type; // mnt6 57 | 58 | typedef typename chained_on_curve_type::pairing::fqe_type fqe_type; 59 | typedef typename chained_on_curve_type::pairing::fqk_type fqk_type; 60 | 61 | typedef typename curve_type::pairing::fp_type field_type; 62 | 63 | public: 64 | typedef element_fp3 Fqe_variable_type; 65 | typedef element_fp3_mul Fqe_mul_component_type; 66 | typedef element_fp3_mul_by_lc Fqe_mul_by_lc_component_type; 67 | typedef element_fp3_squared Fqe_sqr_component_type; 68 | 69 | typedef element_fp6_2over3 Fqk_variable_type; 70 | typedef element_fp6_2over3_mul Fqk_mul_component_type; 71 | typedef element_fp6_2over3_mul_by_2345 Fqk_special_mul_component_type; 72 | typedef element_fp6_2over3_squared Fqk_sqr_component_type; 73 | }; 74 | } // namespace components 75 | } // namespace blueprint 76 | } // namespace crypto3 77 | } // namespace nil 78 | 79 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_MNT4_BASIC_CURVE_COMPONENT_POLICY_HPP 80 | -------------------------------------------------------------------------------- /include/nil/blueprint/components/algebra/curves/detail/r1cs/mnt6.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | // @file Declaration of specializations of basic_curve_component_policy to 26 | // - basic_curve_component_policy. 27 | // 28 | // See pairing_params.hpp . 29 | //---------------------------------------------------------------------------// 30 | 31 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_MNT6_BASIC_CURVE_COMPONENT_POLICY_HPP 32 | #define CRYPTO3_BLUEPRINT_COMPONENTS_MNT6_BASIC_CURVE_COMPONENT_POLICY_HPP 33 | 34 | #include 35 | 36 | #include 37 | #include 38 | 39 | namespace nil { 40 | namespace crypto3 { 41 | namespace blueprint { 42 | namespace components { 43 | 44 | using namespace nil::crypto3::algebra; 45 | 46 | template 47 | class basic_curve_component_policy; 48 | 49 | /** 50 | * Specialization for MNT6. 51 | */ 52 | template 53 | class basic_curve_component_policy> { 54 | using curve_type = typename curves::mnt6; 55 | 56 | typedef typename curve_type::chained_on_curve_type chained_on_curve_type; // mnt4 57 | 58 | typedef typename chained_on_curve_type::pairing::fqe_type fqe_type; 59 | typedef typename chained_on_curve_type::pairing::fqk_type fqk_type; 60 | 61 | typedef typename curve_type::pairing::fp_type field_type; 62 | 63 | public: 64 | typedef element_fp2 Fqe_variable_type; 65 | typedef element_fp2_mul Fqe_mul_component_type; 66 | typedef element_fp2_mul_by_lc Fqe_mul_by_lc_component_type; 67 | typedef element_fp2_squared Fqe_sqr_component_type; 68 | 69 | typedef element_fp4 Fqk_variable_type; 70 | typedef element_fp4_mul Fqk_mul_component_type; 71 | typedef element_fp4_mul Fqk_special_mul_component_type; 72 | typedef element_fp4_squared Fqk_sqr_component_type; 73 | }; 74 | } // namespace components 75 | } // namespace blueprint 76 | } // namespace crypto3 77 | } // namespace nil 78 | 79 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_MNT6_BASIC_CURVE_COMPONENT_POLICY_HPP 80 | -------------------------------------------------------------------------------- /include/nil/blueprint/components/algebra/curves/pasta/plonk/tripling.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2021 Mikhail Komarov 3 | // Copyright (c) 2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | // @file Declaration of interfaces for auxiliary components for the SHA256 component. 26 | //---------------------------------------------------------------------------// 27 | 28 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_CURVE_ELEMENT_TRIPLING_COMPONENT_HPP 29 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_CURVE_ELEMENT_TRIPLING_COMPONENT_HPP 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | namespace nil { 37 | namespace crypto3 { 38 | namespace blueprint { 39 | namespace components { 40 | 41 | template 42 | class element_g1_tripling; 43 | 44 | template 46 | class element_g1_tripling, CurveType, W0, W1, W2, W3, 47 | W4, W5, W6, W7> : public component { 48 | 49 | typedef snark::plonk_constraint_system arithmetization_type; 50 | typedef blueprint blueprint_type; 51 | 52 | element_g1_doubling_plonk doubling_component; 53 | element_g1_addition_plonk 54 | addition_component; 55 | 56 | public: 57 | element_g1_tripling(blueprint_type &bp) : 58 | component(bp), doubling_component(bp), addition_component(bp) { 59 | } 60 | 61 | void generate_gates() { 62 | doubling_component.generate_gates(); 63 | addition_component.generate_gates(); 64 | } 65 | 66 | void generate_assignments(typename CurveType::value_type &P1) { 67 | generate_assignments(P1, P1.doubled() + P1); 68 | } 69 | 70 | void generate_assignments(typename CurveType::value_type &P1, typename CurveType::value_type &P2) { 71 | doubling_component.generate_assignments(P1, P1.doubled()); 72 | addition_component.generate_assignments(P1.doubled(), P1, P2); 73 | } 74 | }; 75 | 76 | } // namespace components 77 | } // namespace blueprint 78 | } // namespace crypto3 79 | } // namespace nil 80 | 81 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_CURVE_ELEMENT_TRIPLING_COMPONENT_HPP 82 | -------------------------------------------------------------------------------- /include/nil/blueprint/components/algebra/curves/pasta/plonk/types.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2022 Alisa Cherniaeva 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_COMPONENTS_PLONK_TYPES_HPP 26 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_TYPES_HPP 27 | 28 | #include 29 | 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | namespace nil { 36 | namespace crypto3 { 37 | namespace blueprint { 38 | namespace components { 39 | 40 | template 41 | struct var_ec_point { 42 | using var = snark::plonk_variable; 43 | var X; 44 | var Y; 45 | }; 46 | } // namespace components 47 | } // namespace blueprint 48 | } // namespace crypto3 49 | } // namespace nil 50 | 51 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_TYPES_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/components/algebra/fields/plonk/non_native/comparison_mode.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2023 Dmitrii Tabalin 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_COMPONENTS_PLONK_NON_NATIVE_COMPARISON_MODE_HPP 26 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_NON_NATIVE_COMPARISON_MODE_HPP 27 | 28 | namespace nil { 29 | namespace blueprint { 30 | namespace components { 31 | // Ordering is important here: the logic differs for FLAG and non-FLAG modes. 32 | // We use comparison with comparison_mode::FLAG in order to distinguish between the two cases. 33 | enum comparison_mode { 34 | FLAG, 35 | LESS_THAN, 36 | LESS_EQUAL, 37 | GREATER_THAN, 38 | GREATER_EQUAL, 39 | }; 40 | } // namespace components 41 | } // namespace blueprint 42 | } // namespace nil 43 | 44 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_NON_NATIVE_DETAIL_COMPARISON_MODE_HPP 45 | -------------------------------------------------------------------------------- /include/nil/blueprint/components/algebra/fields/r1cs/element_fp.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | // @file Declaration of interfaces for Fp2 components. 26 | // 27 | // The components verify field arithmetic in Fp2 = Fp[U]/(U^2-non_residue), 28 | // where non_residue is in Fp. 29 | //---------------------------------------------------------------------------// 30 | 31 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_FP_COMPONENTS_HPP 32 | #define CRYPTO3_BLUEPRINT_COMPONENTS_FP_COMPONENTS_HPP 33 | 34 | #include 35 | 36 | namespace nil { 37 | namespace crypto3 { 38 | namespace blueprint { 39 | namespace components { 40 | 41 | /******************************** element_fp ************************************/ 42 | 43 | /** 44 | * Component that represents an element_fp. 45 | */ 46 | template 47 | using element_fp = detail::blueprint_linear_combination; 48 | } // namespace components 49 | } // namespace blueprint 50 | } // namespace crypto3 51 | } // namespace nil 52 | 53 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_FP_COMPONENTS_HPP 54 | -------------------------------------------------------------------------------- /include/nil/blueprint/components/detail/r1cs/lookup_1bit.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2020-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // Copyright (c) 2021 Ilias Khairullin 5 | // 6 | // MIT License 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in all 16 | // copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | // SOFTWARE. 25 | //---------------------------------------------------------------------------// 26 | 27 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_DETAIL_LOOKUP_1BIT_HPP 28 | #define CRYPTO3_BLUEPRINT_COMPONENTS_DETAIL_LOOKUP_1BIT_HPP 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | namespace nil { 35 | namespace crypto3 { 36 | namespace blueprint { 37 | namespace components { 38 | /** 39 | * One-bit window lookup table using one constraint 40 | */ 41 | template 42 | struct lookup_1bit : public component { 43 | using field_type = Field; 44 | using field_value_type = typename Field::value_type; 45 | using result_type = detail::blueprint_variable; 46 | 47 | const std::vector constants; 48 | const detail::blueprint_variable bit; 49 | result_type result; 50 | 51 | /// Auto allocation of the result 52 | template 53 | lookup_1bit(blueprint &bp, 54 | const Constants &in_constants, 55 | const detail::blueprint_variable &in_bit) : 56 | component(bp), 57 | constants(std::cbegin(in_constants), std::cend(in_constants)), bit(in_bit) { 58 | assert(this->constants.size() == 2); 59 | this->result.allocate(this->bp); 60 | } 61 | 62 | /// Manual allocation of the result 63 | template 64 | lookup_1bit(blueprint &bp, 65 | const Constants &in_constants, 66 | const detail::blueprint_variable &in_bit, 67 | const result_type &in_result) : 68 | component(bp), 69 | constants(std::cbegin(in_constants), std::cend(in_constants)), bit(in_bit), result(in_result) { 70 | assert(this->constants.size() == 2); 71 | } 72 | 73 | void generate_gates() { 74 | this->bp.add_r1cs_constraint(snark::r1cs_constraint( 75 | {constants[0] + bit * constants[1] - (bit * constants[0])}, 76 | {field_value_type::one()}, 77 | result)); 78 | } 79 | 80 | void generate_assignments() { 81 | std::size_t i = static_cast( 82 | static_cast((this->bp.val(bit)).data)); 83 | this->bp.val(result) = constants[i]; 84 | } 85 | }; 86 | } // namespace components 87 | } // namespace blueprint 88 | } // namespace crypto3 89 | } // namespace nil 90 | 91 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_DETAIL_LOOKUP_1BIT_HPP 92 | -------------------------------------------------------------------------------- /include/nil/blueprint/components/merkle_tree/r1cs/authentication_path.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | // @file Test program that exercises the SEppzkSNARK (first generator, then 26 | // prover, then verifier) on a synthetic R1CS instance. 27 | //---------------------------------------------------------------------------// 28 | 29 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_MERKLE_AUTHENTICATION_PATH_VARIABLE_HPP 30 | #define CRYPTO3_BLUEPRINT_COMPONENTS_MERKLE_AUTHENTICATION_PATH_VARIABLE_HPP 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | namespace nil { 37 | namespace blueprint { 38 | namespace components { 39 | 40 | template 41 | struct merkle_authentication_path_variable : public component { 42 | 43 | const std::size_t tree_depth; 44 | std::vector> left_digests; 45 | std::vector> right_digests; 46 | 47 | merkle_authentication_path_variable(blueprint &bp, const std::size_t tree_depth) : 48 | component(bp), tree_depth(tree_depth) { 49 | for (std::size_t i = 0; i < tree_depth; ++i) { 50 | left_digests.emplace_back(digest_variable(bp, Hash::get_digest_len())); 51 | right_digests.emplace_back(digest_variable(bp, Hash::get_digest_len())); 52 | } 53 | } 54 | 55 | void generate_gates() { 56 | for (std::size_t i = 0; i < tree_depth; ++i) { 57 | left_digests[i].generate_gates(); 58 | right_digests[i].generate_gates(); 59 | } 60 | } 61 | 62 | void generate_assignments(const std::size_t address, 63 | const snark::merkle_authentication_path &path) { 64 | assert(path.size() == tree_depth); 65 | 66 | for (std::size_t i = 0; i < tree_depth; ++i) { 67 | if (address & (1ul << (tree_depth - 1 - i))) { 68 | left_digests[i].generate_assignments(path[i]); 69 | } else { 70 | right_digests[i].generate_assignments(path[i]); 71 | } 72 | } 73 | } 74 | 75 | snark::merkle_authentication_path get_authentication_path(const std::size_t address) const { 76 | snark::merkle_authentication_path result; 77 | for (std::size_t i = 0; i < tree_depth; ++i) { 78 | if (address & (1ul << (tree_depth - 1 - i))) { 79 | result.emplace_back(left_digests[i].get_digest()); 80 | } else { 81 | result.emplace_back(right_digests[i].get_digest()); 82 | } 83 | } 84 | 85 | return result; 86 | } 87 | }; 88 | } // namespace components 89 | } // namespace blueprint 90 | } // namespace nil 91 | 92 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_MERKLE_AUTHENTICATION_PATH_VARIABLE_HPP 93 | -------------------------------------------------------------------------------- /include/nil/blueprint/components/systems/snark/plonk/kimchi/detail/binding.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2021 Mikhail Komarov 3 | // Copyright (c) 2021 Nikita Kaskov 4 | // Copyright (c) 2022 Ilia Shirobokov 5 | // 6 | // MIT License 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in all 16 | // copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | // SOFTWARE. 25 | //---------------------------------------------------------------------------// 26 | 27 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_BINDING_HPP 28 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_BINDING_HPP 29 | 30 | #include 31 | 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | namespace nil { 40 | namespace crypto3 { 41 | namespace blueprint { 42 | namespace components { 43 | 44 | template 45 | struct binding { 46 | using var = snark::plonk_variable; 47 | using commitment_parms_type = typename KimchiParamsType::commitment_params_type; 48 | using kimchi_constants = zk::components::kimchi_inner_constants; 49 | 50 | template 51 | struct fr_data { 52 | private: 53 | constexpr static const std::size_t f_comm_msm_size = kimchi_constants::f_comm_msm_size; 54 | constexpr static const std::size_t lookup_columns = KimchiParamsType::circuit_params::lookup_columns; 55 | 56 | public: 57 | std::array scalars; 58 | std::array, BatchSize> f_comm_scalars; 59 | std::array cip_shifted; 60 | 61 | std::array neg_pub; 62 | std::array zeta_to_srs_len; 63 | var zeta_to_domain_size_minus_1; 64 | 65 | std::array joint_combiner_powers_prepared; 66 | }; 67 | 68 | template 69 | struct fq_data { }; 70 | 71 | struct fq_sponge_output { 72 | var joint_combiner; 73 | var beta; // beta and gamma can be combined from limbs in the base circuit 74 | var gamma; 75 | var alpha; 76 | var zeta; 77 | var fq_digest; // TODO overflow check 78 | std::array challenges; 79 | var c; 80 | }; 81 | }; 82 | } // namespace components 83 | } // namespace blueprint 84 | } // namespace crypto3 85 | } // namespace nil 86 | 87 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_BINDING_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/components/systems/snark/plonk/kimchi/proof_system/circuit_description.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2022 Ilia Shirobokov 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_COMPONENTS_PLONK_PROOF_SYSTEM_CIRCUIT_DESCRIPTION_HPP 26 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_PROOF_SYSTEM_CIRCUIT_DESCRIPTION_HPP 27 | 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | namespace nil { 34 | namespace crypto3 { 35 | namespace blueprint { 36 | namespace components { 37 | template 38 | struct kimchi_circuit_description { 39 | using index_terms_list = IndexTermsList; 40 | 41 | static const std::size_t witness_columns = WitnessColumns; 42 | static const std::size_t permut_size = PermutSize; 43 | 44 | static const std::size_t alpha_powers_n = index_terms_list::alpha_powers_n; 45 | 46 | static const bool poseidon_gate = index_terms_list::poseidon_gate; 47 | static const bool ec_arithmetic_gates = index_terms_list::ec_arithmetic_gates; 48 | static const bool chacha_gate = index_terms_list::chacha_gate; 49 | static const bool generic_gate = index_terms_list::generic_gate; 50 | 51 | static const std::size_t poseidon_gates_count = index_terms_list::poseidon_gates_count; 52 | static const std::size_t ec_arithmetic_gates_count = index_terms_list::ec_arithmetic_gates_count; 53 | 54 | static const bool use_lookup = index_terms_list::lookup_columns > 0; 55 | static const bool joint_lookup = index_terms_list::joint_lookup; 56 | static const std::size_t lookup_columns = index_terms_list::lookup_columns; 57 | static const bool lookup_runtime = index_terms_list::lookup_runtime; 58 | }; 59 | } // namespace components 60 | } // namespace blueprint 61 | } // namespace crypto3 62 | } // namespace nil 63 | 64 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_PROOF_SYSTEM_CIRCUIT_DESCRIPTION_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/components/systems/snark/plonk/kimchi/proof_system/kimchi_commitment_params.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2021 Mikhail Komarov 3 | // Copyright (c) 2021 Nikita Kaskov 4 | // Copyright (c) 2022 Ilia Shirobokov 5 | // 6 | // MIT License 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in all 16 | // copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | // SOFTWARE. 25 | //---------------------------------------------------------------------------// 26 | 27 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_PROOF_SYSTEM_KIMCHI_COMMITMENT_PARAMS_HPP 28 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_PROOF_SYSTEM_KIMCHI_COMMITMENT_PARAMS_HPP 29 | 30 | #include 31 | 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | namespace nil { 38 | namespace crypto3 { 39 | namespace blueprint { 40 | namespace components { 41 | template 42 | struct kimchi_commitment_params_type { 43 | constexpr static std::size_t max_poly_size = MaxPolySize; 44 | constexpr static std::size_t eval_rounds = EvalRounds; 45 | constexpr static std::size_t split_poly_eval_size = max_poly_size == (1 << eval_rounds) ? 1 : 2; 46 | constexpr static std::size_t srs_len = SrsLen; 47 | 48 | // TODO we can set commitments size values from template but for now it looks like we can just fix 49 | // it 50 | constexpr static std::size_t shifted_commitment_split = 1; 51 | constexpr static std::size_t max_comm_size = 1; 52 | constexpr static std::size_t w_comm_size = 1; 53 | }; 54 | } // namespace components 55 | } // namespace blueprint 56 | } // namespace crypto3 57 | } // namespace nil 58 | 59 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_PROOF_SYSTEM_KIMCHI_COMMITMENT_PARAMS_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/components/systems/snark/plonk/kimchi/proof_system/kimchi_params.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2021 Mikhail Komarov 3 | // Copyright (c) 2021 Nikita Kaskov 4 | // Copyright (c) 2022 Ilia Shirobokov 5 | // 6 | // MIT License 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in all 16 | // copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | // SOFTWARE. 25 | //---------------------------------------------------------------------------// 26 | 27 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_PARAMS_HPP 28 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_PARAMS_HPP 29 | 30 | #include 31 | 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | namespace nil { 38 | namespace crypto3 { 39 | namespace blueprint { 40 | namespace components { 41 | template 43 | struct kimchi_params_type { 44 | using commitment_params_type = CommitmentParamsType; 45 | using curve_type = CurveType; 46 | using circuit_params = CircuitDescriptionType; 47 | 48 | constexpr static std::size_t alpha_powers_n = CircuitDescriptionType::alpha_powers_n; 49 | constexpr static std::size_t public_input_size = PublicInputSize; 50 | constexpr static std::size_t witness_columns = CircuitDescriptionType::witness_columns; 51 | constexpr static std::size_t permut_size = CircuitDescriptionType::permut_size; 52 | 53 | constexpr static bool use_lookup = CircuitDescriptionType::use_lookup; 54 | 55 | constexpr static std::size_t eval_points_amount = 2; 56 | constexpr static std::size_t scalar_challenge_size = 128; 57 | 58 | constexpr static std::size_t prev_challenges_size = PrevChalSize; 59 | 60 | constexpr static std::size_t lookup_comm_size = 0; // TODO: 61 | constexpr static std::size_t index_term_size() { 62 | return circuit_params::index_terms_list::size; 63 | } 64 | 65 | constexpr static std::size_t witness_commitment_size = 1; 66 | constexpr static std::size_t z_commitment_size = 1; 67 | constexpr static std::size_t t_commitment_size = 1; 68 | constexpr static std::size_t lookup_runtime_commitment_size = 1; 69 | constexpr static std::size_t lookup_sorted_commitment_size = 1; 70 | constexpr static std::size_t lookup_aggregated_commitment_size = 1; 71 | }; 72 | } // namespace components 73 | } // namespace blueprint 74 | } // namespace crypto3 75 | } // namespace nil 76 | 77 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_PARAMS_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/components/systems/snark/plonk/kimchi/types/alpha_argument_type.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2022 Ilia Shirobokov 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_COMPONENTS_PLONK_KIMCHI_TYPES_ALPHA_ARGUMENT_TYPE_HPP 26 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_TYPES_ALPHA_ARGUMENT_TYPE_HPP 27 | 28 | #include 29 | #include 30 | 31 | namespace nil { 32 | namespace crypto3 { 33 | namespace blueprint { 34 | namespace components { 35 | enum argument_type { 36 | Permutation, 37 | Generic, 38 | Zero, 39 | Lookup, 40 | }; 41 | } // namespace components 42 | } // namespace blueprint 43 | } // namespace crypto3 44 | } // namespace nil 45 | 46 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_TYPES_ALPHA_ARGUMENT_TYPE_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/components/systems/snark/plonk/kimchi/types/binding.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2021 Mikhail Komarov 3 | // Copyright (c) 2021 Nikita Kaskov 4 | // Copyright (c) 2022 Ilia Shirobokov 5 | // 6 | // MIT License 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in all 16 | // copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | // SOFTWARE. 25 | //---------------------------------------------------------------------------// 26 | 27 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_BINDING_HPP 28 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_BINDING_HPP 29 | 30 | #include 31 | 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | namespace nil { 40 | namespace crypto3 { 41 | namespace blueprint { 42 | namespace components { 43 | 44 | template 45 | struct binding { 46 | using var = snark::plonk_variable; 47 | using commitment_parms_type = typename KimchiParamsType::commitment_params_type; 48 | using kimchi_constants = zk::components::kimchi_inner_constants; 49 | 50 | template 51 | struct fr_data { 52 | private: 53 | constexpr static const std::size_t f_comm_msm_size = kimchi_constants::f_comm_msm_size; 54 | constexpr static const std::size_t lookup_columns = KimchiParamsType::circuit_params::lookup_columns; 55 | 56 | public: 57 | std::array scalars; 58 | std::array, BatchSize> f_comm_scalars; 59 | std::array cip_shifted; 60 | 61 | std::array neg_pub; 62 | std::array zeta_to_srs_len; 63 | var zeta_to_domain_size_minus_1; 64 | 65 | std::array joint_combiner_powers_prepared; 66 | }; 67 | 68 | template 69 | struct fq_data { }; 70 | 71 | struct fq_sponge_output { 72 | var joint_combiner; 73 | var beta; // beta and gamma can be combined from limbs in the base circuit 74 | var gamma; 75 | var alpha; 76 | var zeta; 77 | var fq_digest; // TODO overflow check 78 | std::array challenges; 79 | var c; 80 | }; 81 | }; 82 | } // namespace components 83 | } // namespace blueprint 84 | } // namespace crypto3 85 | } // namespace nil 86 | 87 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_BINDING_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/components/systems/snark/plonk/kimchi/types/column_type.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2022 Ilia Shirobokov 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_COMPONENTS_PLONK_KIMCHI_DETAIL_CONSTRAINTS_COLUMN_TYPE_HPP 26 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_DETAIL_CONSTRAINTS_COLUMN_TYPE_HPP 27 | 28 | #include 29 | #include 30 | 31 | namespace nil { 32 | namespace crypto3 { 33 | namespace blueprint { 34 | namespace components { 35 | enum column_type { 36 | Witness, 37 | Coefficient, 38 | Z, 39 | LookupSorted, 40 | LookupAggreg, 41 | LookupKindIndex, // ChaCha = 0, ChaChaFinal = 1, LookupGate = 2, RangeCheckGate = 3 42 | LookupTable, 43 | LookupRuntimeSelector, 44 | LookupRuntimeTable, 45 | CompleteAdd, 46 | VarBaseMul, 47 | EndoMul, 48 | EndoMulScalar, 49 | Poseidon, 50 | ChaCha0, 51 | ChaCha1, 52 | ChaCha2, 53 | ChaChaFinal, 54 | RangeCheck0, 55 | RangeCheck1 56 | }; 57 | } // namespace components 58 | } // namespace blueprint 59 | } // namespace crypto3 60 | } // namespace nil 61 | 62 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_DETAIL_CONSTRAINTS_COLUMN_TYPE_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/components/systems/snark/plonk/kimchi/types/commitment.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2022 Alisa Cherniaeva 3 | // Copyright (c) 2022 Ilia Shirobokov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_DETAIL_COMMITMENT_HPP 27 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_DETAIL_COMMITMENT_HPP 28 | 29 | #include 30 | 31 | #include 32 | 33 | #include 34 | #include 35 | 36 | #include 37 | 38 | namespace nil { 39 | namespace crypto3 { 40 | namespace blueprint { 41 | namespace components { 42 | 43 | template 44 | struct kimchi_commitment_type { 45 | using var_ec_point = typename zk::components::var_ec_point; 46 | std::array parts; 47 | }; 48 | } // namespace components 49 | } // namespace blueprint 50 | } // namespace crypto3 51 | } // namespace nil 52 | 53 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_DETAIL_COMMITMENT_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/components/systems/snark/plonk/kimchi/types/evaluation_proof.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2022 Ilia Shirobokov 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_COMPONENTS_PLONK_KIMCHI_TYPES_EVALUATION_PROOF_HPP 26 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_TYPES_EVALUATION_PROOF_HPP 27 | 28 | #include 29 | 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | namespace nil { 36 | namespace crypto3 { 37 | namespace blueprint { 38 | namespace components { 39 | 40 | template 41 | struct kimchi_lookup_evaluations { 42 | using var = snark::plonk_variable; 43 | 44 | std::array sorted; 45 | 46 | var aggreg; 47 | var table; 48 | 49 | var runtime; 50 | 51 | kimchi_lookup_evaluations() { 52 | } 53 | }; 54 | 55 | template 56 | struct kimchi_proof_evaluations { 57 | using var = snark::plonk_variable; 58 | // witness polynomials 59 | std::array w; 60 | // permutation polynomial 61 | var z; 62 | // permutation polynomials 63 | // (PERMUTS-1 evaluations because the last permutation is only used in commitment form) 64 | std::array s; 65 | // /// lookup-related evaluations 66 | kimchi_lookup_evaluations lookup; 67 | // /// evaluation of the generic selector polynomial 68 | var generic_selector; 69 | // /// evaluation of the poseidon selector polynomial 70 | var poseidon_selector; 71 | 72 | kimchi_proof_evaluations() { 73 | } 74 | }; 75 | } // namespace components 76 | } // namespace blueprint 77 | } // namespace crypto3 78 | } // namespace nil 79 | 80 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_TYPES_EVALUATION_PROOF_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/components/systems/snark/plonk/kimchi/types/index_term_type.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2022 Ilia Shirobokov 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_COMPONENTS_PLONK_KIMCHI_DETAIL_CONSTRAINTS_INDEX_TERM_TYPE_HPP 26 | #define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_DETAIL_CONSTRAINTS_INDEX_TERM_TYPE_HPP 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | namespace nil { 34 | namespace crypto3 { 35 | namespace blueprint { 36 | namespace components { 37 | struct index_term_type { 38 | const column_type type; 39 | const std::size_t index; 40 | const char *str_repr; 41 | const std::size_t rows_amount; 42 | }; 43 | } // namespace components 44 | } // namespace blueprint 45 | } // namespace crypto3 46 | } // namespace nil 47 | 48 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_KIMCHI_DETAIL_CONSTRAINTS_INDEX_TERM_TYPE_HPP -------------------------------------------------------------------------------- /include/nil/blueprint/detail/huang_lu.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2023 Dmitrii Tabalin 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 | #include 26 | #include 27 | #include 28 | 29 | namespace nil { 30 | namespace blueprint { 31 | namespace components { 32 | namespace detail { 33 | // Implementing [https://arxiv.org/pdf/1907.04505.pdf] 34 | // 11/9 approximation ratio 35 | std::unordered_map huang_lu( 36 | std::list> &sizes, 37 | std::size_t agent_amount) { 38 | sizes.sort( 39 | [](const std::pair &a, 40 | const std::pair &b) { 41 | return a.second > b.second; 42 | } 43 | ); 44 | std::size_t max_item = 0, 45 | sum = 0; 46 | for (auto [key, item] : sizes) { 47 | max_item = std::max(max_item, item); 48 | sum += item; 49 | } 50 | std::size_t left = std::max((sum + agent_amount - 1) / agent_amount, 51 | max_item); 52 | std::size_t right = 2 * left; 53 | std::unordered_map best_assignment; 54 | std::unordered_map assignment; 55 | std::list> tasks_remaning; 56 | while (left < right) { 57 | std::size_t threshold = left + (right - left) / 2; 58 | tasks_remaning = sizes; 59 | 60 | for (std::size_t i = 0; i < agent_amount; i++) { 61 | std::size_t curr_bundle_size = 0; 62 | for (auto it = tasks_remaning.begin(); 63 | it != tasks_remaning.end();) { 64 | 65 | if (curr_bundle_size + it->second <= threshold) { 66 | assignment[it->first] = i; 67 | curr_bundle_size += it->second; 68 | it = tasks_remaning.erase(it); 69 | } else { 70 | it++; 71 | } 72 | } 73 | if (curr_bundle_size == 0) { 74 | break; 75 | } 76 | } 77 | if (tasks_remaning.size() == 0) { 78 | right = threshold; 79 | best_assignment = assignment; 80 | } else { 81 | left = threshold + 1; 82 | } 83 | 84 | if (threshold == left) { 85 | break; 86 | } 87 | } 88 | return best_assignment; 89 | } 90 | } // namespace detail 91 | } // namespace components 92 | } // namespace blueprint 93 | } // namespace nil -------------------------------------------------------------------------------- /include/nil/blueprint/utils/gate_mover.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2023 Dmitrii Tabalin 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 | #pragma once 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | namespace nil { 35 | namespace blueprint { 36 | template 37 | class gate_mover : public boost::static_visitor< 38 | nil::crypto3::math::expression>> { 39 | 40 | using var = nil::crypto3::zk::snark::plonk_variable; 41 | std::function var_mover; 42 | public: 43 | using expression = nil::crypto3::math::expression; 44 | using term_type = nil::crypto3::math::term; 45 | using pow_operation = nil::crypto3::math::pow_operation; 46 | using binary_arithmetic_operation = nil::crypto3::math::binary_arithmetic_operation; 47 | 48 | gate_mover(std::function var_mover_) : var_mover(var_mover_) {} 49 | 50 | expression visit(const expression& expr) { 51 | return boost::apply_visitor(*this, expr.get_expr()); 52 | } 53 | 54 | expression operator()(const term_type& term) { 55 | std::vector vars; 56 | auto coeff = term.get_coeff(); 57 | for (const auto& var: term.get_vars()) { 58 | vars.emplace_back(var_mover(var)); 59 | } 60 | term_type result(vars, coeff); 61 | return result; 62 | } 63 | 64 | expression operator()(const pow_operation& pow) { 65 | expression base = boost::apply_visitor( 66 | *this, pow.get_expr().get_expr()); 67 | return pow_operation(base, pow.get_power()); 68 | } 69 | 70 | expression operator()( 71 | const binary_arithmetic_operation& op) { 72 | expression left = 73 | boost::apply_visitor(*this, op.get_expr_left().get_expr()); 74 | expression right = 75 | boost::apply_visitor(*this, op.get_expr_right().get_expr()); 76 | switch (op.get_op()) { 77 | case nil::crypto3::math::ArithmeticOperator::ADD: 78 | return left + right; 79 | case nil::crypto3::math::ArithmeticOperator::SUB: 80 | return left - right; 81 | case nil::crypto3::math::ArithmeticOperator::MULT: 82 | return left * right; 83 | default: 84 | __builtin_unreachable(); 85 | } 86 | } 87 | }; 88 | } // namespace blueprint 89 | } // namespace nil -------------------------------------------------------------------------------- /include/nil/blueprint/zkevm/stack.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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 | #pragma once 26 | 27 | #include 28 | 29 | #include 30 | 31 | namespace nil { 32 | namespace blueprint { 33 | 34 | class zkevm_stack { 35 | public: 36 | using word_type = zkevm_word_type; 37 | 38 | void push(const word_type& word) { 39 | stack.push(word); 40 | } 41 | 42 | word_type pop() { 43 | word_type word = stack.top(); 44 | stack.pop(); 45 | return word; 46 | } 47 | 48 | word_type top() { 49 | return stack.top(); 50 | } 51 | 52 | void swap() { 53 | word_type a = pop(); 54 | word_type b = pop(); 55 | push(a); 56 | push(b); 57 | } 58 | 59 | void dup() { 60 | word_type a = pop(); 61 | push(a); 62 | push(a); 63 | } 64 | private: 65 | std::stack stack; 66 | }; 67 | } // namespace blueprint 68 | } // namespace nil 69 | -------------------------------------------------------------------------------- /include/nil/blueprint/zkevm/zkevm_machine_interface.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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 | #pragma once 26 | 27 | #include 28 | #include 29 | 30 | namespace nil { 31 | namespace blueprint { 32 | // at the time I am writing this there is no interface to the zkevm machine 33 | // this is a placeholder 34 | class zkevm_machine_interface { 35 | public: 36 | using word_type = zkevm_word_type; 37 | zkevm_stack stack; 38 | }; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /include/nil/blueprint/zkevm/zkevm_operation.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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 | #pragma once 26 | 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | namespace nil { 40 | namespace blueprint { 41 | 42 | template 43 | class zkevm_circuit; 44 | 45 | // interface class for generic zkevm operation 46 | template 47 | class zkevm_operation { 48 | public: 49 | enum class gate_class { 50 | // gate on if the operation is first in the circuit 51 | FIRST_OP, 52 | // gate always on if the operation is executed 53 | MIDDLE_OP, 54 | // gate on if the operation is last in the circuit 55 | LAST_OP, 56 | // gate on if the operation is not last in the circuit 57 | NOT_LAST_OP 58 | }; 59 | 60 | using zkevm_circuit_type = zkevm_circuit; 61 | using constraint_type = nil::crypto3::zk::snark::plonk_constraint; 62 | using assignment_type = assignment>; 63 | using var = nil::crypto3::zk::snark::plonk_variable; 64 | 65 | zkevm_operation() {} 66 | 67 | virtual ~zkevm_operation() = default; 68 | // note that some parts of the map may be empty 69 | // we expect that most of the operations would only use MIDDLE_OP 70 | virtual std::map> generate_gates(zkevm_circuit_type &zkevm_circuit) = 0; 71 | 72 | virtual void generate_assignments(zkevm_circuit_type &zkevm_circuit, zkevm_machine_interface &machine); 73 | // should return the same rows amount for everyс operation right now 74 | // here in case we would make it dynamic in the future 75 | virtual std::size_t rows_amount() = 0; 76 | // utility funciton 77 | static var var_gen(const std::vector &witness_cols, std::size_t i, int32_t offset = 0) { 78 | return var(witness_cols[i], offset, true, var::column_type::witness); 79 | }; 80 | }; 81 | } // namespace blueprint 82 | } // namespace nil 83 | -------------------------------------------------------------------------------- /include/nil/blueprint/zkevm/zkevm_word.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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 | #pragma once 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | namespace nil { 33 | namespace blueprint { 34 | 35 | constexpr static const 36 | boost::multiprecision::number< 37 | boost::multiprecision::backends::cpp_int_modular_backend<257>> zkevm_modulus = 38 | 0x10000000000000000000000000000000000000000000000000000000000000000_cppui_modular257; 39 | 40 | constexpr static const boost::multiprecision::backends::modular_params< 41 | boost::multiprecision::backends::cpp_int_modular_backend<257>> 42 | zkevm_modular_params = zkevm_modulus.backend(); 43 | 44 | typedef boost::multiprecision::number< 45 | boost::multiprecision::backends::modular_adaptor< 46 | boost::multiprecision::backends::cpp_int_modular_backend<257>, 47 | boost::multiprecision::backends::modular_params_ct< 48 | boost::multiprecision::backends::cpp_int_modular_backend<257>, 49 | zkevm_modular_params>>> 50 | zkevm_word_type; 51 | 52 | template 53 | constexpr zkevm_word_type zwordc(const T &value) { 54 | return zkevm_word_type::backend_type(value.backend()); 55 | } 56 | 57 | template 58 | std::vector zkevm_word_to_field_element(const zkevm_word_type &word) { 59 | using value_type = typename BlueprintFieldType::value_type; 60 | std::vector chunks; 61 | constexpr const std::size_t chunk_size = 16; 62 | constexpr const std::size_t num_chunks = 256 / chunk_size; 63 | using integral_type = boost::multiprecision::number< 64 | boost::multiprecision::backends::cpp_int_modular_backend<257>>; 65 | constexpr const integral_type mask = 66 | integral_type((zkevm_word_type(1) << chunk_size) - 1); 67 | integral_type word_copy = integral_type(word); 68 | for (std::size_t i = 0; i < num_chunks; ++i) { 69 | chunks.push_back(word_copy & mask); 70 | word_copy >>= chunk_size; 71 | } 72 | return chunks; 73 | } 74 | 75 | template 76 | std::vector chunk_64_to_16( 77 | const typename BlueprintFieldType::value_type &value 78 | ) { 79 | using value_type = typename BlueprintFieldType::value_type; 80 | using integral_type = typename BlueprintFieldType::integral_type; 81 | std::vector chunks; 82 | constexpr const std::size_t chunk_size = 16; 83 | constexpr const std::size_t num_chunks = 4; 84 | constexpr const integral_type mask = (integral_type(1) << chunk_size) - 1; 85 | integral_type value_copy = integral_type(value.data); 86 | for (std::size_t i = 0; i < num_chunks; ++i) { 87 | chunks.push_back(static_cast(value_copy & mask)); 88 | value_copy >>= chunk_size; 89 | } 90 | return chunks; 91 | } 92 | } // namespace blueprint 93 | } // namespace nil 94 | -------------------------------------------------------------------------------- /include/nil/detail/static_pow.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2021 Mikhail Komarov 3 | // Copyright (c) 2021 Ilias Khairullin 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef CRYPTO3_DETAIL_STATIC_POW_HPP 26 | #define CRYPTO3_DETAIL_STATIC_POW_HPP 27 | 28 | namespace nil { 29 | namespace crypto3 { 30 | namespace detail { 31 | template 32 | constexpr T pow(T x, U n) { 33 | T result = 1; 34 | while (n > 0) { 35 | if (n % 2 == 0) { 36 | // n is even 37 | x = x * x; 38 | n = n / 2; 39 | } else { 40 | // n isn't even 41 | result = result * x; 42 | n = n - 1; 43 | } 44 | } 45 | return result; 46 | } 47 | } // namespace detail 48 | } // namespace crypto3 49 | } // namespace nil 50 | 51 | #endif // #ifndef CRYPTO3_DETAIL_STATIC_POW_HPP 52 | -------------------------------------------------------------------------------- /run_tests.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | echo "building ${TEST_LIST[*]}" 5 | ninja -k 0 -j $NIX_BUILD_CORES 6 | 7 | echo "finish" 8 | -------------------------------------------------------------------------------- /test/algebra/fields/plonk/non_native/chop_and_glue_non_native.hpp: -------------------------------------------------------------------------------- 1 | template 2 | std::array chop_non_native(typename NonNativeFieldType::value_type input) { 3 | typename NonNativeFieldType::integral_type input_integral = typename NonNativeFieldType::integral_type(input.data); 4 | 5 | std::array output; 6 | 7 | typename NonNativeFieldType::integral_type base = 1; 8 | typename NonNativeFieldType::integral_type mask = (base << 66) - 1; 9 | 10 | output[0] = input_integral & mask; 11 | output[1] = (input_integral >> 66) & mask; 12 | output[2] = (input_integral >> 132) & mask; 13 | output[3] = (input_integral >> 198) & mask; 14 | 15 | return output; 16 | } 17 | 18 | template 19 | typename NonNativeFieldType::value_type glue_non_native(std::array input) { 20 | typename NonNativeFieldType::integral_type base = 1; 21 | typename NonNativeFieldType::integral_type chunk_size = (base << 66); 22 | 23 | std::array input_integral; 24 | 25 | for (std::size_t i = 0; i < input.size(); i++) { 26 | assert(input[i] < chunk_size); 27 | input_integral[i] = typename FieldType::integral_type(input[i].data); 28 | } 29 | 30 | typename NonNativeFieldType::integral_type output_integral = 31 | input_integral[0] + (input_integral[1] << 66) + (input_integral[2] << 132) + (input_integral[3] << 198); 32 | 33 | typename NonNativeFieldType::value_type output = typename NonNativeFieldType::value_type(output_integral); 34 | 35 | return output; 36 | } 37 | 38 | template 39 | std::vector create_public_input(std::array a, 40 | std::array b) { 41 | std::vector public_input; 42 | for (std::size_t i = 0; i < a.size(); i++) { 43 | public_input.push_back(a[i]); 44 | } 45 | for (std::size_t i = 0; i < b.size(); i++) { 46 | public_input.push_back(b[i]); 47 | } 48 | return public_input; 49 | } 50 | 51 | template 52 | std::vector 53 | create_public_input_1_value(std::array b) { 54 | std::vector public_input; 55 | for (std::size_t i = 0; i < b.size(); i++) { 56 | public_input.push_back(b[i]); 57 | } 58 | return public_input; 59 | } -------------------------------------------------------------------------------- /test/algebra/fields/r1cs/arithmetic.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_ELEMENT_FP2_COMPONENT_TEST_HPP 27 | #define CRYPTO3_BLUEPRINT_COMPONENTS_ELEMENT_FP2_COMPONENT_TEST_HPP 28 | 29 | #include 30 | 31 | using namespace nil::crypto3::zk; 32 | 33 | template class Fpk_variableT, 34 | template class Fpk_mul_componentT> 35 | blueprint test_field_element_mul(typename FieldType::value_type a_value, 36 | typename FieldType::value_type b_value){ 37 | using field_type = FieldType; 38 | using element_component = Fpk_variableT; 39 | using element_mul_component = Fpk_mul_componentT; 40 | using base_field_type = typename field_type::base_field_type; 41 | 42 | blueprint bp; 43 | 44 | element_component A(bp, a_value); 45 | element_component B(bp, b_value); 46 | element_component result(bp); 47 | 48 | element_mul_component el_mul_instance(bp, A, B, result); 49 | el_mul_instance.generate_gates(); 50 | el_mul_instance.generate_assignments(); 51 | 52 | const typename field_type::value_type res = result.get_element(); 53 | 54 | BOOST_CHECK(bp.is_satisfied()); 55 | BOOST_CHECK(res == (a_value * b_value)); 56 | 57 | return bp; 58 | } 59 | 60 | template class Fpk_variableT, 61 | template class Fpk_squared_componentT> 62 | blueprint test_field_element_squared(typename FieldType::value_type a_value){ 63 | using field_type = FieldType; 64 | using element_component = Fpk_variableT; 65 | using element_squared_component = Fpk_squared_componentT; 66 | using base_field_type = typename field_type::base_field_type; 67 | 68 | blueprint bp; 69 | 70 | element_component A(bp, a_value); 71 | element_component result(bp); 72 | 73 | element_squared_component el_squared_instance(bp, A, result); 74 | el_squared_instance.generate_gates(); 75 | el_squared_instance.generate_assignments(); 76 | 77 | const typename field_type::value_type res = result.get_element(); 78 | 79 | BOOST_CHECK(bp.is_satisfied()); 80 | BOOST_CHECK(res == (a_value.squared())); 81 | 82 | return bp; 83 | } 84 | 85 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_ELEMENT_FP2_COMPONENT_TEST_HPP 86 | -------------------------------------------------------------------------------- /test/algebra/fields/r1cs/fp2.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #define BOOST_TEST_MODULE element_fp2_test 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | #include "arithmetic.hpp" 40 | 41 | using namespace nil::crypto3; 42 | using namespace nil::crypto3::zk; 43 | using namespace nil::crypto3::algebra; 44 | 45 | BOOST_AUTO_TEST_SUITE(field_element_arithmetic_component_test_suite) 46 | 47 | BOOST_AUTO_TEST_CASE(field_element_mul_component_test_mnt4_case) { 48 | using curve_type = typename curves::mnt4<298>; 49 | using field_type = typename curve_type::template g2_type<>::field_type; 50 | using base_field_type = typename curve_type::base_field_type; 51 | 52 | std::size_t tries_quantity = 500; 53 | std::cout << "Starting element Fp2 mul component test for MNT4-298 " << tries_quantity << " times ..." << std::endl; 54 | auto begin = std::chrono::high_resolution_clock::now(); 55 | 56 | for (std::size_t i = 0; i < tries_quantity; i++) { 57 | typename field_type::value_type a_value = random_element(); 58 | typename field_type::value_type b_value = random_element(); 59 | 60 | blueprint bp = 61 | test_field_element_mul(a_value, b_value); 62 | 63 | BOOST_CHECK(bp.is_satisfied()); 64 | } 65 | auto end = std::chrono::high_resolution_clock::now(); 66 | auto elapsed = std::chrono::duration_cast(end - begin); 67 | std::cout << "Element Fp2 mul component test for MNT4-298 finished, average time: " 68 | << elapsed.count() * 1e-9 / tries_quantity << std::endl; 69 | } 70 | 71 | BOOST_AUTO_TEST_CASE(field_element_squared_component_test_mnt4_case) { 72 | using curve_type = typename curves::mnt4<298>; 73 | using field_type = typename curve_type::template g2_type<>::field_type; 74 | using base_field_type = typename curve_type::base_field_type; 75 | 76 | std::size_t tries_quantity = 500; 77 | std::cout << "Starting element Fp2 squared component test for MNT4-298 " << tries_quantity << " times ..." 78 | << std::endl; 79 | auto begin = std::chrono::high_resolution_clock::now(); 80 | 81 | for (std::size_t i = 0; i < tries_quantity; i++) { 82 | typename field_type::value_type a_value = random_element(); 83 | 84 | blueprint bp = 85 | test_field_element_squared(a_value); 86 | 87 | BOOST_CHECK(bp.is_satisfied()); 88 | } 89 | auto end = std::chrono::high_resolution_clock::now(); 90 | auto elapsed = std::chrono::duration_cast(end - begin); 91 | std::cout << "Element Fp2 squared component test for MNT4-298 finished, average time: " 92 | << elapsed.count() * 1e-9 / tries_quantity << std::endl; 93 | } 94 | 95 | BOOST_AUTO_TEST_SUITE_END() 96 | -------------------------------------------------------------------------------- /test/algebra/fields/r1cs/fp3.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #define BOOST_TEST_MODULE element_fp3_test 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | #include "arithmetic.hpp" 40 | 41 | using namespace nil::crypto3; 42 | using namespace nil::crypto3::zk; 43 | using namespace nil::crypto3::algebra; 44 | 45 | BOOST_AUTO_TEST_SUITE(field_element_arithmetic_component_test_suite) 46 | 47 | BOOST_AUTO_TEST_CASE(field_element_mul_component_test_mnt6_case) { 48 | using curve_type = typename curves::mnt6<298>; 49 | using field_type = typename curve_type::template g2_type<>::field_type; 50 | using base_field_type = typename curve_type::base_field_type; 51 | 52 | std::size_t tries_quantity = 500; 53 | std::cout << "Starting element Fp3 mul component test for MNT6-298 " << tries_quantity << " times ..." << std::endl; 54 | auto begin = std::chrono::high_resolution_clock::now(); 55 | 56 | for (std::size_t i = 0; i < tries_quantity; i++) { 57 | typename field_type::value_type a_value = random_element(); 58 | typename field_type::value_type b_value = random_element(); 59 | 60 | blueprint bp = 61 | test_field_element_mul(a_value, b_value); 62 | 63 | BOOST_CHECK(bp.is_satisfied()); 64 | } 65 | auto end = std::chrono::high_resolution_clock::now(); 66 | auto elapsed = std::chrono::duration_cast(end - begin); 67 | std::cout << "Element Fp3 mul component test for MNT6-298 finished, average time: " 68 | << elapsed.count() * 1e-9 / tries_quantity << std::endl; 69 | } 70 | 71 | BOOST_AUTO_TEST_CASE(field_element_squared_component_test_mnt6_case) { 72 | using curve_type = typename curves::mnt6<298>; 73 | using field_type = typename curve_type::template g2_type<>::field_type; 74 | using base_field_type = typename curve_type::base_field_type; 75 | 76 | std::size_t tries_quantity = 500; 77 | std::cout << "Starting element Fp3 squared component test for MNT6-298 " << tries_quantity << " times ..." 78 | << std::endl; 79 | auto begin = std::chrono::high_resolution_clock::now(); 80 | 81 | for (std::size_t i = 0; i < tries_quantity; i++) { 82 | typename field_type::value_type a_value = random_element(); 83 | 84 | blueprint bp = 85 | test_field_element_squared(a_value); 86 | 87 | BOOST_CHECK(bp.is_satisfied()); 88 | } 89 | auto end = std::chrono::high_resolution_clock::now(); 90 | auto elapsed = std::chrono::duration_cast(end - begin); 91 | std::cout << "Element Fp3 squared component test for MNT6-298 finished, average time: " 92 | << elapsed.count() * 1e-9 / tries_quantity << std::endl; 93 | } 94 | 95 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/algebra/fields/r1cs/fp4.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #define BOOST_TEST_MODULE element_fp2_test 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | #include "arithmetic.hpp" 40 | 41 | using namespace nil::crypto3; 42 | using namespace nil::crypto3::zk; 43 | using namespace nil::crypto3::algebra; 44 | 45 | BOOST_AUTO_TEST_SUITE(field_element_arithmetic_component_test_suite) 46 | 47 | BOOST_AUTO_TEST_CASE(field_element_mul_component_test_mnt4_case) { 48 | using curve_type = typename curves::mnt4<298>; 49 | using field_type = typename curve_type::gt_type; 50 | using base_field_type = typename curve_type::base_field_type; 51 | 52 | std::size_t tries_quantity = 500; 53 | std::cout << "Starting element Fp4 mul component test for MNT4-298 " << tries_quantity << " times ..." << std::endl; 54 | auto begin = std::chrono::high_resolution_clock::now(); 55 | 56 | for (std::size_t i = 0; i < tries_quantity; i++) { 57 | typename field_type::value_type a_value = random_element(); 58 | typename field_type::value_type b_value = random_element(); 59 | 60 | blueprint bp = 61 | test_field_element_mul(a_value, b_value); 62 | 63 | BOOST_CHECK(bp.is_satisfied()); 64 | } 65 | auto end = std::chrono::high_resolution_clock::now(); 66 | auto elapsed = std::chrono::duration_cast(end - begin); 67 | std::cout << "Element Fp4 mul component test for MNT4-298 finished, average time: " 68 | << elapsed.count() * 1e-9 / tries_quantity << std::endl; 69 | } 70 | 71 | BOOST_AUTO_TEST_CASE(field_element_squared_component_test_mnt4_case) { 72 | using curve_type = typename curves::mnt4<298>; 73 | using field_type = typename curve_type::gt_type; 74 | using base_field_type = typename curve_type::base_field_type; 75 | 76 | std::size_t tries_quantity = 500; 77 | std::cout << "Starting element Fp4 squared component test for MNT4-298 " << tries_quantity << " times ..." 78 | << std::endl; 79 | auto begin = std::chrono::high_resolution_clock::now(); 80 | 81 | for (std::size_t i = 0; i < tries_quantity; i++) { 82 | typename field_type::value_type a_value = random_element(); 83 | 84 | blueprint bp = 85 | test_field_element_squared(a_value); 86 | 87 | BOOST_CHECK(bp.is_satisfied()); 88 | } 89 | auto end = std::chrono::high_resolution_clock::now(); 90 | auto elapsed = std::chrono::duration_cast(end - begin); 91 | std::cout << "Element Fp4 squared component test for MNT4-298 finished, average time: " 92 | << elapsed.count() * 1e-9 / tries_quantity << std::endl; 93 | } 94 | 95 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/algebra/fields/r1cs/fp6_2over3.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #define BOOST_TEST_MODULE element_fp3_test 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | #include "arithmetic.hpp" 40 | 41 | using namespace nil::crypto3; 42 | using namespace nil::crypto3::zk; 43 | using namespace nil::crypto3::algebra; 44 | 45 | BOOST_AUTO_TEST_SUITE(field_element_arithmetic_component_test_suite) 46 | 47 | BOOST_AUTO_TEST_CASE(field_element_mul_component_test_mnt6_case) { 48 | using curve_type = typename curves::mnt6<298>; 49 | using field_type = typename curve_type::gt_type; 50 | using base_field_type = typename curve_type::base_field_type; 51 | 52 | std::size_t tries_quantity = 500; 53 | std::cout << "Starting element Fp6_2over3 mul component test for MNT6-298 " << tries_quantity << " times ..." << std::endl; 54 | auto begin = std::chrono::high_resolution_clock::now(); 55 | 56 | for (std::size_t i = 0; i < tries_quantity; i++){ 57 | typename field_type::value_type a_value = random_element(); 58 | typename field_type::value_type b_value = random_element(); 59 | 60 | blueprint bp = test_field_element_mul(a_value, b_value); 63 | 64 | BOOST_CHECK(bp.is_satisfied()); 65 | } 66 | auto end = std::chrono::high_resolution_clock::now(); 67 | auto elapsed = std::chrono::duration_cast(end - begin); 68 | std::cout << "Element Fp6_2over3 mul component test for MNT6-298 finished, average time: " << elapsed.count() * 1e-9 / tries_quantity << std::endl; 69 | } 70 | 71 | BOOST_AUTO_TEST_CASE(field_element_squared_component_test_mnt6_case) { 72 | using curve_type = typename curves::mnt6<298>; 73 | using field_type = typename curve_type::gt_type; 74 | using base_field_type = typename curve_type::base_field_type; 75 | 76 | std::size_t tries_quantity = 500; 77 | std::cout << "Starting element Fp6_2over3 squared component test for MNT6-298 " << tries_quantity << " times ..." << std::endl; 78 | auto begin = std::chrono::high_resolution_clock::now(); 79 | 80 | for (std::size_t i = 0; i < tries_quantity; i++){ 81 | typename field_type::value_type a_value = random_element(); 82 | 83 | blueprint bp = test_field_element_squared(a_value); 86 | 87 | BOOST_CHECK(bp.is_satisfied()); 88 | } 89 | auto end = std::chrono::high_resolution_clock::now(); 90 | auto elapsed = std::chrono::duration_cast(end - begin); 91 | std::cout << "Element Fp6_2over3 squared component test for MNT6-298 finished, average time: " << elapsed.count() * 1e-9 / tries_quantity << std::endl; 92 | } 93 | 94 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/algebra/pairing/weierstrass/r1cs/miller_loop.cpp: -------------------------------------------------------------------------------- 1 | /** @file 2 | ***************************************************************************** 3 | 4 | Implementation of interfaces for gadgets for Miller loops. 5 | 6 | See weierstrass_miller_loop.hpp . 7 | 8 | ***************************************************************************** 9 | * @author This file is part of libsnark, developed by SCIPR Lab 10 | * and contributors (see AUTHORS). 11 | * @copyright MIT license (see LICENSE file) 12 | *****************************************************************************/ 13 | 14 | #define BOOST_TEST_MODULE weierstrass_miller_loop_components_test 15 | 16 | #include 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "weierstrass_miller_loop.hpp" 32 | 33 | using namespace nil::crypto3::zk; 34 | using namespace nil::crypto3::algebra; 35 | 36 | BOOST_AUTO_TEST_SUITE(weierstrass_miller_loop_components_test_suite) 37 | 38 | BOOST_AUTO_TEST_CASE(weierstrass_miller_loop_mnt4_miller_loop_components_test) { 39 | test_mnt_miller_loop>(); 40 | } 41 | 42 | BOOST_AUTO_TEST_CASE(weierstrass_miller_loop_mnt6_miller_loop_components_test) { 43 | test_mnt_miller_loop>(); 44 | } 45 | 46 | BOOST_AUTO_TEST_CASE(weierstrass_miller_loop_mnt4_e_over_e_miller_loop_components_test) { 47 | test_mnt_e_over_e_miller_loop>(); 48 | } 49 | 50 | BOOST_AUTO_TEST_CASE(weierstrass_miller_loop_mnt6_e_over_e_miller_loop_components_test) { 51 | test_mnt_e_over_e_miller_loop>(); 52 | } 53 | 54 | BOOST_AUTO_TEST_CASE(weierstrass_miller_loop_mnt4_e_times_e_miller_loop_components_test) { 55 | test_mnt_e_times_e_over_e_miller_loop>(); 56 | } 57 | 58 | BOOST_AUTO_TEST_CASE(weierstrass_miller_loop_mnt6_e_times_e_miller_loop_components_test) { 59 | test_mnt_e_times_e_over_e_miller_loop>(); 60 | } 61 | 62 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/hashes/plonk/detail/sha_table_generators_base4.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2023 Dmitrii Tabalin 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 plonk_sha256_sha_table_generators_base4_test 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | #include 36 | 37 | using namespace nil; 38 | 39 | BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) 40 | 41 | BOOST_AUTO_TEST_CASE(blueprint_plonk_sha256_reverse_base4_generation) { 42 | using curve_type = crypto3::algebra::curves::pallas; 43 | using BlueprintFieldType = typename curve_type::base_field_type; 44 | using integral_type = typename BlueprintFieldType::integral_type; 45 | using value_type = typename BlueprintFieldType::value_type; 46 | std::cerr << "Starting sha256 reverse base4 generation" << std::endl; 47 | std::unordered_set, 49 | nil::blueprint::components::detail::SumHash> output_set; 50 | nil::blueprint::components::detail::generate_base4_reverse_table( 51 | output_set, 32); 52 | print_sha_table_to_stream(output_set, std::cout); 53 | } 54 | 55 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/hashes/plonk/detail/sha_table_generators_base7.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2023 Dmitrii Tabalin 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 plonk_sha256_sha_table_generators_base7_test 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | #include 36 | 37 | using namespace nil; 38 | 39 | BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) 40 | 41 | BOOST_AUTO_TEST_CASE(blueprint_plonk_sha256_reverse_base7_generation) { 42 | using curve_type = crypto3::algebra::curves::pallas; 43 | using BlueprintFieldType = typename curve_type::base_field_type; 44 | using integral_type = typename BlueprintFieldType::integral_type; 45 | using value_type = typename BlueprintFieldType::value_type; 46 | std::cerr << "Starting sha256 reverse base7 generation" << std::endl; 47 | std::unordered_set, 49 | nil::blueprint::components::detail::SumHash> output_set; 50 | nil::blueprint::components::detail::generate_base7_reverse_table(output_set, 32); 51 | print_sha_table_to_stream(output_set, std::cout); 52 | } 53 | 54 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/hashes/r1cs/knapsack.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #define BOOST_TEST_MODULE knapsack_component_test 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include "knapsack.hpp" 37 | 38 | using namespace nil::crypto3::algebra; 39 | using namespace nil::crypto3::zk; 40 | 41 | BOOST_AUTO_TEST_SUITE(knapsack_component_test_suite) 42 | 43 | BOOST_AUTO_TEST_CASE(knapsack_component_test_bls12_381_case) { 44 | std::cout << "Starting Knapsack component test for BLS12-381 ..." << std::endl; 45 | auto begin = std::chrono::high_resolution_clock::now(); 46 | test_knapsack_crh_with_bit_out_component::scalar_field_type>(); 47 | auto end = std::chrono::high_resolution_clock::now(); 48 | auto elapsed = std::chrono::duration_cast(end - begin); 49 | std::cout << "Knapsack component test for BLS12-381 finished, time: " << elapsed.count() * 1e-9 << std::endl; 50 | } 51 | 52 | BOOST_AUTO_TEST_CASE(knapsack_component_test_mnt4_case) { 53 | std::cout << "Starting Knapsack component test for MNT4-298 ..." << std::endl; 54 | auto begin = std::chrono::high_resolution_clock::now(); 55 | test_knapsack_crh_with_bit_out_component::scalar_field_type>(); 56 | auto end = std::chrono::high_resolution_clock::now(); 57 | auto elapsed = std::chrono::duration_cast(end - begin); 58 | std::cout << "Knapsack component test for MNT4-298 finished, time: " << elapsed.count() * 1e-9 << std::endl; 59 | } 60 | 61 | BOOST_AUTO_TEST_CASE(knapsack_component_test_mnt6_case) { 62 | std::cout << "Starting Knapsack component test for MNT6-298 ..." << std::endl; 63 | auto begin = std::chrono::high_resolution_clock::now(); 64 | test_knapsack_crh_with_bit_out_component::scalar_field_type>(); 65 | auto end = std::chrono::high_resolution_clock::now(); 66 | auto elapsed = std::chrono::duration_cast(end - begin); 67 | std::cout << "Knapsack component test for MNT6-298 finished, time: " << elapsed.count() * 1e-9 << std::endl; 68 | } 69 | 70 | BOOST_AUTO_TEST_CASE(knapsack_component_test_edwards_183_case) { 71 | std::cout << "Starting Knapsack component test for Edwards-183 ..." << std::endl; 72 | auto begin = std::chrono::high_resolution_clock::now(); 73 | test_knapsack_crh_with_bit_out_component::scalar_field_type>(); 74 | auto end = std::chrono::high_resolution_clock::now(); 75 | auto elapsed = std::chrono::duration_cast(end - begin); 76 | std::cout << "Knapsack component test for Edwards-183 finished, time: " << elapsed.count() * 1e-9 << std::endl; 77 | } 78 | 79 | BOOST_AUTO_TEST_SUITE_END() 80 | -------------------------------------------------------------------------------- /test/hashes/r1cs/sha256.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #define BOOST_TEST_MODULE sha256_component_test 27 | 28 | #include 29 | 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include "sha256.hpp" 38 | 39 | using namespace nil::crypto3::algebra; 40 | using namespace nil::crypto3; 41 | using namespace nil::crypto3::zk; 42 | 43 | BOOST_AUTO_TEST_SUITE(sha2_256_component_test_suite) 44 | BOOST_AUTO_TEST_CASE(sha256_component_test_bls12_381_case) { 45 | std::cout << "Starting SHA256 component test for BLS12-381 ..." << std::endl; 46 | auto begin = std::chrono::high_resolution_clock::now(); 47 | sha2_two_to_one_bp::scalar_field_type>(); 48 | auto end = std::chrono::high_resolution_clock::now(); 49 | auto elapsed = std::chrono::duration_cast(end - begin); 50 | std::cout << "SHA256 component test for BLS12-381 finished, time: " << elapsed.count() * 1e-9 << std::endl; 51 | } 52 | 53 | BOOST_AUTO_TEST_CASE(sha256_component_test_mnt4_case) { 54 | std::cout << "Starting SHA256 component test for MNT4-298 ..." << std::endl; 55 | auto begin = std::chrono::high_resolution_clock::now(); 56 | sha2_two_to_one_bp::scalar_field_type>(); 57 | auto end = std::chrono::high_resolution_clock::now(); 58 | auto elapsed = std::chrono::duration_cast(end - begin); 59 | std::cout << "SHA256 component test for MNT4-298 finished, time: " << elapsed.count() * 1e-9 << std::endl; 60 | } 61 | 62 | BOOST_AUTO_TEST_CASE(sha256_component_test_mnt6_case) { 63 | std::cout << "Starting SHA256 component test for MNT6-298 ..." << std::endl; 64 | auto begin = std::chrono::high_resolution_clock::now(); 65 | sha2_two_to_one_bp::scalar_field_type>(); 66 | auto end = std::chrono::high_resolution_clock::now(); 67 | auto elapsed = std::chrono::duration_cast(end - begin); 68 | std::cout << "SHA256 component test for MNT6-298 finished, time: " << elapsed.count() * 1e-9 << std::endl; 69 | } 70 | 71 | BOOST_AUTO_TEST_CASE(sha256_component_test_edwards_183_case) { 72 | std::cout << "Starting SHA256 component test for Edwards-183 ..." << std::endl; 73 | auto begin = std::chrono::high_resolution_clock::now(); 74 | sha2_two_to_one_bp::scalar_field_type>(); 75 | auto end = std::chrono::high_resolution_clock::now(); 76 | auto elapsed = std::chrono::duration_cast(end - begin); 77 | std::cout << "SHA256 component test for Edwards-183 finished, time: " << elapsed.count() * 1e-9 << std::endl; 78 | } 79 | 80 | BOOST_AUTO_TEST_SUITE_END() 81 | -------------------------------------------------------------------------------- /test/routing/r1cs/as_waksman.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #define BOOST_TEST_MODULE as_waksman_components_test 27 | 28 | #include 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | 37 | using namespace nil::crypto3; 38 | using namespace nil::crypto3::zk; 39 | using namespace nil::crypto3::algebra; 40 | 41 | template 42 | void test_as_waksman_routing_component(const std::size_t num_packets, const std::size_t packet_size) { 43 | blueprint bp; 44 | integer_permutation permutation(num_packets); 45 | permutation.random_shuffle(); 46 | 47 | std::vector> randbits(num_packets), outbits(num_packets); 48 | for (std::size_t packet_idx = 0; packet_idx < num_packets; ++packet_idx) { 49 | randbits[packet_idx].allocate(bp, packet_size); 50 | outbits[packet_idx].allocate(bp, packet_size); 51 | 52 | for (std::size_t bit_idx = 0; bit_idx < packet_size; ++bit_idx) { 53 | bp.val(randbits[packet_idx][bit_idx]) = 54 | (rand() % 2) ? FieldType::value_type::zero() : FieldType::value_type::zero(); 55 | } 56 | } 57 | as_waksman_routing_component r(bp, num_packets, randbits, outbits); 58 | r.generate_gates(); 59 | 60 | r.generate_assignments(permutation); 61 | 62 | BOOST_CHECK(bp.is_satisfied()); 63 | for (std::size_t packet_idx = 0; packet_idx < num_packets; ++packet_idx) { 64 | for (std::size_t bit_idx = 0; bit_idx < packet_size; ++bit_idx) { 65 | BOOST_CHECK(bp.val(outbits[permutation.get(packet_idx)][bit_idx]) == bp.val(randbits[packet_idx][bit_idx])); 66 | } 67 | } 68 | 69 | bp.val(components::blueprint_variable(10)) = typename FieldType::value_type(12345); 70 | BOOST_CHECK(!bp.is_satisfied()); 71 | } 72 | 73 | BOOST_AUTO_TEST_SUITE(as_waksman_components_test_suite) 74 | 75 | BOOST_AUTO_TEST_CASE(as_waksman_components_test) { 76 | } 77 | 78 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/routing/r1cs/benes.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #define BOOST_TEST_MODULE benes_components_test 27 | 28 | #include 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | 37 | using namespace nil::crypto3; 38 | using namespace nil::crypto3::zk; 39 | using namespace nil::crypto3::algebra; 40 | 41 | template 42 | void test_benes_routing_component(const std::size_t num_packets, const std::size_t packet_size) { 43 | const std::size_t dimension = static_cast(std::ceil(std::log2(num_packets))); 44 | assert(num_packets == 1ul << dimension); 45 | 46 | blueprint bp; 47 | integer_permutation permutation(num_packets); 48 | permutation.random_shuffle(); 49 | 50 | std::vector> randbits(num_packets), outbits(num_packets); 51 | for (std::size_t packet_idx = 0; packet_idx < num_packets; ++packet_idx) { 52 | randbits[packet_idx].allocate(bp, packet_size); 53 | outbits[packet_idx].allocate(bp, packet_size); 54 | 55 | for (std::size_t bit_idx = 0; bit_idx < packet_size; ++bit_idx) { 56 | bp.val(randbits[packet_idx][bit_idx]) = 57 | (rand() % 2) ? FieldType::value_type::zero() : FieldType::value_type::zero(); 58 | } 59 | } 60 | 61 | benes_routing_component r(bp, num_packets, randbits, outbits, num_packets); 62 | r.generate_gates(); 63 | r.generate_assignments(permutation); 64 | 65 | assert(bp.is_satisfied()); 66 | for (std::size_t packet_idx = 0; packet_idx < num_packets; ++packet_idx) { 67 | for (std::size_t bit_idx = 0; bit_idx < packet_size; ++bit_idx) { 68 | assert(bp.val(outbits[permutation.get(packet_idx)][bit_idx]) == bp.val(randbits[packet_idx][bit_idx])); 69 | } 70 | } 71 | 72 | bp.val(blueprint_variable(10)) = typename FieldType::value_type(12345); 73 | assert(!bp.is_satisfied()); 74 | } 75 | 76 | BOOST_AUTO_TEST_SUITE(benes_components_test_suite) 77 | 78 | BOOST_AUTO_TEST_CASE(benes_components_test) { 79 | } 80 | 81 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/routing_algorithms/routing_algorithms.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | // @file Functions to test the algorithms that route on Benes and AS-Waksman networks. 26 | //---------------------------------------------------------------------------// 27 | 28 | #define BOOST_TEST_MODULE routing_algorithms_test 29 | 30 | #include 31 | 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | using namespace nil::crypto3::zk; 38 | 39 | /** 40 | * Test Benes network routing for all permutations on 2^static_cast(std::ceil(std::log2(N))) elements. 41 | */ 42 | void test_benes(const std::size_t N) { 43 | integer_permutation permutation(1ul << static_cast(std::ceil(std::log2(N)))); 44 | 45 | do { 46 | const benes_routing routing = get_benes_routing(permutation); 47 | assert(valid_benes_routing(permutation, routing)); 48 | } while (permutation.next_permutation()); 49 | } 50 | 51 | /** 52 | * Test AS-Waksman network routing for all permutations on N elements. 53 | */ 54 | void test_as_waksman(const std::size_t N) { 55 | integer_permutation permutation(N); 56 | 57 | do { 58 | const as_waksman_routing routing = get_as_waksman_routing(permutation); 59 | assert(valid_as_waksman_routing(permutation, routing)); 60 | } while (permutation.next_permutation()); 61 | } 62 | 63 | BOOST_AUTO_TEST_SUITE(routing_algorithms_test_suite) 64 | 65 | BOOST_AUTO_TEST_CASE(routing_algorithms_test) { 66 | std::size_t bn_size = 8; 67 | printf("* for all permutations on %zu elements\n", bn_size); 68 | test_benes(bn_size); 69 | 70 | std::size_t asw_max_size = 9; 71 | for (std::size_t i = 2; i <= asw_max_size; ++i) { 72 | test_as_waksman(i); 73 | } 74 | } 75 | 76 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/verifiers/kimchi/sponge/transcript_fr.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2022 Polina Chernyshova 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_auxiliary_transcript_test 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | #include 40 | #include 41 | #include <../test/verifiers/kimchi/sponge/aux_transcript_fr.hpp> 42 | 43 | #include "test_plonk_component.hpp" 44 | 45 | using namespace nil::crypto3; 46 | 47 | BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) 48 | 49 | BOOST_AUTO_TEST_CASE(blueprint_plonk_transcript_0) { 50 | auto start = std::chrono::high_resolution_clock::now(); 51 | 52 | using curve_type = algebra::curves::vesta; 53 | using BlueprintFieldType = typename curve_type::scalar_field_type; 54 | constexpr std::size_t WitnessColumns = 15; 55 | constexpr std::size_t PublicInputColumns = 1; 56 | constexpr std::size_t ConstantColumns = 1; 57 | constexpr std::size_t SelectorColumns = 16; 58 | using ArithmetizationParams = zk::snark::plonk_arithmetization_params; 60 | using ArithmetizationType = zk::snark::plonk_constraint_system; 62 | using AssignmentType = blueprint::assignment; 63 | 64 | constexpr size_t num_squeezes = 1; 65 | using component_type = zk::components::aux_fr; 67 | using hash_type = nil::crypto3::hashes::keccak_1600<256>; 68 | constexpr std::size_t Lambda = 40; 69 | 70 | using var = zk::snark::plonk_variable; 71 | 72 | std::vector input; 73 | var zero(0, 0, false, var::column_type::public_input); 74 | typename component_type::params_type params = {input, zero}; 75 | std::vector public_input = {0}; 76 | typename BlueprintFieldType::value_type result = 0x00000000000000000000000000000000C873AF205DFABB8A304600F3E09EEBA8_cppui_modular256; 77 | auto result_check = [&result](AssignmentType &assignment, 78 | component_type::result_type &real_res) { 79 | assert(result == assignment.var_value(real_res.squeezed)); 80 | }; 81 | test_component (params, public_input, result_check); 82 | 83 | auto duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - start); 84 | std::cout << "kimchi transcript_fr: " << duration.count() << "ms" << std::endl; 85 | } 86 | 87 | BOOST_AUTO_TEST_SUITE_END() 88 | -------------------------------------------------------------------------------- /test/verifiers/placeholder/data/merkle_tree_poseidon/circuit.crct: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NilFoundation/zkllvm-blueprint/faa6439e00bfcdb72fc4314f42f29240a716ae91/test/verifiers/placeholder/data/merkle_tree_poseidon/circuit.crct -------------------------------------------------------------------------------- /test/verifiers/placeholder/data/merkle_tree_poseidon/common.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NilFoundation/zkllvm-blueprint/faa6439e00bfcdb72fc4314f42f29240a716ae91/test/verifiers/placeholder/data/merkle_tree_poseidon/common.dat -------------------------------------------------------------------------------- /test/verify_r1cs_scheme.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2018-2021 Mikhail Komarov 3 | // Copyright (c) 2020-2021 Nikita Kaskov 4 | // 5 | // MIT License 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | //---------------------------------------------------------------------------// 25 | 26 | #ifndef CRYPTO3_BLUEPRINT_COMPONENTS_VERIFY_R1CS_SCHEME_COMPONENT_TEST_HPP 27 | #define CRYPTO3_BLUEPRINT_COMPONENTS_VERIFY_R1CS_SCHEME_COMPONENT_TEST_HPP 28 | 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | 37 | #include 38 | 39 | using namespace nil::crypto3; 40 | using namespace nil::crypto3::zk; 41 | using namespace nil::crypto3::algebra; 42 | 43 | template> 44 | bool verify_component(blueprint::blueprint bp) { 45 | 46 | if (bp.num_variables() == 0x00) { 47 | std::cout << "Empty blueprint!" << std::endl; 48 | return false; 49 | } 50 | 51 | using field_type = typename CurveType::scalar_field_type; 52 | using scheme_type = SchemeType; 53 | 54 | const snark::r1cs_constraint_system constraint_system = bp.get_constraint_system(); 55 | 56 | auto begin = std::chrono::high_resolution_clock::now(); 57 | const typename scheme_type::keypair_type keypair = generate(constraint_system); 58 | auto end = std::chrono::high_resolution_clock::now(); 59 | auto elapsed = std::chrono::duration_cast(end - begin); 60 | std::cout << "Key generation finished, time: " << elapsed.count() * 1e-9 << std::endl; 61 | 62 | begin = std::chrono::high_resolution_clock::now(); 63 | const typename scheme_type::proof_type proof = 64 | prove(keypair.first, bp.primary_input(), bp.auxiliary_input()); 65 | end = std::chrono::high_resolution_clock::now(); 66 | elapsed = std::chrono::duration_cast(end - begin); 67 | std::cout << "Proving finished, time: " << elapsed.count() * 1e-9 << std::endl; 68 | 69 | begin = std::chrono::high_resolution_clock::now(); 70 | bool verified = verify(keypair.second, bp.primary_input(), proof); 71 | end = std::chrono::high_resolution_clock::now(); 72 | elapsed = std::chrono::duration_cast(end - begin); 73 | 74 | std::cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << std::endl; 75 | std::cout << "Verification finished, time: " << elapsed.count() * 1e-9 << std::endl; 76 | std::cout << "Verification status: " << verified << std::endl; 77 | 78 | return verified; 79 | } 80 | 81 | template<> 82 | bool verify_component, snark::r1cs_gg_ppzksnark>>( 83 | blueprint::blueprint::scalar_field_type> bp) { 84 | std::cout << "Warning! r1cs_gg_ppzksnark for Edwards-183 is not implemented yet" << std::endl; 85 | 86 | return false; 87 | } 88 | 89 | #endif // CRYPTO3_BLUEPRINT_COMPONENTS_VERIFY_R1CS_SCHEME_COMPONENT_TEST_HPP 90 | -------------------------------------------------------------------------------- /test/zkevm/opcode_tester.hpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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 | #pragma once 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | namespace nil { 34 | namespace blueprint { 35 | zkevm_machine_interface get_empty_machine() { 36 | return zkevm_machine_interface(); 37 | } 38 | } // namespace blueprint 39 | } // namespace nil -------------------------------------------------------------------------------- /test/zkevm/opcodes/add_sub.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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 | #include "nil/crypto3/algebra/fields/pallas/base_field.hpp" 26 | #define BOOST_TEST_MODULE zkevm_add_test 27 | 28 | #include 29 | 30 | #include 31 | #include "nil/blueprint/zkevm/zkevm_word.hpp" 32 | 33 | #include 34 | #include 35 | 36 | #include 37 | #include "../opcode_tester.hpp" 38 | 39 | #include 40 | 41 | using namespace nil::blueprint; 42 | using namespace nil::crypto3::algebra; 43 | 44 | BOOST_AUTO_TEST_SUITE(zkevm_add_test_suite) 45 | 46 | BOOST_AUTO_TEST_CASE(zkevm_add_test) { 47 | using field_type = fields::pallas_base_field; 48 | using arithmentization_type = nil::crypto3::zk::snark::plonk_constraint_system; 49 | using assignment_type = assignment; 50 | using circuit_type = circuit; 51 | using zkevm_machine_type = zkevm_machine_interface; 52 | assignment_type assignment(0, 0, 0, 0); 53 | circuit_type circuit; 54 | zkevm_circuit zkevm_circuit(assignment, circuit); 55 | zkevm_machine_type machine = get_empty_machine(); 56 | // incorrect test logic, but we have no memory operations so 57 | machine.stack.push(zwordc(0x1234567890_cppui_modular257)); 58 | machine.stack.push(zwordc(0x1b70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 59 | zkevm_circuit.assign_opcode(zkevm_opcode::ADD, machine); 60 | zkevm_circuit.assign_opcode(zkevm_opcode::SUB, machine); 61 | machine.stack.push(zwordc(0xFb70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 62 | machine.stack.push(zwordc(0xFb70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 63 | zkevm_circuit.assign_opcode(zkevm_opcode::ADD, machine); 64 | zkevm_circuit.assign_opcode(zkevm_opcode::SUB, machine); 65 | machine.stack.push(zwordc(0x1b70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 66 | machine.stack.push(zwordc(0x1234567890_cppui_modular257)); 67 | zkevm_circuit.assign_opcode(zkevm_opcode::ADD, machine); 68 | zkevm_circuit.assign_opcode(zkevm_opcode::SUB, machine); 69 | zkevm_circuit.finalize_test(); 70 | // assignment.export_table(std::cout); 71 | // circuit.export_circuit(std::cout); 72 | nil::crypto3::zk::snark::basic_padding(assignment); 73 | BOOST_ASSERT(is_satisfied(circuit, assignment) == true); 74 | } 75 | 76 | BOOST_AUTO_TEST_SUITE_END() 77 | -------------------------------------------------------------------------------- /test/zkevm/opcodes/div.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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 zkevm_div_test 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | #include 36 | #include "../opcode_tester.hpp" 37 | 38 | #include 39 | 40 | using namespace nil::blueprint; 41 | using namespace nil::crypto3::algebra; 42 | 43 | BOOST_AUTO_TEST_SUITE(zkevm_mul_test_suite) 44 | 45 | BOOST_AUTO_TEST_CASE(zkevm_mul_test) { 46 | using field_type = curves::alt_bn128<254>::base_field_type; 47 | using arithmentization_type = nil::crypto3::zk::snark::plonk_constraint_system; 48 | using assignment_type = assignment; 49 | using circuit_type = circuit; 50 | using zkevm_machine_type = zkevm_machine_interface; 51 | assignment_type assignment(0, 0, 0, 0); 52 | circuit_type circuit; 53 | zkevm_circuit zkevm_circuit(assignment, circuit); 54 | zkevm_machine_type machine = get_empty_machine(); 55 | // incorrect test logic, but we have no memory operations so 56 | // check all overflows for chunks 57 | machine.stack.push(zwordc(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui_modular257)); 58 | machine.stack.push(zwordc(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui_modular257)); 59 | zkevm_circuit.assign_opcode(zkevm_opcode::DIV, machine); 60 | machine.stack.push(1234567890); 61 | machine.stack.push(zwordc(0x1b70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 62 | zkevm_circuit.assign_opcode(zkevm_opcode::DIV, machine); 63 | machine.stack.push(zwordc(0xFb70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 64 | machine.stack.push(zwordc(0xFb70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 65 | zkevm_circuit.assign_opcode(zkevm_opcode::DIV, machine); 66 | machine.stack.push(zwordc(0x1b70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 67 | machine.stack.push(1234567890); 68 | zkevm_circuit.assign_opcode(zkevm_opcode::DIV, machine); 69 | machine.stack.push(0); 70 | machine.stack.push(1234567890); 71 | zkevm_circuit.assign_opcode(zkevm_opcode::DIV, machine); 72 | zkevm_circuit.finalize_test(); 73 | // assignment.export_table(std::cout); 74 | // circuit.export_circuit(std::cout); 75 | nil::crypto3::zk::snark::basic_padding(assignment); 76 | BOOST_ASSERT(is_satisfied(circuit, assignment) == true); 77 | } 78 | 79 | BOOST_AUTO_TEST_SUITE_END() 80 | -------------------------------------------------------------------------------- /test/zkevm/opcodes/iszero.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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 zkevm_iszero_test 26 | 27 | #include 28 | 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | #include 35 | #include "../opcode_tester.hpp" 36 | 37 | #include 38 | 39 | using namespace nil::blueprint; 40 | using namespace nil::crypto3::algebra; 41 | 42 | BOOST_AUTO_TEST_SUITE(zkevm_iszero_test_suite) 43 | 44 | BOOST_AUTO_TEST_CASE(zkevm_iszero_test) { 45 | using field_type = fields::goldilocks64; 46 | using arithmentization_type = nil::crypto3::zk::snark::plonk_constraint_system; 47 | using assignment_type = assignment; 48 | using circuit_type = circuit; 49 | using zkevm_machine_type = zkevm_machine_interface; 50 | assignment_type assignment(0, 0, 0, 0); 51 | circuit_type circuit; 52 | zkevm_circuit zkevm_circuit(assignment, circuit); 53 | zkevm_machine_type machine = get_empty_machine(); 54 | // incorrect test logic, but we have no memory operations so 55 | machine.stack.push(1234567890); 56 | machine.stack.push(0); 57 | zkevm_circuit.assign_opcode(zkevm_opcode::ISZERO, machine); 58 | machine.stack.pop(); 59 | zkevm_circuit.assign_opcode(zkevm_opcode::ISZERO, machine); 60 | zkevm_circuit.finalize_test(); 61 | // assignment.export_table(std::cout); 62 | // circuit.export_circuit(std::cout); 63 | nil::crypto3::zk::snark::basic_padding(assignment); 64 | BOOST_ASSERT(is_satisfied(circuit, assignment) == true); 65 | } 66 | 67 | BOOST_AUTO_TEST_SUITE_END() 68 | -------------------------------------------------------------------------------- /test/zkevm/opcodes/mul.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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 zkevm_mul_test 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | #include 36 | #include "../opcode_tester.hpp" 37 | 38 | #include 39 | 40 | using namespace nil::blueprint; 41 | using namespace nil::crypto3::algebra; 42 | 43 | BOOST_AUTO_TEST_SUITE(zkevm_mul_test_suite) 44 | 45 | BOOST_AUTO_TEST_CASE(zkevm_mul_test) { 46 | using field_type = curves::alt_bn128<254>::base_field_type; 47 | using arithmentization_type = nil::crypto3::zk::snark::plonk_constraint_system; 48 | using assignment_type = assignment; 49 | using circuit_type = circuit; 50 | using zkevm_machine_type = zkevm_machine_interface; 51 | assignment_type assignment(0, 0, 0, 0); 52 | circuit_type circuit; 53 | zkevm_circuit zkevm_circuit(assignment, circuit); 54 | zkevm_machine_type machine = get_empty_machine(); 55 | // incorrect test logic, but we have no memory operations so 56 | // check all overflows for chunks 57 | machine.stack.push(zwordc(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui_modular257)); 58 | machine.stack.push(zwordc(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui_modular257)); 59 | zkevm_circuit.assign_opcode(zkevm_opcode::MUL, machine); 60 | machine.stack.push(1234567890); 61 | machine.stack.push(zwordc(0x1b70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 62 | zkevm_circuit.assign_opcode(zkevm_opcode::MUL, machine); 63 | machine.stack.push(zwordc(0xFb70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 64 | machine.stack.push(zwordc(0xFb70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 65 | zkevm_circuit.assign_opcode(zkevm_opcode::MUL, machine); 66 | machine.stack.push(zwordc(0x1b70726fb8d3a24da9ff9647225a18412b8f010425938504d73ebc8801e2e016_cppui_modular257)); 67 | machine.stack.push(1234567890); 68 | zkevm_circuit.assign_opcode(zkevm_opcode::MUL, machine); 69 | zkevm_circuit.finalize_test(); 70 | 71 | // assignment.export_table(std::cout); 72 | // circuit.export_circuit(std::cout); 73 | nil::crypto3::zk::snark::basic_padding(assignment); 74 | BOOST_ASSERT(is_satisfied(circuit, assignment) == true); 75 | } 76 | 77 | BOOST_AUTO_TEST_SUITE_END() 78 | -------------------------------------------------------------------------------- /test/zkevm/state_selector.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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_zkevm_state_selector_test 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | #include "../test_plonk_component.hpp" 40 | 41 | using namespace nil; 42 | 43 | template 44 | void test_state_selector(std::size_t options_amount, std::size_t option){ 45 | BOOST_ASSERT(option < options_amount); 46 | constexpr std::size_t PublicInputColumns = 1; 47 | constexpr std::size_t ConstantColumns = 0; 48 | constexpr std::size_t SelectorColumns = 1; 49 | const std::size_t WitnessColumns = (options_amount + 1) / 2 + 2; 50 | zk::snark::plonk_table_description desc( 51 | WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns); 52 | using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system; 53 | using hash_type = nil::crypto3::hashes::keccak_1600<256>; 54 | constexpr std::size_t Lambda = 40; 55 | using AssignmentType = nil::blueprint::assignment>; 56 | 57 | using value_type = typename BlueprintFieldType::value_type; 58 | using var = typename crypto3::zk::snark::plonk_variable; 59 | using component_type = blueprint::components::state_selector; 60 | 61 | typename component_type::input_type instance_input = { 62 | var(0, 0, false, var::column_type::public_input) 63 | }; 64 | 65 | std::vector public_input = {value_type(option)}; 66 | auto result_check = [](AssignmentType &assignment, 67 | typename component_type::result_type &real_res) { 68 | return true; 69 | }; 70 | 71 | std::vector witnesses(WitnessColumns); 72 | std::iota(witnesses.begin(), witnesses.end(), 0); 73 | 74 | component_type component_instance = component_type(witnesses, std::array{0}, 75 | std::array{0}, options_amount); 76 | nil::crypto3::test_component 77 | (component_instance, desc, public_input, result_check, instance_input, 78 | nil::blueprint::connectedness_check_type::type::STRONG, options_amount); 79 | } 80 | 81 | BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) 82 | 83 | BOOST_AUTO_TEST_CASE(blueprint_plonk_equality_flag_test_vesta) { 84 | using field_type = typename crypto3::algebra::fields::goldilocks64_base_field; 85 | boost::random::mt19937 gen(1444); 86 | 87 | for (std::size_t i = 1; i < 30; i++) { 88 | boost::uniform_int<> distrib(0, i - 1); 89 | test_state_selector(i, distrib(gen)); 90 | test_state_selector(i, distrib(gen)); 91 | test_state_selector(i, distrib(gen)); 92 | test_state_selector(i, distrib(gen)); 93 | } 94 | } 95 | 96 | BOOST_AUTO_TEST_SUITE_END() 97 | -------------------------------------------------------------------------------- /test/zkevm/zkevm_word.cpp: -------------------------------------------------------------------------------- 1 | //---------------------------------------------------------------------------// 2 | // Copyright (c) 2024 Dmitrii Tabalin 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_zkevm_word_utils_test 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | #include 35 | 36 | #include 37 | 38 | using namespace nil; 39 | using namespace nil::blueprint; 40 | 41 | BOOST_AUTO_TEST_SUITE(blueprint_zkevm_word_utils_test_suite) 42 | 43 | BOOST_AUTO_TEST_CASE(blueprint_zkevm_word_goldilocks64_test, * boost::unit_test::disabled()) { 44 | using field_type = crypto3::algebra::fields::goldilocks64; 45 | using value_type = field_type::value_type; 46 | using word_type = zkevm_word_type; 47 | 48 | word_type word = 0x123456789abcdef0; 49 | std::vector chunks = zkevm_word_to_field_element(word); 50 | BOOST_CHECK_EQUAL(chunks.size(), 16); 51 | BOOST_CHECK_EQUAL(chunks[0], 0xdef0); 52 | BOOST_CHECK_EQUAL(chunks[1], 0x9abc); 53 | BOOST_CHECK_EQUAL(chunks[2], 0x5678); 54 | BOOST_CHECK_EQUAL(chunks[3], 0x1234); 55 | for (std::size_t i = 4; i < 16; ++i) { 56 | BOOST_CHECK_EQUAL(chunks[i], 0); 57 | } 58 | } 59 | 60 | BOOST_AUTO_TEST_CASE(blueprint_zkevm_word_pallas_test) { 61 | using field_type = crypto3::algebra::fields::pallas_base_field; 62 | using value_type = field_type::value_type; 63 | using word_type = zkevm_word_type; 64 | 65 | word_type word = 0x123456789abcdef0; 66 | std::vector chunks = zkevm_word_to_field_element(word); 67 | BOOST_CHECK_EQUAL(chunks.size(), 16); 68 | BOOST_CHECK_EQUAL(chunks[0], 0xdef0); 69 | BOOST_CHECK_EQUAL(chunks[1], 0x9abc); 70 | BOOST_CHECK_EQUAL(chunks[2], 0x5678); 71 | BOOST_CHECK_EQUAL(chunks[3], 0x1234); 72 | for (std::size_t i = 4; i < 16; ++i) { 73 | BOOST_CHECK_EQUAL(chunks[i], 0); 74 | } 75 | } 76 | 77 | BOOST_AUTO_TEST_SUITE_END() 78 | -------------------------------------------------------------------------------- /zkllvm-blueprint.nix: -------------------------------------------------------------------------------- 1 | { lib, 2 | stdenv, 3 | src_repo, 4 | ninja, 5 | pkg-config, 6 | cmake, 7 | boost183, 8 | # We'll use boost183 by default, but you can override it 9 | boost_lib ? boost183, 10 | gdb, 11 | cmake_modules, 12 | crypto3, 13 | enableDebugging, 14 | enableDebug ? false, 15 | runTests ? false, 16 | }: 17 | let 18 | inherit (lib) optional; 19 | in stdenv.mkDerivation rec { 20 | name = "blueprint"; 21 | 22 | src = src_repo; 23 | 24 | nativeBuildInputs = [ cmake ninja pkg-config ] ++ (lib.optional (!stdenv.isDarwin) gdb); 25 | 26 | # enableDebugging will keep debug symbols in boost 27 | propagatedBuildInputs = [ (if enableDebug then (enableDebugging boost_lib) else boost_lib) ]; 28 | 29 | buildInputs = [cmake_modules crypto3]; 30 | 31 | cmakeFlags = 32 | [ 33 | (if runTests then "-DBUILD_TESTS=TRUE" else "") 34 | (if runTests then "-DCMAKE_ENABLE_TESTS=TRUE" else "") 35 | (if enableDebug then "-DCMAKE_BUILD_TYPE=Debug" else "-DCMAKE_BUILD_TYPE=Release") 36 | (if enableDebug then "-DCMAKE_CXX_FLAGS=-ggdb" else "") 37 | (if enableDebug then "-DCMAKE_CXX_FLAGS=-O0" else "") 38 | "-G Ninja" 39 | ]; 40 | 41 | doBuild = false; 42 | doCheck = runTests; 43 | dontInstall = true; 44 | 45 | buildPhase = '' 46 | echo "skip build" 47 | ''; 48 | 49 | checkPhase = '' 50 | bash ../run_tests.sh 51 | ''; 52 | 53 | shellHook = '' 54 | PS1="\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ " 55 | echo "Welcome to Blueprint development environment!" 56 | ''; 57 | } 58 | --------------------------------------------------------------------------------