replace runtime checks with compile time checks

This commit is contained in:
Mark Spanbroek 2024-10-10 10:57:58 +02:00
parent 55346e5164
commit 30083a16be

View File

@ -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;
#---------------------------------------