bitcoin-random 0.1.21

random numbers in bitcoin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// ---------------- [ File: bitcoin-random/src/fast_random_context.rs ]
crate::ix!();

/**
  | Fast randomness source. This is seeded
  | once with secure random data, but is
  | completely deterministic and does
  | not gather more entropy after that.
  | 
  | This class is not thread-safe.
  |
  */
#[derive(Debug,Getters,MutGetters,Setters)]
#[getset(get="pub",set="pub",get_mut="pub")]
pub struct FastRandomContext {
    requires_seed: bool,
    rng:           ChaCha20,
    bytebuf:       [u8; 64],
    bytebuf_size:  i32,
    bitbuf:        u64,
    bitbuf_size:   i32,
}

impl Default for FastRandomContext {

    fn default() -> Self {
        let deterministic = false;

        Self::new(deterministic)
    }
}

/**
  | Compatibility with the C++11
  | 
  | UniformRandomBitGenerator concept
  |
  */
pub type FastRandomContextResultType = u64;

pub trait RandRange {

    fn randrange(&mut self, range: u64) -> u64;
}

impl RandRange for FastRandomContext {

    /**
      | Generate a random integer in the range
      | [0..range).
      | 
      | Precondition: range > 0.
      |
      */
    fn randrange(&mut self, mut range: u64) -> u64 {

        assert!(range != 0);

        range -= 1;

        let bits: u64 = count_bits(range);

        while true {

            let ret: u64 = self.randbits(bits.try_into().unwrap());

            if ret <= range {
                return ret;
            }
        }

        unreachable!();
    }
}

impl From<&u256> for FastRandomContext {

    /**
      | Initialize with explicit seed (only
      | for testing)
      |
      */
    fn from(seed: &u256) -> Self {
    
        let mut rng = ChaCha20::default();
        rng.set_key(seed.blob().begin(), 32);

        Self {
            requires_seed: false,
            rng:           rng,
            bytebuf:       [0; 64],
            bytebuf_size:  0,
            bitbuf:        0,
            bitbuf_size:   0,
        }
    }
}

impl RngCore for FastRandomContext {

    fn next_u32(&mut self) -> u32 {
        self.randbits(32).try_into().unwrap()
    }

    fn next_u64(&mut self) -> u64 {

        if self.bytebuf_size < 8 {
            self.fill_byte_buffer();
        }

        let offset: isize = self.bytebuf_size.try_into().unwrap();

        let ret: u64 = readle64(
            unsafe {
                self.bytebuf.as_mut_ptr().add(64).offset(-offset)
            }
        );

        self.bytebuf_size -= 8;

        ret
    }

    fn fill_bytes(&mut self, dest: &mut [u8]) {

        if self.requires_seed {
            self.random_seed();
        }

        self.rng.keystream(dest.as_mut_ptr(), dest.len());
    }

    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
        Ok(self.fill_bytes(dest))
    }
}

impl From<&mut FastRandomContext> for FastRandomContext {

    /**
      | Move a FastRandomContext. If the original
      | one is used again, it will be reseeded.
      |
      */
    fn from(other: &mut FastRandomContext) -> Self {

        let mut x: Self = unsafe { std::mem::zeroed() };

        x.requires_seed = other.requires_seed;
        x.rng           = other.rng.clone();
        x.bytebuf       = other.bytebuf;

        x.bytebuf_size  = other.bytebuf_size;
        x.bitbuf        = other.bitbuf;
        x.bitbuf_size   = other.bitbuf_size;

        other.requires_seed = true;
        other.bytebuf_size  = 0;
        other.bitbuf_size   = 0;

        x
    }
}

impl FastRandomContext {

    pub fn new(deterministic: bool) -> Self {
    
        let mut x = Self {
            requires_seed: !deterministic,
            rng:           ChaCha20::default(),
            bytebuf:       [0; 64],
            bytebuf_size:  0,
            bitbuf:        0,
            bitbuf_size:   0,
        };

        if !deterministic {
            return x;
        }

        let mut seed: u256 = u256::default();

        x.rng.set_key(seed.blob().begin(), 32);

        x
    }
    
    pub fn fill_byte_buffer(&mut self)  {

        //I call this unsafe because we are
        //essentially borrowing self as mut twice
        //
        //the below trick, converting to ptr and
        //back is a hack to silence the borrow
        //czecher
        unsafe {
            let ptr = self.bytebuf.as_mut_slice().as_mut_ptr();
            let len = self.bytebuf.len();

            let slice = unsafe { std::slice::from_raw_parts_mut(ptr,len) };

            self.fill_bytes(slice);

            self.bytebuf_size = size_of_val(&self.bytebuf).try_into().unwrap();
        }
        
    }

    pub fn fill_bit_buffer(&mut self)  {
        
        self.bitbuf = self.rand64();
        self.bitbuf_size = 64;
    }
    
    /**
      | Generate a random 64-bit integer.
      |
      */
    pub fn rand64(&mut self) -> u64 {
        self.next_u64()
    }

