From 98150472345b18e2cc4d1d70cddaa5fed429321a Mon Sep 17 00:00:00 2001 From: mratsim Date: Sat, 1 Dec 2018 16:49:45 +0100 Subject: [PATCH] Add shifts and operators test --- hardy/ct_primitives.nim | 7 ++++ tests/all_tests.nim | 73 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/hardy/ct_primitives.nim b/hardy/ct_primitives.nim index 4a530d9..841563a 100644 --- a/hardy/ct_primitives.nim +++ b/hardy/ct_primitives.nim @@ -44,6 +44,13 @@ func `-`*[T: HardBase](x, y: T): T {.magic: "SubU".} func `shr`*[T: HardBase](x: T, y: SomeInteger): T {.magic: "ShrI".} func `shl`*[T: HardBase](x: T, y: SomeInteger): T {.magic: "ShlI".} +func `*`*[T: HardBase](x, y: T): T {.magic: "MulU".} +# Warning ⚠️ : We assume that mul hardware multiplication is constant time +# but this is not always true, especially on ARMv7 and ARMv9 + +# We don't implement div/mod as we can't assume the hardware implementation +# is constant-time + # ############################################################ # # Hardened Boolean primitives diff --git a/tests/all_tests.nim b/tests/all_tests.nim index f1dcf1e..1ca2700 100644 --- a/tests/all_tests.nim +++ b/tests/all_tests.nim @@ -5,9 +5,12 @@ # * 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. -import unittest, random, +import unittest, random, math, ../hardy +# Random seed for reproducibility +randomize(0xDEADBEEF) + template undistinct[T](x: HardBase[T]): T = T(x) @@ -22,12 +25,12 @@ suite "Hardened unsigned integers": high(HardBase[uint64]).undistinct == 0xFFFFFFFF_FFFFFFFF.uint64 test "bitwise `and`, `or`, `xor`, `not`": - var x1 = rand(high(int)).uint64 - var y1 = rand(high(int)).uint64 - var x2 = rand(high(int)).uint64 - var y2 = rand(high(int)).uint64 - var x3 = rand(high(int)).uint64 - var y3 = rand(high(int)).uint64 + 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 bitwise_check(op: untyped): untyped = block: check: @@ -54,3 +57,59 @@ suite "Hardened unsigned integers": not(hard(y1)).undistinct == not y1 not(hard(y2)).undistinct == not y2 not(hard(y3)).undistinct == not y3 + + 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(`*`)