1cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines//===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
2cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines//
3cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines//                     The LLVM Compiler Infrastructure
4cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines//
5cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines// This file is distributed under the University of Illinois Open Source
6cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines// License. See LICENSE.TXT for details.
7cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines//
8cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines//===----------------------------------------------------------------------===//
9cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines//
10cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines// This file implements random number generation (RNG).
11cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines// The current implementation is NOT cryptographically secure as it uses
12cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines// the C++11 <random> facilities.
13cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines//
14cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines//===----------------------------------------------------------------------===//
15cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
16cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines#define DEBUG_TYPE "rng"
17cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines#include "llvm/Support/RandomNumberGenerator.h"
18cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines#include "llvm/Support/CommandLine.h"
19cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines#include "llvm/Support/Debug.h"
20cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
21cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesusing namespace llvm;
22cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
23cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines// Tracking BUG: 19665
24cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines// http://llvm.org/bugs/show_bug.cgi?id=19665
25cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines//
26cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines// Do not change to cl::opt<uint64_t> since this silently breaks argument parsing.
27cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesstatic cl::opt<unsigned long long>
28cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen HinesSeed("rng-seed", cl::value_desc("seed"),
29cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines     cl::desc("Seed for the random number generator"), cl::init(0));
30cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
31cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen HinesRandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
32cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  DEBUG(
33cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    if (Seed == 0)
34cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      errs() << "Warning! Using unseeded random number generator.\n"
35cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  );
36cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
37cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  // Combine seed and salt using std::seed_seq.
38cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  // Entropy: Seed-low, Seed-high, Salt...
39cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::vector<uint32_t> Data;
40cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  Data.reserve(2 + Salt.size()/4 + 1);
41cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  Data.push_back(Seed);
42cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  Data.push_back(Seed >> 32);
43cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
44cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  uint32_t Pack = 0;
45cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  for (size_t I = 0; I < Salt.size(); ++I) {
46cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    Pack <<= 8;
47cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    Pack += Salt[I];
48cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
49cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    if (I%4 == 3)
50cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      Data.push_back(Pack);
51cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  }
52cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  Data.push_back(Pack);
53cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
54cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::seed_seq SeedSeq(Data.begin(), Data.end());
55cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  Generator.seed(SeedSeq);
56cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines}
57cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
58cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesuint64_t RandomNumberGenerator::next(uint64_t Max) {
59cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::uniform_int_distribution<uint64_t> distribution(0, Max - 1);
60cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  return distribution(Generator);
61cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines}
62