From d2c534a3c88ee733fe83b2783813567a26dfe856 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 14 Jun 2024 16:09:01 +0800 Subject: [PATCH] document the entropy compression method compression helps us reduce the amount of data we have to shuttle between the hardware interface and the rest of the world. It takes more samples from the TRNG and represents them in a compressed form that effectively increases the bits per byte of entropy. It does not increase the *rate* of entropy generation -- while each byte has more entropy, the bytes come proportionally slower. However, this is a reasonable trade-off because kernel space calls to fetch the entropy feed are expensive, and what this does is effectively amortize the price of a syscall: the time per syscall is longer, but you get more entropy out per syscall, thus effectively reducing the needed overhead to get a certain amount of entropy. --- libs/cramium-hal/src/sce/trng.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libs/cramium-hal/src/sce/trng.rs b/libs/cramium-hal/src/sce/trng.rs index b6dae631c..da5abbd5f 100644 --- a/libs/cramium-hal/src/sce/trng.rs +++ b/libs/cramium-hal/src/sce/trng.rs @@ -132,6 +132,19 @@ impl Trng { } } + // Perform entropy compression. The TRNG itself is a sparse-1 oracle, which will + // occasionally emit a 1 in a stream of 0's. The 1 itself has some periodicity + // to it, but at a period unrelated to the system clock. The algorithm is basically + // as follows: + // - `rng_var` is a counter [0-255] that spins according to the rate of this loop + // - inspect the TRNG bitstream, bit-by-bit, from LSB to MSB. + // - Every inspection, increment `rng_var` + // - If the inspection result is 1, store `rng_var` as a compressed entropy bit + // - If 0, keep searching; do not store, but also increment all the loop variables + // The result is a stream of at least uniformly distributed numbers. The resulting + // stream does have some long-term periodic behaviors in it, but it is also not entirely + // predictable. Basically, the randomness seems to fluctuate in and out based on how + // much noise is actually being coupled into the TRNG circuit. let mut rng_var: u8 = 0; while self.csr.r(utra::trng::SFR_SR) & Status::BUFREADY.bits() == 0 {} let mut sample = self.csr.r(utra::trng::SFR_BUF);