Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
derive.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//derive.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//derive.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with derive.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
55
56#pragma once
57
58#include <cstdint>
59
61#include "bertini2/mpfr_extensions.hpp" // mpz_int / real_mp (NOT num_traits: that would cycle through random.hpp)
63
64namespace bertini {
65namespace records {
66
68constexpr char RandomDerivationVersion[] = "b2rand/1";
69
78{
79public:
82 DrawStream() = default;
83
86 void Reseed(std::uint64_t master, std::uint64_t domain, std::uint64_t index);
87
89 std::uint64_t Uint64();
90
92 double UnitDouble();
93
96
98 mpz_int Bits(unsigned num_bits);
99
102
105 real_mp UnitRealMp(unsigned digits);
106
107private:
108 // Refill block_ with SHA-256(key || counter) and advance the counter.
109 void NextBlock();
110 // The next unconsumed stream byte (self-seeding from entropy if never seeded).
111 unsigned char NextByte();
112
113 detail::Digest256 key_;
114 std::uint64_t counter_ = 0;
115 unsigned char block_[32];
116 unsigned offset_ = 32;
117 bool seeded_ = false;
118};
119
128
129} // namespace records
130} // namespace bertini
A deterministic, fully-specified random stream (SHA-256 counter mode).
Definition derive.hpp:78
double UnitDouble()
Uniform on [0,1), exactly 53 bits: (Uint64 >> 11) * 2^-53.
real_mp UnitRealMp(unsigned digits)
Uniform on [0,1) at digits decimal digits: ldexp(Bits(k), -k) with k = ceil(digits*log2(10)) + 1,...
DrawStream()=default
Construct unseeded; the first draw self-seeds from entropy (unseeded runs stay random).
mpz_int IntSymmetric(mpz_int const &bound)
Uniform integer on [-bound, bound], by rejection over Bits(bitlen(2*bound)).
std::uint64_t Uint64()
The next 8 stream bytes as a big-endian unsigned 64-bit integer.
mpz_int Bits(unsigned num_bits)
The next n bits as a nonnegative integer (big-endian bytes, masked).
double SymmetricDouble()
Uniform on [-1,1]: 2*UnitDouble() - 1 (both operations exact).
void Reseed(std::uint64_t master, std::uint64_t domain, std::uint64_t index)
Rekey the stream from (master seed, domain tag, stream index) under b2rand/1 and restart its counter.
Dependency-free declarations of the pinned draw primitives (ADR-0044).
Extensions to the Boost.Multiprecision library.
Definition config_encoding.hpp:66
DrawStream & ThreadDrawStream()
This thread's pinned draw stream — the source of every identity-relevant random draw in bertini2.
constexpr char RandomDerivationVersion[]
The version tag of the draw-derivation scheme; bump on any algorithm change.
Definition derive.hpp:68
Definition algorithm_builder.hpp:36
boost::multiprecision::number< boost::multiprecision::backends::gmp_int, boost::multiprecision::et_on > mpz_int
The arbitrary-precision integer type.
Definition mpfr_extensions.hpp:54
boost::multiprecision::number< boost::multiprecision::mpfr_float_backend< 0 >, boost::multiprecision::et_on > real_mp
The arbitrary-precision real number type.
Definition mpfr_extensions.hpp:53
Self-contained SHA-256 (FIPS 180-4) and the Digest256 value type.
A 256-bit content digest – the output of Sha256, used as a persistent identity key.
Definition sha256.hpp:53