All files / random / _seed_bytes_from_uint64.ts

100.00% Branches 0/0
100.00% Lines 9/9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
x15
 
 
x15
 
 
 
 
 
x15
x15
x15
 
x440
 
 
x110
x110
x110


















// Copyright 2018-2025 the Deno authors. MIT license.
import { Pcg32 } from "./_pcg32.ts";

/** Initial increment for the PCG32 algorithm. Only used during seeding. */
const INITIAL_INCREMENT = 11634580027462260723n;

/**
 * Write entropy generated from a scalar bigint seed into the provided Uint8Array, for use as a seed.
 * Modified from https://github.com/rust-random/rand/blob/f7bbcca/rand_core/src/lib.rs#L359-L388
 */
export function seedBytesFromUint64(
  u64: bigint,
  bytes: Uint8Array,
): Uint8Array {
  return new Pcg32({ state: u64, increment: INITIAL_INCREMENT })
    // We advance the state first (to get away from the input value,
    // in case it has low Hamming Weight).
    .step()
    .getRandomValues(bytes);
}