commit 31b22bc3f1ca12d9ee0b3081041b02ef4c1c99ea
parent d42bf83f3eb9a1bb6e233efc3080f6bd303eefa7
Author: Armin Preiml <apreiml@strohwolke.at>
Date: Fri, 3 Nov 2023 15:38:12 +0100
crypto::{math,bigint}: export some util functions
Signed-off-by: Armin Preiml <apreiml@strohwolke.at>
Diffstat:
3 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/crypto/bigint/encoding.ha b/crypto/bigint/encoding.ha
@@ -107,8 +107,9 @@ fn ebitlen(x: const []word) u32 = {
// Get the effective word lenght of 'x'. The effective wordlen is the number of
// words that are used to represent the actual value. Eg. the number 7 will have
-// an effective word length of 1, no matter of len(x).
-fn ewordlen(x: const []word) u32 = {
+// an effective word length of 1, no matter of len(x). The first element
+// containing the encoded word len is not added to the result.
+export fn ewordlen(x: const []word) u32 = {
return (x[0] + WORD_BITSZ) >> WORD_SHIFT;
};
diff --git a/crypto/bigint/util.ha b/crypto/bigint/util.ha
@@ -33,7 +33,7 @@ export fn zero(x: []word, ebitlen: word) void = {
// Checks whether the effective words of 'x' are zero. Returns 1 if so, or 0
// otherwise.
-fn iszero(x: []word) u32 = {
+export fn iszero(x: []word) u32 = {
let z: u32 = 0;
for (let i = ewordlen(x); i > 0; i -= 1) {
@@ -45,3 +45,4 @@ fn iszero(x: []word) u32 = {
fn isodd(x: []word) bool = {
return x[1] & 1 == 1;
};
+
diff --git a/crypto/math/bits.ha b/crypto/math/bits.ha
@@ -166,3 +166,15 @@ export fn cmpu32(x: u32, y: u32) i32 = gtu32(x, y): i32 | -(gtu32(y, x): i32);
assert(cmpu32(0x87, 0x34) == 1);
};
+// Multiplies two u32 and returns result as u64.
+export fn mulu32(x: u32, y: u32) u64 = x: u64 * y: u64;
+
+// Copies 'src' to 'dest' if 'ctl' == 1
+export fn ccopyu32(ctl: u32, dest: []u32, src: const []u32) void = {
+ for (let i = 0z; i < len(dest); i += 1) {
+ const x = src[i];
+ const y = dest[i];
+
+ dest[i] = muxu32(ctl, x, y);
+ };
+};