Enable --styleCheck:usages (#118)

This commit is contained in:
Kim De Mey 2022-02-24 20:09:53 +01:00 committed by GitHub
parent ddfa6c608a
commit e656ad40d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 465 additions and 464 deletions

View File

@ -11,8 +11,8 @@ import stint/[bitops2, endians2, intops, io, modular_arithmetic, literals_stint]
export bitops2, endians2, intops, io, modular_arithmetic, literals_stint
type
Int128* = Stint[128]
Int256* = Stint[256]
Int128* = StInt[128]
Int256* = StInt[256]
UInt128* = StUint[128]
UInt256* = StUint[256]

View File

@ -15,7 +15,8 @@ proc test(args, path: string) =
if not dirExists "build":
mkDir "build"
exec "nim " & getEnv("TEST_LANG", "c") & " " & getEnv("NIMFLAGS") & " " & args &
" --outdir:build -r --hints:off --warnings:off --skipParentCfg " & path
" --outdir:build -r --hints:off --warnings:off --skipParentCfg" &
" --styleCheck:usages --styleCheck:error " & path
task test, "Run all tests - test and production implementation":
# Run tests for internal procs - test implementation (StUint[64] = 2x uint32

View File

@ -9,18 +9,18 @@
import ./private/[bitops2_priv, datatypes]
export Stint, StUint
export StInt, StUint
export IntImpl, intImpl, UintImpl, uintImpl, bitsof # TODO: remove the need to export those
type SomeBigInteger = Stuint|Stint
type SomeBigInteger = StUint|StInt
import ./private/initialization
func zero*[bits: static[int]](T: typedesc[Stuint[bits] or Stint[bits]]): T {.inline.} =
func zero*[bits: static[int]](T: typedesc[StUint[bits] or StInt[bits]]): T {.inline.} =
## Returns the zero of the input type
discard
func one*[bits: static[int]](T: typedesc[Stuint[bits]]): T {.inline.} =
func one*[bits: static[int]](T: typedesc[StUint[bits]]): T {.inline.} =
## Returns the one of the input type
result.data = one(type result.data)
@ -41,12 +41,12 @@ func `-=`*(x: var SomeBigInteger, y: SomeBigInteger) {.inline.} =
import ./private/int_negabs
func `-`*(x: Stint): Stint {.inline.} =
func `-`*(x: StInt): StInt {.inline.} =
## Returns true if input is zero
## false otherwise
result.data = -x.data
func abs*(x: Stint): Stint {.inline.} =
func abs*(x: StInt): StInt {.inline.} =
## Returns true if input is zero
## false otherwise
result.data = abs(x.data)
@ -94,7 +94,7 @@ func isZero*(x: SomeBigInteger): bool {.inline.} =
## false otherwise
x.data.isZero
func isNegative*(x: Stint): bool {.inline.} =
func isNegative*(x: StInt): bool {.inline.} =
## Returns true if input is negative (< 0)
## false otherwise
x.data.isNegative
@ -142,14 +142,14 @@ func `shl`*(x: SomeBigInteger, y: SomeInteger): SomeBigInteger {.inline.} =
import ./private/[int_highlow, uint_highlow]
func high*[bits](_: typedesc[Stint[bits]]): Stint[bits] {.inline.} =
func high*[bits](_: typedesc[StInt[bits]]): StInt[bits] {.inline.} =
result.data = high(type result.data)
func high*[bits](_: typedesc[Stuint[bits]]): Stuint[bits] {.inline.} =
func high*[bits](_: typedesc[StUint[bits]]): StUint[bits] {.inline.} =
result.data = high(type result.data)
func low*[bits](_: typedesc[Stint[bits]]): Stint[bits] {.inline.} =
func low*[bits](_: typedesc[StInt[bits]]): StInt[bits] {.inline.} =
result.data = low(type result.data)
func low*[bits](_: typedesc[Stuint[bits]]): Stuint[bits] {.inline.} =
func low*[bits](_: typedesc[StUint[bits]]): StUint[bits] {.inline.} =
result.data = low(type result.data)
import ./private/uint_exp, math

View File

@ -59,13 +59,13 @@ func stint*[T: SomeInteger](n: T, bits: static[int]): StInt[bits] {.inline.}=
else:
result.data = (type result.data)(n)
func to*(x: SomeInteger, T: typedesc[Stint]): T =
func to*(x: SomeInteger, T: typedesc[StInt]): T =
stint(x, result.bits)
func to*(x: SomeUnsignedInt, T: typedesc[StUint]): T =
stuint(x, result.bits)
func truncate*(num: Stint or StUint, T: typedesc[SomeInteger]): T {.inline.}=
func truncate*(num: StInt or StUint, T: typedesc[SomeInteger]): T {.inline.}=
## Extract the int, uint, int8-int64 or uint8-uint64 portion of a multi-precision integer.
## Note that int and uint are 32-bit on 32-bit platform.
## For unsigned result type, result is modulo 2^(sizeof T in bit)
@ -79,7 +79,7 @@ func truncate*(num: Stint or StUint, T: typedesc[SomeInteger]): T {.inline.}=
else:
cast[T](num.data.leastSignificantWord)
func toInt*(num: Stint or StUint): int {.inline, deprecated:"Use num.truncate(int) instead".}=
func toInt*(num: StInt or StUint): int {.inline, deprecated:"Use num.truncate(int) instead".}=
num.truncate(int)
func bigToSmall(result: var (UintImpl | IntImpl), x: auto) {.inline.} =
@ -246,8 +246,8 @@ func readDecChar(c: char): int {.inline.}=
raise newException(ValueError, "Character out of '0'..'9' range")
ord(c) - ord('0')
func parse*[bits: static[int]](input: string, T: typedesc[Stuint[bits]], radix: static[uint8] = 10): T =
## Parse a string and store the result in a Stint[bits] or Stuint[bits].
func parse*[bits: static[int]](input: string, T: typedesc[StUint[bits]], radix: static[uint8] = 10): T =
## Parse a string and store the result in a StInt[bits] or StUint[bits].
static: doAssert (radix >= 2) and radix <= 16, "Only base from 2..16 are supported"
# TODO: use static[range[2 .. 16]], not supported at the moment (2018-04-26)
@ -267,8 +267,8 @@ func parse*[bits: static[int]](input: string, T: typedesc[Stuint[bits]], radix:
result = result * base + input[curr].readHexChar.stuint(bits)
nextNonBlank(curr, input)
func parse*[bits: static[int]](input: string, T: typedesc[Stint[bits]], radix: static[int8] = 10): T =
## Parse a string and store the result in a Stint[bits] or Stuint[bits].
func parse*[bits: static[int]](input: string, T: typedesc[StInt[bits]], radix: static[int8] = 10): T =
## Parse a string and store the result in a StInt[bits] or StUint[bits].
static: doAssert (radix >= 2) and radix <= 16, "Only base from 2..16 are supported"
# TODO: use static[range[2 .. 16]], not supported at the moment (2018-04-26)
@ -282,7 +282,7 @@ func parse*[bits: static[int]](input: string, T: typedesc[Stint[bits]], radix: s
var
curr = 0 # Current index in the string
isNeg = false
no_overflow: Stuint[bits]
no_overflow: StUint[bits]
if input[curr] == '-':
doAssert radix == 10, "Negative numbers are only supported with base 10 input."
@ -305,16 +305,16 @@ func parse*[bits: static[int]](input: string, T: typedesc[Stint[bits]], radix: s
else:
result = convert[T](no_overflow)
func fromHex*(T: typedesc[StUint|Stint], s: string): T {.inline.} =
func fromHex*(T: typedesc[StUint|StInt], s: string): T {.inline.} =
## Convert an hex string to the corresponding unsigned integer
parse(s, type result, radix = 16)
func hexToUint*[bits: static[int]](hexString: string): Stuint[bits] {.inline.} =
func hexToUint*[bits: static[int]](hexString: string): StUint[bits] {.inline.} =
## Convert an hex string to the corresponding unsigned integer
parse(hexString, type result, radix = 16)
func toString*[bits: static[int]](num: StUint[bits], radix: static[uint8] = 10): string =
## Convert a Stint or Stuint to string.
## Convert a StInt or StUint to string.
## In case of negative numbers:
## - they are prefixed with "-" for base 10.
## - if not base 10, they are returned raw in two-complement form.
@ -339,8 +339,8 @@ func toString*[bits: static[int]](num: StUint[bits], radix: static[uint8] = 10):
reverse(result)
func toString*[bits: static[int]](num: Stint[bits], radix: static[int8] = 10): string =
## Convert a Stint or Stuint to string.
func toString*[bits: static[int]](num: StInt[bits], radix: static[int8] = 10): string =
## Convert a StInt or StUint to string.
## In case of negative numbers:
## - they are prefixed with "-" for base 10.
## - if not base 10, they are returned raw in two-complement form.
@ -353,7 +353,7 @@ func toString*[bits: static[int]](num: Stint[bits], radix: static[int8] = 10): s
result = ""
type T = Stuint[bits]
type T = StUint[bits]
let isNeg = num.isNegative
let num = convert[T](if radix == 10 and isNeg: -num
else: num)
@ -374,19 +374,19 @@ func toString*[bits: static[int]](num: Stint[bits], radix: static[int8] = 10): s
reverse(result)
func `$`*(num: Stint or StUint): string {.inline.}=
func `$`*(num: StInt or StUint): string {.inline.}=
when num.data is SomeInteger:
$num.data
else:
toString(num, 10)
func toHex*[bits: static[int]](num: Stint[bits] or StUint[bits]): string {.inline.}=
func toHex*[bits: static[int]](num: StInt[bits] or StUint[bits]): string {.inline.}=
## Convert to a hex string.
## Output is considered a big-endian base 16 string.
## Leading zeros are stripped. Use dumpHex instead if you need the in-memory representation
toString(num, 16)
func dumpHex*(x: Stint or StUint, order: static[Endianness] = bigEndian): string =
func dumpHex*(x: StInt or StUint, order: static[Endianness] = bigEndian): string =
## Stringify an int to hex.
## Note. Leading zeros are not removed. Use toString(n, base = 16)/toHex instead.
##
@ -415,7 +415,7 @@ func dumpHex*(x: Stint or StUint, order: static[Endianness] = bigEndian): string
result[2*i+1] = hexChars[int byte and 0xF]
else:
{.pragma: restrict, codegenDecl: "$# __restrict $#".}
let bytes {.restrict.}= cast[ptr array[size, byte]](x.unsafeaddr)
let bytes {.restrict.}= cast[ptr array[size, byte]](x.unsafeAddr)
for i in 0 ..< size:
when order == system.cpuEndian:
@ -425,7 +425,7 @@ func dumpHex*(x: Stint or StUint, order: static[Endianness] = bigEndian): string
result[2*i] = hexChars[int bytes[bytes[].high - i] shr 4 and 0xF]
result[2*i+1] = hexChars[int bytes[bytes[].high - i] and 0xF]
proc initFromBytesBE*[bits: static[int]](val: var Stuint[bits], ba: openarray[byte], allowPadding: static[bool] = true) =
proc initFromBytesBE*[bits: static[int]](val: var StUint[bits], ba: openArray[byte], allowPadding: static[bool] = true) =
## Initializes a UInt[bits] value from a byte buffer storing a big-endian
## representation of a number.
##
@ -472,7 +472,7 @@ proc initFromBytesBE*[bits: static[int]](val: var Stuint[bits], ba: openarray[by
else:
for i, b in ba: r_ptr[N-1 - i] = b
func significantBytesBE*(val: openarray[byte]): int {.deprecated.}=
func significantBytesBE*(val: openArray[byte]): int {.deprecated.}=
## Returns the number of significant trailing bytes in a big endian
## representation of a number.
# TODO: move that in https://github.com/status-im/nim-byteutils
@ -481,19 +481,19 @@ func significantBytesBE*(val: openarray[byte]): int {.deprecated.}=
return val.len - i
return 1
func fromBytesBE*(T: type Stuint, ba: openarray[byte],
func fromBytesBE*(T: type StUint, ba: openArray[byte],
allowPadding: static[bool] = true): T =
## This function provides a convenience wrapper around `initFromBytesBE`.
result.initFromBytesBE(ba, allowPadding)
func readUintBE*[bits: static[int]](ba: openarray[byte]): Stuint[bits] =
func readUintBE*[bits: static[int]](ba: openArray[byte]): StUint[bits] =
## Convert a big-endian array of (bits div 8) Bytes to an UInt[bits] (in native host endianness)
## Input:
## - a big-endian openarray of size (bits div 8) at least
## - a big-endian openArray of size (bits div 8) at least
## Returns:
## - A unsigned integer of the same size with `bits` bits
##
## ⚠ If the openarray length is bigger than bits div 8, part converted is undefined behaviour.
## ⚠ If the openArray length is bigger than bits div 8, part converted is undefined behaviour.
result.initFromBytesBE(ba, false)
func toByteArrayBE*[bits: static[int]](n: StUint[bits]): array[bits div 8, byte] =

View File

@ -34,12 +34,12 @@ macro make_mixed_types_ops(op: untyped, ResultTy: untyped, sign: static[Signedne
)
result.add quote do:
proc `op`*[bits: static[int]](a: Stuint[bits], b: `intLit`): `ResultTy` {.inline.}=
proc `op`*[bits: static[int]](a: StUint[bits], b: `intLit`): `ResultTy` {.inline.}=
`op`(a, b.stuint(bits))
if switchInputs:
result.add quote do:
proc `op`*[bits: static[int]](a: `intLit`, b: Stuint[bits]): `ResultTy` {.inline.}=
proc `op`*[bits: static[int]](a: `intLit`, b: StUint[bits]): `ResultTy` {.inline.}=
`op`(a.stuint(bits), b)
if sign != UintOnly:
@ -50,12 +50,12 @@ macro make_mixed_types_ops(op: untyped, ResultTy: untyped, sign: static[Signedne
)
result.add quote do:
proc `op`*[bits: static[int]](a: Stint[bits], b: `intLit`): `ResultTy` {.inline.}=
proc `op`*[bits: static[int]](a: StInt[bits], b: `intLit`): `ResultTy` {.inline.}=
`op`(a, b.stuint(bits))
if switchInputs:
result.add quote do:
proc `op`*[bits: static[int]](a: `intLit`, b: Stint[bits]): `ResultTy` {.inline.}=
proc `op`*[bits: static[int]](a: `intLit`, b: StInt[bits]): `ResultTy` {.inline.}=
`op`(a.stuint(bits), b)
make_mixed_types_ops(`+`, InputType, BothSigned, switchInputs = true)
@ -77,11 +77,11 @@ make_mixed_types_ops(`xor`, InputType, BothSigned, switchInputs = true)
# Specialization / fast path for comparison to zero
# Note system.nim has templates to transform > and >= into <= and <
template mtoIsZero*{a == 0}(a: StUint or Stint): bool = a.isZero
template mtoIsZero*{0 == a}(a: StUint or Stint): bool = a.isZero
template mtoIsZero*{a == 0}(a: StUint or StInt): bool = a.isZero
template mtoIsZero*{0 == a}(a: StUint or StInt): bool = a.isZero
template mtoIsNeg*{a < 0}(a: Stint): bool = a.isNegative
template mtoIsNegOrZero*{a <= 0}(a: Stint): bool = a.isZero or a.isNegative
template mtoIsNeg*{a < 0}(a: StInt): bool = a.isNegative
template mtoIsNegOrZero*{a <= 0}(a: StInt): bool = a.isZero or a.isNegative
template mtoIsPos*{0 < a}(a: Stint): bool = not(a.isZero or a.isNegative)
template mtoIsPosOrZero*{0 <= a}(a: Stint): bool = not a.isNegative
template mtoIsPos*{0 < a}(a: StInt): bool = not(a.isZero or a.isNegative)
template mtoIsPosOrZero*{0 <= a}(a: StInt): bool = not a.isNegative

View File

@ -9,7 +9,7 @@
import ./intops, private/datatypes
func addmod_internal(a, b, m: Stuint): Stuint {.inline.}=
func addmod_internal(a, b, m: StUint): StUint {.inline.}=
## Modular addition
## ⚠⚠ Assume a < m and b < m
@ -23,7 +23,7 @@ func addmod_internal(a, b, m: Stuint): Stuint {.inline.}=
return a - b_from_m
return m - b_from_m + a
func submod_internal(a, b, m: Stuint): Stuint {.inline.}=
func submod_internal(a, b, m: StUint): StUint {.inline.}=
## Modular substraction
## ⚠⚠ Assume a < m and b < m
@ -36,7 +36,7 @@ func submod_internal(a, b, m: Stuint): Stuint {.inline.}=
return m - b + a
func doublemod_internal(a, m: Stuint): Stuint {.inline.}=
func doublemod_internal(a, m: StUint): StUint {.inline.}=
## Double a modulo m. Assume a < m
## Internal proc - used in mulmod
@ -47,7 +47,7 @@ func doublemod_internal(a, m: Stuint): Stuint {.inline.}=
result -= m
result += a
func mulmod_internal(a, b, m: Stuint): Stuint {.inline.}=
func mulmod_internal(a, b, m: StUint): StUint {.inline.}=
## Does (a * b) mod m. Assume a < m and b < m
## Internal proc - used in powmod
@ -65,7 +65,7 @@ func mulmod_internal(a, b, m: Stuint): Stuint {.inline.}=
a = doublemod_internal(a, m)
b = b shr 1
func powmod_internal(a, b, m: Stuint): Stuint {.inline.}=
func powmod_internal(a, b, m: StUint): StUint {.inline.}=
## Compute ``(a ^ b) mod m``, assume a < m
## Internal proc
@ -80,7 +80,7 @@ func powmod_internal(a, b, m: Stuint): Stuint {.inline.}=
b = b shr 1
a = mulmod_internal(a, a, m)
func addmod*(a, b, m: Stuint): Stuint =
func addmod*(a, b, m: StUint): StUint =
## Modular addition
let a_m = if a < m: a
@ -90,7 +90,7 @@ func addmod*(a, b, m: Stuint): Stuint =
result = addmod_internal(a_m, b_m, m)
func submod*(a, b, m: Stuint): Stuint =
func submod*(a, b, m: StUint): StUint =
## Modular substraction
let a_m = if a < m: a
@ -100,7 +100,7 @@ func submod*(a, b, m: Stuint): Stuint =
result = submod_internal(a_m, b_m, m)
func mulmod*(a, b, m: Stuint): Stuint =
func mulmod*(a, b, m: StUint): StUint =
## Modular multiplication
let a_m = if a < m: a
@ -110,7 +110,7 @@ func mulmod*(a, b, m: Stuint): Stuint =
result = mulmod_internal(a_m, b_m, m)
func powmod*(a, b, m: Stuint): Stuint =
func powmod*(a, b, m: StUint): StUint =
## Modular exponentiation
when nimvm:

View File

@ -11,13 +11,13 @@ func convertImpl[T: IntImpl|UintImpl](x: IntImpl|UintImpl): T {.compileTime.} =
result.hi = convertImpl[type(result.hi)](x.hi)
result.lo = x.lo
func convertImpl[T: Stuint|Stint](x: StUint|StInt): T {.compileTime.} =
func convertImpl[T: StUint|StInt](x: StUint|StInt): T {.compileTime.} =
result.data = convertImpl[type(result.data)](x.data)
template convert*[T](x: Stuint|Stint|UintImpl|IntImpl|SomeInteger): T =
template convert*[T](x: StUint|StInt|UintImpl|IntImpl|SomeInteger): T =
when nimvm:
# this is a workaround Nim VM inability to cast
# something non integer
convertImpl[T](x)
else:
cast[T](x)
cast[T](x)

View File

@ -14,4 +14,4 @@ func `*`*[T, T2](x, y: IntImpl[T, T2]): IntImpl[T, T2] {.inline.}=
# For 2-complement representation this is the exact same
# as unsigned multiplication. We don't need to deal with the sign
# TODO: overflow detection.
convert[type result](convert[UIntImpl[T2]](x) * convert[UIntImpl[T2]](y))
convert[type result](convert[UintImpl[T2]](x) * convert[UintImpl[T2]](y))

View File

@ -44,7 +44,7 @@ import ./bitops2_priv, ./conversion, ./initialization,
## ##
###################################################################################################################
func div2n1n[T: SomeunsignedInt](q, r: var T, n_hi, n_lo, d: T)
func div2n1n[T: SomeUnsignedInt](q, r: var T, n_hi, n_lo, d: T)
func div2n1n(q, r: var UintImpl, ah, al, b: UintImpl)
# Forward declaration
@ -128,7 +128,7 @@ func div2n1n(q, r: var UintImpl, ah, al, b: UintImpl) =
div3n2n(q.hi, s, ah.hi, ah.lo, al.hi, b)
div3n2n(q.lo, r, s.hi, s.lo, al.lo, b)
func div2n1n[T: SomeunsignedInt](q, r: var T, n_hi, n_lo, d: T) =
func div2n1n[T: SomeUnsignedInt](q, r: var T, n_hi, n_lo, d: T) =
# doAssert leadingZeros(d) == 0, "Divisor was not normalized"
@ -156,8 +156,8 @@ func div2n1n[T: SomeunsignedInt](q, r: var T, n_hi, n_lo, d: T) =
let
d_hi = d shr halfSize
d_lo = d and halfMask
n_lohi = nlo shr halfSize
n_lolo = nlo and halfMask
n_lohi = n_lo shr halfSize
n_lolo = n_lo and halfMask
# First half of the quotient
let (q1, r1) = halfQR(n_hi, n_lohi, d, d_hi, d_lo)

View File

@ -13,22 +13,22 @@ template chkNot(chk: untyped, a, b: distinct SomeInteger, bits: int) =
chk stint(a, bits).not() == stint(b, bits)
template chkNot(chk: untyped, a, b: string, bits: int) =
chk fromHex(Stint[bits], a).not() == fromHex(Stint[bits], b)
chk fromHex(StInt[bits], a).not() == fromHex(StInt[bits], b)
template chkOr(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stint[bits], a) or fromHex(Stint[bits], b)) == fromHex(Stint[bits], c)
chk (fromHex(StInt[bits], a) or fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkAnd(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stint[bits], a) and fromHex(Stint[bits], b)) == fromHex(Stint[bits], c)
chk (fromHex(StInt[bits], a) and fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkXor(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stint[bits], a) xor fromHex(Stint[bits], b)) == fromHex(Stint[bits], c)
chk (fromHex(StInt[bits], a) xor fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkShl(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk (fromHex(Stint[bits], a) shl b) == fromHex(Stint[bits], c)
chk (fromHex(StInt[bits], a) shl b) == fromHex(StInt[bits], c)
template chkShr(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk (fromHex(Stint[bits], a) shr b) == fromHex(Stint[bits], c)
chk (fromHex(StInt[bits], a) shr b) == fromHex(StInt[bits], c)
template testBitwise(chk, tst: untyped) =
@ -328,7 +328,7 @@ suite "Testing signed int bitwise operations":
for i in 1..255:
let x = 1.i256 shl i
y = y shl 1
check cast[stint.Uint256](x) == y
check cast[stint.UInt256](x) == y
test "Shift Right on positive int":
const leftMost = 1.i256 shl 254
@ -358,7 +358,7 @@ suite "Testing signed int bitwise operations":
const
a = (high(stint.Int256) shl 10) shr 10
b = (high(stint.Uint256) shl 10) shr 10
b = (high(stint.UInt256) shl 10) shr 10
c = (high(stint.Int256) shl 10) shr 10
check a != cast[stint.Int256](b)

View File

@ -10,46 +10,46 @@
import ../stint, unittest, test_helpers
template chkLT(chk: untyped, a, b: string, bits: int) =
chk fromHex(Stint[bits], a) < fromHex(Stint[bits], b)
chk fromHex(StInt[bits], a) < fromHex(StInt[bits], b)
template chknotLT(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(Stint[bits], b) < fromHex(Stint[bits], a)))
template chkNotLT(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StInt[bits], b) < fromHex(StInt[bits], a)))
template chkLTE(chk: untyped, a, b: string, bits: int) =
chk fromHex(Stint[bits], a) <= fromHex(Stint[bits], b)
chk fromHex(StInt[bits], a) <= fromHex(StInt[bits], b)
template chknotLTE(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(Stint[bits], b) <= fromHex(Stint[bits], a)))
template chkNotLTE(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StInt[bits], b) <= fromHex(StInt[bits], a)))
template chkEQ(chk: untyped, a, b: string, bits: int) =
chk fromHex(Stint[bits], a) == fromHex(Stint[bits], b)
chk fromHex(StInt[bits], a) == fromHex(StInt[bits], b)
template chknotEQ(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(Stint[bits], a) == fromHex(Stint[bits], b)))
template chkNotEQ(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StInt[bits], a) == fromHex(StInt[bits], b)))
template chkisZero(chk: untyped, a: string, bits: int) =
chk fromHex(Stint[bits], a).isZero()
template chkIsZero(chk: untyped, a: string, bits: int) =
chk fromHex(StInt[bits], a).isZero()
template chknotisZero(chk: untyped, a: string, bits: int) =
chk (not fromHex(Stint[bits], a).isZero())
template chkNotIsZero(chk: untyped, a: string, bits: int) =
chk (not fromHex(StInt[bits], a).isZero())
template chkisNegative(chk: untyped, a: string, bits: int) =
chk fromHex(Stint[bits], a).isNegative()
template chkIsNegative(chk: untyped, a: string, bits: int) =
chk fromHex(StInt[bits], a).isNegative()
template chknotisNegative(chk: untyped, a: string, bits: int) =
chk (not fromHex(Stint[bits], a).isNegative())
template chkNotIsNegative(chk: untyped, a: string, bits: int) =
chk (not fromHex(StInt[bits], a).isNegative())
template chkisOdd(chk: untyped, a: string, bits: int) =
chk fromHex(Stint[bits], a).isOdd()
template chkIsOdd(chk: untyped, a: string, bits: int) =
chk fromHex(StInt[bits], a).isOdd()
template chknotisOdd(chk: untyped, a: string, bits: int) =
chk (not fromHex(Stint[bits], a).isOdd())
template chkNotIsOdd(chk: untyped, a: string, bits: int) =
chk (not fromHex(StInt[bits], a).isOdd())
template chkisEven(chk: untyped, a: string, bits: int) =
chk fromHex(Stint[bits], a).isEven()
template chkIsEven(chk: untyped, a: string, bits: int) =
chk fromHex(StInt[bits], a).isEven()
template chknotisEven(chk: untyped, a: string, bits: int) =
chk (not fromHex(Stint[bits], a).isEven())
template chkNotIsEven(chk: untyped, a: string, bits: int) =
chk (not fromHex(StInt[bits], a).isEven())
template testComparison(chk, tst: untyped) =
tst "operator `LT`":
@ -84,35 +84,35 @@ template testComparison(chk, tst: untyped) =
chkLT(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator not `LT`":
chknotLT(chk, "0", "F", 8)
chknotLT(chk, "F", "7F", 8)
chknotLT(chk, "FF", "7F", 8)
chkNotLT(chk, "0", "F", 8)
chkNotLT(chk, "F", "7F", 8)
chkNotLT(chk, "FF", "7F", 8)
chknotLT(chk, "0", "F", 16)
chknotLT(chk, "F", "FF", 16)
chknotLT(chk, "FF", "FFF", 16)
chknotLT(chk, "FFFF", "FFF", 16)
chkNotLT(chk, "0", "F", 16)
chkNotLT(chk, "F", "FF", 16)
chkNotLT(chk, "FF", "FFF", 16)
chkNotLT(chk, "FFFF", "FFF", 16)
chknotLT(chk, "0", "F", 32)
chknotLT(chk, "F", "FF", 32)
chknotLT(chk, "FF", "FFF", 32)
chknotLT(chk, "FFFF", "FFFFF", 32)
chknotLT(chk, "FFFFFFFF", "FFFFF", 32)
chkNotLT(chk, "0", "F", 32)
chkNotLT(chk, "F", "FF", 32)
chkNotLT(chk, "FF", "FFF", 32)
chkNotLT(chk, "FFFF", "FFFFF", 32)
chkNotLT(chk, "FFFFFFFF", "FFFFF", 32)
chknotLT(chk, "0", "F", 64)
chknotLT(chk, "F", "FF", 64)
chknotLT(chk, "FF", "FFF", 64)
chknotLT(chk, "FFFF", "FFFFF", 64)
chknotLT(chk, "FFFFF", "FFFFFFFF", 64)
chknotLT(chk, "FFFFFFFFFFFFFFFF", "FFFFFFFF", 64)
chkNotLT(chk, "0", "F", 64)
chkNotLT(chk, "F", "FF", 64)
chkNotLT(chk, "FF", "FFF", 64)
chkNotLT(chk, "FFFF", "FFFFF", 64)
chkNotLT(chk, "FFFFF", "FFFFFFFF", 64)
chkNotLT(chk, "FFFFFFFFFFFFFFFF", "FFFFFFFF", 64)
chknotLT(chk, "0", "F", 128)
chknotLT(chk, "F", "FF", 128)
chknotLT(chk, "FF", "FFF", 128)
chknotLT(chk, "FFFF", "FFFFF", 128)
chknotLT(chk, "FFFFF", "FFFFFFFF", 128)
chknotLT(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chknotLT(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLT(chk, "0", "F", 128)
chkNotLT(chk, "F", "FF", 128)
chkNotLT(chk, "FF", "FFF", 128)
chkNotLT(chk, "FFFF", "FFFFF", 128)
chkNotLT(chk, "FFFFF", "FFFFFFFF", 128)
chkNotLT(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLT(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `LTE`":
chkLTE(chk, "0", "F", 8)
@ -151,35 +151,35 @@ template testComparison(chk, tst: untyped) =
chkLTE(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator not `LTE`":
chknotLTE(chk, "0", "F", 8)
chknotLTE(chk, "F", "7F", 8)
chknotLTE(chk, "FF", "7F", 8)
chkNotLTE(chk, "0", "F", 8)
chkNotLTE(chk, "F", "7F", 8)
chkNotLTE(chk, "FF", "7F", 8)
chknotLTE(chk, "0", "F", 16)
chknotLTE(chk, "F", "FF", 16)
chknotLTE(chk, "FF", "FFF", 16)
chknotLTE(chk, "FFFF", "FFF", 16)
chkNotLTE(chk, "0", "F", 16)
chkNotLTE(chk, "F", "FF", 16)
chkNotLTE(chk, "FF", "FFF", 16)
chkNotLTE(chk, "FFFF", "FFF", 16)
chknotLTE(chk, "0", "F", 32)
chknotLTE(chk, "F", "FF", 32)
chknotLTE(chk, "FF", "FFF", 32)
chknotLTE(chk, "FFFF", "FFFFF", 32)
chknotLTE(chk, "FFFFFFFF", "FFFFF", 32)
chkNotLTE(chk, "0", "F", 32)
chkNotLTE(chk, "F", "FF", 32)
chkNotLTE(chk, "FF", "FFF", 32)
chkNotLTE(chk, "FFFF", "FFFFF", 32)
chkNotLTE(chk, "FFFFFFFF", "FFFFF", 32)
chknotLTE(chk, "0", "F", 64)
chknotLTE(chk, "F", "FF", 64)
chknotLTE(chk, "FF", "FFF", 64)
chknotLTE(chk, "FFFF", "FFFFF", 64)
chknotLTE(chk, "FFFFF", "FFFFFFFF", 64)
chknotLTE(chk, "FFFFFFFFFFFFFFFF", "FFFFFFFF", 64)
chkNotLTE(chk, "0", "F", 64)
chkNotLTE(chk, "F", "FF", 64)
chkNotLTE(chk, "FF", "FFF", 64)
chkNotLTE(chk, "FFFF", "FFFFF", 64)
chkNotLTE(chk, "FFFFF", "FFFFFFFF", 64)
chkNotLTE(chk, "FFFFFFFFFFFFFFFF", "FFFFFFFF", 64)
chknotLTE(chk, "0", "F", 128)
chknotLTE(chk, "F", "FF", 128)
chknotLTE(chk, "FF", "FFF", 128)
chknotLTE(chk, "FFFF", "FFFFF", 128)
chknotLTE(chk, "FFFFF", "FFFFFFFF", 128)
chknotLTE(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chknotLTE(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLTE(chk, "0", "F", 128)
chkNotLTE(chk, "F", "FF", 128)
chkNotLTE(chk, "FF", "FFF", 128)
chkNotLTE(chk, "FFFF", "FFFFF", 128)
chkNotLTE(chk, "FFFFF", "FFFFFFFF", 128)
chkNotLTE(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLTE(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `EQ`":
chkEQ(chk, "0", "0", 8)
@ -213,30 +213,30 @@ template testComparison(chk, tst: untyped) =
chkEQ(chk, "FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator not `EQ`":
chknotEQ(chk, "0", "F", 8)
chknotEQ(chk, "F", "FF", 8)
chkNotEQ(chk, "0", "F", 8)
chkNotEQ(chk, "F", "FF", 8)
chknotEQ(chk, "0", "F", 16)
chknotEQ(chk, "F", "FF", 16)
chknotEQ(chk, "FF", "FFA", 16)
chkNotEQ(chk, "0", "F", 16)
chkNotEQ(chk, "F", "FF", 16)
chkNotEQ(chk, "FF", "FFA", 16)
chknotEQ(chk, "0", "F", 32)
chknotEQ(chk, "F", "FF", 32)
chknotEQ(chk, "FF", "FFF", 32)
chknotEQ(chk, "FFFF", "FAFFF", 32)
chkNotEQ(chk, "0", "F", 32)
chkNotEQ(chk, "F", "FF", 32)
chkNotEQ(chk, "FF", "FFF", 32)
chkNotEQ(chk, "FFFF", "FAFFF", 32)
chknotEQ(chk, "0", "F", 64)
chknotEQ(chk, "F", "FF", 64)
chknotEQ(chk, "FF", "FFF", 64)
chknotEQ(chk, "FFFF", "FFFFF", 64)
chknotEQ(chk, "FFFFF", "FAFFFFFFF", 64)
chkNotEQ(chk, "0", "F", 64)
chkNotEQ(chk, "F", "FF", 64)
chkNotEQ(chk, "FF", "FFF", 64)
chkNotEQ(chk, "FFFF", "FFFFF", 64)
chkNotEQ(chk, "FFFFF", "FAFFFFFFF", 64)
chknotEQ(chk, "0", "F", 128)
chknotEQ(chk, "F", "FF", 128)
chknotEQ(chk, "FF", "FFF", 128)
chknotEQ(chk, "FFFF", "FFFFF", 128)
chknotEQ(chk, "FFFFF", "FFAFFFFF", 128)
chknotEQ(chk, "FFFFFFFFFFF", "AFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotEQ(chk, "0", "F", 128)
chkNotEQ(chk, "F", "FF", 128)
chkNotEQ(chk, "FF", "FFF", 128)
chkNotEQ(chk, "FFFF", "FFFFF", 128)
chkNotEQ(chk, "FFFFF", "FFAFFFFF", 128)
chkNotEQ(chk, "FFFFFFFFFFF", "AFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `isZero`":
chkIsZero(chk, "0", 8)
@ -247,56 +247,56 @@ template testComparison(chk, tst: untyped) =
chkIsZero(chk, "0", 256)
tst "operator not `isZero`":
chknotIsZero(chk, "1", 8)
chknotIsZero(chk, "2", 16)
chknotIsZero(chk, "3", 32)
chknotIsZero(chk, "4", 64)
chknotIsZero(chk, "5", 128)
chknotIsZero(chk, "6", 256)
chkNotIsZero(chk, "1", 8)
chkNotIsZero(chk, "2", 16)
chkNotIsZero(chk, "3", 32)
chkNotIsZero(chk, "4", 64)
chkNotIsZero(chk, "5", 128)
chkNotIsZero(chk, "6", 256)
chknotIsZero(chk, "FF", 8)
chknotIsZero(chk, "FFFF", 16)
chknotIsZero(chk, "FFFFFFFF", 32)
chknotIsZero(chk, "FFFFFFFFFFFFFFFF", 64)
chknotIsZero(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chknotIsZero(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 256)
chkNotIsZero(chk, "FF", 8)
chkNotIsZero(chk, "FFFF", 16)
chkNotIsZero(chk, "FFFFFFFF", 32)
chkNotIsZero(chk, "FFFFFFFFFFFFFFFF", 64)
chkNotIsZero(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotIsZero(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 256)
tst "operator `isNegative`":
chkisNegative(chk, "F0", 8)
chkisNegative(chk, "F000", 16)
chkisNegative(chk, "F0000000", 32)
chkisNegative(chk, "F000000000000000", 64)
chkisNegative(chk, "F0000000000000000000000000000000", 128)
chkisNegative(chk, "F000000000000000000000000000000000000000000000000000000000000000", 256)
chkIsNegative(chk, "F0", 8)
chkIsNegative(chk, "F000", 16)
chkIsNegative(chk, "F0000000", 32)
chkIsNegative(chk, "F000000000000000", 64)
chkIsNegative(chk, "F0000000000000000000000000000000", 128)
chkIsNegative(chk, "F000000000000000000000000000000000000000000000000000000000000000", 256)
chkisNegative(chk, "A1", 8)
chkisNegative(chk, "A200", 16)
chkisNegative(chk, "A3000000", 32)
chkisNegative(chk, "A400000000000000", 64)
chkisNegative(chk, "A5000000000000000000000000000000", 128)
chkisNegative(chk, "A600000000000000000000000000000000000000000000000000000000000000", 256)
chkIsNegative(chk, "A1", 8)
chkIsNegative(chk, "A200", 16)
chkIsNegative(chk, "A3000000", 32)
chkIsNegative(chk, "A400000000000000", 64)
chkIsNegative(chk, "A5000000000000000000000000000000", 128)
chkIsNegative(chk, "A600000000000000000000000000000000000000000000000000000000000000", 256)
tst "operator not `isNegative`":
chknotIsNegative(chk, "0", 8)
chknotIsNegative(chk, "0", 16)
chknotIsNegative(chk, "0", 32)
chknotIsNegative(chk, "0", 64)
chknotIsNegative(chk, "0", 128)
chknotIsNegative(chk, "0", 256)
chkNotIsNegative(chk, "0", 8)
chkNotIsNegative(chk, "0", 16)
chkNotIsNegative(chk, "0", 32)
chkNotIsNegative(chk, "0", 64)
chkNotIsNegative(chk, "0", 128)
chkNotIsNegative(chk, "0", 256)
chknotIsNegative(chk, "1", 8)
chknotIsNegative(chk, "2", 16)
chknotIsNegative(chk, "3", 32)
chknotIsNegative(chk, "4", 64)
chknotIsNegative(chk, "5", 128)
chknotIsNegative(chk, "6", 256)
chkNotIsNegative(chk, "1", 8)
chkNotIsNegative(chk, "2", 16)
chkNotIsNegative(chk, "3", 32)
chkNotIsNegative(chk, "4", 64)
chkNotIsNegative(chk, "5", 128)
chkNotIsNegative(chk, "6", 256)
chknotIsNegative(chk, "71", 8)
chknotIsNegative(chk, "7200", 16)
chknotIsNegative(chk, "73000000", 32)
chknotIsNegative(chk, "7400000000000000", 64)
chknotIsNegative(chk, "75000000000000000000000000000000", 128)
chknotIsNegative(chk, "7600000000000000000000000000000000000000000000000000000000000000", 256)
chkNotIsNegative(chk, "71", 8)
chkNotIsNegative(chk, "7200", 16)
chkNotIsNegative(chk, "73000000", 32)
chkNotIsNegative(chk, "7400000000000000", 64)
chkNotIsNegative(chk, "75000000000000000000000000000000", 128)
chkNotIsNegative(chk, "7600000000000000000000000000000000000000000000000000000000000000", 256)
tst "operator `isOdd`":
chkIsOdd(chk, "1", 8)

View File

@ -10,19 +10,19 @@
import ../stint, unittest, test_helpers
template chkMul(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stint[bits], a) * fromHex(Stint[bits], b)) == fromHex(Stint[bits], c)
chk (fromHex(StInt[bits], a) * fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkDiv(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stint[bits], a) div fromHex(Stint[bits], b)) == fromHex(Stint[bits], c)
chk (fromHex(StInt[bits], a) div fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkMod(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stint[bits], a) mod fromHex(Stint[bits], b)) == fromHex(Stint[bits], c)
chk (fromHex(StInt[bits], a) mod fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkMod(chk: untyped, a, b, c: int, bits: int) =
chk (stint(a, bits) mod stint(b, bits)) == stint(c, bits)
template chkDivMod(chk: untyped, a, b, c, d: string, bits: int) =
chk divmod(fromHex(Stint[bits], a), fromHex(Stint[bits], b)) == (fromHex(Stint[bits], c), fromHex(Stint[bits], d))
chk divmod(fromHex(StInt[bits], a), fromHex(StInt[bits], b)) == (fromHex(StInt[bits], c), fromHex(StInt[bits], d))
template testMuldiv(chk, tst: untyped) =
tst "operator `mul`":
@ -333,8 +333,8 @@ suite "Testing signed int division and modulo implementation":
# check: r.hi == 0'u64
test "Divmod(1234567891234567890, 10) returns the correct result":
let a = cast[Stint[64]](1234567891234567890'i64)
let b = cast[Stint[64]](10'i64)
let a = cast[StInt[64]](1234567891234567890'i64)
let b = cast[StInt[64]](10'i64)
let qr = divmod(a, b)

View File

@ -26,27 +26,27 @@ template chkTruncateStint(chk, number, toType: untyped, res: string, bits: int)
let x = (number.stint(bits)).truncate(toType).toHex()
chk "0x" & x == res
template chkRoundtripStuint(chk: untyped, prefix, str: string, bits, radix: int) =
template chkRoundTripStuint(chk: untyped, prefix, str: string, bits, radix: int) =
block:
let data = prefix & str
let x = parse(data, Stuint[bits], radix)
let x = parse(data, StUint[bits], radix)
let y = x.toString(radix)
chk y == str
template chkRoundtripStuint(chk: untyped, str: string, bits, radix: int) =
chkRoundtripStuint(chk, "", str, bits, radix)
template chkRoundTripStuint(chk: untyped, str: string, bits, radix: int) =
chkRoundTripStuint(chk, "", str, bits, radix)
template chkRoundtripStint(chk: untyped, prefix, str: string, bits, radix: int) =
template chkRoundTripStint(chk: untyped, prefix, str: string, bits, radix: int) =
block:
let data = prefix & str
let x = parse(data, Stint[bits], radix)
let x = parse(data, StInt[bits], radix)
let y = x.toString(radix)
chk y == str
template chkRoundtripStint(chk: untyped, str: string, bits, radix: int) =
chkRoundtripStint(chk, "", str, bits, radix)
template chkRoundTripStint(chk: untyped, str: string, bits, radix: int) =
chkRoundTripStint(chk, "", str, bits, radix)
template chkRoundtripBin(chk, chkProc: untyped, bits, rep: int) =
template chkRoundTripBin(chk, chkProc: untyped, bits, rep: int) =
chkProc(chk, "0", bits, 2)
chkProc(chk, repeat("1", rep), bits, 2)
chkProc(chk, repeat("1010", rep), bits, 2)
@ -56,7 +56,7 @@ template chkRoundtripBin(chk, chkProc: untyped, bits, rep: int) =
chkProc(chk, repeat("1010101", rep), bits, 2)
chkProc(chk, repeat("11111111", rep), bits, 2)
template chkRoundtripHex(chk, chkProc: untyped, bits, rep: int) =
template chkRoundTripHex(chk, chkProc: untyped, bits, rep: int) =
chkProc(chk, "0", bits, 16)
chkProc(chk, repeat("1", rep), bits, 16)
chkProc(chk, repeat("7", rep), bits, 16)
@ -65,13 +65,13 @@ template chkRoundtripHex(chk, chkProc: untyped, bits, rep: int) =
chkProc(chk, repeat("ff", rep), bits, 16)
chkProc(chk, repeat("f0", rep), bits, 16)
template chkRoundtripOct(chk, chkProc: untyped, bits, rep: int) =
template chkRoundTripOct(chk, chkProc: untyped, bits, rep: int) =
chkProc(chk, "0", bits, 8)
chkProc(chk, repeat("1", rep), bits, 8)
chkProc(chk, repeat("7", rep), bits, 8)
chkProc(chk, repeat("177", rep), bits, 8)
template chkRoundtripDec(chk, chkProc: untyped, bits, rep: int) =
template chkRoundTripDec(chk, chkProc: untyped, bits, rep: int) =
chkProc(chk, "0", bits, 10)
chkProc(chk, repeat("1", rep), bits, 10)
chkProc(chk, repeat("9", rep), bits, 10)
@ -84,7 +84,7 @@ func toByteArray(x: static[string]): auto {.compileTime.} =
template chkRoundtripBE(chk: untyped, str: string, bits: int) =
block:
const data = toByteArray(str)
var x: Stuint[bits]
var x: StUint[bits]
initFromBytesBE(x, data)
let y = toByteArrayBE(x)
chk y == data
@ -100,14 +100,14 @@ template chkCTvsRT(chk: untyped, num: untyped, bits: int) =
template chkDumpHexStuint(chk: untyped, BE, LE: string, bits: int) =
block:
let data = BE
let x = fromHex(Stuint[bits], data)
let x = fromHex(StUint[bits], data)
chk dumpHex(x, bigEndian) == data
chk dumpHex(x, littleEndian) == LE
template chkDumpHexStint(chk: untyped, BE, LE: string, bits: int) =
block:
let data = BE
let x = fromHex(Stint[bits], data)
let x = fromHex(StInt[bits], data)
chk dumpHex(x, bigEndian) == data
chk dumpHex(x, littleEndian) == LE
@ -697,19 +697,19 @@ template testIO(chk, tst: untyped) =
chkRoundTripHex(chk, chkRoundTripStuint, 128, 16)
chkRoundTripOct(chk, chkRoundTripStuint, 8, 1)
chkRoundtripStuint(chk, "377", 8, 8)
chkRoundTripStuint(chk, "377", 8, 8)
chkRoundTripOct(chk, chkRoundTripStuint, 16, 1)
chkRoundTripOct(chk, chkRoundTripStuint, 16, 2)
chkRoundtripStuint(chk, "377", 16, 8)
chkRoundtripStuint(chk, "177777", 16, 8)
chkRoundTripStuint(chk, "377", 16, 8)
chkRoundTripStuint(chk, "177777", 16, 8)
chkRoundTripOct(chk, chkRoundTripStuint, 32, 1)
chkRoundTripOct(chk, chkRoundTripStuint, 32, 2)
chkRoundTripOct(chk, chkRoundTripStuint, 32, 3)
chkRoundtripStuint(chk, "377", 32, 8)
chkRoundtripStuint(chk, "177777", 32, 8)
chkRoundtripStuint(chk, "37777777777", 32, 8)
chkRoundTripStuint(chk, "377", 32, 8)
chkRoundTripStuint(chk, "177777", 32, 8)
chkRoundTripStuint(chk, "37777777777", 32, 8)
chkRoundTripOct(chk, chkRoundTripStuint, 64, 1)
chkRoundTripOct(chk, chkRoundTripStuint, 64, 2)
@ -718,10 +718,10 @@ template testIO(chk, tst: untyped) =
chkRoundTripOct(chk, chkRoundTripStuint, 64, 5)
chkRoundTripOct(chk, chkRoundTripStuint, 64, 6)
chkRoundTripOct(chk, chkRoundTripStuint, 64, 7)
chkRoundtripStuint(chk, "377", 64, 8)
chkRoundtripStuint(chk, "177777", 64, 8)
chkRoundtripStuint(chk, "37777777777", 64, 8)
chkRoundtripStuint(chk, "1777777777777777777777", 64, 8)
chkRoundTripStuint(chk, "377", 64, 8)
chkRoundTripStuint(chk, "177777", 64, 8)
chkRoundTripStuint(chk, "37777777777", 64, 8)
chkRoundTripStuint(chk, "1777777777777777777777", 64, 8)
chkRoundTripOct(chk, chkRoundTripStuint, 128, 1)
chkRoundTripOct(chk, chkRoundTripStuint, 128, 2)
@ -737,26 +737,26 @@ template testIO(chk, tst: untyped) =
chkRoundTripOct(chk, chkRoundTripStuint, 128, 12)
chkRoundTripOct(chk, chkRoundTripStuint, 128, 13)
chkRoundTripOct(chk, chkRoundTripStuint, 128, 14)
chkRoundtripStuint(chk, "377", 128, 8)
chkRoundtripStuint(chk, "177777", 128, 8)
chkRoundtripStuint(chk, "37777777777", 128, 8)
chkRoundtripStuint(chk, "1777777777777777777777", 128, 8)
chkRoundtripStuint(chk, "3777777777777777777777777777777777777777777", 128, 8)
chkRoundTripStuint(chk, "377", 128, 8)
chkRoundTripStuint(chk, "177777", 128, 8)
chkRoundTripStuint(chk, "37777777777", 128, 8)
chkRoundTripStuint(chk, "1777777777777777777777", 128, 8)
chkRoundTripStuint(chk, "3777777777777777777777777777777777777777777", 128, 8)
chkRoundTripDec(chk, chkRoundTripStuint, 8, 1)
chkRoundtripStuint(chk, "255", 8, 10)
chkRoundTripStuint(chk, "255", 8, 10)
chkRoundTripDec(chk, chkRoundTripStuint, 16, 1)
chkRoundTripDec(chk, chkRoundTripStuint, 16, 2)
chkRoundtripStuint(chk, "255", 16, 10)
chkRoundtripStuint(chk, "65535", 16, 10)
chkRoundTripStuint(chk, "255", 16, 10)
chkRoundTripStuint(chk, "65535", 16, 10)
chkRoundTripDec(chk, chkRoundTripStuint, 32, 1)
chkRoundTripDec(chk, chkRoundTripStuint, 32, 2)
chkRoundTripDec(chk, chkRoundTripStuint, 32, 3)
chkRoundtripStuint(chk, "255", 32, 10)
chkRoundtripStuint(chk, "65535", 32, 10)
chkRoundtripStuint(chk, "4294967295", 32, 10)
chkRoundTripStuint(chk, "255", 32, 10)
chkRoundTripStuint(chk, "65535", 32, 10)
chkRoundTripStuint(chk, "4294967295", 32, 10)
chkRoundTripDec(chk, chkRoundTripStuint, 64, 1)
chkRoundTripDec(chk, chkRoundTripStuint, 64, 2)
@ -765,10 +765,10 @@ template testIO(chk, tst: untyped) =
chkRoundTripDec(chk, chkRoundTripStuint, 64, 5)
chkRoundTripDec(chk, chkRoundTripStuint, 64, 6)
chkRoundTripDec(chk, chkRoundTripStuint, 64, 7)
chkRoundtripStuint(chk, "255", 64, 10)
chkRoundtripStuint(chk, "65535", 64, 10)
chkRoundtripStuint(chk, "4294967295", 64, 10)
chkRoundtripStuint(chk, "18446744073709551615", 64, 10)
chkRoundTripStuint(chk, "255", 64, 10)
chkRoundTripStuint(chk, "65535", 64, 10)
chkRoundTripStuint(chk, "4294967295", 64, 10)
chkRoundTripStuint(chk, "18446744073709551615", 64, 10)
chkRoundTripDec(chk, chkRoundTripStuint, 128, 1)
chkRoundTripDec(chk, chkRoundTripStuint, 128, 2)
@ -784,25 +784,25 @@ template testIO(chk, tst: untyped) =
chkRoundTripDec(chk, chkRoundTripStuint, 128, 12)
chkRoundTripDec(chk, chkRoundTripStuint, 128, 13)
chkRoundTripDec(chk, chkRoundTripStuint, 128, 14)
chkRoundtripStuint(chk, "255", 128, 10)
chkRoundtripStuint(chk, "65535", 128, 10)
chkRoundtripStuint(chk, "4294967295", 128, 10)
chkRoundtripStuint(chk, "18446744073709551615", 128, 10)
chkRoundtripStuint(chk, "340282366920938463463374607431768211455", 128, 10)
chkRoundTripStuint(chk, "255", 128, 10)
chkRoundTripStuint(chk, "65535", 128, 10)
chkRoundTripStuint(chk, "4294967295", 128, 10)
chkRoundTripStuint(chk, "18446744073709551615", 128, 10)
chkRoundTripStuint(chk, "340282366920938463463374607431768211455", 128, 10)
tst "[stint] parse - toString roundtrip":
chkRoundTripBin(chk, chkRoundTripStint, 8, 1)
chkRoundtripStint(chk, "1" & repeat('0', 7), 8, 2)
chkRoundTripStint(chk, "1" & repeat('0', 7), 8, 2)
chkRoundTripBin(chk, chkRoundTripStint, 16, 1)
chkRoundTripBin(chk, chkRoundTripStint, 16, 2)
chkRoundtripStint(chk, "1" & repeat('0', 15), 16, 2)
chkRoundTripStint(chk, "1" & repeat('0', 15), 16, 2)
chkRoundTripBin(chk, chkRoundTripStint, 32, 1)
chkRoundTripBin(chk, chkRoundTripStint, 32, 2)
chkRoundTripBin(chk, chkRoundTripStint, 32, 3)
chkRoundTripBin(chk, chkRoundTripStint, 32, 4)
chkRoundtripStint(chk, "1" & repeat('0', 31), 32, 2)
chkRoundTripStint(chk, "1" & repeat('0', 31), 32, 2)
chkRoundTripBin(chk, chkRoundTripStint, 64, 1)
chkRoundTripBin(chk, chkRoundTripStint, 64, 2)
@ -812,7 +812,7 @@ template testIO(chk, tst: untyped) =
chkRoundTripBin(chk, chkRoundTripStint, 64, 6)
chkRoundTripBin(chk, chkRoundTripStint, 64, 7)
chkRoundTripBin(chk, chkRoundTripStint, 64, 8)
chkRoundtripStint(chk, "1" & repeat('0', 63), 64, 2)
chkRoundTripStint(chk, "1" & repeat('0', 63), 64, 2)
chkRoundTripBin(chk, chkRoundTripStint, 128, 1)
chkRoundTripBin(chk, chkRoundTripStint, 128, 2)
@ -830,20 +830,20 @@ template testIO(chk, tst: untyped) =
chkRoundTripBin(chk, chkRoundTripStint, 128, 14)
chkRoundTripBin(chk, chkRoundTripStint, 128, 15)
chkRoundTripBin(chk, chkRoundTripStint, 128, 16)
chkRoundtripStint(chk, "1" & repeat('0', 127), 128, 2)
chkRoundTripStint(chk, "1" & repeat('0', 127), 128, 2)
chkRoundTripHex(chk, chkRoundTripStint, 8, 1)
chkRoundtripStint(chk, "8" & repeat('0', 1), 8, 16)
chkRoundTripStint(chk, "8" & repeat('0', 1), 8, 16)
chkRoundTripHex(chk, chkRoundTripStint, 16, 1)
chkRoundTripHex(chk, chkRoundTripStint, 16, 2)
chkRoundtripStint(chk, "8" & repeat('0', 3), 16, 16)
chkRoundTripStint(chk, "8" & repeat('0', 3), 16, 16)
chkRoundTripHex(chk, chkRoundTripStint, 32, 1)
chkRoundTripHex(chk, chkRoundTripStint, 32, 2)
chkRoundTripHex(chk, chkRoundTripStint, 32, 3)
chkRoundTripHex(chk, chkRoundTripStint, 32, 4)
chkRoundtripStint(chk, "8" & repeat('0', 7), 32, 16)
chkRoundTripStint(chk, "8" & repeat('0', 7), 32, 16)
chkRoundTripHex(chk, chkRoundTripStint, 64, 1)
chkRoundTripHex(chk, chkRoundTripStint, 64, 2)
@ -853,7 +853,7 @@ template testIO(chk, tst: untyped) =
chkRoundTripHex(chk, chkRoundTripStint, 64, 6)
chkRoundTripHex(chk, chkRoundTripStint, 64, 7)
chkRoundTripHex(chk, chkRoundTripStint, 64, 8)
chkRoundtripStint(chk, "8" & repeat('0', 15), 64, 16)
chkRoundTripStint(chk, "8" & repeat('0', 15), 64, 16)
chkRoundTripHex(chk, chkRoundTripStint, 128, 1)
chkRoundTripHex(chk, chkRoundTripStint, 128, 2)
@ -871,28 +871,28 @@ template testIO(chk, tst: untyped) =
chkRoundTripHex(chk, chkRoundTripStint, 128, 14)
chkRoundTripHex(chk, chkRoundTripStint, 128, 15)
chkRoundTripHex(chk, chkRoundTripStint, 128, 16)
chkRoundtripStint(chk, "8" & repeat('0', 31), 128, 16)
chkRoundTripStint(chk, "8" & repeat('0', 31), 128, 16)
chkRoundTripOct(chk, chkRoundTripStint, 8, 1)
chkRoundtripStint(chk, "377", 8, 8)
chkRoundtripStint(chk, "200", 8, 8)
chkRoundTripStint(chk, "377", 8, 8)
chkRoundTripStint(chk, "200", 8, 8)
chkRoundTripOct(chk, chkRoundTripStint, 16, 1)
chkRoundTripOct(chk, chkRoundTripStint, 16, 2)
chkRoundtripStint(chk, "377", 16, 8)
chkRoundtripStint(chk, "200", 16, 8)
chkRoundtripStint(chk, "177777", 16, 8)
chkRoundtripStint(chk, "100000", 16, 8)
chkRoundTripStint(chk, "377", 16, 8)
chkRoundTripStint(chk, "200", 16, 8)
chkRoundTripStint(chk, "177777", 16, 8)
chkRoundTripStint(chk, "100000", 16, 8)
chkRoundTripOct(chk, chkRoundTripStint, 32, 1)
chkRoundTripOct(chk, chkRoundTripStint, 32, 2)
chkRoundTripOct(chk, chkRoundTripStint, 32, 3)
chkRoundtripStint(chk, "377", 32, 8)
chkRoundtripStint(chk, "200", 32, 8)
chkRoundtripStint(chk, "177777", 32, 8)
chkRoundtripStint(chk, "100000", 32, 8)
chkRoundtripStint(chk, "37777777777", 32, 8)
chkRoundtripStint(chk, "20000000000", 32, 8)
chkRoundTripStint(chk, "377", 32, 8)
chkRoundTripStint(chk, "200", 32, 8)
chkRoundTripStint(chk, "177777", 32, 8)
chkRoundTripStint(chk, "100000", 32, 8)
chkRoundTripStint(chk, "37777777777", 32, 8)
chkRoundTripStint(chk, "20000000000", 32, 8)
chkRoundTripOct(chk, chkRoundTripStint, 64, 1)
chkRoundTripOct(chk, chkRoundTripStint, 64, 2)
@ -901,14 +901,14 @@ template testIO(chk, tst: untyped) =
chkRoundTripOct(chk, chkRoundTripStint, 64, 5)
chkRoundTripOct(chk, chkRoundTripStint, 64, 6)
chkRoundTripOct(chk, chkRoundTripStint, 64, 7)
chkRoundtripStint(chk, "377", 64, 8)
chkRoundtripStint(chk, "200", 64, 8)
chkRoundtripStint(chk, "177777", 64, 8)
chkRoundtripStint(chk, "100000", 64, 8)
chkRoundtripStint(chk, "37777777777", 64, 8)
chkRoundtripStint(chk, "20000000000", 64, 8)
chkRoundtripStint(chk, "1777777777777777777777", 64, 8)
chkRoundtripStint(chk, "1000000000000000000000", 64, 8)
chkRoundTripStint(chk, "377", 64, 8)
chkRoundTripStint(chk, "200", 64, 8)
chkRoundTripStint(chk, "177777", 64, 8)
chkRoundTripStint(chk, "100000", 64, 8)
chkRoundTripStint(chk, "37777777777", 64, 8)
chkRoundTripStint(chk, "20000000000", 64, 8)
chkRoundTripStint(chk, "1777777777777777777777", 64, 8)
chkRoundTripStint(chk, "1000000000000000000000", 64, 8)
chkRoundTripOct(chk, chkRoundTripStint, 128, 1)
chkRoundTripOct(chk, chkRoundTripStint, 128, 2)
@ -924,42 +924,42 @@ template testIO(chk, tst: untyped) =
chkRoundTripOct(chk, chkRoundTripStint, 128, 12)
chkRoundTripOct(chk, chkRoundTripStint, 128, 13)
chkRoundTripOct(chk, chkRoundTripStint, 128, 14)
chkRoundtripStint(chk, "377", 128, 8)
chkRoundtripStint(chk, "200", 128, 8)
chkRoundtripStint(chk, "177777", 128, 8)
chkRoundtripStint(chk, "100000", 128, 8)
chkRoundtripStint(chk, "37777777777", 128, 8)
chkRoundtripStint(chk, "20000000000", 128, 8)
chkRoundtripStint(chk, "1777777777777777777777", 128, 8)
chkRoundtripStint(chk, "1000000000000000000000", 128, 8)
chkRoundtripStint(chk, "3777777777777777777777777777777777777777777", 128, 8)
chkRoundtripStint(chk, "2000000000000000000000000000000000000000000", 128, 8)
chkRoundTripStint(chk, "377", 128, 8)
chkRoundTripStint(chk, "200", 128, 8)
chkRoundTripStint(chk, "177777", 128, 8)
chkRoundTripStint(chk, "100000", 128, 8)
chkRoundTripStint(chk, "37777777777", 128, 8)
chkRoundTripStint(chk, "20000000000", 128, 8)
chkRoundTripStint(chk, "1777777777777777777777", 128, 8)
chkRoundTripStint(chk, "1000000000000000000000", 128, 8)
chkRoundTripStint(chk, "3777777777777777777777777777777777777777777", 128, 8)
chkRoundTripStint(chk, "2000000000000000000000000000000000000000000", 128, 8)
chkRoundTripDec(chk, chkRoundTripStint, 8, 1)
chkRoundtripStint(chk, "127", 8, 10)
chkRoundtripStint(chk, "-127", 8, 10)
# chkRoundtripStint(chk, "-128", 8, 10) # TODO: not supported yet
chkRoundTripStint(chk, "127", 8, 10)
chkRoundTripStint(chk, "-127", 8, 10)
# chkRoundTripStint(chk, "-128", 8, 10) # TODO: not supported yet
chkRoundTripDec(chk, chkRoundTripStint, 16, 1)
chkRoundTripDec(chk, chkRoundTripStint, 16, 2)
chkRoundtripStint(chk, "255", 16, 10)
chkRoundtripStint(chk, "127", 16, 10)
chkRoundtripStint(chk, "-128", 16, 10)
chkRoundtripStint(chk, "32767", 16, 10)
chkRoundtripStint(chk, "-32767", 16, 10)
#chkRoundtripStint(chk, "-32768", 16, 10) # TODO: not supported yet
chkRoundTripStint(chk, "255", 16, 10)
chkRoundTripStint(chk, "127", 16, 10)
chkRoundTripStint(chk, "-128", 16, 10)
chkRoundTripStint(chk, "32767", 16, 10)
chkRoundTripStint(chk, "-32767", 16, 10)
#chkRoundTripStint(chk, "-32768", 16, 10) # TODO: not supported yet
chkRoundTripDec(chk, chkRoundTripStint, 32, 1)
chkRoundTripDec(chk, chkRoundTripStint, 32, 2)
chkRoundTripDec(chk, chkRoundTripStint, 32, 3)
chkRoundtripStint(chk, "255", 32, 10)
chkRoundtripStint(chk, "127", 32, 10)
chkRoundtripStint(chk, "-128", 32, 10)
chkRoundtripStint(chk, "32767", 32, 10)
chkRoundtripStint(chk, "-32768", 32, 10)
chkRoundtripStint(chk, "65535", 32, 10)
chkRoundtripStint(chk, "-2147483647", 32, 10)
#chkRoundtripStint(chk, "-2147483648", 32, 10) # TODO: not supported yet
chkRoundTripStint(chk, "255", 32, 10)
chkRoundTripStint(chk, "127", 32, 10)
chkRoundTripStint(chk, "-128", 32, 10)
chkRoundTripStint(chk, "32767", 32, 10)
chkRoundTripStint(chk, "-32768", 32, 10)
chkRoundTripStint(chk, "65535", 32, 10)
chkRoundTripStint(chk, "-2147483647", 32, 10)
#chkRoundTripStint(chk, "-2147483648", 32, 10) # TODO: not supported yet
chkRoundTripDec(chk, chkRoundTripStint, 64, 1)
chkRoundTripDec(chk, chkRoundTripStint, 64, 2)
@ -968,17 +968,17 @@ template testIO(chk, tst: untyped) =
chkRoundTripDec(chk, chkRoundTripStint, 64, 5)
chkRoundTripDec(chk, chkRoundTripStint, 64, 6)
chkRoundTripDec(chk, chkRoundTripStint, 64, 7)
chkRoundtripStint(chk, "255", 64, 10)
chkRoundtripStint(chk, "65535", 64, 10)
chkRoundtripStint(chk, "127", 64, 10)
chkRoundtripStint(chk, "-128", 64, 10)
chkRoundtripStint(chk, "32767", 64, 10)
chkRoundtripStint(chk, "-32768", 64, 10)
chkRoundtripStint(chk, "65535", 64, 10)
chkRoundtripStint(chk, "-2147483648", 64, 10)
chkRoundtripStint(chk, "4294967295", 64, 10)
chkRoundtripStint(chk, "-9223372036854775807", 64, 10)
#chkRoundtripStint(chk, "-9223372036854775808", 64, 10) # TODO: not supported yet
chkRoundTripStint(chk, "255", 64, 10)
chkRoundTripStint(chk, "65535", 64, 10)
chkRoundTripStint(chk, "127", 64, 10)
chkRoundTripStint(chk, "-128", 64, 10)
chkRoundTripStint(chk, "32767", 64, 10)
chkRoundTripStint(chk, "-32768", 64, 10)
chkRoundTripStint(chk, "65535", 64, 10)
chkRoundTripStint(chk, "-2147483648", 64, 10)
chkRoundTripStint(chk, "4294967295", 64, 10)
chkRoundTripStint(chk, "-9223372036854775807", 64, 10)
#chkRoundTripStint(chk, "-9223372036854775808", 64, 10) # TODO: not supported yet
chkRoundTripDec(chk, chkRoundTripStint, 128, 1)
chkRoundTripDec(chk, chkRoundTripStint, 128, 2)
@ -994,12 +994,12 @@ template testIO(chk, tst: untyped) =
chkRoundTripDec(chk, chkRoundTripStint, 128, 12)
chkRoundTripDec(chk, chkRoundTripStint, 128, 13)
chkRoundTripDec(chk, chkRoundTripStint, 128, 14)
chkRoundtripStint(chk, "255", 128, 10)
chkRoundtripStint(chk, "65535", 128, 10)
chkRoundtripStint(chk, "4294967295", 128, 10)
chkRoundtripStint(chk, "18446744073709551615", 128, 10)
chkRoundtripStint(chk, "-170141183460469231731687303715884105727", 128, 10)
#chkRoundtripStint(chk, "-170141183460469231731687303715884105728", 128, 10) # TODO: not supported yet
chkRoundTripStint(chk, "255", 128, 10)
chkRoundTripStint(chk, "65535", 128, 10)
chkRoundTripStint(chk, "4294967295", 128, 10)
chkRoundTripStint(chk, "18446744073709551615", 128, 10)
chkRoundTripStint(chk, "-170141183460469231731687303715884105727", 128, 10)
#chkRoundTripStint(chk, "-170141183460469231731687303715884105728", 128, 10) # TODO: not supported yet
tst "roundtrip initFromBytesBE and toByteArrayBE":
chkRoundtripBE(chk, "x", 8)
@ -1070,21 +1070,21 @@ proc main() =
test "Creation from decimal strings":
block:
let a = "123456789".parse(Stint[64])
let a = "123456789".parse(StInt[64])
let b = 123456789.stint(64)
check: a == b
check: 123456789'i64 == cast[int64](a)
block:
let a = "123456789".parse(Stuint[64])
let a = "123456789".parse(StUint[64])
let b = 123456789.stuint(64)
check: a == b
check: 123456789'u64 == cast[uint64](a)
block:
let a = "-123456789".parse(Stint[64])
let a = "-123456789".parse(StInt[64])
let b = (-123456789).stint(64)
check: a == b
@ -1092,14 +1092,14 @@ proc main() =
test "Creation from hex strings":
block:
let a = "0xFF".parse(Stint[64], radix = 16)
let a = "0xFF".parse(StInt[64], radix = 16)
let b = 255.stint(64)
check: a == b
check: 255'i64 == cast[int64](a)
block:
let a = "0xFF".parse(Stuint[64], radix = 16)
let a = "0xFF".parse(StUint[64], radix = 16)
let b = 255.stuint(64)
check: a == b
@ -1109,7 +1109,7 @@ proc main() =
check: a == a2
block:
let a = "0xFFFF".parse(Stint[16], 16)
let a = "0xFFFF".parse(StInt[16], 16)
let b = (-1'i16).stint(16)
check: a == b
@ -1160,7 +1160,7 @@ proc main() =
block:
let s = "1234567890123456789012345678901234567890123456789"
let a = parse(s, StUInt[512])
let a = parse(s, StUint[512])
check: a.toString == s
check: $a == s
@ -1199,7 +1199,7 @@ proc main() =
test "Parsing an unexpected 0x prefix for a decimal string is a CatchableError and not a defect":
let s = "0x123456"
expect(ValueError):
let value = parse(s, StUint[256], 10)
@ -1212,8 +1212,8 @@ proc main() =
test "explicit conversions from basic types":
type
UInt256 = Stuint[256]
Int128 = Stint[128]
UInt256 = StUint[256]
Int128 = StInt[128]
let x = 10.uint16
@ -1222,7 +1222,7 @@ proc main() =
x.to(Int128).bits == 128
test "hex -> uint256":
check: SECPK1_N_HEX.parse(Stuint[256], radix = 16) == SECPK1_N
check: SECPK1_N_HEX.parse(StUint[256], radix = 16) == SECPK1_N
test "uint256 -> hex":
check: SECPK1_N.dumpHex == SECPK1_N_HEX
@ -1238,7 +1238,7 @@ proc main() =
let
bytes = f.toByteArrayBE
nonZeroBytes = significantBytesBE(bytes)
fRestored = Uint256.fromBytesBE(bytes.toOpenArray(bytes.len - nonZeroBytes,
fRestored = UInt256.fromBytesBE(bytes.toOpenArray(bytes.len - nonZeroBytes,
bytes.len - 1))
check f == fRestored
@ -1309,16 +1309,16 @@ proc main() =
)
test "Alice signature":
check: alice.raw_sig.r.parse(Stuint[256], 16) == "80536744857756143861726945576089915884233437828013729338039544043241440681784".u256
check: alice.raw_sig.s.parse(Stuint[256], 16) == "1902566422691403459035240420865094128779958320521066670269403689808757640701".u256
check: alice.raw_sig.r.parse(StUint[256], 16) == "80536744857756143861726945576089915884233437828013729338039544043241440681784".u256
check: alice.raw_sig.s.parse(StUint[256], 16) == "1902566422691403459035240420865094128779958320521066670269403689808757640701".u256
test "Bob signature":
check: bob.raw_sig.r.parse(Stuint[256], 16) == "41741612198399299636429810387160790514780876799439767175315078161978521003886".u256
check: bob.raw_sig.s.parse(Stuint[256], 16) == "47545396818609319588074484786899049290652725314938191835667190243225814114102".u256
check: bob.raw_sig.r.parse(StUint[256], 16) == "41741612198399299636429810387160790514780876799439767175315078161978521003886".u256
check: bob.raw_sig.s.parse(StUint[256], 16) == "47545396818609319588074484786899049290652725314938191835667190243225814114102".u256
test "Eve signature":
check: eve.raw_sig.r.parse(Stuint[256], 16) == "84467545608142925331782333363288012579669270632210954476013542647119929595395".u256
check: eve.raw_sig.s.parse(Stuint[256], 16) == "43529886636775750164425297556346136250671451061152161143648812009114516499167".u256
check: eve.raw_sig.r.parse(StUint[256], 16) == "84467545608142925331782333363288012579669270632210954476013542647119929595395".u256
check: eve.raw_sig.s.parse(StUint[256], 16) == "43529886636775750164425297556346136250671451061152161143648812009114516499167".u256
test "Using stint values in a hash table":
block:

View File

@ -13,22 +13,22 @@ template chkNot(chk: untyped, a, b: distinct SomeInteger, bits: int) =
chk stuint(a, bits).not() == stuint(b, bits)
template chkNot(chk: untyped, a, b: string, bits: int) =
chk fromHex(Stuint[bits], a).not() == fromHex(Stuint[bits], b)
chk fromHex(StUint[bits], a).not() == fromHex(StUint[bits], b)
template chkOr(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stuint[bits], a) or fromHex(Stuint[bits], b)) == fromHex(Stuint[bits], c)
chk (fromHex(StUint[bits], a) or fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkAnd(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stuint[bits], a) and fromHex(Stuint[bits], b)) == fromHex(Stuint[bits], c)
chk (fromHex(StUint[bits], a) and fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkXor(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stuint[bits], a) xor fromHex(Stuint[bits], b)) == fromHex(Stuint[bits], c)
chk (fromHex(StUint[bits], a) xor fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkShl(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk (fromHex(Stuint[bits], a) shl b) == fromHex(Stuint[bits], c)
chk (fromHex(StUint[bits], a) shl b) == fromHex(StUint[bits], c)
template chkShr(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk (fromHex(Stuint[bits], a) shr b) == fromHex(Stuint[bits], c)
chk (fromHex(StUint[bits], a) shr b) == fromHex(StUint[bits], c)
template testBitwise(chk, tst: untyped) =

View File

@ -10,40 +10,40 @@
import ../stint, unittest, test_helpers
template chkLT(chk: untyped, a, b: string, bits: int) =
chk fromHex(Stuint[bits], a) < fromHex(Stuint[bits], b)
chk fromHex(StUint[bits], a) < fromHex(StUint[bits], b)
template chknotLT(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(Stuint[bits], b) < fromHex(Stuint[bits], a)))
template chkNotLT(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StUint[bits], b) < fromHex(StUint[bits], a)))
template chkLTE(chk: untyped, a, b: string, bits: int) =
chk fromHex(Stuint[bits], a) <= fromHex(Stuint[bits], b)
chk fromHex(StUint[bits], a) <= fromHex(StUint[bits], b)
template chknotLTE(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(Stuint[bits], b) <= fromHex(Stuint[bits], a)))
template chkNotLTE(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StUint[bits], b) <= fromHex(StUint[bits], a)))
template chkEQ(chk: untyped, a, b: string, bits: int) =
chk fromHex(Stuint[bits], a) == fromHex(Stuint[bits], b)
chk fromHex(StUint[bits], a) == fromHex(StUint[bits], b)
template chknotEQ(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(Stuint[bits], a) == fromHex(Stuint[bits], b)))
template chkNotEQ(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StUint[bits], a) == fromHex(StUint[bits], b)))
template chkisZero(chk: untyped, a: string, bits: int) =
chk fromHex(Stuint[bits], a).isZero()
template chkIsZero(chk: untyped, a: string, bits: int) =
chk fromHex(StUint[bits], a).isZero()
template chknotisZero(chk: untyped, a: string, bits: int) =
chk (not fromHex(Stuint[bits], a).isZero())
template chkNotIsZero(chk: untyped, a: string, bits: int) =
chk (not fromHex(StUint[bits], a).isZero())
template chkisOdd(chk: untyped, a: string, bits: int) =
chk fromHex(Stuint[bits], a).isOdd()
template chkIsOdd(chk: untyped, a: string, bits: int) =
chk fromHex(StUint[bits], a).isOdd()
template chknotisOdd(chk: untyped, a: string, bits: int) =
chk (not fromHex(Stuint[bits], a).isOdd())
template chkNotIsOdd(chk: untyped, a: string, bits: int) =
chk (not fromHex(StUint[bits], a).isOdd())
template chkisEven(chk: untyped, a: string, bits: int) =
chk fromHex(Stuint[bits], a).isEven()
template chkIsEven(chk: untyped, a: string, bits: int) =
chk fromHex(StUint[bits], a).isEven()
template chknotisEven(chk: untyped, a: string, bits: int) =
chk (not fromHex(Stuint[bits], a).isEven())
template chkNotIsEven(chk: untyped, a: string, bits: int) =
chk (not fromHex(StUint[bits], a).isEven())
template testComparison(chk, tst: untyped) =
tst "operator `LT`":
@ -73,30 +73,30 @@ template testComparison(chk, tst: untyped) =
chkLT(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator not `LT`":
chknotLT(chk, "0", "F", 8)
chknotLT(chk, "F", "FF", 8)
chkNotLT(chk, "0", "F", 8)
chkNotLT(chk, "F", "FF", 8)
chknotLT(chk, "0", "F", 16)
chknotLT(chk, "F", "FF", 16)
chknotLT(chk, "FF", "FFF", 16)
chkNotLT(chk, "0", "F", 16)
chkNotLT(chk, "F", "FF", 16)
chkNotLT(chk, "FF", "FFF", 16)
chknotLT(chk, "0", "F", 32)
chknotLT(chk, "F", "FF", 32)
chknotLT(chk, "FF", "FFF", 32)
chknotLT(chk, "FFFF", "FFFFF", 32)
chkNotLT(chk, "0", "F", 32)
chkNotLT(chk, "F", "FF", 32)
chkNotLT(chk, "FF", "FFF", 32)
chkNotLT(chk, "FFFF", "FFFFF", 32)
chknotLT(chk, "0", "F", 64)
chknotLT(chk, "F", "FF", 64)
chknotLT(chk, "FF", "FFF", 64)
chknotLT(chk, "FFFF", "FFFFF", 64)
chknotLT(chk, "FFFFF", "FFFFFFFF", 64)
chkNotLT(chk, "0", "F", 64)
chkNotLT(chk, "F", "FF", 64)
chkNotLT(chk, "FF", "FFF", 64)
chkNotLT(chk, "FFFF", "FFFFF", 64)
chkNotLT(chk, "FFFFF", "FFFFFFFF", 64)
chknotLT(chk, "0", "F", 128)
chknotLT(chk, "F", "FF", 128)
chknotLT(chk, "FF", "FFF", 128)
chknotLT(chk, "FFFF", "FFFFF", 128)
chknotLT(chk, "FFFFF", "FFFFFFFF", 128)
chknotLT(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLT(chk, "0", "F", 128)
chkNotLT(chk, "F", "FF", 128)
chkNotLT(chk, "FF", "FFF", 128)
chkNotLT(chk, "FFFF", "FFFFF", 128)
chkNotLT(chk, "FFFFF", "FFFFFFFF", 128)
chkNotLT(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `LTE`":
chkLTE(chk, "0", "F", 8)
@ -130,30 +130,30 @@ template testComparison(chk, tst: untyped) =
chkLTE(chk, "FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator not `LTE`":
chknotLTE(chk, "0", "F", 8)
chknotLTE(chk, "F", "FF", 8)
chkNotLTE(chk, "0", "F", 8)
chkNotLTE(chk, "F", "FF", 8)
chknotLTE(chk, "0", "F", 16)
chknotLTE(chk, "F", "FF", 16)
chknotLTE(chk, "FF", "FFF", 16)
chkNotLTE(chk, "0", "F", 16)
chkNotLTE(chk, "F", "FF", 16)
chkNotLTE(chk, "FF", "FFF", 16)
chknotLTE(chk, "0", "F", 32)
chknotLTE(chk, "F", "FF", 32)
chknotLTE(chk, "FF", "FFF", 32)
chknotLTE(chk, "FFFF", "FFFFF", 32)
chkNotLTE(chk, "0", "F", 32)
chkNotLTE(chk, "F", "FF", 32)
chkNotLTE(chk, "FF", "FFF", 32)
chkNotLTE(chk, "FFFF", "FFFFF", 32)
chknotLTE(chk, "0", "F", 64)
chknotLTE(chk, "F", "FF", 64)
chknotLTE(chk, "FF", "FFF", 64)
chknotLTE(chk, "FFFF", "FFFFF", 64)
chknotLTE(chk, "FFFFF", "FFFFFFFF", 64)
chkNotLTE(chk, "0", "F", 64)
chkNotLTE(chk, "F", "FF", 64)
chkNotLTE(chk, "FF", "FFF", 64)
chkNotLTE(chk, "FFFF", "FFFFF", 64)
chkNotLTE(chk, "FFFFF", "FFFFFFFF", 64)
chknotLTE(chk, "0", "F", 128)
chknotLTE(chk, "F", "FF", 128)
chknotLTE(chk, "FF", "FFF", 128)
chknotLTE(chk, "FFFF", "FFFFF", 128)
chknotLTE(chk, "FFFFF", "FFFFFFFF", 128)
chknotLTE(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLTE(chk, "0", "F", 128)
chkNotLTE(chk, "F", "FF", 128)
chkNotLTE(chk, "FF", "FFF", 128)
chkNotLTE(chk, "FFFF", "FFFFF", 128)
chkNotLTE(chk, "FFFFF", "FFFFFFFF", 128)
chkNotLTE(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `EQ`":
chkEQ(chk, "0", "0", 8)
@ -186,30 +186,30 @@ template testComparison(chk, tst: untyped) =
chkEQ(chk, "FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator not `EQ`":
chknotEQ(chk, "0", "F", 8)
chknotEQ(chk, "F", "FF", 8)
chkNotEQ(chk, "0", "F", 8)
chkNotEQ(chk, "F", "FF", 8)
chknotEQ(chk, "0", "F", 16)
chknotEQ(chk, "F", "FF", 16)
chknotEQ(chk, "FF", "FFF", 16)
chkNotEQ(chk, "0", "F", 16)
chkNotEQ(chk, "F", "FF", 16)
chkNotEQ(chk, "FF", "FFF", 16)
chknotEQ(chk, "0", "F", 32)
chknotEQ(chk, "F", "FF", 32)
chknotEQ(chk, "FF", "FFF", 32)
chknotEQ(chk, "FFFF", "FFFFF", 32)
chkNotEQ(chk, "0", "F", 32)
chkNotEQ(chk, "F", "FF", 32)
chkNotEQ(chk, "FF", "FFF", 32)
chkNotEQ(chk, "FFFF", "FFFFF", 32)
chknotEQ(chk, "0", "F", 64)
chknotEQ(chk, "F", "FF", 64)
chknotEQ(chk, "FF", "FFF", 64)
chknotEQ(chk, "FFFF", "FFFFF", 64)
chknotEQ(chk, "FFFFF", "FFFFFFFF", 64)
chkNotEQ(chk, "0", "F", 64)
chkNotEQ(chk, "F", "FF", 64)
chkNotEQ(chk, "FF", "FFF", 64)
chkNotEQ(chk, "FFFF", "FFFFF", 64)
chkNotEQ(chk, "FFFFF", "FFFFFFFF", 64)
chknotEQ(chk, "0", "F", 128)
chknotEQ(chk, "F", "FF", 128)
chknotEQ(chk, "FF", "FFF", 128)
chknotEQ(chk, "FFFF", "FFFFF", 128)
chknotEQ(chk, "FFFFF", "FFFFFFFF", 128)
chknotEQ(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotEQ(chk, "0", "F", 128)
chkNotEQ(chk, "F", "FF", 128)
chkNotEQ(chk, "FF", "FFF", 128)
chkNotEQ(chk, "FFFF", "FFFFF", 128)
chkNotEQ(chk, "FFFFF", "FFFFFFFF", 128)
chkNotEQ(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `isZero`":
chkIsZero(chk, "0", 8)
@ -220,12 +220,12 @@ template testComparison(chk, tst: untyped) =
chkIsZero(chk, "0", 256)
tst "operator not `isZero`":
chknotIsZero(chk, "1", 8)
chknotIsZero(chk, "2", 16)
chknotIsZero(chk, "3", 32)
chknotIsZero(chk, "4", 64)
chknotIsZero(chk, "5", 128)
chknotIsZero(chk, "6", 256)
chkNotIsZero(chk, "1", 8)
chkNotIsZero(chk, "2", 16)
chkNotIsZero(chk, "3", 32)
chkNotIsZero(chk, "4", 64)
chkNotIsZero(chk, "5", 128)
chkNotIsZero(chk, "6", 256)
tst "operator `isOdd`":
chkIsOdd(chk, "1", 8)

View File

@ -10,10 +10,10 @@
import ../stint, unittest, math, test_helpers
template chkPow(chk: untyped, a, b, c: string, bits: int) =
chk pow(fromHex(Stuint[bits], a), fromHex(Stuint[bits], b)) == fromHex(Stuint[bits], c)
chk pow(fromHex(StUint[bits], a), fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkPow(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk pow(fromHex(Stuint[bits], a), b) == fromHex(Stuint[bits], c)
chk pow(fromHex(StUint[bits], a), b) == fromHex(StUint[bits], c)
template testExp(chk, tst: untyped) =
tst "BigInt BigInt Pow":

View File

@ -9,17 +9,17 @@
import ../stint, unittest, math, test_helpers
template chkAddmod(chk: untyped, a, b, m, c: string, bits: int) =
chk addmod(fromHex(Stuint[bits], a), fromHex(Stuint[bits], b), fromHex(Stuint[bits], m)) == fromHex(Stuint[bits], c)
template chkAddMod(chk: untyped, a, b, m, c: string, bits: int) =
chk addmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template chkSubmod(chk: untyped, a, b, m, c: string, bits: int) =
chk submod(fromHex(Stuint[bits], a), fromHex(Stuint[bits], b), fromHex(Stuint[bits], m)) == fromHex(Stuint[bits], c)
template chkSubMod(chk: untyped, a, b, m, c: string, bits: int) =
chk submod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template chkMulmod(chk: untyped, a, b, m, c: string, bits: int) =
chk mulmod(fromHex(Stuint[bits], a), fromHex(Stuint[bits], b), fromHex(Stuint[bits], m)) == fromHex(Stuint[bits], c)
template chkMulMod(chk: untyped, a, b, m, c: string, bits: int) =
chk mulmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template chkPowmod(chk: untyped, a, b, m, c: string, bits: int) =
chk powmod(fromHex(Stuint[bits], a), fromHex(Stuint[bits], b), fromHex(Stuint[bits], m)) == fromHex(Stuint[bits], c)
template chkPowMod(chk: untyped, a, b, m, c: string, bits: int) =
chk powmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template testModArith(chk, tst: untyped) =
tst "addmod":

View File

@ -10,16 +10,16 @@
import ../stint, unittest, test_helpers
template chkMul(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stuint[bits], a) * fromHex(Stuint[bits], b)) == fromHex(Stuint[bits], c)
chk (fromHex(StUint[bits], a) * fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkDiv(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stuint[bits], a) div fromHex(Stuint[bits], b)) == fromHex(Stuint[bits], c)
chk (fromHex(StUint[bits], a) div fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkMod(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stuint[bits], a) mod fromHex(Stuint[bits], b)) == fromHex(Stuint[bits], c)
chk (fromHex(StUint[bits], a) mod fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkDivMod(chk: untyped, a, b, c, d: string, bits: int) =
chk divmod(fromHex(Stuint[bits], a), fromHex(Stuint[bits], b)) == (fromHex(Stuint[bits], c), fromHex(Stuint[bits], d))
chk divmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b)) == (fromHex(StUint[bits], c), fromHex(StUint[bits], d))
template testMuldiv(chk, tst: untyped) =
tst "operator `mul`":
@ -293,8 +293,8 @@ suite "Testing specific failures highlighted by property-based testing":
let u = 65696211516342324'u64
let v = 174261910798982'u64
let a = cast[Stuint[64]](u)
let b = cast[Stuint[64]](v)
let a = cast[StUint[64]](u)
let b = cast[StUint[64]](v)
let z = u mod v
let tz = cast[uint64](a mod b)
@ -305,8 +305,8 @@ suite "Testing specific failures highlighted by property-based testing":
let u = 15080397990160655'u64
let v = 600432699691'u64
let a = cast[Stuint[64]](u)
let b = cast[Stuint[64]](v)
let a = cast[StUint[64]](u)
let b = cast[StUint[64]](v)
let z = u mod v
let tz = cast[uint64](a mod b)