Electroneum
multiexp.h
Go to the documentation of this file.
1 // Copyright (c) 2018, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30 
31 #pragma once
32 
33 #include <vector>
34 #include "ringct/rctOps.h"
35 #include "ringct/multiexp.h"
36 
38 {
44 };
45 
46 template<test_multiexp_algorithm algorithm, size_t npoints, size_t c=0>
48 {
49 public:
50  static const size_t loop_count = npoints >= 1024 ? 10 : npoints < 256 ? 1000 : 100;
51 
52  bool init()
53  {
54  data.resize(npoints);
55  res = rct::identity();
56  for (size_t n = 0; n < npoints; ++n)
57  {
58  data[n].scalar = rct::skGen();
60  if (ge_frombytes_vartime(&data[n].point, point.bytes))
61  return false;
62  rct::key kn = rct::scalarmultKey(point, data[n].scalar);
63  res = rct::addKeys(res, kn);
64  }
65  straus_cache = rct::straus_init_cache(data);
66  pippenger_cache = rct::pippenger_init_cache(data);
67  return true;
68  }
69 
70  bool test()
71  {
72  switch (algorithm)
73  {
75  return res == bos_coster_heap_conv_robust(data);
76  case multiexp_straus:
77  return res == straus(data);
79  return res == straus(data, straus_cache);
80  case multiexp_pippenger:
81  return res == pippenger(data, NULL, 0, c);
83  return res == pippenger(data, pippenger_cache, 0, c);
84  default:
85  return false;
86  }
87  }
88 
89 private:
90  std::vector<rct::MultiexpData> data;
91  std::shared_ptr<rct::straus_cached_data> straus_cache;
92  std::shared_ptr<rct::pippenger_cached_data> pippenger_cache;
93  rct::key res;
94 };
std::shared_ptr< straus_cached_data > straus_init_cache(const std::vector< MultiexpData > &data, size_t N=0)
rct::key straus(const std::vector< MultiexpData > &data, const std::shared_ptr< straus_cached_data > &cache=NULL, size_t STEP=0)
bool test()
Definition: multiexp.h:70
void scalarmultKey(key &aP, const key &P, const key &a)
Definition: rctOps.cpp:368
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)
std::shared_ptr< pippenger_cached_data > pippenger_init_cache(const std::vector< MultiexpData > &data, size_t start_offset=0, size_t N=0)
bool init()
Definition: multiexp.h:52
void skGen(key &sk)
Definition: rctOps.cpp:253
void scalarmultBase(key &aG, const key &a)
Definition: rctOps.cpp:350
rct::key bos_coster_heap_conv_robust(std::vector< MultiexpData > data)
unsigned char bytes[32]
Definition: rctTypes.h:86
key identity()
Definition: rctOps.h:73
static const size_t loop_count
Definition: multiexp.h:50
int ge_frombytes_vartime(ge_p3 *, const unsigned char *)
void addKeys(key &AB, const key &A, const key &B)
Definition: rctOps.cpp:420
test_multiexp_algorithm
Definition: multiexp.h:37