disable powmod at compile time

This commit is contained in:
andri lim 2019-10-25 17:50:55 +07:00 committed by zah
parent ea82fdfb39
commit 9ec615f0ef
2 changed files with 46 additions and 34 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 ./intops
import ./intops, private/datatypes
func addmod_internal(a, b, m: Stuint): Stuint {.inline.}=
## Modular addition
@ -90,7 +90,7 @@ func addmod*(a, b, m: Stuint): Stuint =
result = addmod_internal(a_m, b_m, m)
proc submod*(a, b, m: Stuint): Stuint =
func submod*(a, b, m: Stuint): Stuint =
## Modular substraction
let a_m = if a < m: a
@ -110,9 +110,16 @@ func mulmod*(a, b, m: Stuint): Stuint =
result = mulmod_internal(a_m, b_m, m)
proc powmod*(a, b, m: Stuint): Stuint =
func powmod*[T](a, b, m: T): T =
## Modular exponentiation
when nimvm:
doAssert false, "cannot use powmod at compile-time"
else:
# we need this ugly branch
# because of nim-lang/Nim#12517
discard
let a_m = if a < m: a
else: a mod m

View File

@ -100,41 +100,46 @@ template testModArith(chk, tst: untyped) =
chkMulMod(chk, "1", "FFFFFFFFFFFFFFFF", "C", "3", 128)
chkMulMod(chk, "1", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "C", "3", 128)
tst "powmod":
chkPowMod(chk, "C", "3", "C", "0", 8)
chkPowMod(chk, "1", "3", "C", "1", 8)
chkPowMod(chk, "1", "FF", "C", "1", 8)
chkPowMod(chk, "FF", "3", "C", "3", 8)
# TODO: bug #98
when nimvm:
# this ugly branch needed due to nim-lang/Nim#12518
discard
else:
tst "powmod":
chkPowMod(chk, "C", "3", "C", "0", 8)
chkPowMod(chk, "1", "3", "C", "1", 8)
chkPowMod(chk, "1", "FF", "C", "1", 8)
chkPowMod(chk, "FF", "3", "C", "3", 8)
chkPowMod(chk, "C", "3", "C", "0", 16)
chkPowMod(chk, "1", "3", "C", "1", 16)
chkPowMod(chk, "1", "FF", "C", "1", 16)
chkPowMod(chk, "FF", "3", "C", "3", 16)
chkPowMod(chk, "FFFF", "3", "C", "3", 16)
chkPowMod(chk, "C", "3", "C", "0", 16)
chkPowMod(chk, "1", "3", "C", "1", 16)
chkPowMod(chk, "1", "FF", "C", "1", 16)
chkPowMod(chk, "FF", "3", "C", "3", 16)
chkPowMod(chk, "FFFF", "3", "C", "3", 16)
chkPowMod(chk, "C", "3", "C", "0", 32)
chkPowMod(chk, "1", "3", "C", "1", 32)
chkPowMod(chk, "1", "FF", "C", "1", 32)
chkPowMod(chk, "FF", "3", "C", "3", 32)
chkPowMod(chk, "FFFF", "3", "C", "3", 32)
chkPowMod(chk, "FFFFFFFF", "3", "C", "3", 32)
chkPowMod(chk, "C", "3", "C", "0", 32)
chkPowMod(chk, "1", "3", "C", "1", 32)
chkPowMod(chk, "1", "FF", "C", "1", 32)
chkPowMod(chk, "FF", "3", "C", "3", 32)
chkPowMod(chk, "FFFF", "3", "C", "3", 32)
chkPowMod(chk, "FFFFFFFF", "3", "C", "3", 32)
chkPowMod(chk, "C", "3", "C", "0", 64)
chkPowMod(chk, "1", "3", "C", "1", 64)
chkPowMod(chk, "1", "FF", "C", "1", 64)
chkPowMod(chk, "FF", "3", "C", "3", 64)
chkPowMod(chk, "FFFF", "3", "C", "3", 64)
chkPowMod(chk, "FFFFFFFF", "3", "C", "3", 64)
chkPowMod(chk, "FFFFFFFFFFFFFFFF", "3", "C", "3", 64)
chkPowMod(chk, "C", "3", "C", "0", 64)
chkPowMod(chk, "1", "3", "C", "1", 64)
chkPowMod(chk, "1", "FF", "C", "1", 64)
chkPowMod(chk, "FF", "3", "C", "3", 64)
chkPowMod(chk, "FFFF", "3", "C", "3", 64)
chkPowMod(chk, "FFFFFFFF", "3", "C", "3", 64)
chkPowMod(chk, "FFFFFFFFFFFFFFFF", "3", "C", "3", 64)
chkPowMod(chk, "C", "3", "C", "0", 128)
chkPowMod(chk, "1", "3", "C", "1", 128)
chkPowMod(chk, "1", "FF", "C", "1", 128)
chkPowMod(chk, "FF", "3", "C", "3", 128)
chkPowMod(chk, "FFFF", "3", "C", "3", 128)
chkPowMod(chk, "FFFFFFFF", "3", "C", "3", 128)
chkPowMod(chk, "FFFFFFFFFFFFFFFF", "3", "C", "3", 128)
chkPowMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "C", "3", 128)
chkPowMod(chk, "C", "3", "C", "0", 128)
chkPowMod(chk, "1", "3", "C", "1", 128)
chkPowMod(chk, "1", "FF", "C", "1", 128)
chkPowMod(chk, "FF", "3", "C", "3", 128)
chkPowMod(chk, "FFFF", "3", "C", "3", 128)
chkPowMod(chk, "FFFFFFFF", "3", "C", "3", 128)
chkPowMod(chk, "FFFFFFFFFFFFFFFF", "3", "C", "3", 128)
chkPowMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "C", "3", 128)
static:
testModArith(ctCheck, ctTest)