2020-02-08 17:50:01 +00:00
|
|
|
# Constantine
|
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
|
|
|
# Copyright (c) 2020-Present Mamy André-Ratsimbazafy
|
|
|
|
# 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.
|
|
|
|
|
2020-02-25 19:55:23 +00:00
|
|
|
import unittest,
|
2020-02-12 20:57:39 +00:00
|
|
|
../constantine/io/io_bigints,
|
2020-03-16 15:33:51 +00:00
|
|
|
../constantine/arithmetic/bigints,
|
2020-02-11 23:20:31 +00:00
|
|
|
../constantine/config/common,
|
2020-03-16 15:33:51 +00:00
|
|
|
../constantine/primitives
|
2020-02-08 17:50:01 +00:00
|
|
|
|
2020-02-11 23:25:48 +00:00
|
|
|
proc main() =
|
|
|
|
suite "isZero":
|
|
|
|
test "isZero for zero":
|
|
|
|
var x: BigInt[128]
|
|
|
|
check: x.isZero().bool
|
|
|
|
test "isZero for non-zero":
|
|
|
|
block:
|
|
|
|
var x = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
|
|
|
check: not x.isZero().bool
|
|
|
|
block:
|
|
|
|
var x = fromHex(BigInt[128], "0x00000000_00000001_00000000_00000000")
|
|
|
|
check: not x.isZero().bool
|
|
|
|
block:
|
|
|
|
var x = fromHex(BigInt[128], "0xFFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF")
|
|
|
|
check: not x.isZero().bool
|
|
|
|
|
|
|
|
suite "Arithmetic operations - Addition":
|
|
|
|
test "Adding 2 zeros":
|
2020-02-08 17:50:01 +00:00
|
|
|
var a = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000000")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000000")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, ctrue(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
check: a.isZero().bool
|
|
|
|
|
|
|
|
test "Adding 1 zero - real addition":
|
|
|
|
block:
|
|
|
|
var a = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000000")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, ctrue(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
|
|
|
|
let c = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
|
|
|
check:
|
|
|
|
bool(a == c)
|
|
|
|
block:
|
|
|
|
var a = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000000")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, ctrue(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
|
|
|
|
let c = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
|
|
|
check:
|
|
|
|
bool(a == c)
|
|
|
|
|
|
|
|
test "Adding 1 zero - fake addition":
|
|
|
|
block:
|
|
|
|
var a = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000000")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, cfalse(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
|
|
|
|
let c = a
|
|
|
|
check:
|
|
|
|
bool(a == c)
|
|
|
|
block:
|
|
|
|
var a = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000000")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, cfalse(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
|
|
|
|
let c = a
|
|
|
|
check:
|
|
|
|
bool(a == c)
|
|
|
|
|
|
|
|
test "Adding non-zeros - real addition":
|
|
|
|
block:
|
|
|
|
var a = fromHex(BigInt[128], "0x00000000_00000001_00000000_00000000")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, ctrue(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
|
|
|
|
let c = fromHex(BigInt[128], "0x00000000_00000001_00000000_00000001")
|
|
|
|
check:
|
|
|
|
bool(a == c)
|
|
|
|
block:
|
|
|
|
var a = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000001_00000000_00000000")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, ctrue(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
|
|
|
|
let c = fromHex(BigInt[128], "0x00000000_00000001_00000000_00000001")
|
|
|
|
check:
|
|
|
|
bool(a == c)
|
|
|
|
|
|
|
|
test "Adding non-zeros - fake addition":
|
|
|
|
block:
|
|
|
|
var a = fromHex(BigInt[128], "0x00000000_00000001_00000000_00000000")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, cfalse(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
|
|
|
|
let c = a
|
|
|
|
check:
|
|
|
|
bool(a == c)
|
|
|
|
block:
|
|
|
|
var a = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000001_00000000_00000000")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, cfalse(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
|
|
|
|
let c = a
|
|
|
|
check:
|
|
|
|
bool(a == c)
|
|
|
|
|
|
|
|
test "Addition limbs carry":
|
|
|
|
block:
|
|
|
|
var a = fromHex(BigInt[128], "0x00000000_FFFFFFFF_FFFFFFFF_FFFFFFFE")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, ctrue(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
|
|
|
|
let c = fromHex(BigInt[128], "0x00000000_FFFFFFFF_FFFFFFFF_FFFFFFFF")
|
|
|
|
check:
|
|
|
|
bool(a == c)
|
|
|
|
not bool(carry)
|
|
|
|
|
|
|
|
block:
|
|
|
|
var a = fromHex(BigInt[128], "0x00000000_FFFFFFFF_FFFFFFFF_FFFFFFFF")
|
|
|
|
let b = fromHex(BigInt[128], "0x00000000_00000000_00000000_00000001")
|
2020-02-25 00:26:21 +00:00
|
|
|
let carry = a.cadd(b, ctrue(Word))
|
2020-02-11 23:25:48 +00:00
|
|
|
|
|
|
|
let c = fromHex(BigInt[128], "0x00000001_00000000_00000000_00000000")
|
|
|
|
check:
|
|
|
|
bool(a == c)
|
|
|
|
not bool(carry)
|
|
|
|
|
|
|
|
suite "Modular operations - small modulus":
|
|
|
|
# Vectors taken from Stint - https://github.com/status-im/nim-stint
|
|
|
|
test "100 mod 13":
|
|
|
|
# Test 1 word and more than 1 word
|
|
|
|
block:
|
|
|
|
let a = BigInt[7].fromUint(100'u32)
|
|
|
|
let m = BigInt[4].fromUint(13'u8)
|
|
|
|
|
|
|
|
var r: BigInt[4]
|
|
|
|
r.reduce(a, m)
|
|
|
|
check:
|
|
|
|
bool(r == BigInt[4].fromUint(100'u8 mod 13))
|
|
|
|
|
|
|
|
block: #
|
|
|
|
let a = BigInt[32].fromUint(100'u32)
|
|
|
|
let m = BigInt[4].fromUint(13'u8)
|
|
|
|
|
|
|
|
var r: BigInt[4]
|
|
|
|
r.reduce(a, m)
|
|
|
|
check:
|
|
|
|
bool(r == BigInt[4].fromUint(100'u8 mod 13))
|
|
|
|
|
|
|
|
block: #
|
|
|
|
let a = BigInt[64].fromUint(100'u32)
|
|
|
|
let m = BigInt[4].fromUint(13'u8)
|
|
|
|
|
|
|
|
var r: BigInt[4]
|
|
|
|
r.reduce(a, m)
|
|
|
|
check:
|
|
|
|
bool(r == BigInt[4].fromUint(100'u8 mod 13))
|
|
|
|
|
|
|
|
test "2^64 mod 3":
|
|
|
|
let a = BigInt[65].fromHex("0x1_00000000_00000000")
|
|
|
|
let m = BigInt[8].fromUint(3'u8)
|
|
|
|
|
|
|
|
var r: BigInt[8]
|
|
|
|
r.reduce(a, m)
|
2020-02-08 17:50:01 +00:00
|
|
|
check:
|
2020-02-11 23:25:48 +00:00
|
|
|
bool(r == BigInt[8].fromUint(1'u8))
|
2020-02-08 17:50:01 +00:00
|
|
|
|
2020-02-11 23:25:48 +00:00
|
|
|
test "1234567891234567890 mod 10":
|
|
|
|
let a = BigInt[64].fromUint(1234567891234567890'u64)
|
|
|
|
let m = BigInt[8].fromUint(10'u8)
|
2020-02-08 17:50:01 +00:00
|
|
|
|
2020-02-11 23:25:48 +00:00
|
|
|
var r: BigInt[8]
|
|
|
|
r.reduce(a, m)
|
2020-02-08 17:50:01 +00:00
|
|
|
check:
|
2020-02-11 23:25:48 +00:00
|
|
|
bool(r == BigInt[8].fromUint(0'u8))
|
2020-02-08 17:50:01 +00:00
|
|
|
|
2020-02-11 23:25:48 +00:00
|
|
|
suite "Modular operations - small modulus - Stint specific failures highlighted by property-based testing":
|
|
|
|
# Vectors taken from Stint - https://github.com/status-im/nim-stint
|
|
|
|
test "Modulo: 65696211516342324 mod 174261910798982":
|
|
|
|
let u = 65696211516342324'u64
|
|
|
|
let v = 174261910798982'u64
|
2020-02-08 17:50:01 +00:00
|
|
|
|
2020-02-11 23:25:48 +00:00
|
|
|
let a = BigInt[56].fromUint(u)
|
|
|
|
let m = BigInt[48].fromUint(v)
|
2020-02-08 17:50:01 +00:00
|
|
|
|
2020-02-11 23:25:48 +00:00
|
|
|
var r: BigInt[48]
|
|
|
|
r.reduce(a, m)
|
2020-02-08 18:09:20 +00:00
|
|
|
|
|
|
|
check:
|
2020-02-11 23:25:48 +00:00
|
|
|
bool(r == BigInt[48].fromUint(u mod v))
|
2020-02-08 18:09:20 +00:00
|
|
|
|
2020-02-11 23:25:48 +00:00
|
|
|
test "Modulo: 15080397990160655 mod 600432699691":
|
|
|
|
let u = 15080397990160655'u64
|
|
|
|
let v = 600432699691'u64
|
2020-02-10 18:57:35 +00:00
|
|
|
|
2020-02-11 23:25:48 +00:00
|
|
|
let a = BigInt[54].fromUint(u)
|
|
|
|
let m = BigInt[40].fromUint(v)
|
2020-02-10 18:57:35 +00:00
|
|
|
|
2020-02-11 23:25:48 +00:00
|
|
|
var r: BigInt[40]
|
2020-02-10 18:57:35 +00:00
|
|
|
r.reduce(a, m)
|
|
|
|
|
|
|
|
check:
|
2020-02-11 23:25:48 +00:00
|
|
|
bool(r == BigInt[40].fromUint(u mod v))
|
2020-02-09 17:17:32 +00:00
|
|
|
|
2020-02-11 23:25:48 +00:00
|
|
|
main()
|