25 #ifndef CRYPTO3_VDF_CHIA_HPP
26 #define CRYPTO3_VDF_CHIA_HPP
37 #if defined(CRYPTO3_VDF_GMP) || defined(CRYPTO3_VDF_MPIR)
39 template<
typename T = mpz_t>
51 template<
typename Integer,
typename NumberType = mpz_t>
52 static inline void make_state(state_type<NumberType> &state, NumberType discriminant) {
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);
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);
74 template<
typename T,
typename I>
75 inline static void compute(state_type<T> &state, I itr) {
76 policy_type::discriminant_generator(state, d);
78 mpz_abs(state.L, state.form.d);
79 mpz_root(state.L, state.L, 4);
81 for (
int i = 0; i < itr; i++) {
82 policy_type::nudupl(state);
83 policy_type::fast_reduce(state);
87 #elif defined(CRYPTO3_VDF_FLINT)
89 template<
typename T = fmpz_t>
101 template<
typename Integer,
typename NumberType = fmpz_t>
102 static inline void make_state(state_type<NumberType> &state, NumberType discriminant) {
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);
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);
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);
128 for (
int i = 0; i < itr; i++) {
129 policy_type::nudupl(state);
130 policy_type::fast_reduce(state);
134 #elif defined(CRYPTO3_VDF_BOOST)
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);
152 state.L =
sqrt(state.L);
153 state.L =
sqrt(state.L);
155 for (
int i = 0; i < difficulty; i++) {
156 policy_type::nudupl(state);
157 policy_type::fast_reduce(state);
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) {
177 state.form.c = (b * b - discriminant) / (a * 4);
178 state.form.d = discriminant;
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);
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: chia_functions.hpp:34
Definition: chia_policy.hpp:88