hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit c05c29658669c85794cca2bde5e94865c9a63c28
parent 3e56a6ed2cf98ba1fa72a524272577f88801c53e
Author: Armin Preiml <apreiml@strohwolke.at>
Date:   Fri, 17 Jun 2022 14:35:33 +0200

change cmp{slice,u8} to eq{slice,u8}

To match the names of the new functions equ32 and to free the function
name for a cmpu8, which is similar to cmpu32 and matches the semantic of
sort::cmpfunc.

Signed-off-by: Armin Preiml <apreiml@strohwolke.at>

Diffstat:
Mcrypto/authenc.ha | 2+-
Mcrypto/cipher/gcm.ha | 4++--
Mcrypto/math/bits.ha | 24++++++++++++------------
3 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/crypto/authenc.ha b/crypto/authenc.ha @@ -202,7 +202,7 @@ export fn compare(a: []u8, b: []u8) bool = { return false; }; - return math::cmpslice(a, b) == 1; + return math::eqslice(a, b) == 1; }; // TODO: Add additional entry-points which provide a finer degree of control diff --git a/crypto/cipher/gcm.ha b/crypto/cipher/gcm.ha @@ -1,7 +1,7 @@ // License: MPL-2.0 // (c) 2022 Armin Preiml <apreiml@strohwolke.at> use bytes; -use crypto::math::{xor,cmpslice}; +use crypto::math::{xor,eqslice}; use endian::{beputu64, beputu32, begetu32}; use errors; use io; @@ -227,7 +227,7 @@ export fn gcm_verify(s: *gcmstream, tag: [16]u8) (void | errors::invalid) = { encrypt(s.block, tmp, s.y0); xor(tmp, tmp, s.tagbuf); - if (cmpslice(tag, tmp) == 0) { + if (eqslice(tag, tmp) == 0) { return errors::invalid; }; }; diff --git a/crypto/math/bits.ha b/crypto/math/bits.ha @@ -62,30 +62,30 @@ export fn xor(dest: []u8, a: []u8, b: []u8) void = { // Compare two byte slices in constant time. // // Returns 1 if the two slices have the same contents, 0 otherwise. -export fn cmpslice(x: []u8, y: []u8) int = { +export fn eqslice(x: []u8, y: []u8) int = { assert(len(x) == len(y), "slices must have the same length"); let v: u8 = 0; for (let i = 0z; i < len(x); i += 1) { v |= x[i] ^ y[i]; }; - return cmpu8(v, 0); + return equ8(v, 0); }; -@test fn cmpslice() void = { - assert(cmpslice([], []) == 1); - assert(cmpslice([0], [0]) == 1); - assert(cmpslice([1], [0]) == 0); - assert(cmpslice([1, 0], [0, 0]) == 0); - assert(cmpslice([0, 0], [0, 0]) == 1); +@test fn eqslice() void = { + assert(eqslice([], []) == 1); + assert(eqslice([0], [0]) == 1); + assert(eqslice([1], [0]) == 0); + assert(eqslice([1, 0], [0, 0]) == 0); + assert(eqslice([0, 0], [0, 0]) == 1); - assert(cmpslice([0x12, 0xAB], [0x12, 0xAB]) == 1); - assert(cmpslice([0x12, 0xAB], [0x12, 0xAC]) == 0); - assert(cmpslice([0x12, 0xAB], [0x11, 0xAB]) == 0); + assert(eqslice([0x12, 0xAB], [0x12, 0xAB]) == 1); + assert(eqslice([0x12, 0xAB], [0x12, 0xAC]) == 0); + assert(eqslice([0x12, 0xAB], [0x11, 0xAB]) == 0); }; // Compare two bytes in constant time. Returns 1 if the bytes are the same // value, 0 otherwise. -export fn cmpu8(x: u8, y: u8) int = ((((x ^ y) : u32) - 1) >> 31) : int; +export fn equ8(x: u8, y: u8) int = ((((x ^ y) : u32) - 1) >> 31) : int; // Returns x if ctl == 1 and y if ctl == 0. export fn muxu32(ctl: u32, x: u32, y: u32) u32 = y ^ ((-(ctl: i32)): u32 & (x ^ y));