chia_policy.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 //
4 // MIT License
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 #ifndef CRYPTO3_CHIA_POLICY_HPP
26 #define CRYPTO3_CHIA_POLICY_HPP
27 
28 #if defined(CRYPTO3_VDF_FLINT)
29 
30 #include <flint/fmpz.h>
31 
32 #elif defined(CRYPTO3_VDF_GMP)
33 
34 #include <gmpxx.h>
35 
36 #elif defined(CRYPTO3_VDF_MPIR)
37 
38 #include <mpirxx.h>
39 
40 #elif defined(CRYPTO3_VDF_BOOST)
41 
42 #include <nil/crypto3/multiprecision/number.hpp>
43 
44 #endif
45 
46 namespace nil {
47  namespace crypto3 {
48  namespace vdf {
49 #ifdef CRYPTO3_VDF_BOOST
50  using namespace nil::crypto3::multiprecision;
51 #endif
52 
57  template<typename NumberType>
59  typedef NumberType number_type;
60 #if defined(CRYPTO3_VDF_GMP) || defined(CRYPTO3_VDF_MPIR)
61 
63  mpz_inits(a, b, c, NULL);
64  }
65 
66 #elif defined(CRYPTO3_VDF_FLINT)
67 
68 #endif
69 
70  // y = ax^2 + bxy + y^2
74  number_type d; // discriminant
75  };
76 
77  namespace detail {
78  struct chia_policy {
79 
80 #define LOG2(X) (63 - __builtin_clzll((X)))
81 
82  constexpr static const int64_t threshold = 1UL << 31;
83  constexpr static const int64_t double_threshold = 1UL << 31;
84  constexpr static const int64_t exp_threshold = 63;
85  constexpr static const int64_t maxv = ((1UL << 63) - 1);
86 
87  template<typename NumberType>
88  struct state_type {
89  typedef NumberType number_type;
91 #if defined(CRYPTO3_VDF_GMP) || defined(CRYPTO3_VDF_MPIR)
92 
93  state_type() {
94  mpz_inits(D, L, NULL);
95  mpz_inits(r, ra, s, p, NULL);
96  mpz_inits(G, dx, dy, By, Dy, x, y, t1, t2, bx, by, ax, ay, q, t, Q1, NULL);
97  mpz_inits(faa, fab, fac, fba, fbb, fbc, fca, fcb, fcc, NULL);
98  }
99 
100 #elif defined(CRYPTO3_VDF_FLINT)
101 
102  state_type() {
103  fmpz_init(D);
104  fmpz_init(L);
105  fmpz_init(r);
106  fmpz_init(ra);
107  fmpz_init(s);
108  fmpz_init(p);
109  fmpz_init(G);
110  fmpz_init(dx);
111  fmpz_init(dy);
112  fmpz_init(By);
113  fmpz_init(Dy);
114  fmpz_init(x);
115  fmpz_init(y);
116  fmpz_init(t1);
117  fmpz_init(t2);
118  fmpz_init(bx);
119  fmpz_init(by);
120  fmpz_init(ax);
121  fmpz_init(ay);
122  fmpz_init(q);
123  fmpz_init(t);
124  fmpz_init(Q1);
125  fmpz_init(faa);
126  fmpz_init(fab);
127  fmpz_init(fac);
128  fmpz_init(fba);
129  fmpz_init(fbb);
130  fmpz_init(fbc);
131  fmpz_init(fca);
132  fmpz_init(fcb);
133  fmpz_init(fcc);
134  }
135 #endif
136 
138  number_type r, ra, s, p;
139  number_type G, dx, dy, By, Dy, x, y, t1, t2, bx, by, ax, ay, q, t, Q1;
140 
141  number_type faa, fab, fac, fba, fbb, fbc, fca, fcb, fcc;
142 
144  };
145  };
146  } // namespace detail
147  } // namespace vdf
148  } // namespace crypto3
149 } // namespace nil
150 
151 #endif // CRYPTO3_CHIA_POLICY_HPP
Definition: pair.hpp:31
Defines y = ax^2 + bxy + y^2.
Definition: chia_policy.hpp:58
number_type b
Definition: chia_policy.hpp:72
number_type a
Definition: chia_policy.hpp:71
number_type c
Definition: chia_policy.hpp:73
number_type d
Definition: chia_policy.hpp:74
NumberType number_type
Definition: chia_policy.hpp:59
binary_quadratic_form< number_type > form_type
Definition: chia_policy.hpp:90
number_type ax
Definition: chia_policy.hpp:139
number_type p
Definition: chia_policy.hpp:138
NumberType number_type
Definition: chia_policy.hpp:89
form_type form
Definition: chia_policy.hpp:143
number_type D
Definition: chia_policy.hpp:137
number_type faa
Definition: chia_policy.hpp:141
Definition: chia_policy.hpp:78