assert() -> doAssert()
This commit is contained in:
parent
d002fbb155
commit
6853ebe97c
26
stint/io.nim
26
stint/io.nim
|
@ -17,7 +17,7 @@ template static_check_size(T: typedesc[SomeInteger], bits: static[int]) =
|
||||||
# To avoid a costly runtime check, we refuse storing into StUint types smaller
|
# To avoid a costly runtime check, we refuse storing into StUint types smaller
|
||||||
# than the input type.
|
# than the input type.
|
||||||
|
|
||||||
static: assert sizeof(T) * 8 <= bits, "Input type (" & $T &
|
static: doAssert sizeof(T) * 8 <= bits, "Input type (" & $T &
|
||||||
") cannot be stored in a multi-precision " &
|
") cannot be stored in a multi-precision " &
|
||||||
$bits & "-bit integer." &
|
$bits & "-bit integer." &
|
||||||
"\nUse a smaller input type instead. This is a compile-time check" &
|
"\nUse a smaller input type instead. This is a compile-time check" &
|
||||||
|
@ -32,7 +32,7 @@ func assignLo(result: var (UintImpl | IntImpl), n: SomeInteger) {.inline.} =
|
||||||
func stuint*[T: SomeInteger](n: T, bits: static[int]): StUint[bits] {.inline.}=
|
func stuint*[T: SomeInteger](n: T, bits: static[int]): StUint[bits] {.inline.}=
|
||||||
## Converts an integer to an arbitrary precision integer.
|
## Converts an integer to an arbitrary precision integer.
|
||||||
|
|
||||||
assert n >= 0.T
|
doAssert n >= 0.T
|
||||||
when result.data is UintImpl:
|
when result.data is UintImpl:
|
||||||
static_check_size(T, bits)
|
static_check_size(T, bits)
|
||||||
assignLo(result.data, n)
|
assignLo(result.data, n)
|
||||||
|
@ -87,16 +87,16 @@ func skipPrefixes(current_idx: var int, str: string, radix: range[2..16]) {.inli
|
||||||
if str.len < 2:
|
if str.len < 2:
|
||||||
return
|
return
|
||||||
|
|
||||||
assert current_idx == 0, "skipPrefixes only works for prefixes (position 0 and 1 of the string)"
|
doAssert current_idx == 0, "skipPrefixes only works for prefixes (position 0 and 1 of the string)"
|
||||||
if str[0] == '0':
|
if str[0] == '0':
|
||||||
if str[1] in {'x', 'X'}:
|
if str[1] in {'x', 'X'}:
|
||||||
assert radix == 16, "Parsing mismatch, 0x prefix is only valid for a hexadecimal number (base 16)"
|
doAssert radix == 16, "Parsing mismatch, 0x prefix is only valid for a hexadecimal number (base 16)"
|
||||||
current_idx = 2
|
current_idx = 2
|
||||||
elif str[1] in {'o', 'O'}:
|
elif str[1] in {'o', 'O'}:
|
||||||
assert radix == 8, "Parsing mismatch, 0o prefix is only valid for an octal number (base 8)"
|
doAssert radix == 8, "Parsing mismatch, 0o prefix is only valid for an octal number (base 8)"
|
||||||
current_idx = 2
|
current_idx = 2
|
||||||
elif str[1] in {'b', 'B'}:
|
elif str[1] in {'b', 'B'}:
|
||||||
assert radix == 2, "Parsing mismatch, 0b prefix is only valid for a binary number (base 2)"
|
doAssert radix == 2, "Parsing mismatch, 0b prefix is only valid for a binary number (base 2)"
|
||||||
current_idx = 2
|
current_idx = 2
|
||||||
|
|
||||||
func nextNonBlank(current_idx: var int, s: string) {.inline.} =
|
func nextNonBlank(current_idx: var int, s: string) {.inline.} =
|
||||||
|
@ -116,7 +116,7 @@ func readDecChar(c: range['0'..'9']): int {.inline.}=
|
||||||
func parse*[bits: static[int]](input: string, T: typedesc[Stuint[bits]], radix: static[uint8] = 10): T =
|
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].
|
## Parse a string and store the result in a Stint[bits] or Stuint[bits].
|
||||||
|
|
||||||
static: assert (radix >= 2) and radix <= 16, "Only base from 2..16 are supported"
|
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)
|
# TODO: use static[range[2 .. 16]], not supported at the moment (2018-04-26)
|
||||||
|
|
||||||
# TODO: we can special case hex result/input as an array of bytes
|
# TODO: we can special case hex result/input as an array of bytes
|
||||||
|
@ -137,7 +137,7 @@ func parse*[bits: static[int]](input: string, T: typedesc[Stuint[bits]], radix:
|
||||||
func parse*[bits: static[int]](input: string, T: typedesc[Stint[bits]], radix: static[int8] = 10): T =
|
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].
|
## Parse a string and store the result in a Stint[bits] or Stuint[bits].
|
||||||
|
|
||||||
static: assert (radix >= 2) and radix <= 16, "Only base from 2..16 are supported"
|
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)
|
# TODO: use static[range[2 .. 16]], not supported at the moment (2018-04-26)
|
||||||
|
|
||||||
# TODO: we can special case hex result/input as an array of bytes
|
# TODO: we can special case hex result/input as an array of bytes
|
||||||
|
@ -152,7 +152,7 @@ func parse*[bits: static[int]](input: string, T: typedesc[Stint[bits]], radix: s
|
||||||
no_overflow: Stuint[bits]
|
no_overflow: Stuint[bits]
|
||||||
|
|
||||||
if input[curr] == '-':
|
if input[curr] == '-':
|
||||||
assert radix == 10, "Negative numbers are only supported with base 10 input."
|
doAssert radix == 10, "Negative numbers are only supported with base 10 input."
|
||||||
isNeg = true
|
isNeg = true
|
||||||
inc curr
|
inc curr
|
||||||
else:
|
else:
|
||||||
|
@ -186,7 +186,7 @@ func toString*[bits: static[int]](num: StUint[bits], radix: static[uint8] = 10):
|
||||||
## - they are prefixed with "-" for base 10.
|
## - they are prefixed with "-" for base 10.
|
||||||
## - if not base 10, they are returned raw in two-complement form.
|
## - if not base 10, they are returned raw in two-complement form.
|
||||||
|
|
||||||
static: assert (radix >= 2) and radix <= 16, "Only base from 2..16 are supported"
|
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)
|
# TODO: use static[range[2 .. 16]], not supported at the moment (2018-04-26)
|
||||||
|
|
||||||
const hexChars = "0123456789abcdef"
|
const hexChars = "0123456789abcdef"
|
||||||
|
@ -209,7 +209,7 @@ func toString*[bits: static[int]](num: Stint[bits], radix: static[int8] = 10): s
|
||||||
## - they are prefixed with "-" for base 10.
|
## - they are prefixed with "-" for base 10.
|
||||||
## - if not base 10, they are returned raw in two-complement form.
|
## - if not base 10, they are returned raw in two-complement form.
|
||||||
|
|
||||||
static: assert (radix >= 2) and radix <= 16, "Only base from 2..16 are supported"
|
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)
|
# TODO: use static[range[2 .. 16]], not supported at the moment (2018-04-26)
|
||||||
|
|
||||||
const hexChars = "0123456789abcdef"
|
const hexChars = "0123456789abcdef"
|
||||||
|
@ -287,9 +287,9 @@ proc initFromBytesBE*[bits: static[int]](val: var Stuint[bits], ba: openarray[by
|
||||||
const N = bits div 8
|
const N = bits div 8
|
||||||
|
|
||||||
when not allowPadding:
|
when not allowPadding:
|
||||||
assert(ba.len == N)
|
doAssert(ba.len == N)
|
||||||
else:
|
else:
|
||||||
assert ba.len <= N
|
doAssert ba.len <= N
|
||||||
|
|
||||||
{.pragma: restrict, codegenDecl: "$# __restrict $#".}
|
{.pragma: restrict, codegenDecl: "$# __restrict $#".}
|
||||||
let r_ptr {.restrict.} = cast[ptr array[N, byte]](val.addr)
|
let r_ptr {.restrict.} = cast[ptr array[N, byte]](val.addr)
|
||||||
|
|
|
@ -13,8 +13,8 @@ func addmod_internal(a, b, m: Stuint): Stuint {.inline.}=
|
||||||
## Modular addition
|
## Modular addition
|
||||||
## ⚠⚠ Assume a < m and b < m
|
## ⚠⚠ Assume a < m and b < m
|
||||||
|
|
||||||
assert a < m
|
doAssert a < m
|
||||||
assert b < m
|
doAssert b < m
|
||||||
|
|
||||||
# We don't do a_m + b_m directly to avoid overflows
|
# We don't do a_m + b_m directly to avoid overflows
|
||||||
let b_from_m = m - b
|
let b_from_m = m - b
|
||||||
|
@ -27,8 +27,8 @@ func submod_internal(a, b, m: Stuint): Stuint {.inline.}=
|
||||||
## Modular substraction
|
## Modular substraction
|
||||||
## ⚠⚠ Assume a < m and b < m
|
## ⚠⚠ Assume a < m and b < m
|
||||||
|
|
||||||
assert a < m
|
doAssert a < m
|
||||||
assert b < m
|
doAssert b < m
|
||||||
|
|
||||||
# We don't do a_m - b_m directly to avoid underflows
|
# We don't do a_m - b_m directly to avoid underflows
|
||||||
if a >= b:
|
if a >= b:
|
||||||
|
@ -40,7 +40,7 @@ func doublemod_internal(a, m: Stuint): Stuint {.inline.}=
|
||||||
## Double a modulo m. Assume a < m
|
## Double a modulo m. Assume a < m
|
||||||
## Internal proc - used in mulmod
|
## Internal proc - used in mulmod
|
||||||
|
|
||||||
assert a < m
|
doAssert a < m
|
||||||
|
|
||||||
result = a
|
result = a
|
||||||
if a >= m - a:
|
if a >= m - a:
|
||||||
|
@ -51,8 +51,8 @@ func mulmod_internal(a, b, m: Stuint): Stuint {.inline.}=
|
||||||
## Does (a * b) mod m. Assume a < m and b < m
|
## Does (a * b) mod m. Assume a < m and b < m
|
||||||
## Internal proc - used in powmod
|
## Internal proc - used in powmod
|
||||||
|
|
||||||
assert a < m
|
doAssert a < m
|
||||||
assert b < m
|
doAssert b < m
|
||||||
|
|
||||||
var (a, b) = (a, b)
|
var (a, b) = (a, b)
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ func powmod_internal(a, b, m: Stuint): Stuint {.inline.}=
|
||||||
## Compute ``(a ^ b) mod m``, assume a < m
|
## Compute ``(a ^ b) mod m``, assume a < m
|
||||||
## Internal proc
|
## Internal proc
|
||||||
|
|
||||||
assert a < m
|
doAssert a < m
|
||||||
|
|
||||||
var (a, b) = (a, b)
|
var (a, b) = (a, b)
|
||||||
result = one(type a)
|
result = one(type a)
|
||||||
|
|
|
@ -122,7 +122,7 @@ proc div3n2n[T: SomeUnsignedInt](
|
||||||
|
|
||||||
func div2n1n(q, r: var UintImpl, ah, al, b: UintImpl) =
|
func div2n1n(q, r: var UintImpl, ah, al, b: UintImpl) =
|
||||||
|
|
||||||
# assert countLeadingZeroBits(b) == 0, "Divisor was not normalized"
|
# doAssert countLeadingZeroBits(b) == 0, "Divisor was not normalized"
|
||||||
|
|
||||||
var s: UintImpl
|
var s: UintImpl
|
||||||
div3n2n(q.hi, s, ah.hi, ah.lo, al.hi, b)
|
div3n2n(q.hi, s, ah.hi, ah.lo, al.hi, b)
|
||||||
|
@ -130,7 +130,7 @@ func div2n1n(q, r: var UintImpl, ah, al, b: UintImpl) =
|
||||||
|
|
||||||
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) =
|
||||||
|
|
||||||
# assert countLeadingZeroBits(d) == 0, "Divisor was not normalized"
|
# doAssert countLeadingZeroBits(d) == 0, "Divisor was not normalized"
|
||||||
|
|
||||||
const
|
const
|
||||||
size = bitsof(q)
|
size = bitsof(q)
|
||||||
|
@ -170,7 +170,7 @@ func div2n1n[T: SomeunsignedInt](q, r: var T, n_hi, n_lo, d: T) =
|
||||||
|
|
||||||
func divmodBZ[T](x, y: UintImpl[T], q, r: var UintImpl[T])=
|
func divmodBZ[T](x, y: UintImpl[T], q, r: var UintImpl[T])=
|
||||||
|
|
||||||
assert y.isZero.not() # This should be checked on release mode in the divmod caller proc
|
doAssert y.isZero.not() # This should be checked on release mode in the divmod caller proc
|
||||||
|
|
||||||
if y.hi.isZero:
|
if y.hi.isZero:
|
||||||
# Shortcut if divisor is smaller than half the size of the type
|
# Shortcut if divisor is smaller than half the size of the type
|
||||||
|
@ -207,7 +207,7 @@ func divmodBS(x, y: UintImpl, q, r: var UintImpl) =
|
||||||
## Division for multi-precision unsigned uint
|
## Division for multi-precision unsigned uint
|
||||||
## Implementation through binary shift division
|
## Implementation through binary shift division
|
||||||
|
|
||||||
assert y.isZero.not() # This should be checked on release mode in the divmod caller proc
|
doAssert y.isZero.not() # This should be checked on release mode in the divmod caller proc
|
||||||
|
|
||||||
type SubTy = type x.lo
|
type SubTy = type x.lo
|
||||||
|
|
||||||
|
|
|
@ -200,7 +200,7 @@ suite "Testing conversion functions: Hex, Bytes, Endianness using secp256k1 curv
|
||||||
# sig = ecc.sign(msghash)
|
# sig = ecc.sign(msghash)
|
||||||
# print(" sig='{}',".format(encode_hex(sig)))
|
# print(" sig='{}',".format(encode_hex(sig)))
|
||||||
# print(" raw_sig='{}')".format(crypto._decode_sig(sig)))
|
# print(" raw_sig='{}')".format(crypto._decode_sig(sig)))
|
||||||
# assert crypto.ecdsa_recover(msghash, sig) == pubkey
|
# doAssert crypto.ecdsa_recover(msghash, sig) == pubkey
|
||||||
# """
|
# """
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
@ -14,7 +14,7 @@ suite "Testing unsigned int bitwise operations":
|
||||||
|
|
||||||
let b = a * a
|
let b = a * a
|
||||||
let z = 10000'u16
|
let z = 10000'u16
|
||||||
assert cast[uint16](b) == z, "Test cannot proceed, something is wrong with the multiplication implementation"
|
doAssert cast[uint16](b) == z, "Test cannot proceed, something is wrong with the multiplication implementation"
|
||||||
|
|
||||||
|
|
||||||
let u = 10000.stuint(64)
|
let u = 10000.stuint(64)
|
||||||
|
|
Loading…
Reference in New Issue