2018-07-24 14:52:18 +00:00
|
|
|
# hardy
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2018-12-01 15:49:45 +00:00
|
|
|
import unittest, random, math,
|
2018-09-05 04:38:34 +00:00
|
|
|
../hardy
|
2018-07-24 14:52:18 +00:00
|
|
|
|
2018-12-01 15:49:45 +00:00
|
|
|
# Random seed for reproducibility
|
|
|
|
randomize(0xDEADBEEF)
|
|
|
|
|
2018-12-01 15:32:21 +00:00
|
|
|
template undistinct[T](x: HardBase[T]): T =
|
|
|
|
T(x)
|
|
|
|
|
|
|
|
suite "Hardened unsigned integers":
|
|
|
|
test "High - getting the biggest representable number":
|
|
|
|
check:
|
|
|
|
high(HardBase[byte]).undistinct == 0xFF.byte
|
|
|
|
high(HardBase[uint8]).undistinct == 0xFF.uint8
|
|
|
|
|
|
|
|
high(HardBase[uint16]).undistinct == 0xFFFF.uint16
|
|
|
|
high(HardBase[uint32]).undistinct == 0xFFFFFFFF.uint32
|
|
|
|
high(HardBase[uint64]).undistinct == 0xFFFFFFFF_FFFFFFFF.uint64
|
|
|
|
|
|
|
|
test "bitwise `and`, `or`, `xor`, `not`":
|
2018-12-01 15:49:45 +00:00
|
|
|
let x1 = rand(high(int)).uint64
|
|
|
|
let y1 = rand(high(int)).uint64
|
|
|
|
let x2 = rand(high(int)).uint64
|
|
|
|
let y2 = rand(high(int)).uint64
|
|
|
|
let x3 = rand(high(int)).uint64
|
|
|
|
let y3 = rand(high(int)).uint64
|
2018-12-01 15:32:21 +00:00
|
|
|
template bitwise_check(op: untyped): untyped =
|
|
|
|
block:
|
|
|
|
check:
|
|
|
|
op(hard(0'u32), hard(0'u32)).undistinct == op(0'u32, 0'u32)
|
|
|
|
op(hard(0'u32), hard(1'u32)).undistinct == op(0'u32, 1'u32)
|
|
|
|
op(hard(1234'u64), hard(5678'u64)).undistinct == op(1234'u64, 5678'u64)
|
|
|
|
|
|
|
|
op(x1.hard, y1.hard).undistinct == op(x1, y1)
|
|
|
|
op(x2.hard, y2.hard).undistinct == op(x2, y2)
|
|
|
|
op(x3.hard, y3.hard).undistinct == op(x3, y3)
|
|
|
|
bitwise_check(`and`)
|
|
|
|
bitwise_check(`or`)
|
|
|
|
bitwise_check(`xor`)
|
|
|
|
|
2018-07-24 14:52:18 +00:00
|
|
|
block:
|
2018-12-01 15:32:21 +00:00
|
|
|
check:
|
|
|
|
not(hard(0'u32)).undistinct == not 0'u32
|
|
|
|
not(hard(1'u32)).undistinct == not 1'u32
|
|
|
|
not(hard(1234'u64)).undistinct == not 1234'u64
|
2018-12-01 17:01:41 +00:00
|
|
|
not(hard(5678'u64)).undistinct == not 5678'u64
|
2018-12-01 15:32:21 +00:00
|
|
|
not(hard(x1)).undistinct == not x1
|
|
|
|
not(hard(x2)).undistinct == not x2
|
|
|
|
not(hard(x3)).undistinct == not x3
|
|
|
|
not(hard(y1)).undistinct == not y1
|
|
|
|
not(hard(y2)).undistinct == not y2
|
|
|
|
not(hard(y3)).undistinct == not y3
|
2018-12-01 15:49:45 +00:00
|
|
|
|
|
|
|
test "Logical shifts":
|
|
|
|
let x1 = rand(high(int)).uint64
|
|
|
|
let y1 = rand(high(int)).uint64
|
|
|
|
let x2 = rand(high(int)).uint64
|
|
|
|
let y2 = rand(high(int32)).uint64
|
|
|
|
let x3 = rand(high(int32)).uint64
|
|
|
|
let y3 = rand(high(int32)).uint64
|
|
|
|
|
|
|
|
let s1 = rand(10)
|
|
|
|
let s2 = rand(10)
|
|
|
|
let s3 = rand(10)
|
|
|
|
|
|
|
|
template shift_check(op: untyped): untyped =
|
|
|
|
block:
|
|
|
|
check:
|
|
|
|
op(hard(0'u32), 1).undistinct == op(0'u32, 1)
|
|
|
|
op(hard(1'u32), 2).undistinct == op(1'u32, 2)
|
|
|
|
op(hard(1234'u64), 3).undistinct == op(1234'u64, 3)
|
|
|
|
op(hard(2'u64^30), 1).undistinct == op(2'u64^30, 1)
|
|
|
|
op(hard(2'u64^31 + 1), 1).undistinct == op(2'u64^31 + 1, 1)
|
|
|
|
op(hard(2'u64^32), 1).undistinct == op(2'u64^32, 1)
|
|
|
|
|
|
|
|
op(x1.hard, s1).undistinct == op(x1, s1)
|
|
|
|
op(x2.hard, s2).undistinct == op(x2, s2)
|
|
|
|
op(x3.hard, s3).undistinct == op(x3, s3)
|
|
|
|
|
|
|
|
|
|
|
|
op(y1.hard, s1).undistinct == op(y1, s1)
|
|
|
|
op(y2.hard, s2).undistinct == op(y2, s2)
|
|
|
|
op(y3.hard, s3).undistinct == op(y3, s3)
|
|
|
|
|
|
|
|
shift_check(`shl`)
|
|
|
|
shift_check(`shr`)
|
|
|
|
|
|
|
|
|
|
|
|
test "Operators `+`, `-`, `*`":
|
|
|
|
let x1 = rand(high(int)).uint64
|
|
|
|
let y1 = rand(high(int)).uint64
|
|
|
|
let x2 = rand(high(int)).uint64
|
|
|
|
let y2 = rand(high(int)).uint64
|
|
|
|
let x3 = rand(high(int)).uint64
|
|
|
|
let y3 = rand(high(int)).uint64
|
|
|
|
template operator_check(op: untyped): untyped =
|
|
|
|
block:
|
|
|
|
check:
|
|
|
|
op(hard(0'u32), hard(0'u32)).undistinct == op(0'u32, 0'u32)
|
|
|
|
op(hard(0'u32), hard(1'u32)).undistinct == op(0'u32, 1'u32)
|
|
|
|
op(hard(1234'u64), hard(5678'u64)).undistinct == op(1234'u64, 5678'u64)
|
|
|
|
|
|
|
|
op(x1.hard, y1.hard).undistinct == op(x1, y1)
|
|
|
|
op(x2.hard, y2.hard).undistinct == op(x2, y2)
|
|
|
|
op(x3.hard, y3.hard).undistinct == op(x3, y3)
|
|
|
|
operator_check(`+`)
|
|
|
|
operator_check(`-`)
|
|
|
|
operator_check(`*`)
|
2018-12-01 16:04:55 +00:00
|
|
|
|
|
|
|
test "Unary `-`, returning the 2-complement of an unsigned integer":
|
|
|
|
let x1 = rand(high(int)).uint64
|
|
|
|
let y1 = rand(high(int)).uint64
|
|
|
|
let x2 = rand(high(int)).uint64
|
|
|
|
let y2 = rand(high(int)).uint64
|
|
|
|
let x3 = rand(high(int)).uint64
|
|
|
|
let y3 = rand(high(int)).uint64
|
|
|
|
check:
|
|
|
|
(-hard(0'u32)).undistinct == 0
|
|
|
|
(-high(HardBase[uint32])).undistinct == 1'u32
|
|
|
|
(-hard(0x80000000'u32)).undistinct == 0x80000000'u32 # This is low(int32) == 0b10000..0000
|
|
|
|
|
|
|
|
undistinct(-x1.hard) == undistinct(not(x1.hard) + hard(1'u64))
|
|
|
|
undistinct(-x2.hard) == undistinct(not(x2.hard) + hard(1'u64))
|
|
|
|
undistinct(-x3.hard) == undistinct(not(x3.hard) + hard(1'u64))
|
|
|
|
undistinct(-y1.hard) == undistinct(not(y1.hard) + hard(1'u64))
|
|
|
|
undistinct(-y2.hard) == undistinct(not(y2.hard) + hard(1'u64))
|
|
|
|
undistinct(-y3.hard) == undistinct(not(y3.hard) + hard(1'u64))
|
2018-12-01 16:32:08 +00:00
|
|
|
|
|
|
|
suite "Hardened booleans":
|
|
|
|
test "Boolean not":
|
|
|
|
check:
|
|
|
|
not(htrue(uint32)).bool == false
|
|
|
|
not(hfalse(uint32)).bool == true
|
|
|
|
|
|
|
|
test "Comparison":
|
|
|
|
check:
|
|
|
|
bool(hard(0'u32) != hard(0'u32)) == false
|
|
|
|
bool(hard(0'u32) != hard(1'u32)) == true
|
|
|
|
|
|
|
|
bool(hard(10'u32) == hard(10'u32)) == true
|
|
|
|
bool(hard(10'u32) != hard(20'u32)) == true
|
|
|
|
|
|
|
|
bool(hard(10'u32) <= hard(10'u32)) == true
|
|
|
|
bool(hard(10'u32) <= hard(20'u32)) == true
|
|
|
|
bool(hard(10'u32) <= hard(5'u32)) == false
|
|
|
|
bool(hard(10'u32) <= hard(0xFFFFFFFF'u32)) == true
|
|
|
|
|
|
|
|
bool(hard(10'u32) < hard(10'u32)) == false
|
|
|
|
bool(hard(10'u32) < hard(20'u32)) == true
|
|
|
|
bool(hard(10'u32) < hard(5'u32)) == false
|
|
|
|
bool(hard(10'u32) < hard(0xFFFFFFFF'u32)) == true
|
|
|
|
|
|
|
|
bool(hard(10'u32) > hard(10'u32)) == false
|
|
|
|
bool(hard(10'u32) > hard(20'u32)) == false
|
|
|
|
bool(hard(10'u32) > hard(5'u32)) == true
|
|
|
|
bool(hard(10'u32) > hard(0xFFFFFFFF'u32)) == false
|
|
|
|
|
|
|
|
bool(hard(10'u32) >= hard(10'u32)) == true
|
|
|
|
bool(hard(10'u32) >= hard(20'u32)) == false
|
|
|
|
bool(hard(10'u32) >= hard(5'u32)) == true
|
|
|
|
bool(hard(10'u32) >= hard(0xFFFFFFFF'u32)) == false
|
2018-12-01 17:01:41 +00:00
|
|
|
|
2018-12-01 17:03:52 +00:00
|
|
|
test "Multiplexer/selector - mux(ctl, x, y) <=> ctl? x: y":
|
2018-12-01 17:01:41 +00:00
|
|
|
let u = 10'u32.hard
|
|
|
|
let v = 20'u32.hard
|
|
|
|
let w = 5'u32.hard
|
|
|
|
|
|
|
|
let y = htrue(uint32)
|
|
|
|
let n = hfalse(uint32)
|
|
|
|
|
|
|
|
check:
|
2018-12-01 17:03:52 +00:00
|
|
|
bool(mux(y, u, v) == u)
|
|
|
|
bool(mux(n, u, v) == v)
|
2018-12-01 17:01:41 +00:00
|
|
|
|
2018-12-01 17:03:52 +00:00
|
|
|
bool(mux(y, u, w) == u)
|
|
|
|
bool(mux(n, u, w) == w)
|
|
|
|
|
|
|
|
bool(mux(y, v, w) == v)
|
|
|
|
bool(mux(n, v, w) == w)
|