    /**
      | Generate a random (bits)-bit integer.
      |
      */
    pub fn randbits(&mut self, bits: i32) -> u64 {
        
        if bits == 0 {
            0

        } else if bits > 32 {
            self.rand64() >> (64 - bits)

        } else {

            if self.bitbuf_size < bits {
                self.fill_bit_buffer();
            }

            let ret: u64 = self.bitbuf & (!0 >> (64 - bits));
            self.bitbuf >>= bits;
            self.bitbuf_size -= bits;
            ret
        }
    }

    /**
      | Generate a random 32-bit integer.
      |
      */
    pub fn rand32(&mut self) -> u32 {
        self.next_u32()
    }

    /**
      | Generate a random boolean.
      |
      */
    pub fn randbool(&mut self) -> bool {
        self.randbits(1) != 0
    }
    
    pub fn min() -> u64 {
        0
    }
    
    pub fn max() -> u64 {
        u64::MAX
    }
    
    #[inline] pub fn invoke(&mut self) -> u64 {
        self.rand64()
    }
    
    pub fn random_seed(&mut self)  {
        
        let seed: u256 = get_rand_hash();
        self.rng.set_key(seed.blob().begin(), 32);
        self.requires_seed = false;
    }
    
    /**
      | generate a random uint256.
      |
      */
    pub fn rand256(&mut self) -> u256 {
        
        if self.bytebuf_size < 32 {
            self.fill_byte_buffer();
        }

        let mut ret = u256::default();

        let offset: isize = self.bytebuf_size.try_into().unwrap();

        unsafe {
            let dst = ret.blob().begin() as *mut c_void;
            let src = self.bytebuf.as_mut_ptr().add(64).offset(-offset);

            libc::memcpy(
                dst, 
                src as *const c_void, 
                32
            );
        }

        self.bytebuf_size -= 32;

        ret
    }
    
    /**
      | Generate random bytes.
      |
      */
    pub fn randbytes(&mut self, len: usize) -> Vec<u8> {
        if self.requires_seed {
            self.random_seed();
        }
        let mut ret = Vec::<u8>::with_capacity(len);
        if len > 0 {
            unsafe {
                self.rng.keystream(ret.as_mut_ptr(), len);
                ret.set_len(len); // <-- add this
            }
        }
        ret
    }
}

#[cfg(test)]
mod fast_random_context_spec {
    use super::*;

    #[traced_test]
    fn deterministic_from_same_seed_is_identical() {
        let seed = u256::default(); // explicit seed (all-zero is fine for test determinism)
        let mut a = FastRandomContext::from(&seed);
        let mut b = FastRandomContext::from(&seed);

        // A few mixed draws from both contexts should match 1:1.
        assert_eq!(a.rand32(), b.rand32());
        assert_eq!(a.rand64(), b.rand64());

        // randbits edge cases
        assert_eq!(a.randbits(0), 0);
        assert_eq!(b.randbits(0), 0);

        let x1 = a.randbits(1);
        let y1 = b.randbits(1);
        assert_eq!(x1, y1);
        assert!(x1 < 2);

        let x32 = a.randbits(32);
        let y32 = b.randbits(32);
        assert_eq!(x32, y32);
        assert!(x32 < (1u64 << 32));

        let x33 = a.randbits(33);
        let y33 = b.randbits(33);
        assert_eq!(x33, y33);
        assert!(x33 < (1u64 << 33));
    }

    #[traced_test]
    fn randrange_is_within_bounds_and_handles_small_ranges() {
        let mut ctx = FastRandomContext::new(true); // deterministic
        for r in [1u64, 2, 3, 10, 1_000_000] {
            for _ in 0..100 {
                let v = ctx.randrange(r);
                assert!(v < r, "v={v} should be < {r}");
            }
        }
    }

    #[traced_test]
    fn next_u64_consumes_from_internal_buffer() {
        let mut ctx = FastRandomContext::new(true);
        let a = ctx.next_u64();
        let b = ctx.next_u64();
        // There is no requirement they differ, but deterministic ChaCha20 with a fixed key will.
        assert_ne!(a, b);
    }

    #[traced_test]
    fn fill_bytes_writes_requested_length() {
        let mut ctx = FastRandomContext::new(true);
        let mut buf = [0u8; 17];
        ctx.fill_bytes(&mut buf);
        // We can at least check that the buffer changed (deterministically).
        assert!(buf.iter().any(|&x| x != 0));
    }

    #[traced_test]
    fn move_semantics_mark_source_for_reseed_and_preserve_state_in_target() {
        let mut src = FastRandomContext::new(true);
        // Pull a value to force some internal state to be used.
        let _ = src.rand32();

        // Move into `dst`.
        let dst = FastRandomContext::from(&mut src);

        // Source marked for reseed and emptied bit/byte buffers.
        assert!(src.requires_seed);
        assert_eq!(src.bytebuf_size, 0);
        assert_eq!(src.bitbuf_size, 0);

        // Destination should be usable without reseed.
        // (We can’t assert exact bytes here without duplicating the move algorithm;
        // it’s sufficient that this does not panic and produces a value.)
        let _ = &dst; // move worked; dst is valid
    }

    #[traced_test]
    fn randbytes_returns_len_and_fills_data() {
        let mut ctx = FastRandomContext::new(true);
        let want = 64usize;
        let v = ctx.randbytes(want);
        assert_eq!(v.len(), want);
        assert!(v.iter().any(|&x| x != 0));
    }
}