2020-06-12 21:53:08 +00:00
|
|
|
# Stint
|
2021-02-21 19:21:56 +00:00
|
|
|
# Copyright 2018-Present Status Research & Development GmbH
|
2020-06-12 21:53:08 +00:00
|
|
|
# Licensed under either of
|
|
|
|
#
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
#
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
# Internal
|
|
|
|
./private/datatypes,
|
2021-02-21 19:21:56 +00:00
|
|
|
./private/uint_bitwise,
|
2020-06-13 14:44:13 +00:00
|
|
|
./private/uint_shift,
|
2021-02-21 19:21:56 +00:00
|
|
|
./private/uint_addsub,
|
2022-01-12 17:25:55 +00:00
|
|
|
./private/uint_mul,
|
|
|
|
./private/uint_div,
|
2023-06-13 12:51:49 +00:00
|
|
|
./private/primitives/addcarry_subborrow,
|
|
|
|
stew/bitops2
|
2020-06-12 21:53:08 +00:00
|
|
|
|
|
|
|
export StUint
|
|
|
|
|
|
|
|
# Initialization
|
|
|
|
# --------------------------------------------------------
|
2023-06-12 14:07:15 +00:00
|
|
|
{.push raises: [], inline, noinit, gcsafe.}
|
2020-06-12 21:53:08 +00:00
|
|
|
|
|
|
|
func setZero*(a: var StUint) =
|
|
|
|
## Set ``a`` to 0
|
2020-09-06 14:27:11 +00:00
|
|
|
for i in 0 ..< a.limbs.len:
|
2022-01-23 20:39:26 +00:00
|
|
|
a.limbs[i] = 0
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2020-09-06 14:27:11 +00:00
|
|
|
func setSmallInt(a: var StUint, k: Word) =
|
|
|
|
## Set ``a`` to k
|
2022-01-23 21:45:47 +00:00
|
|
|
a.limbs[0] = k
|
|
|
|
for i in 1 ..< a.limbs.len:
|
|
|
|
a.limbs[i] = 0
|
2020-09-06 14:27:11 +00:00
|
|
|
|
|
|
|
func setOne*(a: var StUint) =
|
|
|
|
setSmallInt(a, 1)
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func zero*[bits: static[int]](T: typedesc[StUint[bits]]): T {.inline.} =
|
2020-06-12 21:53:08 +00:00
|
|
|
## Returns the zero of the input type
|
2023-06-14 06:53:42 +00:00
|
|
|
result.setZero
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func one*[bits: static[int]](T: typedesc[StUint[bits]]): T {.inline.} =
|
2020-06-12 21:53:08 +00:00
|
|
|
## Returns the one of the input type
|
2020-09-06 14:27:11 +00:00
|
|
|
result.setOne()
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func high*[bits](_: typedesc[StUint[bits]]): StUint[bits] {.inline.} =
|
2023-06-09 08:46:21 +00:00
|
|
|
for i in 0 ..< result.limbs.len:
|
2022-01-23 21:45:47 +00:00
|
|
|
result[i] = high(Word)
|
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func low*[bits](_: typedesc[StUint[bits]]): StUint[bits] {.inline.} =
|
2023-06-14 06:53:42 +00:00
|
|
|
result.setZero
|
2020-06-12 21:53:08 +00:00
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
# Comparisons
|
|
|
|
# --------------------------------------------------------
|
2023-06-12 14:07:15 +00:00
|
|
|
{.push raises: [], inline, noinit, gcsafe.}
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func isZero*(a: StUint): bool =
|
2022-01-23 21:45:47 +00:00
|
|
|
for i in 0 ..< a.limbs.len:
|
|
|
|
if a[i] != 0:
|
2020-06-12 21:53:08 +00:00
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
2023-06-20 14:38:02 +00:00
|
|
|
func isOne*(a: StUint): bool =
|
|
|
|
if a.limbs[0] != 1:
|
|
|
|
return false
|
|
|
|
for i in 1 ..< a.limbs.len:
|
|
|
|
if a.limbs[i] != 0:
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `==`*(a, b: StUint): bool {.inline.} =
|
2020-06-12 21:53:08 +00:00
|
|
|
## Unsigned `equal` comparison
|
2022-01-23 21:45:47 +00:00
|
|
|
for i in 0 ..< a.limbs.len:
|
|
|
|
if a[i] != b[i]:
|
2020-06-12 21:53:08 +00:00
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `<`*(a, b: StUint): bool {.inline.} =
|
2020-06-12 21:53:08 +00:00
|
|
|
## Unsigned `less than` comparison
|
|
|
|
var diff: Word
|
|
|
|
var borrow: Borrow
|
2022-01-23 21:45:47 +00:00
|
|
|
for i in 0 ..< a.limbs.len:
|
|
|
|
subB(borrow, diff, a[i], b[i], borrow)
|
2020-06-12 21:53:08 +00:00
|
|
|
return bool(borrow)
|
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `<=`*(a, b: StUint): bool {.inline.} =
|
2020-06-12 21:53:08 +00:00
|
|
|
## Unsigned `less or equal` comparison
|
2020-06-13 15:10:26 +00:00
|
|
|
not(b < a)
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func isOdd*(a: StUint): bool {.inline.} =
|
2020-06-12 21:53:08 +00:00
|
|
|
## Returns true if input is off
|
|
|
|
## false otherwise
|
2022-01-23 21:45:47 +00:00
|
|
|
bool(a[0] and 1)
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func isEven*(a: StUint): bool {.inline.} =
|
2020-06-12 21:53:08 +00:00
|
|
|
## Returns true if input is zero
|
|
|
|
## false otherwise
|
2022-01-23 21:45:47 +00:00
|
|
|
not a.isOdd()
|
2020-06-12 21:53:08 +00:00
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
# Bitwise operations
|
|
|
|
# --------------------------------------------------------
|
2023-06-12 14:07:15 +00:00
|
|
|
{.push raises: [], inline, noinit, gcsafe.}
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `not`*(a: StUint): StUint =
|
2020-06-12 21:53:08 +00:00
|
|
|
## Bitwise complement of unsigned integer a
|
|
|
|
## i.e. flips all bits of the input
|
2021-02-21 19:21:56 +00:00
|
|
|
result.bitnot(a)
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `or`*(a, b: StUint): StUint =
|
2020-06-12 21:53:08 +00:00
|
|
|
## `Bitwise or` of numbers a and b
|
2021-02-21 19:21:56 +00:00
|
|
|
result.bitor(a, b)
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `and`*(a, b: StUint): StUint =
|
2020-06-12 21:53:08 +00:00
|
|
|
## `Bitwise and` of numbers a and b
|
2021-02-21 19:21:56 +00:00
|
|
|
result.bitand(a, b)
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `xor`*(a, b: StUint): StUint =
|
2020-06-12 21:53:08 +00:00
|
|
|
## `Bitwise xor` of numbers x and y
|
2021-02-21 19:21:56 +00:00
|
|
|
result.bitxor(a, b)
|
2020-09-06 13:54:57 +00:00
|
|
|
|
2021-02-21 19:21:56 +00:00
|
|
|
{.pop.} # End noInit
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2021-02-21 19:21:56 +00:00
|
|
|
export
|
|
|
|
countOnes,
|
|
|
|
parity,
|
|
|
|
leadingZeros,
|
|
|
|
trailingZeros,
|
|
|
|
firstOne
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2020-06-13 15:03:50 +00:00
|
|
|
{.push raises: [], inline, gcsafe.}
|
|
|
|
|
2023-06-14 00:38:23 +00:00
|
|
|
func `shr`*(a: StUint, k: Natural): StUint =
|
2020-06-13 14:44:13 +00:00
|
|
|
## Shift right by k bits
|
2021-02-21 19:21:56 +00:00
|
|
|
result.shiftRight(a, k)
|
2020-06-13 14:44:13 +00:00
|
|
|
|
2023-06-14 00:38:23 +00:00
|
|
|
func `shl`*(a: StUint, k: Natural): StUint =
|
2020-06-13 14:44:13 +00:00
|
|
|
## Shift left by k bits
|
2021-02-21 19:21:56 +00:00
|
|
|
result.shiftLeft(a, k)
|
2020-06-13 14:44:13 +00:00
|
|
|
|
2023-06-14 00:38:23 +00:00
|
|
|
func setBit*(a: var StUint, k: Natural) =
|
2023-06-14 01:06:44 +00:00
|
|
|
## set bit at position `k`
|
|
|
|
## k = 0..a.bits-1
|
2023-06-13 12:51:49 +00:00
|
|
|
let limbIndex = k div WordBitWidth
|
|
|
|
let bitIndex = k mod WordBitWidth
|
|
|
|
setBit(a.limbs[limbIndex], bitIndex)
|
|
|
|
|
2023-06-14 00:38:23 +00:00
|
|
|
func clearBit*(a: var StUint, k: Natural) =
|
2023-06-14 01:06:44 +00:00
|
|
|
## set bit at position `k`
|
|
|
|
## k = 0..a.bits-1
|
2023-06-13 12:51:49 +00:00
|
|
|
let limbIndex = k div WordBitWidth
|
|
|
|
let bitIndex = k mod WordBitWidth
|
|
|
|
clearBit(a.limbs[limbIndex], bitIndex)
|
2023-06-14 00:38:23 +00:00
|
|
|
|
|
|
|
func getBit*(a: StUint, k: Natural): bool =
|
2023-06-14 01:06:44 +00:00
|
|
|
## set bit at position `k`
|
|
|
|
## k = 0..a.bits-1
|
2023-06-14 00:38:23 +00:00
|
|
|
let limbIndex = k div WordBitWidth
|
|
|
|
let bitIndex = k mod WordBitWidth
|
|
|
|
getBit(a.limbs[limbIndex], bitIndex)
|
2023-06-13 12:51:49 +00:00
|
|
|
|
2020-06-13 15:03:50 +00:00
|
|
|
{.pop.}
|
2020-06-13 14:44:13 +00:00
|
|
|
|
2020-06-12 21:53:08 +00:00
|
|
|
# Addsub
|
|
|
|
# --------------------------------------------------------
|
2023-06-12 14:07:15 +00:00
|
|
|
{.push raises: [], inline, noinit, gcsafe.}
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `+`*(a, b: StUint): StUint =
|
2020-09-06 13:54:57 +00:00
|
|
|
## Addition for multi-precision unsigned int
|
2021-02-21 19:21:56 +00:00
|
|
|
result.sum(a, b)
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2021-02-21 19:21:56 +00:00
|
|
|
export `+=`
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `-`*(a, b: StUint): StUint =
|
2020-09-06 13:54:57 +00:00
|
|
|
## Substraction for multi-precision unsigned int
|
2021-02-21 19:21:56 +00:00
|
|
|
result.diff(a, b)
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2021-02-21 19:21:56 +00:00
|
|
|
export `-=`
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2021-02-21 19:21:56 +00:00
|
|
|
export inc
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `+`*(a: StUint, b: SomeUnsignedInt): StUint =
|
2020-09-06 13:54:57 +00:00
|
|
|
## Addition for multi-precision unsigned int
|
|
|
|
## with an unsigned integer
|
2021-02-21 19:21:56 +00:00
|
|
|
result.sum(a, Word(b))
|
2020-09-06 13:54:57 +00:00
|
|
|
|
2021-02-21 19:21:56 +00:00
|
|
|
export `+=`
|
2020-09-06 13:54:57 +00:00
|
|
|
|
2020-06-12 21:53:08 +00:00
|
|
|
{.pop.}
|
2021-02-21 19:21:56 +00:00
|
|
|
|
2020-06-12 21:53:08 +00:00
|
|
|
# Multiplication
|
|
|
|
# --------------------------------------------------------
|
2020-06-13 10:29:31 +00:00
|
|
|
# Multiplication is implemented in a separate file at the limb-level
|
|
|
|
# - It's too big to be inlined (especially with unrolled loops)
|
|
|
|
# - It's implemented at the limb-level so that
|
|
|
|
# in the future Stuint[254] and Stuint256] share a common codepath
|
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
{.push raises: [], inline, noinit, gcsafe.}
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `*`*(a, b: StUint): StUint =
|
2020-06-12 21:53:08 +00:00
|
|
|
## Integer multiplication
|
|
|
|
result.limbs.prod(a.limbs, b.limbs)
|
2022-01-23 21:45:47 +00:00
|
|
|
result.clearExtraBitsOverMSB()
|
2020-06-12 21:53:08 +00:00
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
|
|
|
|
# Exponentiation
|
|
|
|
# --------------------------------------------------------
|
2020-09-06 14:27:11 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
{.push raises: [], noinit, gcsafe.}
|
2020-09-06 14:27:11 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func pow*(a: StUint, e: Natural): StUint =
|
2020-09-06 14:27:11 +00:00
|
|
|
## Compute ``a`` to the power of ``e``,
|
|
|
|
## ``e`` must be non-negative
|
|
|
|
|
|
|
|
# Implementation uses exponentiation by squaring
|
|
|
|
# See Nim math module: https://github.com/nim-lang/Nim/blob/4ed24aa3eb78ba4ff55aac3008ec3c2427776e50/lib/pure/math.nim#L429
|
|
|
|
# And Eli Bendersky's blog: https://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms
|
|
|
|
|
|
|
|
var (a, e) = (a, e)
|
|
|
|
result.setOne()
|
|
|
|
|
|
|
|
while true:
|
|
|
|
if bool(e and 1): # if y is odd
|
|
|
|
result = result * a
|
|
|
|
e = e shr 1
|
|
|
|
if e == 0:
|
|
|
|
break
|
|
|
|
a = a * a
|
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func pow*[aBits, eBits](a: StUint[aBits], e: StUint[eBits]): StUint[aBits] =
|
2020-09-06 14:27:11 +00:00
|
|
|
## Compute ``x`` to the power of ``y``,
|
|
|
|
## ``x`` must be non-negative
|
|
|
|
# Implementation uses exponentiation by squaring
|
|
|
|
# See Nim math module: https://github.com/nim-lang/Nim/blob/4ed24aa3eb78ba4ff55aac3008ec3c2427776e50/lib/pure/math.nim#L429
|
|
|
|
# And Eli Bendersky's blog: https://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms
|
|
|
|
|
|
|
|
var (a, e) = (a, e)
|
|
|
|
result.setOne()
|
|
|
|
|
|
|
|
while true:
|
|
|
|
if e.isOdd:
|
|
|
|
result = result * a
|
|
|
|
e = e shr 1
|
|
|
|
if e.isZero:
|
|
|
|
break
|
|
|
|
a = a * a
|
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
|
|
|
|
# Division & Modulo
|
|
|
|
# --------------------------------------------------------
|
2023-06-12 14:07:15 +00:00
|
|
|
{.push raises: [], inline, noinit, gcsafe.}
|
2022-01-23 21:45:47 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `div`*(x, y: StUint): StUint =
|
2022-01-23 21:45:47 +00:00
|
|
|
## Division operation for multi-precision unsigned uint
|
2023-06-12 14:07:15 +00:00
|
|
|
var tmp{.noinit.}: StUint
|
2022-01-23 21:45:47 +00:00
|
|
|
divRem(result.limbs, tmp.limbs, x.limbs, y.limbs)
|
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func `mod`*(x, y: StUint): StUint =
|
2022-01-23 21:45:47 +00:00
|
|
|
## Remainder operation for multi-precision unsigned uint
|
2023-06-12 14:07:15 +00:00
|
|
|
var tmp{.noinit.}: StUint
|
2022-01-23 21:45:47 +00:00
|
|
|
divRem(tmp.limbs, result.limbs, x.limbs, y.limbs)
|
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
func divmod*(x, y: StUint): tuple[quot, rem: StUint] =
|
2022-01-23 21:45:47 +00:00
|
|
|
## Division and remainder operations for multi-precision unsigned uint
|
|
|
|
divRem(result.quot.limbs, result.rem.limbs, x.limbs, y.limbs)
|
2022-01-12 17:25:55 +00:00
|
|
|
|
2022-01-23 21:45:47 +00:00
|
|
|
{.pop.}
|