chia.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_VDF_CHIA_HPP
26 #define CRYPTO3_VDF_CHIA_HPP
27 
29 
30 namespace nil {
31  namespace crypto3 {
32  namespace vdf {
33  class chia {
35 
36  public:
37 #if defined(CRYPTO3_VDF_GMP) || defined(CRYPTO3_VDF_MPIR)
38 
39  template<typename T = mpz_t>
40  using state_type = typename policy_type::state_type<T>;
41 
51  template<typename Integer, typename NumberType = mpz_t>
52  static inline void make_state(state_type<NumberType> &state, NumberType discriminant) {
53  NumberType denom;
54  mpz_init(denom);
55  mpz_set_ui(state.form.a, 2);
56  mpz_set_ui(state.form.b, 1);
57  mpz_mul(state.form.c, state.form.b, state.form.b);
58  mpz_sub(state.form.c, state.form.c, d);
59  mpz_mul_ui(denom, state.form.a, 4);
60  mpz_fdiv_q(state.form.c, state.form.c, denom);
61  mpz_set(state.form.d, discriminant);
62  fast_reduce(state);
63  mpz_clear(denom);
64  }
65 
66  template<typename NumberType = mpz_t>
67  static inline state_type<NumberType> make_state(const NumberType &a, const NumberType &b,
68  const NumberType &discriminant) {
69  state_type<NumberType> state;
70  make_state(state, discriminant, a, b);
71  return state;
72  }
73 
74  template<typename T, typename I>
75  inline static void compute(state_type<T> &state, I itr) {
76  policy_type::discriminant_generator(state, d);
77 
78  mpz_abs(state.L, state.form.d);
79  mpz_root(state.L, state.L, 4);
80 
81  for (int i = 0; i < itr; i++) {
82  policy_type::nudupl(state);
83  policy_type::fast_reduce(state);
84  }
85  }
86 
87 #elif defined(CRYPTO3_VDF_FLINT)
88 
89  template<typename T = fmpz_t>
90  using state_type = typename policy_type::state_type<T>;
91 
101  template<typename Integer, typename NumberType = fmpz_t>
102  static inline void make_state(state_type<NumberType> &state, NumberType discriminant) {
103  NumberType denom;
104  fmpz_init(denom);
105  fmpz_set_ui(state.form.a, 2);
106  fmpz_set_ui(state.form.b, 1);
107  fmpz_mul(state.form.c, state.form.b, state.form.b);
108  fmpz_sub(state.form.c, state.form.c, d);
109  fmpz_mul_ui(denom, state.form.a, 4);
110  fmpz_fdiv_q(state.form.c, state.form.c, denom);
111  fmpz_set(state.form.d, discriminant);
112  fast_reduce(state);
113  fmpz_clear(denom);
114  }
115 
116  template<typename NumberType = fmpz_t>
117  static inline state_type<NumberType> make_state(NumberType a, NumberType b, NumberType discriminant) {
118  state_type<NumberType> state;
119  make_state(state, discriminant, a, b);
120  return state;
121  }
122 
123  template<typename T, typename I>
124  inline static void compute(state_type<T> &state, I itr) {
125  fmpz_abs(state.L, state.form.d);
126  fmpz_root(state.L, state.L, 4);
127 
128  for (int i = 0; i < itr; i++) {
129  policy_type::nudupl(state);
130  policy_type::fast_reduce(state);
131  }
132  }
133 
134 #elif defined(CRYPTO3_VDF_BOOST)
135 
136  template<typename T>
137  using state_type = typename policy_type::state_type<T>;
138 
147  template<typename Backend, expression_template_option ExpressionTemplates, typename Integer>
148  inline static void compute(state_type<number<Backend, ExpressionTemplates>> &state,
149  Integer difficulty) {
150  state.L = abs(state.form.d);
151  //#FIXME: requires root(state.L, 4) functions
152  state.L = sqrt(state.L);
153  state.L = sqrt(state.L);
154 
155  for (int i = 0; i < difficulty; i++) {
156  policy_type::nudupl(state);
157  policy_type::fast_reduce(state);
158  }
159  }
160 
170  template<typename Backend, expression_template_option ExpressionTemplates>
171  static inline void make_state(state_type<number<Backend, ExpressionTemplates>> &state,
172  const number<Backend, ExpressionTemplates> &discriminant,
173  const number<Backend, ExpressionTemplates> &a = 2,
174  const number<Backend, ExpressionTemplates> &b = 1) {
175  state.form.a = a;
176  state.form.b = b;
177  state.form.c = (b * b - discriminant) / (a * 4);
178  state.form.d = discriminant;
179  fast_reduce(state);
180  }
181 
182  template<typename Backend, expression_template_option ExpressionTemplates>
183  static inline state_type<number<Backend, ExpressionTemplates>>
184  make_state(const number<Backend, ExpressionTemplates> &a,
185  const number<Backend, ExpressionTemplates> &b,
186  const number<Backend, ExpressionTemplates> &discriminant) {
187  state_type<number<Backend, ExpressionTemplates>> state;
188  make_state(state, discriminant, a, b);
189  return state;
190  }
191 #endif
192  };
193  } // namespace vdf
194  } // namespace crypto3
195 } // namespace nil
196 
197 #endif // CRYPTO3_CHIA_HPP
Definition: chia.hpp:33
constexpr nil::crypto3::detail::remove_complex_t< T > abs(T x)
computes the absolute value
Definition: algebra/include/nil/crypto3/algebra/scalar/math.hpp:76
constexpr double sqrt(double x)
computes the square root
Definition: algebra/include/nil/crypto3/algebra/scalar/math.hpp:47
void compute(const NumberType &challenge, Integer difficulty, typename Vdf::template state_type< NumberType > &out)
Definition: vdf/include/nil/crypto3/vdf/algorithm/compute.hpp:39
Definition: pair.hpp:31
Definition: chia_functions.hpp:34