diff --git a/cbindings/fixed_pedersen.t.cc b/cbindings/fixed_pedersen.t.cc index cf736156..b4a5c7c2 100644 --- a/cbindings/fixed_pedersen.t.cc +++ b/cbindings/fixed_pedersen.t.cc @@ -95,6 +95,29 @@ TEST_CASE("we can compute multi-exponentiations with a fixed set of generators") sxt_multiexp_handle_free(hp); } + SECTION("we can read and write a handle to a file with cpu backend") { + cbn::reset_backend_for_testing(); + const sxt_config config = {SXT_CPU_BACKEND, 0}; + + REQUIRE(sxt_init(&config) == 0); + bastst::temp_file temp_file{std::ios::binary}; + temp_file.stream().close(); + + wrapped_handle h{generators.data(), 2}; + REQUIRE(h.h != nullptr); + + sxt_multiexp_handle_write_to_file(h.h, temp_file.name().c_str()); + + auto hp = sxt_multiexp_handle_new_from_file(SXT_CURVE_RISTRETTO255, temp_file.name().c_str()); + + uint8_t scalars[] = {1, 0, 0, 2}; + c21t::element_p3 res; + sxt_fixed_multiexponentiation(&res, hp, 2, 1, 2, scalars); + REQUIRE(res == generators[0] + 2 * 256 * generators[1]); + + sxt_multiexp_handle_free(hp); + } + SECTION("we can compute a multiexponentiation in packed form") { cbn::reset_backend_for_testing(); const sxt_config config = {SXT_GPU_BACKEND, 0}; diff --git a/sxt/cbindings/backend/computational_backend.cc b/sxt/cbindings/backend/computational_backend.cc index cdaad9cf..02ea9983 100644 --- a/sxt/cbindings/backend/computational_backend.cc +++ b/sxt/cbindings/backend/computational_backend.cc @@ -21,20 +21,6 @@ #include "sxt/multiexp/pippenger2/in_memory_partition_table_accessor.h" namespace sxt::cbnbck { -//-------------------------------------------------------------------------------------------------- -// read_partition_table_accessor -//-------------------------------------------------------------------------------------------------- -std::unique_ptr -computational_backend::read_partition_table_accessor(cbnb::curve_id_t curve_id, - const char* filename) const noexcept { - std::unique_ptr res; - cbnb::switch_curve_type( - curve_id, [&](std::type_identity, std::type_identity) noexcept { - res = std::make_unique>(filename); - }); - return res; -} - //-------------------------------------------------------------------------------------------------- // write_partition_table_accessor //-------------------------------------------------------------------------------------------------- diff --git a/sxt/cbindings/backend/computational_backend.h b/sxt/cbindings/backend/computational_backend.h index e91c342f..77848094 100644 --- a/sxt/cbindings/backend/computational_backend.h +++ b/sxt/cbindings/backend/computational_backend.h @@ -124,8 +124,8 @@ class computational_backend { const unsigned* output_lengths, unsigned num_outputs, const uint8_t* scalars) const noexcept = 0; - std::unique_ptr - read_partition_table_accessor(cbnb::curve_id_t curve_id, const char* filename) const noexcept; + virtual std::unique_ptr + read_partition_table_accessor(cbnb::curve_id_t curve_id, const char* filename) const noexcept = 0; void write_partition_table_accessor(cbnb::curve_id_t curve_id, const mtxpp2::partition_table_accessor_base& accessor, diff --git a/sxt/cbindings/backend/cpu_backend.cc b/sxt/cbindings/backend/cpu_backend.cc index 3284f546..a4f5371d 100644 --- a/sxt/cbindings/backend/cpu_backend.cc +++ b/sxt/cbindings/backend/cpu_backend.cc @@ -213,6 +213,21 @@ void cpu_backend::fixed_multiexponentiation(void* res, cbnb::curve_id_t curve_id }); } +//-------------------------------------------------------------------------------------------------- +// read_partition_table_accessor +//-------------------------------------------------------------------------------------------------- +std::unique_ptr +cpu_backend::read_partition_table_accessor(cbnb::curve_id_t curve_id, + const char* filename) const noexcept { + std::unique_ptr res; + cbnb::switch_curve_type(curve_id, [&](std::type_identity, + std::type_identity) noexcept { + res = + std::make_unique>(filename, basm::alloc_t{}); + }); + return res; +} + //-------------------------------------------------------------------------------------------------- // get_cpu_backend //-------------------------------------------------------------------------------------------------- diff --git a/sxt/cbindings/backend/cpu_backend.h b/sxt/cbindings/backend/cpu_backend.h index 1993bc11..4e8292d9 100644 --- a/sxt/cbindings/backend/cpu_backend.h +++ b/sxt/cbindings/backend/cpu_backend.h @@ -78,6 +78,10 @@ class cpu_backend final : public computational_backend { const unsigned* output_bit_table, const unsigned* output_lengths, unsigned num_outputs, const uint8_t* scalars) const noexcept override; + + std::unique_ptr + read_partition_table_accessor(cbnb::curve_id_t curve_id, + const char* filename) const noexcept override; }; //-------------------------------------------------------------------------------------------------- diff --git a/sxt/cbindings/backend/gpu_backend.cc b/sxt/cbindings/backend/gpu_backend.cc index dbd5bfc6..b9e7bf4e 100644 --- a/sxt/cbindings/backend/gpu_backend.cc +++ b/sxt/cbindings/backend/gpu_backend.cc @@ -255,6 +255,20 @@ void gpu_backend::fixed_multiexponentiation(void* res, cbnb::curve_id_t curve_id }); } +//-------------------------------------------------------------------------------------------------- +// read_partition_table_accessor +//-------------------------------------------------------------------------------------------------- +std::unique_ptr +gpu_backend::read_partition_table_accessor(cbnb::curve_id_t curve_id, + const char* filename) const noexcept { + std::unique_ptr res; + cbnb::switch_curve_type( + curve_id, [&](std::type_identity, std::type_identity) noexcept { + res = std::make_unique>(filename); + }); + return res; +} + //-------------------------------------------------------------------------------------------------- // get_gpu_backend //-------------------------------------------------------------------------------------------------- diff --git a/sxt/cbindings/backend/gpu_backend.h b/sxt/cbindings/backend/gpu_backend.h index b5da9a47..24c07e96 100644 --- a/sxt/cbindings/backend/gpu_backend.h +++ b/sxt/cbindings/backend/gpu_backend.h @@ -80,6 +80,10 @@ class gpu_backend final : public computational_backend { const unsigned* output_bit_table, const unsigned* output_lengths, unsigned num_outputs, const uint8_t* scalars) const noexcept override; + + std::unique_ptr + read_partition_table_accessor(cbnb::curve_id_t curve_id, + const char* filename) const noexcept override; }; //-------------------------------------------------------------------------------------------------- diff --git a/sxt/multiexp/pippenger2/BUILD b/sxt/multiexp/pippenger2/BUILD index c072c949..1913d885 100644 --- a/sxt/multiexp/pippenger2/BUILD +++ b/sxt/multiexp/pippenger2/BUILD @@ -153,6 +153,7 @@ sxt_cc_component( "//sxt/base/device:memory_utility", "//sxt/base/error:assert", "//sxt/base/error:panic", + "//sxt/base/memory:alloc", "//sxt/memory/management:managed_array", "//sxt/memory/resource:pinned_resource", ], diff --git a/sxt/multiexp/pippenger2/in_memory_partition_table_accessor.h b/sxt/multiexp/pippenger2/in_memory_partition_table_accessor.h index bf4fdd76..ba366bbe 100644 --- a/sxt/multiexp/pippenger2/in_memory_partition_table_accessor.h +++ b/sxt/multiexp/pippenger2/in_memory_partition_table_accessor.h @@ -25,6 +25,7 @@ #include "sxt/base/device/memory_utility.h" #include "sxt/base/error/assert.h" #include "sxt/base/error/panic.h" +#include "sxt/base/memory/alloc.h" #include "sxt/memory/management/managed_array.h" #include "sxt/memory/resource/pinned_resource.h" #include "sxt/multiexp/pippenger2/partition_table_accessor.h" @@ -37,8 +38,9 @@ template class in_memory_partition_table_accessor final : public partition_table_accessor { public: - explicit in_memory_partition_table_accessor(std::string_view filename) noexcept - : table_{memr::get_pinned_resource()} { + explicit in_memory_partition_table_accessor( + std::string_view filename, basm::alloc_t alloc = memr::get_pinned_resource()) noexcept + : table_{alloc} { std::ifstream in{std::string{filename}, std::ios::binary}; if (!in.good()) { baser::panic("failed to open {}: {}", filename, std::strerror(errno));