From 30083a16be3d2aab2f43692e398c3647fe7baaff Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 10 Oct 2024 10:57:58 +0200 Subject: [PATCH] replace runtime checks with compile time checks --- goldilocks_hash/monolith/sponge.nim | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/goldilocks_hash/monolith/sponge.nim b/goldilocks_hash/monolith/sponge.nim index fbb454a..5c00b50 100644 --- a/goldilocks_hash/monolith/sponge.nim +++ b/goldilocks_hash/monolith/sponge.nim @@ -20,16 +20,21 @@ type state: F12 lenModRate: uint -func numberOfBits(T: static typedesc): int = - if T is F: return 63 - if T is byte: return 8 - if T is bool: return 1 - raiseAssert("unsupported input type for sponge construction" ) +func numberOfBits(T: static typedesc): int {.compileTime.} = + when T is F: + 63 + elif T is byte: + 8 + elif T is bool: + 1 + else: + {.error: "unsupported input type for sponge construction".} func initialize[T: static typedesc, rate: static int](sponge: var Sponge[T,rate]) = - doAssert(rate >= 1 and rate <= 8, "with t=12, rate must be at most 8 (and positive)" ) - let nbits = numberOfBits(T) - let IV = toF( 0x10000*uint64(nbits) + 0x100*12 + uint64(rate) ) # domain separation IV := (65536*nbits + 256*t + r) + when not rate >= 1 and rate <= 8: + {.error: "with t=12, rate must be at most 8 (and positive)".} + const nbits = numberOfBits(T) + const IV = toF( 0x10000*uint64(nbits) + 0x100*12 + uint64(rate) ) # domain separation IV := (65536*nbits + 256*t + r) sponge.state[8] = IV; #---------------------------------------