Add more tests for multiplexer

This commit is contained in:
mratsim 2018-12-01 18:03:52 +01:00
parent d545147b0b
commit cae9f743d3
2 changed files with 9 additions and 3 deletions

View File

@ -80,7 +80,7 @@ func `not`*(ctl: HardBool): HardBool {.inline.}=
## Negate a constant-time boolean
(type result)(ctl.undistinct xor (type ctl.undistinct)(1))
template select*[T: HardBase](ctl: HardBool[T], x, y: T): T =
template mux*[T: HardBase](ctl: HardBool[T], x, y: T): T =
## Multiplexer / selector
## Returns x if ctl == 1
## else returns y

View File

@ -167,7 +167,7 @@ suite "Hardened booleans":
bool(hard(10'u32) >= hard(5'u32)) == true
bool(hard(10'u32) >= hard(0xFFFFFFFF'u32)) == false
test "Multiplexer/selector - select(ctl, x, y) <=> ctl? x: y":
test "Multiplexer/selector - mux(ctl, x, y) <=> ctl? x: y":
let u = 10'u32.hard
let v = 20'u32.hard
let w = 5'u32.hard
@ -176,5 +176,11 @@ suite "Hardened booleans":
let n = hfalse(uint32)
check:
bool(select(y, u, v) == u)
bool(mux(y, u, v) == u)
bool(mux(n, u, v) == v)
bool(mux(y, u, w) == u)
bool(mux(n, u, w) == w)
bool(mux(y, v, w) == v)
bool(mux(n, v, w) == w)