2023-07-31 21:07:48 +00:00
|
|
|
## Copyright (c) 2020-2023 Status Research & Development GmbH
|
2020-07-28 22:43:09 +00:00
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
|
|
|
## This module implements different operations which will execute in amount of
|
|
|
|
## time (or memory space) independent of the input size.
|
|
|
|
|
|
|
|
type
|
|
|
|
CT* = object
|
|
|
|
|
2020-07-30 17:09:49 +00:00
|
|
|
when sizeof(int) == 8:
|
|
|
|
type
|
|
|
|
AnyItem* = byte|char|int8|uint16|int16|uint32|int32|uint64|int64|uint|int
|
|
|
|
elif sizeof(int) == 4:
|
|
|
|
type
|
|
|
|
AnyItem* = byte|char|int8|uint16|int16|uint32|int32|uint|int
|
|
|
|
|
2023-07-31 21:07:48 +00:00
|
|
|
{.push raises: [].}
|
2023-02-14 13:29:30 +00:00
|
|
|
|
2020-07-30 17:09:49 +00:00
|
|
|
proc isEqual*[A: AnyItem, B: AnyItem](c: typedesc[CT], a: openArray[A],
|
2023-02-14 13:29:30 +00:00
|
|
|
b: openArray[B]): bool =
|
2020-07-28 22:43:09 +00:00
|
|
|
## Perform constant time comparison of two arrays ``a`` and ``b``.
|
|
|
|
##
|
|
|
|
## Please note that it only makes sense to compare arrays of the same length.
|
|
|
|
## If length of arrays is not equal only part of array will be compared.
|
|
|
|
##
|
|
|
|
## Procedure returns ``true`` if arrays of same length are equal or
|
|
|
|
## part of array's content is equal to another array's content if arrays
|
|
|
|
## lengths are different.
|
|
|
|
##
|
2020-07-30 17:09:49 +00:00
|
|
|
## Beware that arrays ``a`` and ``b`` MUST NOT BE empty. Types ``A`` and
|
|
|
|
## ``B`` should be equal in size, e.g. ``(sizeof(A) == sizeof(B))``
|
2020-07-28 22:43:09 +00:00
|
|
|
doAssert(len(a) > 0 and len(b) > 0)
|
2020-07-30 17:09:49 +00:00
|
|
|
doAssert(sizeof(A) == sizeof(B))
|
2020-07-28 22:43:09 +00:00
|
|
|
var count = min(len(a), len(b))
|
2020-07-30 17:09:49 +00:00
|
|
|
var res = 0'u
|
2020-07-28 22:43:09 +00:00
|
|
|
while count > 0:
|
|
|
|
dec(count)
|
2020-07-30 17:09:49 +00:00
|
|
|
let av = when A is uint: a[count] else: uint(a[count])
|
|
|
|
let bv = when B is uint: b[count] else: uint(b[count])
|
|
|
|
res = res or (av xor bv)
|
|
|
|
(res == 0'u)
|