Enable modular arithmetic tests

This commit is contained in:
mratsim 2018-10-06 11:55:08 +02:00 committed by tersec
parent 69b5630de1
commit 7591373f15
3 changed files with 17 additions and 17 deletions

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ./datatypes, ./initialization
import ./datatypes, ./initialization, ./uint_bitwise_ops
func low*(T: typedesc[UintImpl]): T {.inline.}=
zero(T)

View File

@ -12,8 +12,8 @@ import test_uint_endianness,
test_uint_bitwise,
test_uint_addsub,
test_uint_muldiv,
test_uint_exp
# test_uint_modular_arithmetic
test_uint_exp,
test_uint_modular_arithmetic
import test_int_endianness,
test_int_comparison,

View File

@ -13,34 +13,34 @@ suite "Modular arithmetic":
test "Modular addition":
# uint16 rolls over at 65535
let a = 50000.stuint(16)
let b = 20000.stuint(16)
let m = 60000.stuint(16)
let a = 50000'u16.stuint(16)
let b = 20000'u16.stuint(16)
let m = 60000'u16.stuint(16)
check: addmod(a, b, m) == 10000.stuint(16)
check: addmod(a, b, m) == 10000'u16.stuint(16)
test "Modular substraction":
let a = 5.stuint(16)
let b = 7.stuint(16)
let m = 20.stuint(16)
let a = 5'u16.stuint(16)
let b = 7'u16.stuint(16)
let m = 20'u16.stuint(16)
check: submod(a, b, m) == 18.stuint(16)
check: submod(a, b, m) == 18'u16.stuint(16)
test "Modular multiplication":
# https://www.wolframalpha.com/input/?i=(1234567890+*+987654321)+mod+999999999
# --> 345_679_002
let a = 1234567890.stuint(64)
let b = 987654321.stuint(64)
let m = 999999999.stuint(64)
let a = 1234567890'u64.stuint(64)
let b = 987654321'u64.stuint(64)
let m = 999999999'u64.stuint(64)
check: mulmod(a, b, m) == 345_679_002.stuint(64)
check: mulmod(a, b, m) == 345_679_002'u64.stuint(64)
test "Modular exponentiation":
# https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/fast-modular-exponentiation
check:
powmod(5.stuint(16), 117.stuint(16), 19.stuint(16)) == 1.stuint(16)
powmod(3.stuint(16), 1993.stuint(16), 17.stuint(16)) == 14.stuint(16)
powmod(5'u16.stuint(16), 117'u16.stuint(16), 19'u16.stuint(16)) == 1'u16.stuint(16)
powmod(3'u16.stuint(16), 1993'u16.stuint(16), 17'u16.stuint(16)) == 14'u16.stuint(16)
check:
powmod(12.stuint(256), 34.stuint(256), high(UInt256)) == "4922235242952026704037113243122008064".u256