random_element.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_ALGEBRA_RANDOM_ELEMENT_HPP
27 #define CRYPTO3_ALGEBRA_RANDOM_ELEMENT_HPP
28 
30 
31 #include <nil/crypto3/multiprecision/debug_adaptor.hpp>
32 #include <nil/crypto3/multiprecision/cpp_bin_float.hpp>
33 #include <nil/crypto3/multiprecision/cpp_int.hpp>
34 
35 #include <boost/core/ignore_unused.hpp>
36 
37 #include <boost/random/independent_bits.hpp>
38 #include <boost/random/discard_block.hpp>
39 #include <boost/random/xor_combine.hpp>
40 #include <boost/random/mersenne_twister.hpp>
41 #include <boost/random/random_number_generator.hpp>
42 #include <boost/random/uniform_int.hpp>
43 #include <boost/random/uniform_smallint.hpp>
44 #include <boost/random/discrete_distribution.hpp>
45 #include <boost/random/random_device.hpp>
46 
47 #include <random>
48 
49 namespace nil {
50  namespace crypto3 {
51  namespace algebra {
52 
53  template<
54  typename FieldType,
55  typename DistributionType = boost::random::uniform_int_distribution<typename FieldType::integral_type>,
56  typename GeneratorType = boost::random::mt19937>
57  typename std::enable_if<is_field<FieldType>::value && !(is_extended_field<FieldType>::value),
58  typename FieldType::value_type>::type
60 
61  using field_type = FieldType;
62  using distribution_type = DistributionType;
63  using generator_type = GeneratorType;
64 
65  distribution_type d(0, field_type::modulus);
66 
67  boost::random_device rd;
68  // rd.seed(time(0));
69 
70  typename field_type::value_type value(d(rd));
71 
72  return value;
73  }
74 
75  template<
76  typename FieldType,
77  typename DistributionType = boost::random::uniform_int_distribution<typename FieldType::integral_type>,
78  typename GeneratorType = boost::random::mt19937>
79  typename std::enable_if<is_field<FieldType>::value && is_extended_field<FieldType>::value,
80  typename FieldType::value_type>::type
82 
83  using field_type = FieldType;
84  using distribution_type = DistributionType;
85  using generator_type = GeneratorType;
86 
87  typename field_type::value_type::data_type data;
88  const std::size_t data_dimension = field_type::arity / field_type::underlying_field_type::arity;
89 
90  for (int n = 0; n < data_dimension; ++n) {
91  data[n] =
92  random_element<typename FieldType::underlying_field_type, distribution_type, generator_type>();
93  }
94 
95  return typename field_type::value_type(data);
96  }
97 
98  template<typename CurveGroupType,
99  typename DistributionType =
100  boost::random::uniform_int_distribution<typename CurveGroupType::field_type::integral_type>,
101  typename GeneratorType = boost::random::mt19937>
102  typename std::enable_if<is_curve_group<CurveGroupType>::value, typename CurveGroupType::value_type>::type
104 
105  using curve_type = typename CurveGroupType::curve_type;
106  using field_type = typename curve_type::scalar_field_type;
107  using distribution_type = boost::random::uniform_int_distribution<typename field_type::integral_type>;
108  using generator_type = GeneratorType;
109 
110  return random_element<typename curve_type::scalar_field_type, distribution_type, generator_type>() *
111  CurveGroupType::value_type::one();
112  }
113 
114  } // namespace algebra
115  } // namespace crypto3
116 } // namespace nil
117 #endif // CRYPTO3_ALGEBRA_RANDOM_ELEMENT_HPP
std::enable_if< is_field< FieldType >::value &&!(is_extended_field< FieldType >::value), typename FieldType::value_type >::type random_element()
Definition: random_element.hpp:59
Definition: pair.hpp:31
static const bool value
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:106