summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-11-26 13:20:35 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-11-26 13:20:35 +0700
commit4f151774bd05b3855e573accf63a0f8ecfda43ac (patch)
tree89fcf3d1e06b6282e6d470f19cdb07fe7d84064e
parentCode cleanup (diff)
downloadBouncyCastle.NET-ed25519-4f151774bd05b3855e573accf63a0f8ecfda43ac.tar.xz
Refactoring in Math.EC.Rfc8032
-rw-r--r--crypto/src/math/ec/custom/gm/SM2P256V1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1Field.cs2
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256R1Field.cs2
-rw-r--r--crypto/src/math/ec/rfc8032/Ed448.cs6
-rw-r--r--crypto/src/math/ec/rfc8032/Scalar25519.cs22
-rw-r--r--crypto/src/math/ec/rfc8032/Scalar448.cs32
-rw-r--r--crypto/src/math/raw/Nat224.cs336
-rw-r--r--crypto/src/math/raw/Nat256.cs474
-rw-r--r--crypto/src/math/raw/Nat448.cs45
-rw-r--r--crypto/src/math/raw/Nat512.cs4
10 files changed, 883 insertions, 42 deletions
diff --git a/crypto/src/math/ec/custom/gm/SM2P256V1Field.cs b/crypto/src/math/ec/custom/gm/SM2P256V1Field.cs
index 6fbe849a8..d4d3a8efe 100644
--- a/crypto/src/math/ec/custom/gm/SM2P256V1Field.cs
+++ b/crypto/src/math/ec/custom/gm/SM2P256V1Field.cs
@@ -50,7 +50,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.GM
             uint[] z = Nat.FromBigInteger(256, x);
             if (z[7] >= P7 && Nat256.Gte(z, P))
             {
-                Nat256.SubFrom(P, z);
+                Nat256.SubFrom(P, z, 0);
             }
             return z;
         }
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
index a065ee790..5c6a66e8d 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
@@ -56,7 +56,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] z = Nat.FromBigInteger(256, x);
             if (z[7] == P7 && Nat256.Gte(z, P))
             {
-                Nat256.SubFrom(P, z);
+                Nat256.SubFrom(P, z, 0);
             }
             return z;
         }
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
index 10465dc6f..4aa3eb59d 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
@@ -50,7 +50,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             uint[] z = Nat.FromBigInteger(256, x);
             if (z[7] == P7 && Nat256.Gte(z, P))
             {
-                Nat256.SubFrom(P, z);
+                Nat256.SubFrom(P, z, 0);
             }
             return z;
         }
