hare

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

commit c5d94e5c3834651b8a0d2753dd775a4363b934e7
parent 59e47486b111886fc6de8f6be54f6b6715e53d2d
Author: Armin Preiml <apreiml@strohwolke.at>
Date:   Fri,  3 Nov 2023 15:38:10 +0100

crypto::bigint: decode only actual encoded words

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

Diffstat:
Mcrypto/bigint/+test/encoding_test.ha | 14++++++++++++++
Mcrypto/bigint/encoding.ha | 3++-
2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/crypto/bigint/+test/encoding_test.ha b/crypto/bigint/+test/encoding_test.ha @@ -3,6 +3,7 @@ use bytes; + @test fn encode() void = { const decoded: [12]u8 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; let result: [12]u8 = [0...]; @@ -32,6 +33,19 @@ use bytes; assert(bytes::equal(result, decoded)); }; +@test fn decodebigger() void = { + const encoded: [_]word = [ + 0x32, // only 50 effective bits + 0x7fffffff, 0x0007ffff, 0x7fffffff, + ]; + + let result: [8]u8 = [0xaa...]; + + decode(result, encoded); + assert(bytes::equal(result, + [0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])); +}; + @test fn encmoddec() void = { const input: [4]u8 = [0, 0, 0, 10]; diff --git a/crypto/bigint/encoding.ha b/crypto/bigint/encoding.ha @@ -119,9 +119,10 @@ export fn decode(dest: []u8, src: const []word) void = { let acc: u64 = 0; let accbits: u64 = 0; let sidx: size = 1; + let sz = ewordlen(src); for (let i = len(dest) - 1; i < len(dest); i -= 1) { if (accbits < 8) { - if (sidx < len(src)) { + if (sidx <= sz) { acc |= ((src[sidx]: u64) << accbits: u64): u64; sidx += 1; };