aboutsummaryrefslogtreecommitdiff
path: root/src/block.rs
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2025-04-09 09:19:19 +0200
committerDaniel Schadt <kingdread@gmx.de>2025-04-09 09:19:19 +0200
commit778da03f0d80c751a45fc3249d05a2859c4ddfcd (patch)
tree48cecdacb23800ae01a2e9e234b2cec3d49919a2 /src/block.rs
parenta46e0233cf33f8b82414fa4b03ab1dc710d18ffe (diff)
downloadzears-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.rs11
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 {