diff options
author | Daniel Schadt <kingdread@gmx.de> | 2025-04-09 09:19:19 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2025-04-09 09:19:19 +0200 |
commit | 778da03f0d80c751a45fc3249d05a2859c4ddfcd (patch) | |
tree | 48cecdacb23800ae01a2e9e234b2cec3d49919a2 /src/block.rs | |
parent | a46e0233cf33f8b82414fa4b03ab1dc710d18ffe (diff) | |
download | zears-778da03f0d80c751a45fc3249d05a2859c4ddfcd.tar.gz zears-778da03f0d80c751a45fc3249d05a2859c4ddfcd.tar.bz2 zears-778da03f0d80c751a45fc3249d05a2859c4ddfcd.zip |
fix overflow for long messages
Diffstat (limited to 'src/block.rs')
-rw-r--r-- | src/block.rs | 11 |
1 files changed, 11 insertions, 0 deletions
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 { |