2018-03-02 10:48:08 +00:00
|
|
|
# Mpint
|
|
|
|
# Copyright 2018 Status Research & Development GmbH
|
|
|
|
# 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.
|
2018-02-16 16:48:54 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
import ./uint_type, macros
|
2018-02-16 16:48:54 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
macro optim(x: typed): untyped =
|
|
|
|
let size = getSize(x)
|
2018-03-26 14:47:04 +00:00
|
|
|
|
|
|
|
if size > 64:
|
|
|
|
result = quote do:
|
2018-04-21 10:12:05 +00:00
|
|
|
array[`size` div 64, uint64]
|
2018-03-26 14:47:04 +00:00
|
|
|
elif size == 64:
|
|
|
|
result = quote do:
|
2018-04-21 10:12:05 +00:00
|
|
|
uint64
|
2018-03-26 14:47:04 +00:00
|
|
|
elif size == 32:
|
2018-03-26 14:05:19 +00:00
|
|
|
result = quote do:
|
2018-04-21 10:12:05 +00:00
|
|
|
uint32
|
2018-03-26 14:47:04 +00:00
|
|
|
elif size == 16:
|
2018-03-26 14:05:19 +00:00
|
|
|
result = quote do:
|
2018-04-21 10:12:05 +00:00
|
|
|
uint16
|
2018-03-26 14:47:04 +00:00
|
|
|
elif size == 8:
|
2018-03-26 14:05:19 +00:00
|
|
|
result = quote do:
|
2018-04-21 10:12:05 +00:00
|
|
|
uint8
|
2018-03-26 14:47:04 +00:00
|
|
|
else:
|
|
|
|
error "Unreachable path reached"
|
2018-02-17 16:57:26 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
func isZero*(n: SomeUnsignedInt): bool {.inline.} =
|
2018-03-26 10:45:10 +00:00
|
|
|
n == 0
|
2018-02-17 16:57:26 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
func isZero*(n: MpUintImpl): bool {.inline.} =
|
2018-03-26 14:05:19 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
when optim(`n`) is array:
|
|
|
|
for val in cast[optim(n)](n):
|
|
|
|
if val != 0:
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
cast[optim(n)](n) == 0
|
|
|
|
|
|
|
|
func `<`*(x, y: MpUintImpl): bool {.noInit, inline.}=
|
|
|
|
|
|
|
|
when optim(x) is array:
|
|
|
|
let
|
|
|
|
x_ptr = cast[ptr optim(x)](x.unsafeaddr)
|
|
|
|
y_ptr = cast[ptr optim(y)](y.unsafeaddr)
|
|
|
|
|
|
|
|
when system.cpuEndian == bigEndian:
|
|
|
|
for i in 0..<x_ptr[].len:
|
|
|
|
if x_ptr[i] != y_ptr[i]:
|
|
|
|
return x_ptr[i] < y_ptr[i]
|
|
|
|
return false # They're equal
|
|
|
|
else: # littleEndian, the most significant bytes are on the right
|
|
|
|
for i in countdown(x_ptr[].len - 1, 0):
|
|
|
|
if x_ptr[i] != y_ptr[i]:
|
|
|
|
return x_ptr[i] < y_ptr[i]
|
|
|
|
return false # They're equal
|
|
|
|
else:
|
|
|
|
cast[optim(x)](x) < cast[optim(y)](y)
|
2018-03-26 14:05:19 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
func `==`*(x, y: MpUintImpl): bool {.noInit, inline.}=
|
2018-03-26 14:05:19 +00:00
|
|
|
# Equal comparison for multi-precision integers
|
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
when optim(x) is array:
|
|
|
|
let
|
|
|
|
x_ptr = cast[ptr optim(x)](x.unsafeaddr)
|
|
|
|
y_ptr = cast[ptr optim(y)](y.unsafeaddr)
|
2018-03-26 14:05:19 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
for i in 0..<x_ptr[].len:
|
|
|
|
if x_ptr[i] != y_ptr[i]:
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
cast[optim(x)](x) < cast[optim(y)](y)
|
|
|
|
|
|
|
|
func `<=`*(x, y: MpUintImpl): bool {.noInit, inline.}=
|
2018-03-26 14:05:19 +00:00
|
|
|
# Lower or equal comparison for multi-precision integers
|
2018-04-21 10:12:05 +00:00
|
|
|
|
|
|
|
when optim(x) is array:
|
|
|
|
let
|
|
|
|
x_ptr = cast[ptr optim(x)](x.unsafeaddr)
|
|
|
|
y_ptr = cast[ptr optim(y)](y.unsafeaddr)
|
|
|
|
|
|
|
|
when system.cpuEndian == bigEndian:
|
|
|
|
for i in 0..<x_ptr[].len:
|
|
|
|
if x_ptr[i] != y_ptr[i]:
|
|
|
|
return x_ptr[i] < y_ptr[i]
|
|
|
|
return true # They're equal
|
|
|
|
else: # littleEndian, the most significant bytes are on the right
|
|
|
|
for i in countdown(x_ptr[].len - 1, 0):
|
|
|
|
if x_ptr[i] != y_ptr[i]:
|
|
|
|
return x_ptr[i] < y_ptr[i]
|
|
|
|
return true # They're equal
|
|
|
|
else:
|
|
|
|
cast[optim(x)](x) <= cast[optim(y)](y)
|