Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
non native multiplication bbf
Browse files Browse the repository at this point in the history
Antoine Cyr committed Dec 29, 2024
1 parent 3d24531 commit c1c2887
Showing 20 changed files with 124 additions and 202 deletions.
3 changes: 2 additions & 1 deletion crypto3.nix
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
boost,
gdb,
lldb,
mold,
cmake_modules,
enableDebugging,
enableDebug ? false,
@@ -57,4 +58,4 @@ in stdenv.mkDerivation {
PS1="\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "
echo "Welcome to Crypto3 development environment!"
'';
}
}
57 changes: 0 additions & 57 deletions crypto3/cmake/CheckSSE.cmake
Original file line number Diff line number Diff line change
@@ -58,63 +58,6 @@ macro(check_sse)
set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 available on host")
endif(SSE42_TRUE)

elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
execute_process(COMMAND "/usr/bin/env -S sysctl -n machdep.cpu.features" OUTPUT_VARIABLE
CPUINFO)

string(REGEX REPLACE "^.*[^S](SSE2).*$" "\\1" SSE_THERE "${CPUINFO}")
string(COMPARE EQUAL "SSE2" "${SSE_THERE}" SSE2_TRUE)
if(SSE2_TRUE)
set(SSE2_FOUND true CACHE BOOL "SSE2 available on host")
else(SSE2_TRUE)
set(SSE2_FOUND false CACHE BOOL "SSE2 available on host")
endif(SSE2_TRUE)

string(REGEX REPLACE "^.*[^S](SSE3).*$" "\\1" SSE_THERE "${CPUINFO}")
string(COMPARE EQUAL "SSE3" "${SSE_THERE}" SSE3_TRUE)
if(SSE3_TRUE)
set(SSE3_FOUND true CACHE BOOL "SSE3 available on host")
else(SSE3_TRUE)
set(SSE3_FOUND false CACHE BOOL "SSE3 available on host")
endif(SSE3_TRUE)

string(REGEX REPLACE "^.*(SSSE3).*$" "\\1" SSE_THERE "${CPUINFO}")
string(COMPARE EQUAL "SSSE3" "${SSE_THERE}" SSSE3_TRUE)
if(SSSE3_TRUE)
set(SSSE3_FOUND true CACHE BOOL "SSSE3 available on host")
else(SSSE3_TRUE)
set(SSSE3_FOUND false CACHE BOOL "SSSE3 available on host")
endif(SSSE3_TRUE)

string(REGEX REPLACE "^.*(SSE4.1).*$" "\\1" SSE_THERE "${CPUINFO}")
string(COMPARE EQUAL "SSE4.1" "${SSE_THERE}" SSE41_TRUE)
if(SSE41_TRUE)
set(SSE4_1_FOUND true CACHE BOOL "SSE4.1 available on host")
else(SSE41_TRUE)
set(SSE4_1_FOUND false CACHE BOOL "SSE4.1 available on host")
endif(SSE41_TRUE)

string(REGEX REPLACE "^.*(SSE4.2).*$" "\\1" SSE_THERE "${CPUINFO}")
string(COMPARE EQUAL "SSE4.2" "${SSE_THERE}" SSE42_TRUE)
if(SSE42_TRUE)
set(SSE4_2_FOUND true CACHE BOOL "SSE4.2 available on host")
else(SSE42_TRUE)
set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 available on host")
endif(SSE42_TRUE)

elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
# TODO
set(SSE2_FOUND true CACHE BOOL "SSE2 available on host")
set(SSE3_FOUND false CACHE BOOL "SSE3 available on host")
set(SSSE3_FOUND false CACHE BOOL "SSSE3 available on host")
set(SSE4_1_FOUND false CACHE BOOL "SSE4.1 available on host")
set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 available on host")
else(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(SSE2_FOUND true CACHE BOOL "SSE2 available on host")
set(SSE3_FOUND false CACHE BOOL "SSE3 available on host")
set(SSSE3_FOUND false CACHE BOOL "SSSE3 available on host")
set(SSE4_1_FOUND false CACHE BOOL "SSE4.1 available on host")
set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 available on host")
endif(CMAKE_SYSTEM_NAME MATCHES "Linux")

if(CMAKE_COMPILER_IS_GNUCXX)
Original file line number Diff line number Diff line change
@@ -49,14 +49,8 @@ namespace nil {
#ifdef __ZKLLVM__
typedef int integral_type;
#else
typedef boost::multiprecision::backends::cpp_int_modular_backend<modulus_bits> modular_backend;

typedef boost::multiprecision::number<modular_backend> integral_type;
typedef boost::multiprecision::number<
boost::multiprecision::backends::cpp_int_modular_backend<16 * modulus_bits>>
extended_integral_type;

typedef boost::multiprecision::backends::modular_params<modular_backend> modular_params_type;
typedef nil::crypto3::multiprecision::big_uint<modulus_bits> integral_type;
typedef nil::crypto3::multiprecision::big_uint<16 * modulus_bits> extended_integral_type;
#endif
};

Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ namespace nil {
}

boost::random::independent_bits_engine<std::mt19937, 256, nil::crypto3::multiprecision::big_uint<Bits>> rng;
nil::crypto3::multiprecision::montgomery_big_mod_rt divisor(0u, n),
nil::crypto3::multiprecision::montgomery_big_mod_rt<Bits> divisor(0u, n),
c(rng(), n), x(rng(), n), nn(n, n), xx = x;
do {
x = x * x + c;
61 changes: 61 additions & 0 deletions crypto3/libs/blueprint/include/nil/blueprint/bbf/check_proof.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/verifier.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/params.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp>

#include <nil/crypto3/hash/algorithm/hash.hpp>
#include <nil/crypto3/hash/sha2.hpp>
#include <nil/crypto3/hash/keccak.hpp>

#include <nil/crypto3/zk/snark/arithmetization/plonk/params.hpp>

template<typename FieldType>
bool check_proof(
const nil::blueprint::circuit<zk::snark::plonk_constraint_system<FieldType>> &bp,
const zk::snark::plonk_assignment_table<FieldType> &assignment,
const zk::snark::plonk_table_description<FieldType> &desc) {

std::size_t Lambda = 9;

typedef nil::crypto3::zk::snark::placeholder_circuit_params<FieldType> circuit_params;
using transcript_hash_type = nil::crypto3::hashes::keccak_1600<256>;
using merkle_hash_type = nil::crypto3::hashes::keccak_1600<256>;
using transcript_type = typename nil::crypto3::zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type>;
using lpc_params_type = nil::crypto3::zk::commitments::list_polynomial_commitment_params<
merkle_hash_type,
transcript_hash_type,
2 //m
>;

using lpc_type = nil::crypto3::zk::commitments::list_polynomial_commitment<FieldType, lpc_params_type>;
using lpc_scheme_type = typename nil::crypto3::zk::commitments::lpc_commitment_scheme<lpc_type>;
using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params<circuit_params, lpc_scheme_type>;
typename lpc_type::fri_type::params_type fri_params(1, std::ceil(log2(assignment.rows_amount())), Lambda, 2);
lpc_scheme_type lpc_scheme(fri_params);

std::cout << "Public preprocessor" << std::endl;
typename nil::crypto3::zk::snark::placeholder_public_preprocessor<FieldType,
lpc_placeholder_params_type>::preprocessed_data_type lpc_preprocessed_public_data =
nil::crypto3::zk::snark::placeholder_public_preprocessor<FieldType, lpc_placeholder_params_type>::process(
bp, assignment.public_table(), desc, lpc_scheme, 10);

std::cout << "Private preprocessor" << std::endl;
typename nil::crypto3::zk::snark::placeholder_private_preprocessor<FieldType,
lpc_placeholder_params_type>::preprocessed_data_type lpc_preprocessed_private_data =
nil::crypto3::zk::snark::placeholder_private_preprocessor<FieldType, lpc_placeholder_params_type>::process(
bp, assignment.private_table(), desc);

std::cout << "Prover" << std::endl;
auto lpc_proof = nil::crypto3::zk::snark::placeholder_prover<FieldType, lpc_placeholder_params_type>::process(
lpc_preprocessed_public_data, std::move(lpc_preprocessed_private_data), desc, bp,
lpc_scheme);

// We must not use the same instance of lpc_scheme.
lpc_scheme_type verifier_lpc_scheme(fri_params);

std::cout << "Verifier" << std::endl;
bool verifier_res = nil::crypto3::zk::snark::placeholder_verifier<FieldType, lpc_placeholder_params_type>::process(
lpc_preprocessed_public_data.common_data, lpc_proof, desc, bp, verifier_lpc_scheme);
return verifier_res;
}

Original file line number Diff line number Diff line change
@@ -408,7 +408,11 @@ namespace nil {
assign(typename Component<FieldType, GenerationStage::ASSIGNMENT>::raw_input_type raw_input) {
using generator = Component<FieldType,GenerationStage::ASSIGNMENT>;
using assignment_type = crypto3::zk::snark::plonk_assignment_table<FieldType>;
using context_type = typename nil::blueprint::bbf::context<FieldType, nil::blueprint::bbf::GenerationStage::ASSIGNMENT>;

// actually we should use presets to chose the right size and partly fill it
assignment_type at = assignment_type(witnesses_amount, public_inputs_amount,
presets.constants_amount(), presets.selectors_amount());

// copy preset constants
for(std::size_t i = 0; i < presets.constants_amount(); i++) {
@@ -609,4 +613,4 @@ namespace nil {
} // namespace bbf
} // namespace blueprint
} // namespace nil
#endif // CRYPTO3_BLUEPRINT_PLONK_BBF_CIRCUIT_BUILDER_HPP
#endif // CRYPTO3_BLUEPRINT_PLONK_BBF_CIRCUIT_BUILDER_HPP
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ namespace nil {
return {11,1,0,max_rows_amount};
}

static std::tuple<input_type> form_input(context_type &context_object, raw_input_type raw_input) {
static std::tuple<input_type> form_input(context_type &context_object, raw_input_type raw_input,std::size_t max_rows_amount) {
input_type res;
if constexpr (stage == GenerationStage::ASSIGNMENT) {
res = raw_input.B;
Original file line number Diff line number Diff line change
@@ -53,11 +53,15 @@ namespace nil {
}
}

bool operator==(const row_selector& other) const {
return used_rows_ == other.used_rows_;
void set_interval(std::size_t start_row, std::size_t end_row) {
BOOST_ASSERT( end_row < used_rows_.size());
BOOST_ASSERT( start_row < end_row );
if (start_row < end_row && end_row < used_rows_.size()) {
used_rows_.set(start_row, end_row-start_row + 1, true);
}
}

bool operator[](size_t row) const {
bool is_set(std::size_t row) const {
return used_rows_.at(row);
}

@@ -205,4 +209,4 @@ namespace std {
};
}

#endif // CRYPTO3_BLUEPRINT_PLONK_BBF_ROW_SELECTOR_HPP
#endif // CRYPTO3_BLUEPRINT_PLONK_BBF_ROW_SELECTOR_HPP
10 changes: 5 additions & 5 deletions crypto3/libs/blueprint/include/nil/blueprint/bbf/tester.hpp
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@

#include <nil/blueprint/bbf/is_zero.hpp>
#include <nil/blueprint/bbf/choice_function.hpp>
#include <nil/blueprint/bbf/carry_on_addition.hpp>
// #include <nil/blueprint/bbf/components/detail/carry_on_addition.hpp>
#include <nil/blueprint/bbf/useless.hpp>


@@ -105,7 +105,7 @@ namespace nil {

using Is_Zero = is_zero<FieldType, stage>;
using Choice_Function = choice_function<FieldType, stage, 3>;
using Carry_On_Addition = carry_on_addition<FieldType, stage, 3, 16>;
// using Carry_On_Addition = carry_on_addition<FieldType, stage, 3, 16>;
using Useless = useless<FieldType, stage>;

TYPE const_test = 5;
@@ -117,9 +117,9 @@ namespace nil {
context_type ct2 = context_object.subcontext(ct2_area,0,4);
auto c2 = Choice_Function(ct2,Q,CX,CY, make_links); // make_links delegated to subcomponent

std::vector<std::size_t> ct3_area = {7,8,9,10,11};
context_type ct3 = context_object.subcontext(ct3_area,0,4);
auto c3 = Carry_On_Addition(ct3, CX, CY, make_links);
// std::vector<std::size_t> ct3_area = {7,8,9,10,11};
// context_type ct3 = context_object.subcontext(ct3_area,0,4);
// auto c3 = Carry_On_Addition(ct3, CX, CY, make_links);

std::vector<std::size_t> ct4_area = {12};
context_type ct4 = context_object.subcontext(ct4_area,1,4);
Original file line number Diff line number Diff line change
@@ -37,6 +37,8 @@
#include <nil/crypto3/algebra/curves/vesta.hpp>
#include <nil/crypto3/algebra/curves/pallas.hpp>

#include <nil/blueprint/bbf/check_proof.hpp>

using namespace nil;
using namespace nil::blueprint;

@@ -56,7 +58,7 @@ void test_mod_p_check(const std::vector<typename BlueprintFieldType::value_type>

if (to_pass) {
assert(B.is_satisfied(at) == true);
bool proof = B.check_proof(at, desc);
bool proof = check_proof(B.get_circuit(), at, desc);
std::cout << "Is_proved = " << proof << std::endl;
assert(proof == true);
} else {
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@
#include <nil/blueprint/bbf/components/algebra/fields/non_native/flexible_multiplication.hpp>

#include <nil/blueprint/bbf/circuit_builder.hpp>
#include <nil/blueprint/bbf/check_proof.hpp>



@@ -90,7 +91,7 @@ void test_mult(const std::vector<typename BlueprintFieldType::value_type> &publi

if (to_pass) {
assert(B.is_satisfied(at) == true);
bool proof = B.check_proof(at, desc);
bool proof = check_proof(B.get_circuit(), at, desc);
std::cout << "Is_proved = " << proof << std::endl;
assert(proof == true);
foreign_integral_type R = 0;
4 changes: 3 additions & 1 deletion crypto3/libs/blueprint/test/bbf/detail/carry_on_addition.cpp
Original file line number Diff line number Diff line change
@@ -38,6 +38,8 @@
#include <nil/crypto3/algebra/curves/vesta.hpp>
#include <nil/crypto3/algebra/curves/pallas.hpp>

#include <nil/blueprint/bbf/check_proof.hpp>

using namespace nil;
using namespace nil::blueprint;

@@ -56,7 +58,7 @@ void test_carry_on_addition(const std::vector<typename BlueprintFieldType::value
std::cout << "Is_satisfied = " << pass << std::endl;

assert(B.is_satisfied(at) == true);
bool proof = B.check_proof(at, desc);
bool proof = check_proof(B.get_circuit(), at, desc);
std::cout << "Is_proved = " << proof << std::endl;
assert(proof == true);

4 changes: 3 additions & 1 deletion crypto3/libs/blueprint/test/bbf/detail/range_check_multi.cpp
Original file line number Diff line number Diff line change
@@ -38,6 +38,8 @@
#include <nil/crypto3/algebra/curves/vesta.hpp>
#include <nil/crypto3/algebra/curves/pallas.hpp>

#include <nil/blueprint/bbf/check_proof.hpp>

using namespace nil;
using namespace nil::blueprint;

@@ -56,7 +58,7 @@ void test_range_check(const std::vector<typename BlueprintFieldType::value_type>

if (to_pass) {
assert(B.is_satisfied(at) == true);
bool proof = B.check_proof(at, desc);
bool proof = check_proof(B.get_circuit(), at, desc);
std::cout << "Is_proved = " << proof << std::endl;
assert(proof == true);
} else {
1 change: 0 additions & 1 deletion crypto3/libs/blueprint/test/bbf/tester.cpp
Original file line number Diff line number Diff line change
@@ -40,7 +40,6 @@

#include <nil/blueprint/blueprint/plonk/circuit.hpp>
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
#include <nil/blueprint/bbf/bbf_wrapper.hpp>
#include <nil/blueprint/bbf/circuit_builder.hpp>

#include <nil/blueprint/bbf/tester.hpp>
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@

#pragma once

//Use this line instead if you have issues with mac
//#include <machine/endian.h>
#include <bits/endian.h>

#define NIL_CO3_MP_ENDIAN_BIG_BYTE 0
52 changes: 0 additions & 52 deletions evm-assigner.nix

This file was deleted.

Loading

0 comments on commit c1c2887

Please sign in to comment.