Merge pull request #8679

b986421 adjust multiexp pippenger_cached_data for better reusability (koe)
This commit is contained in:
luigi1111 2023-04-25 11:10:27 -04:00
commit ecf0d01aa2
No known key found for this signature in database
GPG Key ID: F4ACA0183641E010
2 changed files with 53 additions and 43 deletions

View File

@ -443,7 +443,7 @@ size_t straus_get_cache_size(const std::shared_ptr<straus_cached_data> &cache)
return sz;
}
rct::key straus(const std::vector<MultiexpData> &data, const std::shared_ptr<straus_cached_data> &cache, size_t STEP)
ge_p3 straus_p3(const std::vector<MultiexpData> &data, const std::shared_ptr<straus_cached_data> &cache, size_t STEP)
{
CHECK_AND_ASSERT_THROW_MES(cache == NULL || cache->size >= data.size(), "Cache is too small");
MULTIEXP_PERF(PERF_TIMER_UNIT(straus, 1000000));
@ -554,7 +554,13 @@ skipfirst:
ge_p1p1_to_p3(&res_p3, &p1);
}
return res_p3;
}
rct::key straus(const std::vector<MultiexpData> &data, const std::shared_ptr<straus_cached_data> &cache, size_t STEP)
{
rct::key res;
const ge_p3 res_p3 = straus_p3(data, cache, STEP);
ge_p3_tobytes(res.bytes, &res_p3);
return res;
}
@ -571,14 +577,6 @@ size_t get_pippenger_c(size_t N)
return 9;
}
struct pippenger_cached_data
{
size_t size;
ge_cached *cached;
pippenger_cached_data(): size(0), cached(NULL) {}
~pippenger_cached_data() { aligned_free(cached); }
};
std::shared_ptr<pippenger_cached_data> pippenger_init_cache(const std::vector<MultiexpData> &data, size_t start_offset, size_t N)
{
MULTIEXP_PERF(PERF_TIMER_START_UNIT(pippenger_init_cache, 1000000));
@ -586,13 +584,11 @@ std::shared_ptr<pippenger_cached_data> pippenger_init_cache(const std::vector<Mu
if (N == 0)
N = data.size() - start_offset;
CHECK_AND_ASSERT_THROW_MES(N <= data.size() - start_offset, "Bad cache base data");
std::shared_ptr<pippenger_cached_data> cache(new pippenger_cached_data());
std::shared_ptr<pippenger_cached_data> cache = std::make_shared<pippenger_cached_data>();
cache->size = N;
cache->cached = (ge_cached*)aligned_realloc(cache->cached, N * sizeof(ge_cached), 4096);
CHECK_AND_ASSERT_THROW_MES(cache->cached, "Out of memory");
cache->resize(N);
for (size_t i = 0; i < N; ++i)
ge_p3_to_cached(&cache->cached[i], &data[i+start_offset].point);
ge_p3_to_cached(&(*cache)[i], &data[i+start_offset].point);
MULTIEXP_PERF(PERF_TIMER_STOP(pippenger_init_cache));
return cache;
@ -600,14 +596,14 @@ std::shared_ptr<pippenger_cached_data> pippenger_init_cache(const std::vector<Mu
size_t pippenger_get_cache_size(const std::shared_ptr<pippenger_cached_data> &cache)
{
return cache->size * sizeof(*cache->cached);
return cache->size() * sizeof(ge_cached);
}
rct::key pippenger(const std::vector<MultiexpData> &data, const std::shared_ptr<pippenger_cached_data> &cache, size_t cache_size, size_t c)
ge_p3 pippenger_p3(const std::vector<MultiexpData> &data, const std::shared_ptr<pippenger_cached_data> &cache, size_t cache_size, size_t c)
{
if (cache != NULL && cache_size == 0)
cache_size = cache->size;
CHECK_AND_ASSERT_THROW_MES(cache == NULL || cache_size <= cache->size, "Cache is too small");
cache_size = cache->size();
CHECK_AND_ASSERT_THROW_MES(cache == NULL || cache_size <= cache->size(), "Cache is too small");
if (c == 0)
c = get_pippenger_c(data.size());
CHECK_AND_ASSERT_THROW_MES(c <= 9, "c is too large");
@ -661,9 +657,9 @@ rct::key pippenger(const std::vector<MultiexpData> &data, const std::shared_ptr<
if (buckets_init[bucket])
{
if (i < cache_size)
add(buckets[bucket], local_cache->cached[i]);
add(buckets[bucket], (*local_cache)[i]);
else
add(buckets[bucket], local_cache_2->cached[i - cache_size]);
add(buckets[bucket], (*local_cache_2)[i - cache_size]);
}
else
{
@ -700,8 +696,14 @@ rct::key pippenger(const std::vector<MultiexpData> &data, const std::shared_ptr<
}
}
return result;
}
rct::key pippenger(const std::vector<MultiexpData> &data, const std::shared_ptr<pippenger_cached_data> &cache, const size_t cache_size, const size_t c)
{
rct::key res;
ge_p3_tobytes(res.bytes, &result);
const ge_p3 result_p3 = pippenger_p3(data, cache, cache_size, c);
ge_p3_tobytes(res.bytes, &result_p3);
return res;
}

View File

@ -35,10 +35,16 @@
#define MULTIEXP_H
#include <vector>
extern "C"
{
#include "crypto/crypto-ops.h"
}
#include "crypto/crypto.h"
#include "rctTypes.h"
#include "misc_log_ex.h"
#include <boost/align/aligned_allocator.hpp>
namespace rct
{
@ -55,17 +61,19 @@ struct MultiexpData {
};
struct straus_cached_data;
struct pippenger_cached_data;
using pippenger_cached_data = std::vector<ge_cached, boost::alignment::aligned_allocator<ge_cached, 4096>>;
rct::key bos_coster_heap_conv(std::vector<MultiexpData> data);
rct::key bos_coster_heap_conv_robust(std::vector<MultiexpData> data);
std::shared_ptr<straus_cached_data> straus_init_cache(const std::vector<MultiexpData> &data, size_t N =0);
size_t straus_get_cache_size(const std::shared_ptr<straus_cached_data> &cache);
ge_p3 straus_p3(const std::vector<MultiexpData> &data, const std::shared_ptr<straus_cached_data> &cache = NULL, size_t STEP = 0);
rct::key straus(const std::vector<MultiexpData> &data, const std::shared_ptr<straus_cached_data> &cache = NULL, size_t STEP = 0);
std::shared_ptr<pippenger_cached_data> pippenger_init_cache(const std::vector<MultiexpData> &data, size_t start_offset = 0, size_t N =0);
size_t pippenger_get_cache_size(const std::shared_ptr<pippenger_cached_data> &cache);
size_t get_pippenger_c(size_t N);
rct::key pippenger(const std::vector<MultiexpData> &data, const std::shared_ptr<pippenger_cached_data> &cache = NULL, size_t cache_size = 0, size_t c = 0);
ge_p3 pippenger_p3(const std::vector<MultiexpData> &data, const std::shared_ptr<pippenger_cached_data> &cache = NULL, size_t cache_size = 0, size_t c = 0);
rct::key pippenger(const std::vector<MultiexpData> &data, const std::shared_ptr<pippenger_cached_data> &cache = NULL, const size_t cache_size = 0, const size_t c = 0);
}