diff --git a/crypto/src/math/ec/rfc8032/Ed448.cs b/crypto/src/math/ec/rfc8032/Ed448.cs
index 7b774896b..d2c29a41c 100644
--- a/crypto/src/math/ec/rfc8032/Ed448.cs
+++ b/crypto/src/math/ec/rfc8032/Ed448.cs
@@ -1317,11 +1317,11 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
 #endif
         {
             Debug.Assert(nb.Length == ScalarUints);
-            Debug.Assert((int)nb[ScalarUints - 1] >= 0);
+            Debug.Assert(nb[ScalarUints - 1] >> 30 == 0U);
             Debug.Assert(np.Length == 8);
-            Debug.Assert((int)np[7] >> 31 == (int)np[7] >> 1);
+            Debug.Assert((int)np[7] >> 31 == (int)np[7]);
             Debug.Assert(nq.Length == 8);
-            Debug.Assert((int)nq[7] >> 31 == (int)nq[7] >> 1);
+            Debug.Assert((int)nq[7] >> 31 == (int)nq[7]);
 
             Precompute();
 
diff --git a/crypto/src/math/ec/rfc8032/Scalar25519.cs b/crypto/src/math/ec/rfc8032/Scalar25519.cs
index d0c7b7478..00dcd49a1 100644
--- a/crypto/src/math/ec/rfc8032/Scalar25519.cs
+++ b/crypto/src/math/ec/rfc8032/Scalar25519.cs
@@ -36,7 +36,7 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
         internal static bool CheckVar(ReadOnlySpan<byte> s, Span<uint> n)
         {
             Decode(s, n);
-            return !Nat.Gte(Size, n, L);
+            return !Nat256.Gte(n, L);
         }
 #else
         internal static bool CheckVar(byte[] s, uint[] n)
@@ -71,28 +71,28 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
         internal static void Multiply128Var(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y128, Span<uint> z)
         {
             Span<uint> tt = stackalloc uint[16];
-            Nat.Mul(y128, x, tt);
+            Nat256.Mul128(x, y128, tt);
 
             if ((int)y128[3] < 0)
             {
-                Nat.AddTo(8, L, tt[4..]);
-                Nat.SubFrom(8, x, tt[4..]);
+                Nat256.AddTo(L, tt[4..], 0U);
+                Nat256.SubFrom(x, tt[4..], 0);
             }
 
             Span<byte> r = MemoryMarshal.AsBytes(tt);
             Reduce(r, r);
-            tt[..8].CopyTo(z);
+            tt[..Size].CopyTo(z);
         }
 #else
         internal static void Multiply128Var(uint[] x, uint[] y128, uint[] z)
         {
             uint[] tt = new uint[12];
-            Nat.Mul(y128, 0, 4, x, 0, 8, tt, 0);
+            Nat256.Mul128(x, y128, tt);
 
             if ((int)y128[3] < 0)
             {
                 Nat256.AddTo(L, 0, tt, 4, 0U);
-                Nat256.SubFrom(x, 0, tt, 4);
+                Nat256.SubFrom(x, 0, tt, 4, 0);
             }
 
             byte[] bytes = new byte[64];
@@ -391,8 +391,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
              */
 
             Span<uint> Nu = stackalloc uint[16];    LSq.CopyTo(Nu);
-            Span<uint> Nv = stackalloc uint[16];    Nat.Square(8, k, Nv); Nat.AddWordTo(16, 1U, Nv);
-            Span<uint> p  = stackalloc uint[16];    Nat.Mul(8, L, k, p);
+            Span<uint> Nv = stackalloc uint[16];    Nat256.Square(k, Nv); ++Nv[0];
+            Span<uint> p  = stackalloc uint[16];    Nat256.Mul(L, k, p);
             Span<uint> u0 = stackalloc uint[4];     u0.CopyFrom(L);
             Span<uint> u1 = stackalloc uint[4];
             Span<uint> v0 = stackalloc uint[4];     v0.CopyFrom(k);
@@ -443,8 +443,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
              */
 
             uint[] Nu = new uint[16];       Array.Copy(LSq, Nu, 16);
-            uint[] Nv = new uint[16];       Nat.Square(8, k, Nv); Nat.AddWordTo(16, 1U, Nv);
-            uint[] p  = new uint[16];       Nat.Mul(8, L, k, p);
+            uint[] Nv = new uint[16];       Nat256.Square(k, Nv); ++Nv[0];
+            uint[] p  = new uint[16];       Nat256.Mul(L, k, p);
             uint[] u0 = new uint[4];        Array.Copy(L, u0, 4);
             uint[] u1 = new uint[4];
             uint[] v0 = new uint[4];        Array.Copy(k, v0, 4);
diff --git a/crypto/src/math/ec/rfc8032/Scalar448.cs b/crypto/src/math/ec/rfc8032/Scalar448.cs
index c138bea04..5840b05ec 100644
--- a/crypto/src/math/ec/rfc8032/Scalar448.cs
+++ b/crypto/src/math/ec/rfc8032/Scalar448.cs
@@ -95,29 +95,33 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
         internal static void Multiply225Var(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y225, Span<uint> z)
         {
+            Debug.Assert((int)y225[7] >> 31 == (int)y225[7]);
+
             Span<uint> tt = stackalloc uint[29];
             Nat.Mul(y225, x, tt);
 
             if ((int)y225[7] < 0)
             {
-                Nat.AddTo(14, L, tt[8..]);
-                Nat.SubFrom(14, x, tt[8..]);
+                Nat.AddTo(Size, L, tt[8..]);
+                Nat.SubFrom(Size, x, tt[8..]);
             }
 
             Span<byte> r = MemoryMarshal.AsBytes(tt);
             Reduce(r, r);
-            tt[..14].CopyTo(z);
+            tt[..Size].CopyTo(z);
         }
 #else
         internal static void Multiply225Var(uint[] x, uint[] y225, uint[] z)
         {
+            Debug.Assert((int)y225[7] >> 31 == (int)y225[7]);
+
             uint[] tt = new uint[22];
-            Nat.Mul(y225, 0, 8, x, 0, 14, tt, 0);
+            Nat.Mul(y225, 0, 8, x, 0, Size, tt, 0);
 
             if ((int)y225[7] < 0)
             {
-                Nat.AddTo(14, L, 0, tt, 8);
-                Nat.SubFrom(14, x, 0, tt, 8);
+                Nat.AddTo(Size, L, 0, tt, 8);
+                Nat.SubFrom(Size, x, 0, tt, 8);
             }
 
             byte[] bytes = new byte[114];
@@ -698,8 +702,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
              */
 
             Span<uint> Nu = stackalloc uint[28];    LSq.CopyTo(Nu);
-            Span<uint> Nv = stackalloc uint[28];    Nat.Square(14, k, Nv); Nat.AddWordTo(28, 1U, Nv);
-            Span<uint> p  = stackalloc uint[28];    Nat.Mul(14, L, k, p);
+            Span<uint> Nv = stackalloc uint[28];    Nat448.Square(k, Nv); ++Nv[0];
+            Span<uint> p  = stackalloc uint[28];    Nat448.Mul(L, k, p);
             Span<uint> u0 = stackalloc uint[8];     u0.CopyFrom(L);
             Span<uint> u1 = stackalloc uint[8];
             Span<uint> v0 = stackalloc uint[8];     v0.CopyFrom(k);
@@ -736,8 +740,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
                 }
             }
 
-            Debug.Assert((int)v0[7] >> 31 == (int)v0[7] >> 1);
-            Debug.Assert((int)v1[7] >> 31 == (int)v1[7] >> 1);
+            Debug.Assert((int)v0[7] >> 31 == (int)v0[7]);
+            Debug.Assert((int)v1[7] >> 31 == (int)v1[7]);
 
             // v1 * k == v0 mod L
             v0.CopyTo(z0);
@@ -753,8 +757,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
              */
 
             uint[] Nu = new uint[28];       Array.Copy(LSq, Nu, 28);
-            uint[] Nv = new uint[28];       Nat.Square(14, k, Nv); Nat.AddWordTo(28, 1U, Nv);
-            uint[] p  = new uint[28];       Nat.Mul(14, L, k, p);
+            uint[] Nv = new uint[28];       Nat448.Square(k, Nv); ++Nv[0];
+            uint[] p  = new uint[28];       Nat448.Mul(L, k, p);
             uint[] u0 = new uint[8];        Array.Copy(L, u0, 8);
             uint[] u1 = new uint[8];
             uint[] v0 = new uint[8];        Array.Copy(k, v0, 8);
@@ -791,8 +795,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
                 }
             }
 
