summary refs log tree commit diff
path: root/crypto/src
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-02-01 19:10:28 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-02-01 19:10:28 +0700
commit8405b5610adc2405c7cb1d2b25150388728adba6 (patch)
treedc311fdc9782ed7bace841ad74141f17a1b57217 /crypto/src
parentAdd support for delayed modular reduction (diff)
downloadBouncyCastle.NET-ed25519-8405b5610adc2405c7cb1d2b25150388728adba6.tar.xz
Add custom curve for secp521r1 (P-521)
Diffstat (limited to 'crypto/src')
-rw-r--r--crypto/src/crypto/ec/CustomNamedCurves.cs23
-rw-r--r--crypto/src/math/ec/ECFieldElement.cs9
-rw-r--r--crypto/src/math/ec/Mod.cs9
-rw-r--r--crypto/src/math/ec/Nat.cs146
-rw-r--r--crypto/src/math/ec/custom/sec/SecP521R1Curve.cs95
-rw-r--r--crypto/src/math/ec/custom/sec/SecP521R1Field.cs131
-rw-r--r--crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs166
-rw-r--r--crypto/src/math/ec/custom/sec/SecP521R1Point.cs287
8 files changed, 757 insertions, 109 deletions
diff --git a/crypto/src/crypto/ec/CustomNamedCurves.cs b/crypto/src/crypto/ec/CustomNamedCurves.cs
index dcc4ea972..a7319ddbc 100644
--- a/crypto/src/crypto/ec/CustomNamedCurves.cs
+++ b/crypto/src/crypto/ec/CustomNamedCurves.cs
@@ -113,6 +113,27 @@ namespace Org.BouncyCastle.Crypto.EC
             }
         }
 
+        /*
+         * secp521r1
+         */
+        internal class Secp521r1Holder
+            : X9ECParametersHolder
+        {
+            private Secp521r1Holder() { }
+
+            internal static readonly X9ECParametersHolder Instance = new Secp521r1Holder();
+
+            protected override X9ECParameters CreateParameters()
+            {
+                byte[] S = Hex.Decode("D09E8800291CB85396CC6717393284AAA0DA64BA");
+                ECCurve curve = ConfigureCurve(new SecP521R1Curve());
+                ECPoint G = curve.DecodePoint(Hex.Decode("04"
+                    + "00C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
+                    + "011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650"));
+                return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
+            }
+        }
+
         private static readonly IDictionary objIds = Platform.CreateHashtable();
         private static readonly IDictionary curves = Platform.CreateHashtable();
         private static readonly IDictionary names = Platform.CreateHashtable();
@@ -130,9 +151,11 @@ namespace Org.BouncyCastle.Crypto.EC
             DefineCurve("secp192r1", SecObjectIdentifiers.SecP192r1, Secp192r1Holder.Instance);
             DefineCurve("secp256k1", SecObjectIdentifiers.SecP256k1, Secp256k1Holder.Instance);
             DefineCurve("secp256r1", SecObjectIdentifiers.SecP256r1, Secp256r1Holder.Instance);
+            DefineCurve("secp521r1", SecObjectIdentifiers.SecP521r1, Secp521r1Holder.Instance);
 
             objIds.Add(Platform.ToLowerInvariant("P-192"), SecObjectIdentifiers.SecP192r1);
             objIds.Add(Platform.ToLowerInvariant("P-256"), SecObjectIdentifiers.SecP256r1);
+            objIds.Add(Platform.ToLowerInvariant("P-521"), SecObjectIdentifiers.SecP521r1);
         }
 
         public static X9ECParameters GetByName(string name)
diff --git a/crypto/src/math/ec/ECFieldElement.cs b/crypto/src/math/ec/ECFieldElement.cs
index 13ae32e5d..7a4c9da97 100644
--- a/crypto/src/math/ec/ECFieldElement.cs
+++ b/crypto/src/math/ec/ECFieldElement.cs
@@ -390,11 +390,10 @@ namespace Org.BouncyCastle.Math.EC
 
         protected virtual BigInteger ModInverse(BigInteger x)
         {
-            // Our BigInteger.ModInverse performance is quite poor, so use the new Nat/Mod classes here
-            //return x.ModInverse(q);
-            int len = (FieldSize + 31) >> 5;
-            uint[] p = Nat.FromBigInteger(len, q);
-            uint[] n = Nat.FromBigInteger(len, x);
+            int bits = FieldSize;
+            int len = (bits + 31) >> 5;
+            uint[] p = Nat.FromBigInteger(bits, q);
+            uint[] n = Nat.FromBigInteger(bits, x);
             uint[] z = Nat.Create(len);
             Mod.Invert(p, n, z);
             return Nat.ToBigInteger(len, z);
diff --git a/crypto/src/math/ec/Mod.cs b/crypto/src/math/ec/Mod.cs
index bfb2faea4..a05ff77aa 100644
--- a/crypto/src/math/ec/Mod.cs
+++ b/crypto/src/math/ec/Mod.cs
@@ -101,7 +101,7 @@ namespace Org.BouncyCastle.Math.EC
             int count = 0;
             while (u[0] == 0)
             {
-                Nat.ShiftDownWord(u, uLen, 0);
+                Nat.ShiftDownWord(uLen, u, 0);
                 count += 32;
             }
 
@@ -109,7 +109,7 @@ namespace Org.BouncyCastle.Math.EC
                 int zeroes = GetTrailingZeroes(u[0]);
                 if (zeroes > 0)
                 {
-                    Nat.ShiftDownBits(u, uLen, zeroes, 0);
+                    Nat.ShiftDownBits(uLen, u, zeroes, 0);
                     count += zeroes;
                 }
             }
