TARAXA
vrf_wrapper.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <libdevcore/RLP.h>
4 
5 #include "common/types.hpp"
6 #include "sodium/crypto_vrf.h"
7 
8 namespace taraxa::vrf_wrapper {
9 
10 using dev::bytes;
15 
16 std::pair<vrf_pk_t, vrf_sk_t> getVrfKeyPair();
18 bool isValidVrfPublicKey(vrf_pk_t const &pk);
19 // get proof if public is valid
20 std::optional<vrf_proof_t> getVrfProof(vrf_sk_t const &pk, bytes const &msg);
21 // get output if proof is valid
22 std::optional<vrf_output_t> getVrfOutput(vrf_pk_t const &pk, vrf_proof_t const &proof, bytes const &msg,
23  bool strict = true);
24 
26  public:
27  VrfSortitionBase() = default;
28 
29  VrfSortitionBase(vrf_sk_t const &sk, bytes const &msg, uint16_t vote_count = 1) {
30  const auto pk(vrf_wrapper::getVrfPublicKey(sk));
31  assert(isValidVrfPublicKey(pk));
32  proof_ = vrf_wrapper::getVrfProof(sk, msg).value();
33  output_ = vrf_wrapper::getVrfOutput(pk, proof_, msg).value();
34  thresholdFromOutput(vote_count);
35  }
36 
37  static dev::bytes makeVrfInput(taraxa::level_t level, const dev::h256 &period_hash);
38 
39  bool verify(const vrf_pk_t &pk, const bytes &msg, uint16_t vote_count = 1, bool strict = true) const;
40 
41  bool operator==(VrfSortitionBase const &other) const { return proof_ == other.proof_ && output_ == other.output_; }
42 
43  virtual std::ostream &print(std::ostream &strm) const {
44  strm << "\n[VRF SortitionBase] " << std::endl;
45  strm << " proof: " << proof_ << std::endl;
46  strm << " output: " << output_ << std::endl;
47  return strm;
48  }
49 
50  friend std::ostream &operator<<(std::ostream &strm, VrfSortitionBase const &vrf_sortition) {
51  return vrf_sortition.print(strm);
52  }
53 
54  private:
55  void thresholdFromOutput(uint16_t vote_count) const {
56  threshold_ = (((uint16_t)output_[1] << 8) | output_[0]);
57  if (vote_count > 1) {
58  uint16_t min_threshold = threshold_;
59  uint16_t threshold_candidate = threshold_;
60  // Generate sequence of thresholds using simple generator with original threshold as seed
61  const uint16_t a = 48271; // Multiplier term used by C++11's minstd_rand
62  for (uint16_t vote_count_counter = 1; vote_count_counter < vote_count; vote_count_counter++) {
63  threshold_candidate = threshold_candidate * a;
64  if (threshold_candidate < min_threshold) {
65  min_threshold = threshold_candidate;
66  }
67  }
68  threshold_ = min_threshold;
69  }
70  }
71 
72  public:
75  mutable uint16_t threshold_;
76 };
77 
78 } // namespace taraxa::vrf_wrapper
Definition: vrf_wrapper.hpp:25
static dev::bytes makeVrfInput(taraxa::level_t level, const dev::h256 &period_hash)
Definition: vrf_wrapper.cpp:48
virtual std::ostream & print(std::ostream &strm) const
Definition: vrf_wrapper.hpp:43
void thresholdFromOutput(uint16_t vote_count) const
Definition: vrf_wrapper.hpp:55
friend std::ostream & operator<<(std::ostream &strm, VrfSortitionBase const &vrf_sortition)
Definition: vrf_wrapper.hpp:50
vrf_output_t output_
Definition: vrf_wrapper.hpp:74
bool operator==(VrfSortitionBase const &other) const
Definition: vrf_wrapper.hpp:41
VrfSortitionBase(vrf_sk_t const &sk, bytes const &msg, uint16_t vote_count=1)
Definition: vrf_wrapper.hpp:29
uint16_t threshold_
Definition: vrf_wrapper.hpp:75
vrf_proof_t proof_
Definition: vrf_wrapper.hpp:73
bool verify(const vrf_pk_t &pk, const bytes &msg, uint16_t vote_count=1, bool strict=true) const
Definition: vrf_wrapper.cpp:55
std::vector<::byte > bytes
Definition: Common.h:46
Definition: vrf_wrapper.hpp:8
std::optional< vrf_output_t > getVrfOutput(vrf_pk_t const &pk, vrf_proof_t const &proof, bytes const &msg, bool strict=true)
Definition: vrf_wrapper.cpp:32
std::pair< vrf_pk_t, vrf_sk_t > getVrfKeyPair()
Definition: vrf_wrapper.cpp:5
bool isValidVrfPublicKey(vrf_pk_t const &pk)
Definition: vrf_wrapper.cpp:18
std::optional< vrf_proof_t > getVrfProof(vrf_sk_t const &pk, bytes const &msg)
Definition: vrf_wrapper.cpp:22
vrf_pk_t getVrfPublicKey(vrf_sk_t const &sk)
Definition: vrf_wrapper.cpp:12
std::vector< byte > bytes
Definition: types.hpp:53
uint64_t level_t
Definition: types.hpp:46