diff --git a/crypto/src/math/ec/Nat.cs b/crypto/src/math/ec/Nat.cs
index a5a496fad..9d2290ba7 100644
--- a/crypto/src/math/ec/Nat.cs
+++ b/crypto/src/math/ec/Nat.cs
@@ -69,10 +69,11 @@ namespace Org.BouncyCastle.Math.EC
return (uint)c;
}
- public static uint AddWord(int len, uint x, uint[] z)
+ public static uint AddWord(int len, uint x, uint[] z, int zOff)
{
- ulong c = (ulong)x + z[0];
- z[0] = (uint)c;
+ Debug.Assert(zOff < len);
+ ulong c = (ulong)x + z[zOff + 0];
+ z[zOff + 0] = (uint)c;
c >>= 32;
return c == 0 ? 0 : Inc(len, z, 1);
}
@@ -80,13 +81,18 @@ namespace Org.BouncyCastle.Math.EC
public static uint AddWordExt(int len, uint x, uint[] zz, int zzOff)
{
int extLen = len << 1;
- Debug.Assert(zzOff <= (extLen - 1));
+ Debug.Assert(zzOff < extLen);
ulong c = (ulong)x + zz[zzOff];
zz[zzOff] = (uint)c;
c >>= 32;
return c == 0 ? 0 : Inc(extLen, zz, zzOff + 1);
}
+ public static void Copy(int len, uint[] x, uint[] z)
+ {
+ Array.Copy(x, 0, z, 0, len);
+ }
+
public static uint[] Copy(int len, uint[] x)
{
uint[] z = new uint[len];
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1Field.cs b/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
index cfe3202cd..f57804778 100644
--- a/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
@@ -23,7 +23,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static void AddOne(uint[] x, uint[] z)
{
- Array.Copy(x, 0, z, 0, 16);
+ Nat.Copy(16, x, z);
uint c = Nat.Inc(16, z, 0) + x[16];
if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
{
@@ -87,7 +87,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
public static void Reduce23(uint[] z)
{
uint z16 = z[16];
- uint c = Nat.AddWord(16, z16 >> 9, z) + (z16 & P16);
+ uint c = Nat.AddWord(16, z16 >> 9, z, 0) + (z16 & P16);
if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
{
c += Nat.Inc(16, z, 0);
|