@@ -129,14 +129,13 @@ namespace Org.BouncyCastle.Math.EC
                 }
 
                 Debug.Assert(xc == 0 || xc == -1);
-                Nat.ShiftDownBit(x, len, (uint)xc);
+                Nat.ShiftDownBit(len, x, (uint)xc);
             }
         }
 
         private static int GetTrailingZeroes(uint x)
         {
-    //        assert x != 0;
-
+            Debug.Assert(x != 0);
             int count = 0;
             while ((x & 1) == 0)
             {
diff --git a/crypto/src/math/ec/Nat.cs b/crypto/src/math/ec/Nat.cs
index 593cb4ecf..b0213fb97 100644
--- a/crypto/src/math/ec/Nat.cs
+++ b/crypto/src/math/ec/Nat.cs
@@ -46,19 +46,6 @@ namespace Org.BouncyCastle.Math.EC
             return c == 0 ? 0 : Inc(len, z, zOff + 2);
         }
 
-        public static uint AddExt(int len, uint[] xx, uint[] yy, uint[] zz)
-        {
-            int extLen = len << 1;
-            ulong c = 0;
-            for (int i = 0; i < extLen; ++i)
-            {
-                c += (ulong)xx[i] + yy[i];
-                zz[i] = (uint)c;
-                c >>= 32;
-            }
-            return (uint)c;
-        }
-
         public static uint AddToExt(int len, uint[] x, int xOff, uint[] zz, int zzOff)
         {
             Debug.Assert(zzOff <= len);
@@ -79,7 +66,7 @@ namespace Org.BouncyCastle.Math.EC
             ulong c = (ulong)x + zz[zzOff];
             zz[zzOff] = (uint)c;
             c >>= 32;
-            return c == 0 ? 0 : IncExt(len, zz, zzOff + 1);
+            return c == 0 ? 0 : Inc(extLen, zz, zzOff + 1);
         }
 
         public static uint[] Copy(int len, uint[] x)
@@ -94,12 +81,6 @@ namespace Org.BouncyCastle.Math.EC
             return new uint[len];
         }
 
-        public static uint[] CreateExt(int len)
-        {
-            int extLen = len << 1;
-            return new uint[extLen];
-        }
-
         public static int Dec(int len, uint[] z, int zOff)
         {
             Debug.Assert(zOff <= len);
@@ -113,11 +94,24 @@ namespace Org.BouncyCastle.Math.EC
             return -1;
         }
 
