diff --git a/crypto/src/math/ec/Nat.cs b/crypto/src/math/ec/Nat.cs
index 599d929c2..1ddcb1609 100644
--- a/crypto/src/math/ec/Nat.cs
+++ b/crypto/src/math/ec/Nat.cs
@@ -32,18 +32,19 @@ namespace Org.BouncyCastle.Math.EC
return (uint)c;
}
- //public static uint AddDWord(int len, ulong x, uint[] z, int zOff)
- //{
- // Debug.Assert(zOff < (len - 2));
- // ulong c = x;
- // c += (ulong)z[zOff + 0];
- // z[zOff + 0] = (uint)c;
- // c >>= 32;
- // c += (ulong)z[zOff + 1];
- // z[zOff + 1] = (uint)c;
- // c >>= 32;
- // return c == 0 ? 0 : Inc(len, z, zOff + 2);
- //}
+ // TODO Re-write to allow full range for x?
+ public static uint AddDWord(int len, ulong x, uint[] z, int zOff)
+ {
+ Debug.Assert(zOff <= (len - 2));
+ ulong c = x;
+ c += (ulong)z[zOff + 0];
+ z[zOff + 0] = (uint)c;
+ c >>= 32;
+ c += (ulong)z[zOff + 1];
+ z[zOff + 1] = (uint)c;
+ c >>= 32;
+ return c == 0 ? 0 : Inc(len, z, zOff + 2);
+ }
public static uint AddExt(int len, uint[] xx, uint[] yy, uint[] zz)
{
@@ -73,7 +74,8 @@ namespace Org.BouncyCastle.Math.EC
public static uint AddWordExt(int len, uint x, uint[] zz, int zzOff)
{
- Debug.Assert(zzOff < ((len << 1) - 1));
+ int extLen = len << 1;
+ Debug.Assert(zzOff <= (extLen - 1));
ulong c = (ulong)x + zz[zzOff];
zz[zzOff] = (uint)c;
c >>= 32;
@@ -100,16 +102,14 @@ namespace Org.BouncyCastle.Math.EC
public static int Dec(int len, uint[] z, int zOff)
{
- Debug.Assert(zOff < len);
- int i = zOff;
- do
+ Debug.Assert(zOff <= len);
+ for (int i = zOff; i < len; ++i)
{
if (--z[i] != uint.MaxValue)
{
return 0;
}
}
- while (++i < len);
return -1;
}
@@ -172,10 +172,10 @@ namespace Org.BouncyCastle.Math.EC
public static uint Inc(int len, uint[] z, int zOff)
{
- Debug.Assert(zOff < len);
+ Debug.Assert(zOff <= len);
for (int i = zOff; i < len; ++i)
{
- if (++z[i] != 0)
+ if (++z[i] != uint.MinValue)
{
return 0;
}
@@ -186,10 +186,10 @@ namespace Org.BouncyCastle.Math.EC
public static uint IncExt(int len, uint[] zz, int zzOff)
{
int extLen = len;
- Debug.Assert(zzOff < extLen);
+ Debug.Assert(zzOff <= extLen);
for (int i = zzOff; i < extLen; ++i)
{
- if (++zz[i] != 0)
+ if (++zz[i] != uint.MinValue)
{
return 0;
}
@@ -274,7 +274,7 @@ namespace Org.BouncyCastle.Math.EC
public static uint MulWordDwordAdd(int len, uint x, ulong y, uint[] z, int zOff)
{
- Debug.Assert(zOff < (len - 3));
+ Debug.Assert(zOff <= (len - 3));
ulong c = 0, xVal = (ulong)x;
c += xVal * (uint)y + z[zOff + 0];
z[zOff + 0] = (uint)c;
@@ -437,17 +437,19 @@ namespace Org.BouncyCastle.Math.EC
return (int)c;
}
- //public static int SubDWord(int len, ulong x, uint[] z)
- //{
- // long c = -(long)x;
- // c += (long)z[0];
- // z[0] = (uint)c;
- // c >>= 32;
- // c += (long)z[1];
- // z[1] = (uint)c;
- // c >>= 32;
- // return c == 0 ? 0 : Dec(len, z, 2);
- //}
+ // TODO Re-write to allow full range for x?
+ public static int SubDWord(int len, ulong x, uint[] z)
+ {
+ Debug.Assert(len >= 2);
+ long c = -(long)x;
+ c += (long)z[0];
+ z[0] = (uint)c;
+ c >>= 32;
+ c += (long)z[1];
+ z[1] = (uint)c;
+ c >>= 32;
+ return c == 0 ? 0 : Dec(len, z, 2);
+ }
public static int SubExt(int len, uint[] xx, uint[] yy, uint[] zz)
{
diff --git a/crypto/src/math/ec/custom/sec/Nat192.cs b/crypto/src/math/ec/custom/sec/Nat192.cs
index 29f6f28e6..22f79c9d3 100644
--- a/crypto/src/math/ec/custom/sec/Nat192.cs
+++ b/crypto/src/math/ec/custom/sec/Nat192.cs
@@ -60,7 +60,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
// TODO Re-write to allow full range for x?
public static uint AddDWord(ulong x, uint[] z, int zOff)
{
- Debug.Assert(zOff < 4);
+ Debug.Assert(zOff <= 4);
ulong c = x;
c += (ulong)z[zOff + 0];
z[zOff + 0] = (uint)c;
@@ -110,7 +110,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static uint AddWordExt(uint x, uint[] zz, int zzOff)
{
- Debug.Assert(zzOff < 11);
+ Debug.Assert(zzOff <= 11);
ulong c = (ulong)x + zz[zzOff + 0];
zz[zzOff + 0] = (uint)c;
c >>= 32;
@@ -129,16 +129,14 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static int Dec(uint[] z, int zOff)
{
- Debug.Assert(zOff < 6);
- int i = zOff;
- do
+ Debug.Assert(zOff <= 6);
+ for (int i = zOff; i < 6; ++i)
{
if (--z[i] != uint.MaxValue)
{
return 0;
}
}
- while (++i < 6);
return -1;
}
@@ -200,10 +198,10 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static uint Inc(uint[] z, int zOff)
{
- Debug.Assert(zOff < 6);
+ Debug.Assert(zOff <= 6);
for (int i = zOff; i < 6; ++i)
{
- if (++z[i] != 0)
+ if (++z[i] != uint.MinValue)
{
return 0;
}
@@ -213,10 +211,10 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static uint IncExt(uint[] zz, int zzOff)
{
- Debug.Assert(zzOff < 12);
+ Debug.Assert(zzOff <= 12);
for (int i = zzOff; i < 12; ++i)
{
- if (++zz[i] != 0)
+ if (++zz[i] != uint.MinValue)
{
return 0;
}
@@ -385,7 +383,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static uint Mul33DWordAdd(uint x, ulong y, uint[] z, int zOff)
{
Debug.Assert(x >> 31 == 0);
- Debug.Assert(zOff < 2);
+ Debug.Assert(zOff <= 2);
ulong c = 0, xVal = x;
ulong y00 = y & M;
c += xVal * y00 + z[zOff + 0];
@@ -406,7 +404,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static uint MulWordDwordAdd(uint x, ulong y, uint[] z, int zOff)
{
- Debug.Assert(zOff < 4);
+ Debug.Assert(zOff <= 3);
ulong c = 0, xVal = x;
c += xVal * y + z[zOff + 0];
z[zOff + 0] = (uint)c;
diff --git a/crypto/src/math/ec/custom/sec/Nat256.cs b/crypto/src/math/ec/custom/sec/Nat256.cs
index 5837a8820..b0326716a 100644
--- a/crypto/src/math/ec/custom/sec/Nat256.cs
+++ b/crypto/src/math/ec/custom/sec/Nat256.cs
@@ -72,7 +72,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
// TODO Re-write to allow full range for x?
public static uint AddDWord(ulong x, uint[] z, int zOff)
{
- Debug.Assert(zOff < 6);
+ Debug.Assert(zOff <= 6);
ulong c = x;
c += (ulong)z[zOff + 0];
z[zOff + 0] = (uint)c;
@@ -128,7 +128,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static uint AddWordExt(uint x, uint[] zz, int zzOff)
{
- Debug.Assert(zzOff < 15);
+ Debug.Assert(zzOff <= 15);
ulong c = (ulong)x + zz[zzOff + 0];
zz[zzOff + 0] = (uint)c;
c >>= 32;
@@ -147,16 +147,14 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static int Dec(uint[] z, int zOff)
{
- Debug.Assert(zOff < 8);
- int i = zOff;
- do
+ Debug.Assert(zOff <= 8);
+ for (int i = zOff; i < 8; ++i)
{
if (--z[i] != uint.MaxValue)
{
return 0;
}
}
- while (++i < 8);
return -1;
}
@@ -218,10 +216,10 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static uint Inc(uint[] z, int zOff)
{
- Debug.Assert(zOff < 8);
+ Debug.Assert(zOff <= 8);
for (int i = zOff; i < 8; ++i)
{
- if (++z[i] != 0)
+ if (++z[i] != uint.MinValue)
{
return 0;
}
@@ -231,10 +229,10 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static uint IncExt(uint[] zz, int zzOff)
{
- Debug.Assert(zzOff < 16);
+ Debug.Assert(zzOff <= 16);
for (int i = zzOff; i < 16; ++i)
{
- if (++zz[i] != 0)
+ if (++zz[i] != uint.MinValue)
{
return 0;
}
@@ -431,7 +429,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static uint Mul33DWordAdd(uint x, ulong y, uint[] z, int zOff)
{
Debug.Assert(x >> 31 == 0);
- Debug.Assert(zOff < 4);
+ Debug.Assert(zOff <= 4);
ulong c = 0, xVal = x;
ulong y00 = y & M;
c += xVal * y00 + z[zOff + 0];
@@ -452,7 +450,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static uint MulWordDwordAdd(uint x, ulong y, uint[] z, int zOff)
{
- Debug.Assert(zOff < 5);
+ Debug.Assert(zOff <= 5);
ulong c = 0, xVal = x;
c += xVal * y + z[zOff + 0];
z[zOff + 0] = (uint)c;
|