From 778da03f0d80c751a45fc3249d05a2859c4ddfcd Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Wed, 9 Apr 2025 09:19:19 +0200 Subject: fix overflow for long messages --- src/block.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/block.rs') diff --git a/src/block.rs b/src/block.rs index e63062e..abd7ff7 100644 --- a/src/block.rs +++ b/src/block.rs @@ -53,6 +53,17 @@ impl Block { } Block(block) } + + /// Computes self * 2^exponent + /// + /// Ensures that there's no overflow in computing 2^exponent. + pub fn exp(&self, exponent: u32) -> Block { + match exponent { + _ if exponent < 32 => *self * (1 << exponent), + _ if exponent % 2 == 0 => self.exp(exponent / 2).exp(exponent / 2), + _ => (*self * 2).exp(exponent - 1), + } + } } impl From<[u8; 16]> for Block { -- cgit v1.2.3