diff --git a/stint/modular_arithmetic.nim b/stint/modular_arithmetic.nim index e39ec68..a7c9d30 100644 --- a/stint/modular_arithmetic.nim +++ b/stint/modular_arithmetic.nim @@ -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 diff --git a/tests/test_uint_modular_arithmetic.nim b/tests/test_uint_modular_arithmetic.nim index ed96d13..749b1be 100644 --- a/tests/test_uint_modular_arithmetic.nim +++ b/tests/test_uint_modular_arithmetic.nim @@ -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)