-        public static uint[] FromBigInteger(int len, BigInteger x)
+        public static bool Eq(int len, uint[] x, uint[] y)
+        {
+            for (int i = len - 1; i >= 0; --i)
+            {
+                if (x[i] != y[i])
+                {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        public static uint[] FromBigInteger(int bits, BigInteger x)
         {
-            if (x.SignValue < 0 || x.BitLength > (len << 5))
+            if (x.SignValue < 0 || x.BitLength > bits)
                 throw new ArgumentException();
 
+            int len = (bits + 31) >> 5;
             uint[] z = Create(len);
             int i = 0;
             while (x.SignValue != 0)
@@ -156,20 +150,6 @@ namespace Org.BouncyCastle.Math.EC
             return true;
         }
 
-        public static bool GteExt(int len, uint[] xx, uint[] yy)
-        {
-            int extLen = len << 1;
-            for (int i = extLen - 1; i >= 0; --i)
-            {
-                uint xx_i = xx[i], yy_i = yy[i];
-                if (xx_i < yy_i)
-                    return false;
-                if (xx_i > yy_i)
-                    return true;
-            }
-            return true;
-        }
-
         public static uint Inc(int len, uint[] z, int zOff)
         {
             Debug.Assert(zOff <= len);
@@ -183,20 +163,6 @@ namespace Org.BouncyCastle.Math.EC
             return 1;
         }
 
-        public static uint IncExt(int len, uint[] zz, int zzOff)
-        {
-            int extLen = len << 1;
-            Debug.Assert(zzOff <= extLen);
-            for (int i = zzOff; i < extLen; ++i)
-            {
-                if (++zz[i] != uint.MinValue)
-                {
-                    return 0;
-                }
-            }
-            return 1;
-        }
-
         public static bool IsOne(int len, uint[] x)
         {
             if (x[0] != 1)
@@ -229,42 +195,24 @@ namespace Org.BouncyCastle.Math.EC
             return true;
         }
 
-        public static bool IsZeroExt(int len, uint[] xx)
-        {
-            if (xx[0] != 0)
-            {
-                return false;
-            }
-            int extLen = len << 1;
-            for (int i = 1; i < extLen; ++i)
-            {
-                if (xx[i] != 0)
-                {
-                    return false;
-                }
-            }
-            return true;
-        }
-
         public static void Mul(int len, uint[] x, uint[] y, uint[] zz)
         {
             zz[len] = (uint)MulWordExt(len, x[0], y, zz, 0);
 
             for (int i = 1; i < len; ++i)
             {
-                zz[i + len] = (uint)MulWordAddExt(len, x[i], y, 0, zz, i);
+                zz[i + len] = (uint)MulWordAddExt(len, x[i], y, zz, i);
             }
         }
 
-        public static uint MulWordAddExt(int len, uint x, uint[] yy, int yyOff, uint[] zz, int zzOff)
+        public static uint MulWordAddExt(int len, uint x, uint[] y, uint[] zz, int zzOff)
         {
-            Debug.Assert(yyOff <= len);
             Debug.Assert(zzOff <= len);
             ulong c = 0, xVal = (ulong)x;
             int i = 0;
             do
             {
-                c += xVal * yy[yyOff + i] + zz[zzOff + i];
+                c += xVal * y[i] + zz[zzOff + i];
                 zz[zzOff + i] = (uint)c;
                 c >>= 32;
             }
@@ -303,13 +251,13 @@ namespace Org.BouncyCastle.Math.EC
             return (uint)c;
         }
 
-        public static uint ShiftDownBit(uint[] x, int xLen, uint c)
+        public static uint ShiftDownBit(int len, uint[] z, uint c)
         {
-            int i = xLen;
+            int i = len;
             while (--i >= 0)
             {
-                uint next = x[i];
-                x[i] = (next >> 1) | (c << 31);
+                uint next = z[i];
+                z[i] = (next >> 1) | (c << 31);
                 c = next;
             }
             return c << 31;
@@ -327,10 +275,10 @@ namespace Org.BouncyCastle.Math.EC
             return c << 31;
         }
 
-        public static uint ShiftDownBits(uint[] x, int xLen, int bits, uint c)
+        public static uint ShiftDownBits(int len, uint[] x, int bits, uint c)
         {
             Debug.Assert(bits > 0 && bits < 32);
-            int i = xLen;
+            int i = len;
             while (--i >= 0)
             {
                 uint next = x[i];
@@ -340,24 +288,37 @@ namespace Org.BouncyCastle.Math.EC
             return c << -bits;
         }
 
-        public static uint ShiftDownWord(uint[] x, int xLen, uint c)
+        public static uint ShiftDownBitsExt(int len, uint[] xx, int xxOff, int bits, uint c, uint[] z)
         {
-            int i = xLen;
+            Debug.Assert(bits > 0 && bits < 32);
+            int i = len;
             while (--i >= 0)
             {
-                uint next = x[i];
-                x[i] = c;
+                uint next = xx[xxOff + i];
+                z[i] = (next >> bits) | (c << -bits);
+                c = next;
+            }
+            return c << -bits;
+        }
+
+        public static uint ShiftDownWord(int len, uint[] z, uint c)
+        {
+            int i = len;
+            while (--i >= 0)
+            {
+                uint next = z[i];
+                z[i] = c;
                 c = next;
             }
             return c;
         }
 
-        public static uint ShiftUpBit(uint[] x, int xLen, uint c)
+        public static uint ShiftUpBit(int len, uint[] z, uint c)
         {
-            for (int i = 0; i < xLen; ++i)
+            for (int i = 0; i < len; ++i)
             {
-                uint next = x[i];
-                x[i] = (next << 1) | (c >> 31);
+                uint next = z[i];
+                z[i] = (next << 1) | (c >> 31);
                 c = next;
             }
             return c >> 31;
@@ -395,7 +356,7 @@ namespace Org.BouncyCastle.Math.EC
                 AddWordExt(len, c, zz, i << 1);
             }
 
-            ShiftUpBit(zz, extLen, x[0] << 31);
+            ShiftUpBit(extLen, zz, x[0] << 31);
         }
 
         public static uint SquareWordAddExt(int len, uint[] x, int xPos, uint[] zz)
@@ -451,19 +412,6 @@ namespace Org.BouncyCastle.Math.EC
             return c == 0 ? 0 : Dec(len, z, 2);
         }
 
-        public static int SubExt(int len, uint[] xx, uint[] yy, uint[] zz)
-        {
-            int extLen = len << 1;
-            long c = 0;
-            for (int i = 0; i < extLen; ++i)
-            {
-                c += (long)xx[i] - yy[i];
-                zz[i] = (uint)c;
-                c >>= 32;
-            }
-            return (int)c;
-        }
-
         public static int SubFromExt(int len, uint[] x, int xOff, uint[] zz, int zzOff)
         {
             Debug.Assert(zzOff <= len);
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1Curve.cs b/crypto/src/math/ec/custom/sec/SecP521R1Curve.cs
new file mode 100644
index 000000000..24a0a5e33
--- /dev/null
+++ b/crypto/src/math/ec/custom/sec/SecP521R1Curve.cs
@@ -0,0 +1,95 @@
+using System;
+
+using Org.BouncyCastle.Math.Field;
+using Org.BouncyCastle.Utilities.Encoders;
+
+namespace Org.BouncyCastle.Math.EC.Custom.Sec
+{
+    internal class SecP521R1Curve
+        : ECCurve
+    {
+        public static readonly BigInteger q = new BigInteger(1,
+            Hex.Decode("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"));
+
+        private const int SecP521R1_DEFAULT_COORDS = COORD_JACOBIAN;
+
+        protected readonly SecP521R1Point m_infinity;
+
+        public SecP521R1Curve()
+            : base(FiniteFields.GetPrimeField(q))
+        {
+            this.m_infinity = new SecP521R1Point(this, null, null);
+
+            this.m_a = FromBigInteger(new BigInteger(1,
+                Hex.Decode("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC")));
+            this.m_b = FromBigInteger(new BigInteger(1,
+                Hex.Decode("0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00")));
+            this.m_order = new BigInteger(1, Hex.Decode("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409"));
+            this.m_cofactor = BigInteger.One;
+            this.m_coord = SecP521R1_DEFAULT_COORDS;
+        }
+
+        protected override ECCurve CloneCurve()
+        {
+            return new SecP521R1Curve();
+        }
+
+        public override bool SupportsCoordinateSystem(int coord)
+        {
+            switch (coord)
+            {
+                case COORD_JACOBIAN:
+                    return true;
+                default:
+                    return false;
+            }
+        }
+
+        public virtual BigInteger Q
+        {
+            get { return q; }
+        }
+
+        public override ECPoint Infinity
+        {
+            get { return m_infinity; }
+        }
+
+        public override int FieldSize
+        {
+            get { return q.BitLength; }
+        }
+
+        public override ECFieldElement FromBigInteger(BigInteger x)
+        {
+            return new SecP521R1FieldElement(x);
+        }
+
+        protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
+        {
+            return new SecP521R1Point(this, x, y, withCompression);
+        }
+
+        protected override ECPoint DecompressPoint(int yTilde, BigInteger X1)
+        {
+            ECFieldElement x = FromBigInteger(X1);
+            ECFieldElement alpha = x.Square().Add(A).Multiply(x).Add(B);
+            ECFieldElement beta = alpha.Sqrt();
+
+            //
+            // if we can't find a sqrt we haven't got a point on the
+            // curve - run!
+            //
+            if (beta == null)
+                throw new ArithmeticException("Invalid point compression");
+
+            if (beta.TestBitZero() != (yTilde == 1))
+            {
+                // Use the other root
+                beta = beta.Negate();
+            }
+
+            return new SecP521R1Point(this, x, beta, true);
+        }
+    }
+}
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1Field.cs b/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
new file mode 100644
index 000000000..43f012b8b
--- /dev/null
+++ b/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
@@ -0,0 +1,131 @@
+using System;
+using System.Diagnostics;
+
+namespace Org.BouncyCastle.Math.EC.Custom.Sec
+{
+    internal class SecP521R1Field
+    {
+        // 2^521 - 1
+        internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+            0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x1FF };
+        private const int P16 = 0x1FF;
+
+        public static void Add(uint[] x, uint[] y, uint[] z)
+        {
+            uint c = Nat.Add(16, x, y, z) + x[16] + y[16];
+            if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
+            {
+                c += Nat.Inc(16, z, 0);
+                c &= P16;
+            }
+            z[16] = c;
+        }
+
+        public static void AddOne(uint[] x, uint[] z)
+        {
+            Array.Copy(x, 0, z, 0, 16);
+            uint c = Nat.Inc(16, z, 0) + z[16];
+            if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
+            {
+                c += Nat.Inc(16, z, 0);
+                c &= P16;
+            }
+            z[16] = c;
+        }
+
+        public static uint[] FromBigInteger(BigInteger x)
+        {
+            uint[] z = Nat.FromBigInteger(521, x);
+            if (Nat.Eq(17, z, P))
+            {
+                Nat.Zero(17, z);
+            }
+            return z;
+        }
+
+        public static void Half(uint[] x, uint[] z)
+        {
+            uint c0 = x[0] & 1, x16 = x[16], c512 = x16 & 1;
+            Nat.ShiftDownBit(16, x, c512, z);
+            z[16] = (x16 >> 1) | (c0 << 8);
+        }
+
+        public static void Multiply(uint[] x, uint[] y, uint[] z)
+        {
+            uint[] tt = Nat.Create(34);
+            Nat.Mul(17, x, y, tt);
+            Reduce(tt, z);
+        }
+
+        public static void Negate(uint[] x, uint[] z)
+        {
+            if (Nat.IsZero(17, x))
+            {
+                Nat.Zero(17, z);
+            }
+            else
+            {
+                Nat.Sub(17, P, x, z);
+            }
+        }
+
+        public static void Reduce(uint[] xx, uint[] z)
+        {
+            Debug.Assert(xx[33] == 0);
+            Debug.Assert(xx[32] >> 18 == 0);
+            uint xx32 = xx[32];
+            uint c = Nat.ShiftDownBitsExt(16, xx, 16, 9, xx32, z) >> 23;
+            c += xx32 >> 9;
+            c += Nat.Add(16, z, xx, z);
+            if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
+            {
+                c += Nat.Inc(16, z, 0);
+                c &= P16;
+            }
+            z[16] = c;
+        }
+
+        public static void Square(uint[] x, uint[] z)
+        {
+            uint[] tt = Nat.Create(34);
+            Nat.Square(17, x, tt);
+            Reduce(tt, z);
+        }
+
+        public static void SquareN(uint[] x, int n, uint[] z)
+        {
+            Debug.Assert(n > 0);
+            uint[] tt = Nat.Create(34);
+            Nat.Square(17, x, tt);
+            Reduce(tt, z);
+
+            while (--n > 0)
+            {
+                Nat.Square(17, z, tt);
+                Reduce(tt, z);
+            }
+        }
+
+        public static void Subtract(uint[] x, uint[] y, uint[] z)
+        {
+            int c = Nat.Sub(16, x, y, z) + (int)(x[16] - y[16]);
+            if (c < 0)
+            {
+                c += Nat.Dec(16, z, 0);
+                c &= P16;
+            }
+            z[16] = (uint)c;
+        }
+
+        public static void Twice(uint[] x, uint[] z)
+        {
+            uint c = Nat.ShiftUpBit(16, x, 0, z) | (x[16] << 1);
+            if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
+            {
+                c += Nat.Inc(16, z, 0);
+                c &= P16;
+            }
+            z[16] = c;
+        }
+    }
+}
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
new file mode 100644
index 000000000..e47a199f3
--- /dev/null
+++ b/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
@@ -0,0 +1,166 @@
+using System;
+
+using Org.BouncyCastle.Utilities;
+
+namespace Org.BouncyCastle.Math.EC.Custom.Sec
+{
+    internal class SecP521R1FieldElement
+        : ECFieldElement
+    {
+        public static readonly BigInteger Q = SecP521R1Curve.q;
+
+        protected internal readonly uint[] x;
+
+        public SecP521R1FieldElement(BigInteger x)
+        {
+            if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
+                throw new ArgumentException("value invalid for SecP521R1FieldElement", "x");
+
+            this.x = SecP521R1Field.FromBigInteger(x);
+        }
+
+        public SecP521R1FieldElement()
+        {
+            this.x = Nat.Create(17);
+        }
+
+        protected internal SecP521R1FieldElement(uint[] x)
+        {
+            this.x = x;
+        }
+
+        public override bool IsZero
+        {
+            get { return Nat.IsZero(17, x); }
+        }
+
+        public override bool IsOne
+        {
+            get { return Nat.IsOne(17, x); }
+        }
+
+        public override bool TestBitZero()
+        {
+            return Nat.GetBit(x, 0) == 1;
+        }
+
+        public override BigInteger ToBigInteger()
+        {
+            return Nat.ToBigInteger(17, x);
+        }
+
+        public override string FieldName
+        {
+            get { return "SecP521R1Field"; }
+        }
+
+        public override int FieldSize
+        {
+            get { return Q.BitLength; }
+        }
+
+        public override ECFieldElement Add(ECFieldElement b)
+        {
+            uint[] z = Nat.Create(17);
+            SecP521R1Field.Add(x, ((SecP521R1FieldElement)b).x, z);
+            return new SecP521R1FieldElement(z);
+        }
+
+        public override ECFieldElement AddOne()
+        {
+            uint[] z = Nat.Create(17);
+            SecP521R1Field.AddOne(x, z);
+            return new SecP521R1FieldElement(z);
+        }
+
+        public override ECFieldElement Subtract(ECFieldElement b)
+        {
+            uint[] z = Nat.Create(17);
+            SecP521R1Field.Subtract(x, ((SecP521R1FieldElement)b).x, z);
+            return new SecP521R1FieldElement(z);
+        }
+
+        public override ECFieldElement Multiply(ECFieldElement b)
+        {
+            uint[] z = Nat.Create(17);
+            SecP521R1Field.Multiply(x, ((SecP521R1FieldElement)b).x, z);
+            return new SecP521R1FieldElement(z);
+        }
+
+        public override ECFieldElement Divide(ECFieldElement b)
+        {
+            //return Multiply(b.Invert());
+            uint[] z = Nat.Create(17);
+            Mod.Invert(SecP521R1Field.P, ((SecP521R1FieldElement)b).x, z);
+            SecP521R1Field.Multiply(z, x, z);
+            return new SecP521R1FieldElement(z);
+        }
+
+        public override ECFieldElement Negate()
+        {
+            uint[] z = Nat.Create(17);
+            SecP521R1Field.Negate(x, z);
+            return new SecP521R1FieldElement(z);
+        }
+
+        public override ECFieldElement Square()
+        {
+            uint[] z = Nat.Create(17);
+            SecP521R1Field.Square(x, z);
+            return new SecP521R1FieldElement(z);
+        }
+
+        public override ECFieldElement Invert()
+        {
+            //return new SecP521R1FieldElement(ToBigInteger().ModInverse(Q));
+            uint[] z = Nat.Create(17);
+            Mod.Invert(SecP521R1Field.P, x, z);
+            return new SecP521R1FieldElement(z);
+        }
+
+        /**
+         * return a sqrt root - the routine verifies that the calculation returns the right value - if
+         * none exists it returns null.
+         */
+        public override ECFieldElement Sqrt()
+        {
+            // Raise this element to the exponent 2^519
+
+            uint[] x1 = this.x;
+            if (Nat.IsZero(17, x1) || Nat.IsOne(17, x1))
+                return this;
+
+            uint[] t1 = Nat.Create(17);
+            uint[] t2 = Nat.Create(17);
+
+            SecP521R1Field.SquareN(x1, 519, t1);
+            SecP521R1Field.Square(t1, t2);
+
+            return Arrays.AreEqual(x1, t2) ? new SecP521R1FieldElement(t1) : null;
+        }
+
+        public override bool Equals(object obj)
+        {
+            return Equals(obj as SecP521R1FieldElement);
+        }
+
+        public override bool Equals(ECFieldElement other)
+        {
+            return Equals(other as SecP521R1FieldElement);
+        }
+
+        public virtual bool Equals(SecP521R1FieldElement other)
+        {
+            if (this == other)
+                return true;
+            if (null == other)
+                return false;
+            return Arrays.AreEqual(x, other.x);
+        }
+
+        public override int GetHashCode()
+        {
+            return Q.GetHashCode() ^ Arrays.GetHashCode(x);
+        }
+    }
+}
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1Point.cs b/crypto/src/math/ec/custom/sec/SecP521R1Point.cs
new file mode 100644
index 000000000..5da71f078
--- /dev/null
+++ b/crypto/src/math/ec/custom/sec/SecP521R1Point.cs
@@ -0,0 +1,287 @@
+using System;
+
+namespace Org.BouncyCastle.Math.EC.Custom.Sec
+{
+    internal class SecP521R1Point
+        : ECPointBase
+    {
+        /**
+         * Create a point which encodes with point compression.
+         * 
+         * @param curve
+         *            the curve to use
+         * @param x
+         *            affine x co-ordinate
+         * @param y
+         *            affine y co-ordinate
+         * 
+         * @deprecated Use ECCurve.createPoint to construct points
+         */
+        public SecP521R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
+            : this(curve, x, y, false)
+        {
+        }
+
+        /**
+         * Create a point that encodes with or without point compresion.
+         * 
+         * @param curve
+         *            the curve to use
+         * @param x
+         *            affine x co-ordinate
+         * @param y
+         *            affine y co-ordinate
+         * @param withCompression
+         *            if true encode with point compression
+         * 
+         * @deprecated per-point compression property will be removed, refer
+         *             {@link #getEncoded(bool)}
+         */
+        public SecP521R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression)
+            : base(curve, x, y, withCompression)
+        {
+            if ((x == null) != (y == null))
+                throw new ArgumentException("Exactly one of the field elements is null");
+        }
+
+        internal SecP521R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
+            : base(curve, x, y, zs, withCompression)
+        {
+        }
+
+        protected override ECPoint Detach()
+        {
+            return new SecP521R1Point(null, AffineXCoord, AffineYCoord);
+        }
+
+        protected internal override bool CompressionYTilde
+        {
+            get { return this.AffineYCoord.TestBitZero(); }
+        }
+
+        public override ECPoint Add(ECPoint b)
+        {
+            if (this.IsInfinity)
+                return b;
+            if (b.IsInfinity)
+                return this;
+            if (this == b)
+                return Twice();
+
+            ECCurve curve = this.Curve;
+
+            SecP521R1FieldElement X1 = (SecP521R1FieldElement)this.RawXCoord, Y1 = (SecP521R1FieldElement)this.RawYCoord;
+            SecP521R1FieldElement X2 = (SecP521R1FieldElement)b.RawXCoord, Y2 = (SecP521R1FieldElement)b.RawYCoord;
+
+            SecP521R1FieldElement Z1 = (SecP521R1FieldElement)this.RawZCoords[0];
+            SecP521R1FieldElement Z2 = (SecP521R1FieldElement)b.RawZCoords[0];
+
+            uint[] t1 = Nat.Create(17);
+            uint[] t2 = Nat.Create(17);
+            uint[] t3 = Nat.Create(17);
+            uint[] t4 = Nat.Create(17);
+
+            bool Z1IsOne = Z1.IsOne;
+            uint[] U2, S2;
+            if (Z1IsOne)
+            {
+                U2 = X2.x;
+                S2 = Y2.x;
+            }
+            else
+            {
+                S2 = t3;
+                SecP521R1Field.Square(Z1.x, S2);
+
+                U2 = t2;
+                SecP521R1Field.Multiply(S2, X2.x, U2);
+
+                SecP521R1Field.Multiply(S2, Z1.x, S2);
+                SecP521R1Field.Multiply(S2, Y2.x, S2);
+            }
+
+            bool Z2IsOne = Z2.IsOne;
+            uint[] U1, S1;
+            if (Z2IsOne)
+            {
+                U1 = X1.x;
+                S1 = Y1.x;
+            }
+            else
+            {
+                S1 = t4;
+                SecP521R1Field.Square(Z2.x, S1);
+
+                U1 = t1;
+                SecP521R1Field.Multiply(S1, X1.x, U1);
+
+                SecP521R1Field.Multiply(S1, Z2.x, S1);
+                SecP521R1Field.Multiply(S1, Y1.x, S1);
+            }
+
+            uint[] H = Nat.Create(17);
+            SecP521R1Field.Subtract(U1, U2, H);
+
+            uint[] R = t2;
+            SecP521R1Field.Subtract(S1, S2, R);
+
+            // Check if b == this or b == -this
+            if (Nat.IsZero(17, H))
+            {
+                if (Nat.IsZero(17, R))
+                {
+                    // this == b, i.e. this must be doubled
+                    return this.Twice();
+                }
+
+                // this == -b, i.e. the result is the point at infinity
+                return curve.Infinity;
+            }
+
+            uint[] HSquared = t3;
+            SecP521R1Field.Square(H, HSquared);
+
+            uint[] G = Nat.Create(17);
+            SecP521R1Field.Multiply(HSquared, H, G);
+
+            uint[] V = t3;
+            SecP521R1Field.Multiply(HSquared, U1, V);
+
+            SecP521R1Field.Multiply(S1, G, t1);
+
+            SecP521R1FieldElement X3 = new SecP521R1FieldElement(t4);
+            SecP521R1Field.Square(R, X3.x);
+            SecP521R1Field.Add(X3.x, G, X3.x);
+            SecP521R1Field.Subtract(X3.x, V, X3.x);
+            SecP521R1Field.Subtract(X3.x, V, X3.x);
+
+            SecP521R1FieldElement Y3 = new SecP521R1FieldElement(G);
+            SecP521R1Field.Subtract(V, X3.x, Y3.x);
+            SecP521R1Field.Multiply(Y3.x, R, t2);
+            SecP521R1Field.Subtract(t2, t1, Y3.x);
+
+            SecP521R1FieldElement Z3 = new SecP521R1FieldElement(H);
+            if (!Z1IsOne)
+            {
+                SecP521R1Field.Multiply(Z3.x, Z1.x, Z3.x);
+            }
+            if (!Z2IsOne)
+            {
+                SecP521R1Field.Multiply(Z3.x, Z2.x, Z3.x);
+            }
+
+            ECFieldElement[] zs = new ECFieldElement[] { Z3 };
+
+            return new SecP521R1Point(curve, X3, Y3, zs, IsCompressed);
+        }
+
+        public override ECPoint Twice()
+        {
+            if (this.IsInfinity)
+                return this;
+
+            ECCurve curve = this.Curve;
+
+            SecP521R1FieldElement Y1 = (SecP521R1FieldElement)this.RawYCoord;
+            if (Y1.IsZero)
+                return curve.Infinity;
+
+            SecP521R1FieldElement X1 = (SecP521R1FieldElement)this.RawXCoord, Z1 = (SecP521R1FieldElement)this.RawZCoords[0];
+
+            uint[] t1 = Nat.Create(17);
+            uint[] t2 = Nat.Create(17);
+
+            uint[] Y1Squared = Nat.Create(17);
+            SecP521R1Field.Square(Y1.x, Y1Squared);
+
+            uint[] T = Nat.Create(17);
+            SecP521R1Field.Square(Y1Squared, T);
+
+            bool Z1IsOne = Z1.IsOne;
+
+            uint[] Z1Squared = Z1.x;
+            if (!Z1IsOne)
+            {
+                Z1Squared = t2;
+                SecP521R1Field.Square(Z1.x, Z1Squared);
+            }
+
+            SecP521R1Field.Subtract(X1.x, Z1Squared, t1);
+
+            uint[] M = t2;
+            SecP521R1Field.Add(X1.x, Z1Squared, M);
+            SecP521R1Field.Multiply(M, t1, M);
+            SecP521R1Field.Twice(M, t1);
+            SecP521R1Field.Add(M, t1, M);
+
+            uint[] S = Y1Squared;
+            SecP521R1Field.Multiply(Y1Squared, X1.x, S);
+            SecP521R1Field.Twice(S, S);
+            SecP521R1Field.Twice(S, S);
+
+            SecP521R1Field.Twice(T, t1);
+            SecP521R1Field.Twice(t1, t1);
+            SecP521R1Field.Twice(t1, t1);
+
+            SecP521R1FieldElement X3 = new SecP521R1FieldElement(T);
+            SecP521R1Field.Square(M, X3.x);
+            SecP521R1Field.Subtract(X3.x, S, X3.x);
+            SecP521R1Field.Subtract(X3.x, S, X3.x);
+
+            SecP521R1FieldElement Y3 = new SecP521R1FieldElement(S);
+            SecP521R1Field.Subtract(S, X3.x, Y3.x);
+            SecP521R1Field.Multiply(Y3.x, M, Y3.x);
+            SecP521R1Field.Subtract(Y3.x, t1, Y3.x);
+
+            SecP521R1FieldElement Z3 = new SecP521R1FieldElement(M);
+            SecP521R1Field.Twice(Y1.x, Z3.x);
+            if (!Z1IsOne)
+            {
+                SecP521R1Field.Multiply(Z3.x, Z1.x, Z3.x);
+            }
+
+            return new SecP521R1Point(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
+        }
+
+        public override ECPoint TwicePlus(ECPoint b)
+        {
+            if (this == b)
+                return ThreeTimes();
+            if (this.IsInfinity)
+                return b;
+            if (b.IsInfinity)
+                return Twice();
+
+            ECFieldElement Y1 = this.RawYCoord;
+            if (Y1.IsZero)
+                return b;
+
+            return Twice().Add(b);
+        }
+
+        public override ECPoint ThreeTimes()
+        {
+            if (this.IsInfinity || this.RawYCoord.IsZero)
+                return this;
+
+            // NOTE: Be careful about recursions between TwicePlus and ThreeTimes
+            return Twice().Add(this);
+        }
+
+        public override ECPoint Subtract(ECPoint b)
+        {
+            if (b.IsInfinity)
+                return this;
+
+            return Add(b.Negate());
+        }
+
+        public override ECPoint Negate()
+        {
+            if (IsInfinity)
+                return this;
+
+            return new SecP521R1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords, IsCompressed);
+        }
+    }
+}