std::rand::random::<Talk>() Huon Wilson December 18, 2014 - - PowerPoint PPT Presentation

std rand random talk
SMART_READER_LITE
LIVE PREVIEW

std::rand::random::<Talk>() Huon Wilson December 18, 2014 - - PowerPoint PPT Presentation

std::rand::random::<Talk>() Huon Wilson December 18, 2014 http://huonw.github.io/rand-dec14 Digital Randomness Digital Randomness A sequence of bits, e.g. 3 11011110 11111000 01001010 00111100 . . . , Digital Randomness A sequence


slide-1
SLIDE 1

std::rand::random::<Talk>()

Huon Wilson December 18, 2014 http://huonw.github.io/rand-dec14 

slide-2
SLIDE 2

Digital Randomness

slide-3
SLIDE 3

Digital Randomness

A sequence of bits, e.g. 11011110 11111000 01001010 00111100 . . . ,

3

slide-4
SLIDE 4

Digital Randomness

A sequence of bits, e.g. 11011110

  • 222

11111000

  • 248

01001010

  • 74

00111100

  • 60

. . . , Usually generated/consumed in chunks.

3

slide-5
SLIDE 5

Why?

slide-6
SLIDE 6

Why?

Lots of uses for randomness:

▶ simulations: scientifjc, testing ▶ games: shuffming cards, collecting loot ▶ security: keys, session IDs

All want “high quality” random numbers.

5

slide-7
SLIDE 7

What is quality?

slide-8
SLIDE 8

What is quality?

It depends! Usually:

▶ uniformity: every bit has 50% chance of being

0 or 1

▶ unpredictability: the value of a bit can’t be

guessed base on the value of others

7

slide-9
SLIDE 9

How can a deterministic machine be random?

slide-10
SLIDE 10

How can a deterministic machine be random?

Conventional computer RNGs follow patterns. initial seed state0 state1 state2 … random number random number update update The seed controls which pattern.

9

slide-11
SLIDE 11

How can a deterministic machine be random?

Compute the seed (or state), and you know the full stream. RNGs for cryptography need to ensure the seed/state is hard to compute. (Or be true random number generators, e.g. measure nuclear decay.) Bad: XorShift. Good: ChaCha.

10

slide-12
SLIDE 12

Rust

slide-13
SLIDE 13

Rust

Thread-safety (by default)

slide-14
SLIDE 14

Rust Thread-safety

Often a pervasive use of a single global RNG. Languages like C, R, Julia (recently improved in e.g. JuliaLang/julia#8832 ). Automatically guaranteed this isn’t a problem in Rust!

13

slide-15
SLIDE 15

Rust

SIMD: dSFMT

slide-16
SLIDE 16

Rust SIMD: dSFMT

__m128i v, w, x, y, z; // ... x = a->si; z = _mm_slli_epi64(x, DSFMT_SL1); z = _mm_xor_si128(z, b->si); y = _mm_xor_si128(y, z); v = _mm_srli_epi64(y, DSFMT_SR); w = _mm_and_si128(y, sse2_param_mask.i128); v = _mm_xor_si128(v, x); v = _mm_xor_si128(v, w); r->si = v; u->si = y; http://www.math.sci.hiroshima-u.ac.jp/~ m-mat/MT/SFMT/  // ... let y = (a << SSE2_SL) ^ b ^ y; let v = (y >> SSE2_SR) ^ (y & SSE2_PARAMS_MASK) ^ a; *r = v; *u = y; https://github.com/Grieverheart/dsfmt-rs 

15

slide-17
SLIDE 17

Rust SIMD: dSFMT

__m128i v, w, x, y, z; // ... x = a->si; z = _mm_slli_epi64(x, DSFMT_SL1); z = _mm_xor_si128(z, b->si); y = _mm_xor_si128(y, z); v = _mm_srli_epi64(y, DSFMT_SR); w = _mm_and_si128(y, sse2_param_mask.i128); v = _mm_xor_si128(v, x); v = _mm_xor_si128(v, w); r->si = v; u->si = y; http://www.math.sci.hiroshima-u.ac.jp/~ m-mat/MT/SFMT/  // ... let y = (a << SSE2_SL) ^ b ^ y; let v = (y >> SSE2_SR) ^ (y & SSE2_PARAMS_MASK) ^ a; *r = v; *u = y; https://github.com/Grieverheart/dsfmt-rs 

15

slide-18
SLIDE 18

Rust SIMD: dSFMT

Creates essentially the same ASM. Benchmark:

let mut rng: dsfmt::DSFMTRng = SeedableRng::from_seed(12345u32); let mut sum = 0_f64; for _ in range(0u32, 1_000_000_000) { sum += rng.gen() } println!("{}", sum)

C 500014293.513722 User time: 1.86s Rust 500014293.513722 User time: 1.93s

16

slide-19
SLIDE 19

Rust

Traits

slide-20
SLIDE 20

Rust Traits

impl Rand for u8 impl Rand for u16 // ... Get an number with a random value: use std::rand; let x: u8 = rand::random(); let y: u16 = rand::random();

18

slide-21
SLIDE 21

Rust Traits

impl Rand for XorShiftRng impl Rand for ChaChaRng // ... Get an RNG with a random seed: use std::rand; let x: rand::XorShiftRng = rand::random(); let y: rand::ChaChaRng = rand::random();

19

slide-22
SLIDE 22

Rust

Community!

slide-23
SLIDE 23

Rust Community!

E.g.

▶ Careful analysis of documentation/use of

/dev/[u]random

▶ Implement Bernstein’s ChaCha RNG

(http://cr.yp.to/chacha.html , sneves: #17387 )

▶ Update std::rand to use the new, better

getrandom(2) syscall on Linux, when available (strcat and klutzy: #18664 )

21

slide-24
SLIDE 24

Questions?