From bd3e8db852186d4ab9d5dda890d1cd52389b1254 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Mon, 29 Jul 2024 11:54:53 +0200 Subject: [PATCH] Faster validate_prng_nonce --- common/crapto1/crapto1.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/common/crapto1/crapto1.c b/common/crapto1/crapto1.c index 60ae72dad..4a2e9e791 100644 --- a/common/crapto1/crapto1.c +++ b/common/crapto1/crapto1.c @@ -430,10 +430,13 @@ int nonce_distance(uint32_t from, uint32_t to) { * false = hardend prng */ bool validate_prng_nonce(uint32_t nonce) { - // init prng table: - if (nonce_distance(nonce, nonce) == -1) - return false; - return ((65535 - dist[nonce >> 16] + dist[nonce & 0xffff]) % 65535) == 16; + uint16_t x = nonce >> 16; + x = (x & 0xff) << 8 | x >> 8; + for (uint8_t i = 0; i<16; i++) { + x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15; + } + x = (x & 0xff) << 8 | x >> 8; + return x == (nonce & 0xFFFF); } static uint32_t fastfwd[2][8] = {