-            Debug.Assert((int)v0[7] >> 31 == (int)v0[7] >> 1);
-            Debug.Assert((int)v1[7] >> 31 == (int)v1[7] >> 1);
+            Debug.Assert((int)v0[7] >> 31 == (int)v0[7]);
+            Debug.Assert((int)v1[7] >> 31 == (int)v1[7]);
 
             // v1 * k == v0 mod L
             Array.Copy(v0, z0, 8);
diff --git a/crypto/src/math/raw/Nat224.cs b/crypto/src/math/raw/Nat224.cs
index 1aabd3f17..8393dc763 100644
--- a/crypto/src/math/raw/Nat224.cs
+++ b/crypto/src/math/raw/Nat224.cs
@@ -117,9 +117,9 @@ namespace Org.BouncyCastle.Math.Raw
             return (uint)c;
         }
 
-        public static uint AddTo(uint[] x, uint[] z)
+        public static uint AddTo(uint[] x, uint[] z, uint cIn)
         {
-            ulong c = 0;
+            ulong c = cIn;
             c += (ulong)x[0] + z[0];
             z[0] = (uint)c;
             c >>= 32;
@@ -171,6 +171,35 @@ namespace Org.BouncyCastle.Math.Raw
             return (uint)c;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static uint AddTo(ReadOnlySpan<uint> x, Span<uint> z, uint cIn)
+        {
+            ulong c = cIn;
+            c += (ulong)x[0] + z[0];
+            z[0] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[1] + z[1];
+            z[1] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[2] + z[2];
+            z[2] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[3] + z[3];
+            z[3] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[4] + z[4];
+            z[4] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[5] + z[5];
+            z[5] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[6] + z[6];
+            z[6] = (uint)c;
+            c >>= 32;
+            return (uint)c;
+        }
+#endif
+
         public static uint AddToEachOther(uint[] u, int uOff, uint[] v, int vOff)
         {
             ulong c = 0;
@@ -205,6 +234,42 @@ namespace Org.BouncyCastle.Math.Raw
             return (uint)c;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static uint AddToEachOther(Span<uint> u, Span<uint> v)
+        {
+            ulong c = 0;
+            c += (ulong)u[0] + v[0];
+            u[0] = (uint)c;
+            v[0] = (uint)c;
+            c >>= 32;
+            c += (ulong)u[1] + v[1];
+            u[1] = (uint)c;
+            v[1] = (uint)c;
+            c >>= 32;
+            c += (ulong)u[2] + v[2];
+            u[2] = (uint)c;
+            v[2] = (uint)c;
+            c >>= 32;
+            c += (ulong)u[3] + v[3];
+            u[3] = (uint)c;
+            v[3] = (uint)c;
+            c >>= 32;
+            c += (ulong)u[4] + v[4];
+            u[4] = (uint)c;
+            v[4] = (uint)c;
+            c >>= 32;
+            c += (ulong)u[5] + v[5];
+            u[5] = (uint)c;
+            v[5] = (uint)c;
+            c >>= 32;
+            c += (ulong)u[6] + v[6];
+            u[6] = (uint)c;
+            v[6] = (uint)c;
+            c >>= 32;
+            return (uint)c;
+        }
+#endif
+
         public static void Copy(uint[] x, uint[] z)
         {
             z[0] = x[0];
@@ -251,6 +316,22 @@ namespace Org.BouncyCastle.Math.Raw
             return pos;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static bool Diff(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y, Span<uint> z)
+        {
+            bool pos = Gte(x, y);
+            if (pos)
+            {
+                Sub(x, y, z);
+            }
+            else
+            {
+                Sub(y, x, z);
+            }
+            return pos;
+        }
+#endif
+
         public static bool Eq(uint[] x, uint[] y)
         {
             for (int i = 6; i >= 0; --i)
@@ -302,6 +383,21 @@ namespace Org.BouncyCastle.Math.Raw
             return true;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static bool Gte(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y)
+        {
+            for (int i = 6; i >= 0; --i)
+            {
+                uint x_i = x[i], y_i = y[i];
+                if (x_i < y_i)
+                    return false;
+                if (x_i > y_i)
+                    return true;
+            }
+            return true;
+        }
+#endif
+
         public static bool IsOne(uint[] x)
         {
             if (x[0] != 1)
@@ -459,6 +555,72 @@ namespace Org.BouncyCastle.Math.Raw
             }
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static void Mul(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y, Span<uint> zz)
+        {
+            ulong y_0 = y[0];
+            ulong y_1 = y[1];
+            ulong y_2 = y[2];
+            ulong y_3 = y[3];
+            ulong y_4 = y[4];
+            ulong y_5 = y[5];
+            ulong y_6 = y[6];
+
+            {
+                ulong c = 0, x_0 = x[0];
+                c += x_0 * y_0;
+                zz[0] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_1;
+                zz[1] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_2;
+                zz[2] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_3;
+                zz[3] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_4;
+                zz[4] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_5;
+                zz[5] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_6;
+                zz[6] = (uint)c;
+                c >>= 32;
+                zz[7] = (uint)c;
+            }
+
+            for (int i = 1; i < 7; ++i)
+            {
+                ulong c = 0, x_i = x[i];
+                c += x_i * y_0 + zz[i + 0];
+                zz[i + 0] = (uint)c;
+                c >>= 32;
+                c += x_i * y_1 + zz[i + 1];
+                zz[i + 1] = (uint)c;
+                c >>= 32;
+                c += x_i * y_2 + zz[i + 2];
+                zz[i + 2] = (uint)c;
+                c >>= 32;
+                c += x_i * y_3 + zz[i + 3];
+                zz[i + 3] = (uint)c;
+                c >>= 32;
+                c += x_i * y_4 + zz[i + 4];
+                zz[i + 4] = (uint)c;
+                c >>= 32;
+                c += x_i * y_5 + zz[i + 5];
+                zz[i + 5] = (uint)c;
+                c >>= 32;
+                c += x_i * y_6 + zz[i + 6];
+                zz[i + 6] = (uint)c;
+                c >>= 32;
+                zz[i + 7] = (uint)c;
+            }
+        }
+#endif
+
         public static uint MulAddTo(uint[] x, uint[] y, uint[] zz)
         {
             ulong y_0 = y[0];
@@ -1011,6 +1173,147 @@ namespace Org.BouncyCastle.Math.Raw
             zz[zzOff + 13] = (w << 1) | c;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static void Square(ReadOnlySpan<uint> x, Span<uint> zz)
+        {
+            ulong x_0 = x[0];
+            ulong zz_1;
+
+            uint c = 0, w;
+            {
+                int i = 6, j = 14;
+                do
+                {
+                    ulong xVal = x[i--];
+                    ulong p = xVal * xVal;
+                    zz[--j] = (c << 31) | (uint)(p >> 33);
+                    zz[--j] = (uint)(p >> 1);
+                    c = (uint)p;
+                }
+                while (i > 0);
+
+                {
+                    ulong p = x_0 * x_0;
+                    zz_1 = (ulong)(c << 31) | (p >> 33);
+                    zz[0] = (uint)p;
+                    c = (uint)(p >> 32) & 1;
+                }
+            }
+
+            ulong x_1 = x[1];
+            ulong zz_2 = zz[2];
+
+            {
+                zz_1 += x_1 * x_0;
+                w = (uint)zz_1;
+                zz[1] = (w << 1) | c;
+                c = w >> 31;
+                zz_2 += zz_1 >> 32;
+            }
+
+            ulong x_2 = x[2];
+            ulong zz_3 = zz[3];
+            ulong zz_4 = zz[4];
+            {
+                zz_2 += x_2 * x_0;
+                w = (uint)zz_2;
+                zz[2] = (w << 1) | c;
+                c = w >> 31;
+                zz_3 += (zz_2 >> 32) + x_2 * x_1;
+                zz_4 += zz_3 >> 32;
+                zz_3 &= M;
+            }
+
+            ulong x_3 = x[3];
+            ulong zz_5 = zz[5] + (zz_4 >> 32); zz_4 &= M;
+            ulong zz_6 = zz[6] + (zz_5 >> 32); zz_5 &= M;
+            {
+                zz_3 += x_3 * x_0;
+                w = (uint)zz_3;
+                zz[3] = (w << 1) | c;
+                c = w >> 31;
+                zz_4 += (zz_3 >> 32) + x_3 * x_1;
+                zz_5 += (zz_4 >> 32) + x_3 * x_2;
+                zz_4 &= M;
+                zz_6 += zz_5 >> 32;
+                zz_5 &= M;
+            }
+
+            ulong x_4 = x[4];
+            ulong zz_7 = zz[7] + (zz_6 >> 32); zz_6 &= M;
+            ulong zz_8 = zz[8] + (zz_7 >> 32); zz_7 &= M;
+            {
+                zz_4 += x_4 * x_0;
+                w = (uint)zz_4;
+                zz[4] = (w << 1) | c;
+                c = w >> 31;
+                zz_5 += (zz_4 >> 32) + x_4 * x_1;
+                zz_6 += (zz_5 >> 32) + x_4 * x_2;
+                zz_5 &= M;
+                zz_7 += (zz_6 >> 32) + x_4 * x_3;
+                zz_6 &= M;
+                zz_8 += zz_7 >> 32;
+                zz_7 &= M;
+            }
+
+            ulong x_5 = x[5];
+            ulong zz_9 = zz[9] + (zz_8 >> 32); zz_8 &= M;
+            ulong zz_10 = zz[10] + (zz_9 >> 32); zz_9 &= M;
+            {
+                zz_5 += x_5 * x_0;
+                w = (uint)zz_5;
+                zz[5] = (w << 1) | c;
+                c = w >> 31;
+                zz_6 += (zz_5 >> 32) + x_5 * x_1;
+                zz_7 += (zz_6 >> 32) + x_5 * x_2;
+                zz_6 &= M;
+                zz_8 += (zz_7 >> 32) + x_5 * x_3;
+                zz_7 &= M;
+                zz_9 += (zz_8 >> 32) + x_5 * x_4;
+                zz_8 &= M;
+                zz_10 += zz_9 >> 32;
+                zz_9 &= M;
+            }
+
+            ulong x_6 = x[6];
+            ulong zz_11 = zz[11] + (zz_10 >> 32); zz_10 &= M;
+            ulong zz_12 = zz[12] + (zz_11 >> 32); zz_11 &= M;
+            {
+                zz_6 += x_6 * x_0;
+                w = (uint)zz_6;
+                zz[6] = (w << 1) | c;
+                c = w >> 31;
+                zz_7 += (zz_6 >> 32) + x_6 * x_1;
+                zz_8 += (zz_7 >> 32) + x_6 * x_2;
+                zz_9 += (zz_8 >> 32) + x_6 * x_3;
+                zz_10 += (zz_9 >> 32) + x_6 * x_4;
+                zz_11 += (zz_10 >> 32) + x_6 * x_5;
+                zz_12 += zz_11 >> 32;
+            }
+
+            w = (uint)zz_7;
+            zz[7] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_8;
+            zz[8] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_9;
+            zz[9] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_10;
+            zz[10] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_11;
+            zz[11] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_12;
+            zz[12] = (w << 1) | c;
+            c = w >> 31;
+            w = zz[13] + (uint)(zz_12 >> 32);
+            zz[13] = (w << 1) | c;
+        }
+#endif
+
         public static int Sub(uint[] x, uint[] y, uint[] z)
         {
             long c = 0;
@@ -1065,6 +1368,35 @@ namespace Org.BouncyCastle.Math.Raw
             return (int)c;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static int Sub(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y, Span<uint> z)
+        {
+            long c = 0;
+            c += (long)x[0] - y[0];
+            z[0] = (uint)c;
+            c >>= 32;
+            c += (long)x[1] - y[1];
+            z[1] = (uint)c;
+            c >>= 32;
+            c += (long)x[2] - y[2];
+            z[2] = (uint)c;
+            c >>= 32;
+            c += (long)x[3] - y[3];
+            z[3] = (uint)c;
+            c >>= 32;
+            c += (long)x[4] - y[4];
+            z[4] = (uint)c;
+            c >>= 32;
+            c += (long)x[5] - y[5];
+            z[5] = (uint)c;
+            c >>= 32;
+            c += (long)x[6] - y[6];
+            z[6] = (uint)c;
+            c >>= 32;
+            return (int)c;
+        }
+#endif
+
         public static int SubBothFrom(uint[] x, uint[] y, uint[] z)
         {
             long c = 0;
diff --git a/crypto/src/math/raw/Nat256.cs b/crypto/src/math/raw/Nat256.cs
index 47e0644f6..1bedeb51c 100644
--- a/crypto/src/math/raw/Nat256.cs
+++ b/crypto/src/math/raw/Nat256.cs
@@ -135,9 +135,9 @@ namespace Org.BouncyCastle.Math.Raw
             return (uint)c;
         }
 
-        public static uint AddTo(uint[] x, uint[] z)
+        public static uint AddTo(uint[] x, uint[] z, uint cIn)
         {
-            ulong c = 0;
+            ulong c = cIn;
             c += (ulong)x[0] + z[0];
             z[0] = (uint)c;
             c >>= 32;
@@ -195,6 +195,38 @@ namespace Org.BouncyCastle.Math.Raw
             return (uint)c;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static uint AddTo(ReadOnlySpan<uint> x, Span<uint> z, uint cIn)
+        {
+            ulong c = cIn;
+            c += (ulong)x[0] + z[0];
+            z[0] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[1] + z[1];
+            z[1] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[2] + z[2];
+            z[2] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[3] + z[3];
+            z[3] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[4] + z[4];
+            z[4] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[5] + z[5];
+            z[5] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[6] + z[6];
+            z[6] = (uint)c;
+            c >>= 32;
+            c += (ulong)x[7] + z[7];
+            z[7] = (uint)c;
+            c >>= 32;
+            return (uint)c;
+        }
+#endif
+
         public static uint AddToEachOther(uint[] u, int uOff, uint[] v, int vOff)
         {
             ulong c = 0;
@@ -370,6 +402,21 @@ namespace Org.BouncyCastle.Math.Raw
             return true;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static bool Gte(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y)
+        {
+            for (int i = 7; i >= 0; --i)
+            {
+                uint x_i = x[i], y_i = y[i];
+                if (x_i < y_i)
+                    return false;
+                if (x_i > y_i)
+                    return true;
+            }
+            return true;
+        }
+#endif
+
         public static bool IsOne(uint[] x)
         {
             if (x[0] != 1)
@@ -569,6 +616,223 @@ namespace Org.BouncyCastle.Math.Raw
             }
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static void Mul(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y, Span<uint> zz)
+        {
+            ulong y_0 = y[0];
+            ulong y_1 = y[1];
+            ulong y_2 = y[2];
+            ulong y_3 = y[3];
+            ulong y_4 = y[4];
+            ulong y_5 = y[5];
+            ulong y_6 = y[6];
+            ulong y_7 = y[7];
+
+            {
+                ulong c = 0, x_0 = x[0];
+                c += x_0 * y_0;
+                zz[0] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_1;
+                zz[1] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_2;
+                zz[2] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_3;
+                zz[3] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_4;
+                zz[4] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_5;
+                zz[5] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_6;
+                zz[6] = (uint)c;
+                c >>= 32;
+                c += x_0 * y_7;
+                zz[7] = (uint)c;
+                c >>= 32;
+                zz[8] = (uint)c;
+            }
+
+            for (int i = 1; i < 8; ++i)
+            {
+                ulong c = 0, x_i = x[i];
+                c += x_i * y_0 + zz[i + 0];
+                zz[i + 0] = (uint)c;
+                c >>= 32;
+                c += x_i * y_1 + zz[i + 1];
+                zz[i + 1] = (uint)c;
+                c >>= 32;
+                c += x_i * y_2 + zz[i + 2];
+                zz[i + 2] = (uint)c;
+                c >>= 32;
+                c += x_i * y_3 + zz[i + 3];
+                zz[i + 3] = (uint)c;
+                c >>= 32;
+                c += x_i * y_4 + zz[i + 4];
+                zz[i + 4] = (uint)c;
+                c >>= 32;
+                c += x_i * y_5 + zz[i + 5];
+                zz[i + 5] = (uint)c;
+                c >>= 32;
+                c += x_i * y_6 + zz[i + 6];
+                zz[i + 6] = (uint)c;
+                c >>= 32;
+                c += x_i * y_7 + zz[i + 7];
+                zz[i + 7] = (uint)c;
+                c >>= 32;
+                zz[i + 8] = (uint)c;
+            }
+        }
+#endif
+
+        public static void Mul128(uint[] x, uint[] y128, uint[] zz)
+        {
+            ulong x_0 = x[0];
+            ulong x_1 = x[1];
+            ulong x_2 = x[2];
+            ulong x_3 = x[3];
+            ulong x_4 = x[4];
+            ulong x_5 = x[5];
+            ulong x_6 = x[6];
+            ulong x_7 = x[7];
+
+            {
+                ulong c = 0, y_0 = y128[0];
+                c += y_0 * x_0;
+                zz[0] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_1;
+                zz[1] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_2;
+                zz[2] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_3;
+                zz[3] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_4;
+                zz[4] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_5;
+                zz[5] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_6;
+                zz[6] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_7;
+                zz[7] = (uint)c;
+                c >>= 32;
+                zz[8] = (uint)c;
+            }
+
+            for (int i = 1; i < 4; ++i)
+            {
+                ulong c = 0, y_i = y128[i];
+                c += y_i * x_0 + zz[i + 0];
+                zz[i + 0] = (uint)c;
+                c >>= 32;
+                c += y_i * x_1 + zz[i + 1];
+                zz[i + 1] = (uint)c;
+                c >>= 32;
+                c += y_i * x_2 + zz[i + 2];
+                zz[i + 2] = (uint)c;
+                c >>= 32;
+                c += y_i * x_3 + zz[i + 3];
+                zz[i + 3] = (uint)c;
+                c >>= 32;
+                c += y_i * x_4 + zz[i + 4];
+                zz[i + 4] = (uint)c;
+                c >>= 32;
+                c += y_i * x_5 + zz[i + 5];
+                zz[i + 5] = (uint)c;
+                c >>= 32;
+                c += y_i * x_6 + zz[i + 6];
+                zz[i + 6] = (uint)c;
+                c >>= 32;
+                c += y_i * x_7 + zz[i + 7];
+                zz[i + 7] = (uint)c;
+                c >>= 32;
+                zz[i + 8] = (uint)c;
+            }
+        }
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static void Mul128(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y128, Span<uint> zz)
+        {
+            ulong x_0 = x[0];
+            ulong x_1 = x[1];
+            ulong x_2 = x[2];
+            ulong x_3 = x[3];
+            ulong x_4 = x[4];
+            ulong x_5 = x[5];
+            ulong x_6 = x[6];
+            ulong x_7 = x[7];
+
+            {
+                ulong c = 0, y_0 = y128[0];
+                c += y_0 * x_0;
+                zz[0] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_1;
+                zz[1] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_2;
+                zz[2] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_3;
+                zz[3] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_4;
+                zz[4] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_5;
+                zz[5] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_6;
+                zz[6] = (uint)c;
+                c >>= 32;
+                c += y_0 * x_7;
+                zz[7] = (uint)c;
+                c >>= 32;
+                zz[8] = (uint)c;
+            }
+
+            for (int i = 1; i < 4; ++i)
+            {
+                ulong c = 0, y_i = y128[i];
+                c += y_i * x_0 + zz[i + 0];
+                zz[i + 0] = (uint)c;
+                c >>= 32;
+                c += y_i * x_1 + zz[i + 1];
+                zz[i + 1] = (uint)c;
+                c >>= 32;
+                c += y_i * x_2 + zz[i + 2];
+                zz[i + 2] = (uint)c;
+                c >>= 32;
+                c += y_i * x_3 + zz[i + 3];
+                zz[i + 3] = (uint)c;
+                c >>= 32;
+                c += y_i * x_4 + zz[i + 4];
+                zz[i + 4] = (uint)c;
+                c >>= 32;
+                c += y_i * x_5 + zz[i + 5];
+                zz[i + 5] = (uint)c;
+                c >>= 32;
+                c += y_i * x_6 + zz[i + 6];
+                zz[i + 6] = (uint)c;
+                c >>= 32;
+                c += y_i * x_7 + zz[i + 7];
+                zz[i + 7] = (uint)c;
+                c >>= 32;
+                zz[i + 8] = (uint)c;
+            }
+        }
+#endif
+
         public static uint MulAddTo(uint[] x, uint[] y, uint[] zz)
         {
             ulong y_0 = y[0];
@@ -1192,6 +1456,172 @@ namespace Org.BouncyCastle.Math.Raw
             zz[zzOff + 15] = (w << 1) | c;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static void Square(ReadOnlySpan<uint> x, Span<uint> zz)
+        {
+            ulong x_0 = x[0];
+            ulong zz_1;
+
+            uint c = 0, w;
+            {
+                int i = 7, j = 16;
+                do
+                {
+                    ulong xVal = x[i--];
+                    ulong p = xVal * xVal;
+                    zz[--j] = (c << 31) | (uint)(p >> 33);
+                    zz[--j] = (uint)(p >> 1);
+                    c = (uint)p;
+                }
+                while (i > 0);
+
+                {
+                    ulong p = x_0 * x_0;
+                    zz_1 = (ulong)(c << 31) | (p >> 33);
+                    zz[0] = (uint)p;
+                    c = (uint)(p >> 32) & 1;
+                }
+            }
+
+            ulong x_1 = x[1];
+            ulong zz_2 = zz[2];
+
+            {
+                zz_1 += x_1 * x_0;
+                w = (uint)zz_1;
+                zz[1] = (w << 1) | c;
+                c = w >> 31;
+                zz_2 += zz_1 >> 32;
+            }
+
+            ulong x_2 = x[2];
+            ulong zz_3 = zz[3];
+            ulong zz_4 = zz[4];
+            {
+                zz_2 += x_2 * x_0;
+                w = (uint)zz_2;
+                zz[2] = (w << 1) | c;
+                c = w >> 31;
+                zz_3 += (zz_2 >> 32) + x_2 * x_1;
+                zz_4 += zz_3 >> 32;
+                zz_3 &= M;
+            }
+
+            ulong x_3 = x[3];
+            ulong zz_5 = zz[5] + (zz_4 >> 32); zz_4 &= M;
+            ulong zz_6 = zz[6] + (zz_5 >> 32); zz_5 &= M;
+            {
+                zz_3 += x_3 * x_0;
+                w = (uint)zz_3;
+                zz[3] = (w << 1) | c;
+                c = w >> 31;
+                zz_4 += (zz_3 >> 32) + x_3 * x_1;
+                zz_5 += (zz_4 >> 32) + x_3 * x_2;
+                zz_4 &= M;
+                zz_6 += zz_5 >> 32;
+                zz_5 &= M;
+            }
+
+            ulong x_4 = x[4];
+            ulong zz_7 = zz[7] + (zz_6 >> 32); zz_6 &= M;
+            ulong zz_8 = zz[8] + (zz_7 >> 32); zz_7 &= M;
+            {
+                zz_4 += x_4 * x_0;
+                w = (uint)zz_4;
+                zz[4] = (w << 1) | c;
+                c = w >> 31;
+                zz_5 += (zz_4 >> 32) + x_4 * x_1;
+                zz_6 += (zz_5 >> 32) + x_4 * x_2;
+                zz_5 &= M;
+                zz_7 += (zz_6 >> 32) + x_4 * x_3;
+                zz_6 &= M;
+                zz_8 += zz_7 >> 32;
+                zz_7 &= M;
+            }
+
+            ulong x_5 = x[5];
+            ulong zz_9 = zz[9] + (zz_8 >> 32); zz_8 &= M;
+            ulong zz_10 = zz[10] + (zz_9 >> 32); zz_9 &= M;
+            {
+                zz_5 += x_5 * x_0;
+                w = (uint)zz_5;
+                zz[5] = (w << 1) | c;
+                c = w >> 31;
+                zz_6 += (zz_5 >> 32) + x_5 * x_1;
+                zz_7 += (zz_6 >> 32) + x_5 * x_2;
+                zz_6 &= M;
+                zz_8 += (zz_7 >> 32) + x_5 * x_3;
+                zz_7 &= M;
+                zz_9 += (zz_8 >> 32) + x_5 * x_4;
+                zz_8 &= M;
+                zz_10 += zz_9 >> 32;
+                zz_9 &= M;
+            }
+
+            ulong x_6 = x[6];
+            ulong zz_11 = zz[11] + (zz_10 >> 32); zz_10 &= M;
+            ulong zz_12 = zz[12] + (zz_11 >> 32); zz_11 &= M;
+            {
+                zz_6 += x_6 * x_0;
+                w = (uint)zz_6;
+                zz[6] = (w << 1) | c;
+                c = w >> 31;
+                zz_7 += (zz_6 >> 32) + x_6 * x_1;
+                zz_8 += (zz_7 >> 32) + x_6 * x_2;
+                zz_7 &= M;
+                zz_9 += (zz_8 >> 32) + x_6 * x_3;
+                zz_8 &= M;
+                zz_10 += (zz_9 >> 32) + x_6 * x_4;
+                zz_9 &= M;
+                zz_11 += (zz_10 >> 32) + x_6 * x_5;
+                zz_10 &= M;
+                zz_12 += zz_11 >> 32;
+                zz_11 &= M;
+            }
+
+            ulong x_7 = x[7];
+            ulong zz_13 = zz[13] + (zz_12 >> 32); zz_12 &= M;
+            ulong zz_14 = zz[14] + (zz_13 >> 32); zz_13 &= M;
+            {
+                zz_7 += x_7 * x_0;
+                w = (uint)zz_7;
+                zz[7] = (w << 1) | c;
+                c = w >> 31;
+                zz_8 += (zz_7 >> 32) + x_7 * x_1;
+                zz_9 += (zz_8 >> 32) + x_7 * x_2;
+                zz_10 += (zz_9 >> 32) + x_7 * x_3;
+                zz_11 += (zz_10 >> 32) + x_7 * x_4;
+                zz_12 += (zz_11 >> 32) + x_7 * x_5;
+                zz_13 += (zz_12 >> 32) + x_7 * x_6;
+                zz_14 += zz_13 >> 32;
+            }
+
+            w = (uint)zz_8;
+            zz[8] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_9;
+            zz[9] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_10;
+            zz[10] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_11;
+            zz[11] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_12;
+            zz[12] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_13;
+            zz[13] = (w << 1) | c;
+            c = w >> 31;
+            w = (uint)zz_14;
+            zz[14] = (w << 1) | c;
+            c = w >> 31;
+            w = zz[15] + (uint)(zz_14 >> 32);
+            zz[15] = (w << 1) | c;
+        }
+#endif
+
         public static int Sub(uint[] x, uint[] y, uint[] z)
         {
             long c = 0;
@@ -1282,9 +1712,9 @@ namespace Org.BouncyCastle.Math.Raw
             return (int)c;
         }
 
-        public static int SubFrom(uint[] x, uint[] z)
+        public static int SubFrom(uint[] x, uint[] z, int cIn)
         {
-            long c = 0;
+            long c = cIn;
             c += (long)z[0] - x[0];
             z[0] = (uint)c;
             c >>= 32;
@@ -1312,9 +1742,9 @@ namespace Org.BouncyCastle.Math.Raw
             return (int)c;
         }
 
-        public static int SubFrom(uint[] x, int xOff, uint[] z, int zOff)
+        public static int SubFrom(uint[] x, int xOff, uint[] z, int zOff, int cIn)
         {
-            long c = 0;
+            long c = cIn;
             c += (long)z[zOff + 0] - x[xOff + 0];
             z[zOff + 0] = (uint)c;
             c >>= 32;
@@ -1342,6 +1772,38 @@ namespace Org.BouncyCastle.Math.Raw
             return (int)c;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static int SubFrom(ReadOnlySpan<uint> x, Span<uint> z, int cIn)
+        {
+            long c = cIn;
+            c += (long)z[0] - x[0];
+            z[0] = (uint)c;
+            c >>= 32;
+            c += (long)z[1] - x[1];
+            z[1] = (uint)c;
+            c >>= 32;
+            c += (long)z[2] - x[2];
+            z[2] = (uint)c;
+            c >>= 32;
+            c += (long)z[3] - x[3];
+            z[3] = (uint)c;
+            c >>= 32;
+            c += (long)z[4] - x[4];
+            z[4] = (uint)c;
+            c >>= 32;
+            c += (long)z[5] - x[5];
+            z[5] = (uint)c;
+            c >>= 32;
+            c += (long)z[6] - x[6];
+            z[6] = (uint)c;
+            c >>= 32;
+            c += (long)z[7] - x[7];
+            z[7] = (uint)c;
+            c >>= 32;
+            return (int)c;
+        }
+#endif
+
         public static BigInteger ToBigInteger(uint[] x)
         {
             byte[] bs = new byte[32];
diff --git a/crypto/src/math/raw/Nat448.cs b/crypto/src/math/raw/Nat448.cs
index 898e33199..5189d8936 100644
--- a/crypto/src/math/raw/Nat448.cs
+++ b/crypto/src/math/raw/Nat448.cs
@@ -98,13 +98,35 @@ namespace Org.BouncyCastle.Math.Raw
             Nat.AddWordAt(28, c21, zz, 21);
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static void Mul(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y, Span<uint> zz)
+        {
+            Nat224.Mul(x, y, zz);
+            Nat224.Mul(x[7..], y[7..], zz[14..]);
+
+            uint c21 = Nat224.AddToEachOther(zz[7..], zz[14..]);
+            uint c14 = c21 + Nat224.AddTo(zz, zz[7..], 0U);
+            c21 += Nat224.AddTo(zz[21..], zz[14..], c14);
+
+            Span<uint> dx = stackalloc uint[7];
+            Span<uint> dy = stackalloc uint[7];
+            bool neg = Nat224.Diff(x[7..], x, dx) != Nat224.Diff(y[7..], y, dy);
+
+            Span<uint> tt = stackalloc uint[14];
+            Nat224.Mul(dx, dy, tt);
+
+            c21 += neg ? Nat.AddTo(14, tt, zz[7..]) : (uint)Nat.SubFrom(14, tt, zz[7..]);
+            Nat.AddWordAt(28, c21, zz, 21);
+        }
+#endif
+
         public static void Square(uint[] x, uint[] zz)
         {
             Nat224.Square(x, zz);
             Nat224.Square(x, 7, zz, 14);
 
             uint c21 = Nat224.AddToEachOther(zz, 7, zz, 14);
-            uint c14 = c21 + Nat224.AddTo(zz, 0, zz, 7, 0);
+            uint c14 = c21 + Nat224.AddTo(zz, 0, zz, 7, 0U);
             c21 += Nat224.AddTo(zz, 21, zz, 14, c14);
 
             uint[] dx = Nat224.Create();
@@ -117,6 +139,27 @@ namespace Org.BouncyCastle.Math.Raw
             Nat.AddWordAt(28, c21, zz, 21);
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static void Square(ReadOnlySpan<uint> x, Span<uint> zz)
+        {
+            Nat224.Square(x, zz);
+            Nat224.Square(x[7..], zz[14..]);
+
+            uint c21 = Nat224.AddToEachOther(zz[7..], zz[14..]);
+            uint c14 = c21 + Nat224.AddTo(zz, zz[7..], 0U);
+            c21 += Nat224.AddTo(zz[21..], zz[14..], c14);
+
+            Span<uint> dx = stackalloc uint[7];
+            Nat224.Diff(x[7..], x, dx);
+
+            Span<uint> tt = stackalloc uint[14];
+            Nat224.Square(dx, tt);
+
+            c21 += (uint)Nat.SubFrom(14, tt, zz[7..]);
+            Nat.AddWordAt(28, c21, zz, 21);
+        }
+#endif
+
         public static BigInteger ToBigInteger64(ulong[] x)
         {
             byte[] bs = new byte[56];
diff --git a/crypto/src/math/raw/Nat512.cs b/crypto/src/math/raw/Nat512.cs
index 2312e1cf2..f8a34027f 100644
--- a/crypto/src/math/raw/Nat512.cs
+++ b/crypto/src/math/raw/Nat512.cs
@@ -16,7 +16,7 @@ namespace Org.BouncyCastle.Math.Raw
             Nat256.Mul(x, 8, y, 8, zz, 16);
 
             uint c24 = Nat256.AddToEachOther(zz, 8, zz, 16);
-            uint c16 = c24 + Nat256.AddTo(zz, 0, zz, 8, 0);
+            uint c16 = c24 + Nat256.AddTo(zz, 0, zz, 8, 0U);
             c24 += Nat256.AddTo(zz, 24, zz, 16, c16);
 
             uint[] dx = Nat256.Create(), dy = Nat256.Create();
@@ -35,7 +35,7 @@ namespace Org.BouncyCastle.Math.Raw
             Nat256.Square(x, 8, zz, 16);
 
             uint c24 = Nat256.AddToEachOther(zz, 8, zz, 16);
-            uint c16 = c24 + Nat256.AddTo(zz, 0, zz, 8, 0);
+            uint c16 = c24 + Nat256.AddTo(zz, 0, zz, 8, 0U);
             c24 += Nat256.AddTo(zz, 24, zz, 16, c16);
 
             uint[] dx = Nat256.Create();