iota.lib.cpp
IOTA C++ Library
pow.hpp
1 //
2 // MIT License
3 //
4 // Copyright (c) 2017-2018 Thibault Martinez and Simon Ninon
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //
24 //
25 
26 #pragma once
27 
28 #include <mutex>
29 
30 #include <iota/constants.hpp>
31 #include <iota/crypto/i_pow.hpp>
32 #include <iota/types/trits.hpp>
33 
34 namespace IOTA {
35 
36 namespace Crypto {
37 
41 class Pow : public IPow {
42 private:
43  static constexpr uint64_t hBits = 0xFFFFFFFFFFFFFFFF;
44  static constexpr uint64_t lBits = 0x0000000000000000;
45 
46  static constexpr uint64_t low0 = 0xDB6DB6DB6DB6DB6D;
47  static constexpr uint64_t high0 = 0xB6DB6DB6DB6DB6DB;
48  static constexpr uint64_t low1 = 0xF1F8FC7E3F1F8FC7;
49  static constexpr uint64_t high1 = 0x8FC7E3F1F8FC7E3F;
50  static constexpr uint64_t low2 = 0x7FFFE00FFFFC01FF;
51  static constexpr uint64_t high2 = 0xFFC01FFFF803FFFF;
52  static constexpr uint64_t low3 = 0xFFC0000007FFFFFF;
53  static constexpr uint64_t high3 = 0x003FFFFFFFFFFFFF;
54 
55  static constexpr uint64_t nonceOffset = TritHashLength - TritNonceLength;
56  static constexpr uint64_t nonceInitStart = nonceOffset + 4;
57  static constexpr uint64_t nonceIncrementStart = nonceInitStart + TritNonceLength / 3;
58 
59  static constexpr int stateSize = PowStateSize;
60  static constexpr int numberOfRounds = PowNumberOfRounds;
61 
62 public:
66  Pow() = default;
70  virtual ~Pow() = default;
71 
72 public:
82  Types::Trytes operator()(const Types::Trytes& trytes, int minWeightMagnitude,
83  int threads = 0) override;
84 
85 private:
86  static inline void initialize(uint64_t* stateLow, uint64_t* stateHigh,
87  const IOTA::Types::Trits& trits);
88  static inline void transform(uint64_t* stateLow, uint64_t* stateHigh, uint64_t* scratchpadLow,
89  uint64_t* scratchpadHigh);
90  static inline void increment(uint64_t* stateLow, uint64_t* stateHigh, int fromIndex, int toIndex);
91  inline Types::Trits loop(uint64_t* stateLow, uint64_t* stateHigh, int minWeightMagnitude);
92 
93 private:
94  bool stop_ = true;
95  std::mutex mtx_;
96 };
97 
98 } // namespace Crypto
99 
100 } // namespace IOTA
Definition: i_pow.hpp:37
Definition: pow.hpp:41
virtual ~Pow()=default
Types::Trytes operator()(const Types::Trytes &trytes, int minWeightMagnitude, int threads=0) override
Definition: core.hpp:33