From f865e4951965559cc0b88de87fa062c03d2f67a9 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sat, 4 Jun 2016 11:30:26 +0200 Subject: [PATCH] made ranges real voldemort types --- source/bitmanip.d | 2 -- source/chacha20.d | 23 +++-------------------- source/salsa20.d | 24 +++--------------------- 3 files changed, 6 insertions(+), 43 deletions(-) diff --git a/source/bitmanip.d b/source/bitmanip.d index 93e8335..b712579 100644 --- a/source/bitmanip.d +++ b/source/bitmanip.d @@ -7,7 +7,6 @@ UIntType rotateLeft(UIntType)(in UIntType val, in size_t len) nothrow @nogc pure if (isUnsigned!UIntType) { auto reducedLen = len % (8 * UIntType.sizeof); - // TODO: ensure the compiler does not create different code paths here return cast(UIntType)((val << reducedLen) | (val >> (8 * UIntType.sizeof - reducedLen))); } @@ -24,7 +23,6 @@ UIntType rotateRight(UIntType)(in UIntType val, in size_t len) nothrow @nogc pur if (isUnsigned!UIntType) { auto reducedLen = len % (8 * UIntType.sizeof); - // TODO: ensure the compiler does not create different code paths here return cast(UIntType)((val >> reducedLen) | (val << (8 * UIntType.sizeof - reducedLen))); } diff --git a/source/chacha20.d b/source/chacha20.d index 408bd3e..3baec5d 100644 --- a/source/chacha20.d +++ b/source/chacha20.d @@ -13,24 +13,13 @@ public: auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce) if (isInputRange!R && is(ElementType!R : ubyte)) { - static struct rangeResult + struct rangeResult { private: - ubyte[32] key; - ubyte[8] nonce; ulong count; R range; ubyte[] chachaSection; public: - this(R range, in ubyte[32] key, in ubyte[8] nonce) - { - this.range = range; - this.key = key; - this.nonce = nonce; - this.count = 0; - chachaSection = chacha20Exp(key, nonce ~ littleEndianInv(count)); - } - bool empty() @property { return range.empty || (count == ulong.max && chachaSection.empty); @@ -57,17 +46,11 @@ auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce) { auto save() @property { - rangeResult copy; - copy.range = range.save; - copy.key = key.dup; - copy.nonce = nonce.dup; - copy.count = count; - copy.chachaSection = chachaSection.dup; - return copy; + return rangeResult(count, range.save, chachaSection.dup); } } } - return rangeResult(range, key, nonce); + return rangeResult(0UL, range, chacha20Exp(key, nonce ~ littleEndianInv(0UL))); } private: diff --git a/source/salsa20.d b/source/salsa20.d index b06b7e4..200adbf 100644 --- a/source/salsa20.d +++ b/source/salsa20.d @@ -11,24 +11,13 @@ public: auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce) if (isInputRange!R && is(ElementType!R : ubyte)) { - static struct rangeResult + struct rangeResult { private: - ubyte[32] key; - ubyte[8] nonce; ulong count; R range; ubyte[] salsaSection; public: - this(R range, in ubyte[32] key, in ubyte[8] nonce) - { - this.range = range; - this.key = key; - this.nonce = nonce; - this.count = 0; - salsaSection = salsa20Exp(key, nonce ~ littleEndianInv(count)); - } - bool empty() @property { return range.empty || (count == ulong.max && salsaSection.empty); @@ -55,18 +44,11 @@ auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce) { auto save() @property { - rangeResult copy; - copy.range = range.save; - copy.key = key.dup; - copy.nonce = nonce.dup; - copy.count = count; - copy.salsaSection = salsaSection.dup; - return copy; + return rangeResult(count, range.save, salsaSection.dup); } } } - - return rangeResult(range, key, nonce); + return rangeResult(0UL, range, salsa20Exp(key, nonce ~ littleEndianInv(0UL))); } private: