made ranges real voldemort types

This commit is contained in:
Johannes Loher 2016-06-04 11:30:26 +02:00
parent 4452bd6b0a
commit f865e49519
3 changed files with 6 additions and 43 deletions

View file

@ -7,7 +7,6 @@ UIntType rotateLeft(UIntType)(in UIntType val, in size_t len) nothrow @nogc pure
if (isUnsigned!UIntType) if (isUnsigned!UIntType)
{ {
auto reducedLen = len % (8 * UIntType.sizeof); 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))); 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) if (isUnsigned!UIntType)
{ {
auto reducedLen = len % (8 * UIntType.sizeof); 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))); return cast(UIntType)((val >> reducedLen) | (val << (8 * UIntType.sizeof - reducedLen)));
} }

View file

@ -13,24 +13,13 @@ public:
auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce) auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
if (isInputRange!R && is(ElementType!R : ubyte)) if (isInputRange!R && is(ElementType!R : ubyte))
{ {
static struct rangeResult struct rangeResult
{ {
private: private:
ubyte[32] key;
ubyte[8] nonce;
ulong count; ulong count;
R range; R range;
ubyte[] chachaSection; ubyte[] chachaSection;
public: 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 bool empty() @property
{ {
return range.empty || (count == ulong.max && chachaSection.empty); 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 auto save() @property
{ {
rangeResult copy; return rangeResult(count, range.save, chachaSection.dup);
copy.range = range.save;
copy.key = key.dup;
copy.nonce = nonce.dup;
copy.count = count;
copy.chachaSection = chachaSection.dup;
return copy;
} }
} }
} }
return rangeResult(range, key, nonce); return rangeResult(0UL, range, chacha20Exp(key, nonce ~ littleEndianInv(0UL)));
} }
private: private:

View file

@ -11,24 +11,13 @@ public:
auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce) auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
if (isInputRange!R && is(ElementType!R : ubyte)) if (isInputRange!R && is(ElementType!R : ubyte))
{ {
static struct rangeResult struct rangeResult
{ {
private: private:
ubyte[32] key;
ubyte[8] nonce;
ulong count; ulong count;
R range; R range;
ubyte[] salsaSection; ubyte[] salsaSection;
public: 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 bool empty() @property
{ {
return range.empty || (count == ulong.max && salsaSection.empty); 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 auto save() @property
{ {
rangeResult copy; return rangeResult(count, range.save, salsaSection.dup);
copy.range = range.save;
copy.key = key.dup;
copy.nonce = nonce.dup;
copy.count = count;
copy.salsaSection = salsaSection.dup;
return copy;
} }
} }
} }
return rangeResult(0UL, range, salsa20Exp(key, nonce ~ littleEndianInv(0UL)));
return rangeResult(range, key, nonce);
} }
private: private: