2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::device) ircd::rand::device
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
// on linux: uses RDRND or /dev/urandom
|
|
|
|
// on windows: TODO: verify construction source
|
2017-09-23 01:29:06 +02:00
|
|
|
};
|
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::mt) ircd::rand::mt
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
device()
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::dict::alnum) ircd::rand::dict::alnum
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::dict::alpha) ircd::rand::dict::alpha
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::dict::upper) ircd::rand::dict::upper
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::dict::lower) ircd::rand::dict::lower
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::dict::numeric) ircd::rand::dict::numeric
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
"0123456789"
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2020-10-07 01:01:53 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::rand::fill(const mutable_buffer &out)
|
|
|
|
{
|
|
|
|
uint64_t *const __restrict__ val
|
|
|
|
{
|
|
|
|
reinterpret_cast<uint64_t *>(data(out))
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr size_t word_size
|
|
|
|
{
|
|
|
|
sizeof(*val)
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i(0);
|
|
|
|
for(; i < size(out) / word_size; ++i)
|
|
|
|
val[i] = integer();
|
|
|
|
|
|
|
|
for(size_t j(i * word_size); j < size(out) % word_size; ++j)
|
|
|
|
out[j] = integer();
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
ircd::string_view
|
2020-10-07 00:46:36 +02:00
|
|
|
ircd::rand::string(const mutable_buffer &out,
|
|
|
|
const std::string &dict)
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
std::uniform_int_distribution<size_t> dist
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
0, dict.size() - 1
|
2017-09-23 01:29:06 +02:00
|
|
|
};
|
|
|
|
|
2018-02-15 21:53:00 +01:00
|
|
|
std::generate(data(out), data(out) + size(out), [&dict, &dist]
|
2017-09-30 01:35:55 +02:00
|
|
|
{
|
2020-10-07 00:46:36 +02:00
|
|
|
return char(dict.at(dist(mt)));
|
2017-09-30 01:35:55 +02:00
|
|
|
});
|
|
|
|
|
2018-02-15 21:53:00 +01:00
|
|
|
return out;
|
2017-09-23 01:29:06 +02:00
|
|
|
}
|
2020-10-07 11:59:30 +02:00
|
|
|
|
|
|
|
template<>
|
|
|
|
ircd::u512x1
|
|
|
|
ircd::rand::vector()
|
|
|
|
{
|
|
|
|
return u64x8
|
|
|
|
{
|
|
|
|
integer(), integer(),
|
|
|
|
integer(), integer(),
|
|
|
|
integer(), integer(),
|
|
|
|
integer(), integer(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
ircd::u256x1
|
|
|
|
ircd::rand::vector()
|
|
|
|
{
|
|
|
|
return u64x4
|
|
|
|
{
|
|
|
|
integer(), integer(),
|
|
|
|
integer(), integer(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
ircd::u128x1
|
|
|
|
ircd::rand::vector()
|
|
|
|
{
|
|
|
|
return u64x2
|
|
|
|
{
|
|
|
|
integer(), integer()
|
|
|
|
};
|
|
|
|
}
|