summary refs log tree commit diff
path: root/crypto/src/math/ec/custom
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2020-07-04 16:42:57 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-07-04 16:42:57 +0700
commitb27bcfb49d403d9c0d4f4924de5f869db1c041cc (patch)
tree72c3a1bae971caa7575c1bb54fb9baa30d51ca36 /crypto/src/math/ec/custom
parentBlind the inversion when normalizing (diff)
downloadBouncyCastle.NET-ed25519-b27bcfb49d403d9c0d4f4924de5f869db1c041cc.tar.xz
EC updates from bc-java
Diffstat (limited to 'crypto/src/math/ec/custom')
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1Curve.cs15
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1Field.cs104
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs5
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256R1Curve.cs15
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256R1Field.cs103
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecP384R1Curve.cs15
-rw-r--r--crypto/src/math/ec/custom/sec/SecP384R1Field.cs105
-rw-r--r--crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs4
-rw-r--r--crypto/src/math/ec/custom/sec/SecP521R1Curve.cs15
-rw-r--r--crypto/src/math/ec/custom/sec/SecP521R1Field.cs100
-rw-r--r--crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs4
12 files changed, 463 insertions, 26 deletions
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs b/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
index f51477c63..123305cd4 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
@@ -1,6 +1,7 @@
 using System;
 
 using Org.BouncyCastle.Math.Raw;
+using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities.Encoders;
 
 namespace Org.BouncyCastle.Math.EC.Custom.Sec
@@ -90,6 +91,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return new SecP256K1LookupTable(this, table, len);
         }
 
+        public override ECFieldElement RandomFieldElement(SecureRandom r)
+        {
+            uint[] x = Nat256.Create();
+            SecP256K1Field.Random(r, x);
+            return new SecP256K1FieldElement(x);
+        }
+
+        public override ECFieldElement RandomFieldElementMult(SecureRandom r)
+        {
+            uint[] x = Nat256.Create();
+            SecP256K1Field.RandomMult(r, x);
+            return new SecP256K1FieldElement(x);
+        }
+
         private class SecP256K1LookupTable
             : AbstractECLookupTable
         {
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
index b0646e93f..1f11c9c78 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Field.cs
@@ -1,16 +1,18 @@
 using System;
 using System.Diagnostics;
 
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Math.Raw;
+using Org.BouncyCastle.Security;
 
 namespace Org.BouncyCastle.Math.EC.Custom.Sec
 {
     internal class SecP256K1Field
     {
         // 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1
-        internal static readonly uint[] P = new uint[]{ 0xFFFFFC2F, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        private static readonly uint[] P = new uint[]{ 0xFFFFFC2F, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
             0xFFFFFFFF, 0xFFFFFFFF };
-        internal static readonly uint[] PExt = new uint[]{ 0x000E90A1, 0x000007A2, 0x00000001, 0x00000000, 0x00000000,
+        private static readonly uint[] PExt = new uint[]{ 0x000E90A1, 0x000007A2, 0x00000001, 0x00000000, 0x00000000,
             0x00000000, 0x00000000, 0x00000000, 0xFFFFF85E, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
             0xFFFFFFFF, 0xFFFFFFFF };
         private static readonly uint[] PExtInv = new uint[]{ 0xFFF16F5F, 0xFFFFF85D, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF,
@@ -72,6 +74,80 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             }
         }
 
+        public static void Inv(uint[] x, uint[] z)
+        {
+            /*
+             * Raise this element to the exponent 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 3
+             *
+             * Breaking up the exponent's binary representation into "repunits", we get:
+             * { 223 1s } { 1 0s } { 22 1s } { 4 0s } { 1 1s } { 1 0s } { 2 1s } { 1 0s } { 1 1s }
+             *
+             * Therefore we need an addition chain containing 1, 2, 22, 223 (the lengths of the repunits)
+             * We use: [1], [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
+             */
+
+            if (0 != IsZero(x))
+                throw new ArgumentException("cannot be 0", "x");
+
+            uint[] x1 = x;
+            uint[] x2 = Nat256.Create();
+            Square(x1, x2);
+            Multiply(x2, x1, x2);
+            uint[] x3 = Nat256.Create();
+            Square(x2, x3);
+            Multiply(x3, x1, x3);
+            uint[] x6 = Nat256.Create();
+            SquareN(x3, 3, x6);
+            Multiply(x6, x3, x6);
+            uint[] x9 = x6;
+            SquareN(x6, 3, x9);
+            Multiply(x9, x3, x9);
+            uint[] x11 = x9;
+            SquareN(x9, 2, x11);
+            Multiply(x11, x2, x11);
+            uint[] x22 = Nat256.Create();
+            SquareN(x11, 11, x22);
+            Multiply(x22, x11, x22);
+            uint[] x44 = x11;
+            SquareN(x22, 22, x44);
+            Multiply(x44, x22, x44);
+            uint[] x88 = Nat256.Create();
+            SquareN(x44, 44, x88);
+            Multiply(x88, x44, x88);
+            uint[] x176 = Nat256.Create();
+            SquareN(x88, 88, x176);
+            Multiply(x176, x88, x176);
+            uint[] x220 = x88;
+            SquareN(x176, 44, x220);
+            Multiply(x220, x44, x220);
+            uint[] x223 = x44;
+            SquareN(x220, 3, x223);
+            Multiply(x223, x3, x223);
+
+            uint[] t = x223;
+            SquareN(t, 23, t);
+            Multiply(t, x22, t);
+            SquareN(t, 5, t);
+            Multiply(t, x1, t);
+            SquareN(t, 3, t);
+            Multiply(t, x2, t);
+            SquareN(t, 2, t);
+
+            // NOTE that x1 and z could be the same array
+            Multiply(x1, t, z);
+        }
+
+        public static int IsZero(uint[] x)
+        {
+            uint d = 0;
+            for (int i = 0; i < 8; ++i)
+            {
+                d |= x[i];
+            }
+            d = (d >> 1) | (d & 1);
+            return ((int)d - 1) >> 31;
+        }
+
         public static void Multiply(uint[] x, uint[] y, uint[] z)
         {
             uint[] tt = Nat256.CreateExt();
@@ -93,9 +169,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Negate(uint[] x, uint[] z)
         {
-            if (Nat256.IsZero(x))
+            if (0 != IsZero(x))
             {
-                Nat256.Zero(z);
+                Nat256.Sub(P, P, z);
             }
             else
             {
@@ -103,6 +179,26 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             }
         }
 
+        public static void Random(SecureRandom r, uint[] z)
+        {
+            byte[] bb = new byte[8 * 4];
+            do
+            {
+                r.NextBytes(bb);
+                Pack.LE_To_UInt32(bb, 0, z, 0, 8);
+            }
+            while (0 == Nat.LessThan(8, z, P));
+        }
+
+        public static void RandomMult(SecureRandom r, uint[] z)
+        {
+            do
+            {
+                Random(r, z);
+            }
+            while (0 != IsZero(z));
+        }
+
         public static void Reduce(uint[] xx, uint[] z)
         {
             ulong cc = Nat256.Mul33Add(PInv33, xx, 8, xx, 0, z, 0);
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs
index 5d1fea8b5..d2ff05cb7 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1FieldElement.cs
@@ -1,5 +1,4 @@
 using System;
-using System.Diagnostics;
 
 using Org.BouncyCastle.Math.Raw;
 using Org.BouncyCastle.Utilities;
@@ -95,7 +94,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             //return Multiply(b.Invert());
             uint[] z = Nat256.Create();
-            Mod.Invert(SecP256K1Field.P, ((SecP256K1FieldElement)b).x, z);
+            SecP256K1Field.Inv(((SecP256K1FieldElement)b).x, z);
             SecP256K1Field.Multiply(z, x, z);
             return new SecP256K1FieldElement(z);
         }
@@ -118,7 +117,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             //return new SecP256K1FieldElement(ToBigInteger().ModInverse(Q));
             uint[] z = Nat256.Create();
-            Mod.Invert(SecP256K1Field.P, x, z);
+            SecP256K1Field.Inv(x, z);
             return new SecP256K1FieldElement(z);
         }
 
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1Curve.cs b/crypto/src/math/ec/custom/sec/SecP256R1Curve.cs
index 7cc456b2d..b90f5d048 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1Curve.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1Curve.cs
@@ -1,6 +1,7 @@
 using System;
 
 using Org.BouncyCastle.Math.Raw;
+using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities.Encoders;
 
 namespace Org.BouncyCastle.Math.EC.Custom.Sec
@@ -92,6 +93,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return new SecP256R1LookupTable(this, table, len);
         }
 
+        public override ECFieldElement RandomFieldElement(SecureRandom r)
+        {
+            uint[] x = Nat256.Create();
+            SecP256R1Field.Random(r, x);
+            return new SecP256R1FieldElement(x);
+        }
+
+        public override ECFieldElement RandomFieldElementMult(SecureRandom r)
+        {
+            uint[] x = Nat256.Create();
+            SecP256R1Field.RandomMult(r, x);
+            return new SecP256R1FieldElement(x);
+        }
+
         private class SecP256R1LookupTable
             : AbstractECLookupTable
         {
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
index 5b3de6d36..0b4918820 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1Field.cs
@@ -1,20 +1,22 @@
 using System;
 using System.Diagnostics;
 
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Math.Raw;
+using Org.BouncyCastle.Security;
 
 namespace Org.BouncyCastle.Math.EC.Custom.Sec
 {
     internal class SecP256R1Field
     {
         // 2^256 - 2^224 + 2^192 + 2^96 - 1
-        internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000,
+        private static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000,
             0x00000001, 0xFFFFFFFF };
-        internal static readonly uint[] PExt = new uint[]{ 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFF,
+        private static readonly uint[] PExt = new uint[]{ 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFF,
             0xFFFFFFFF, 0xFFFFFFFE, 0x00000001, 0xFFFFFFFE, 0x00000001, 0xFFFFFFFE, 0x00000001, 0x00000001, 0xFFFFFFFE,
             0x00000002, 0xFFFFFFFE };
-        internal const uint P7 = 0xFFFFFFFF;
-        internal const uint PExt15 = 0xFFFFFFFE;
+        private const uint P7 = 0xFFFFFFFF;
+        private const uint PExt15 = 0xFFFFFFFE;
 
         public static void Add(uint[] x, uint[] y, uint[] z)
         {
@@ -66,6 +68,75 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             }
         }
 
+        public static void Inv(uint[] x, uint[] z)
+        {
+            /*
+             * Raise this element to the exponent 2^256 - 2^224 + 2^192 + 2^96 - 3
+             *
+             * Breaking up the exponent's binary representation into "repunits", we get:
+             * { 32 1s } { 31 0s } { 1 1s } { 96 0s } { 94 1s } { 1 0s} { 1 1s}
+             *
+             * Therefore we need an addition chain containing 1, 32, 94 (the lengths of the repunits)
+             * We use: [1], 2, 4, 8, 16, [32], 64, 80, 88, 92, [94]
+             */
+
+            if (0 != IsZero(x))
+                throw new ArgumentException("cannot be 0", "x");
+
+            uint[] x1 = x;
+            uint[] x2 = Nat256.Create();
+            Square(x1, x2);
+            Multiply(x2, x1, x2);
+            uint[] x4 = Nat256.Create();
+            SquareN(x2, 2, x4);
+            Multiply(x4, x2, x4);
+            uint[] x8 = Nat256.Create();
+            SquareN(x4, 4, x8);
+            Multiply(x8, x4, x8);
+            uint[] x16 = Nat256.Create();
+            SquareN(x8, 8, x16);
+            Multiply(x16, x8, x16);
+            uint[] x32 = Nat256.Create();
+            SquareN(x16, 16, x32);
+            Multiply(x32, x16, x32);
+            uint[] x64 = Nat256.Create();
+            SquareN(x32, 32, x64);
+            Multiply(x64, x32, x64);
+            uint[] x80 = x64;
+            SquareN(x64, 16, x80);
+            Multiply(x80, x16, x80);
+            uint[] x88 = x16;
+            SquareN(x80, 8, x88);
+            Multiply(x88, x8, x88);
+            uint[] x92 = x8;
+            SquareN(x88, 4, x92);
+            Multiply(x92, x4, x92);
+            uint[] x94 = x4;
+            SquareN(x92, 2, x94);
+            Multiply(x94, x2, x94);
+
+            uint[] t = x32;
+            SquareN(t, 32, t);
+            Multiply(t, x1, t);
+            SquareN(t, 190, t);
+            Multiply(t, x94, t);
+            SquareN(t, 2, t);
+
+            // NOTE that x1 and z could be the same array
+            Multiply(x1, t, z);
+        }
+
+        public static int IsZero(uint[] x)
+        {
+            uint d = 0;
+            for (int i = 0; i < 8; ++i)
+            {
+                d |= x[i];
+            }
+            d = (d >> 1) | (d & 1);
+            return ((int)d - 1) >> 31;
+        }
+
         public static void Multiply(uint[] x, uint[] y, uint[] z)
         {
             uint[] tt = Nat256.CreateExt();
@@ -84,9 +155,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Negate(uint[] x, uint[] z)
         {
-            if (Nat256.IsZero(x))
+            if (0 != IsZero(x))
             {
-                Nat256.Zero(z);
+                Nat256.Sub(P, P, z);
             }
             else
             {
@@ -94,6 +165,26 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             }
         }
 
+        public static void Random(SecureRandom r, uint[] z)
+        {
+            byte[] bb = new byte[8 * 4];
+            do
+            {
+                r.NextBytes(bb);
+                Pack.LE_To_UInt32(bb, 0, z, 0, 8);
+            }
+            while (0 == Nat.LessThan(8, z, P));
+        }
+
+        public static void RandomMult(SecureRandom r, uint[] z)
+        {
+            do
+            {
+                Random(r, z);
+            }
+            while (0 != IsZero(z));
+        }
+
         public static void Reduce(uint[] xx, uint[] z)
         {
             long xx08 = xx[8], xx09 = xx[9], xx10 = xx[10], xx11 = xx[11];
diff --git a/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs
index 1a18ec38e..bee322091 100644
--- a/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256R1FieldElement.cs
@@ -94,7 +94,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             //return Multiply(b.Invert());
             uint[] z = Nat256.Create();
-            Mod.Invert(SecP256R1Field.P, ((SecP256R1FieldElement)b).x, z);
+            SecP256R1Field.Inv(((SecP256R1FieldElement)b).x, z);
             SecP256R1Field.Multiply(z, x, z);
             return new SecP256R1FieldElement(z);
         }
@@ -117,7 +117,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             //return new SecP256R1FieldElement(ToBigInteger().ModInverse(Q));
             uint[] z = Nat256.Create();
-            Mod.Invert(SecP256R1Field.P, x, z);
+            SecP256R1Field.Inv(x, z);
             return new SecP256R1FieldElement(z);
         }
 
diff --git a/crypto/src/math/ec/custom/sec/SecP384R1Curve.cs b/crypto/src/math/ec/custom/sec/SecP384R1Curve.cs
index 890a72329..b57788ea0 100644
--- a/crypto/src/math/ec/custom/sec/SecP384R1Curve.cs
+++ b/crypto/src/math/ec/custom/sec/SecP384R1Curve.cs
@@ -1,6 +1,7 @@
 using System;
 
 using Org.BouncyCastle.Math.Raw;
+using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities.Encoders;
 
 namespace Org.BouncyCastle.Math.EC.Custom.Sec
@@ -92,6 +93,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return new SecP384R1LookupTable(this, table, len);
         }
 
+        public override ECFieldElement RandomFieldElement(SecureRandom r)
+        {
+            uint[] x = Nat.Create(12);
+            SecP384R1Field.Random(r, x);
+            return new SecP384R1FieldElement(x);
+        }
+
+        public override ECFieldElement RandomFieldElementMult(SecureRandom r)
+        {
+            uint[] x = Nat.Create(12);
+            SecP384R1Field.RandomMult(r, x);
+            return new SecP384R1FieldElement(x);
+        }
+
         private class SecP384R1LookupTable
             : AbstractECLookupTable
         {
diff --git a/crypto/src/math/ec/custom/sec/SecP384R1Field.cs b/crypto/src/math/ec/custom/sec/SecP384R1Field.cs
index 0780df3f0..f15678497 100644
--- a/crypto/src/math/ec/custom/sec/SecP384R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP384R1Field.cs
@@ -1,16 +1,18 @@
 using System;
 using System.Diagnostics;
 
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Math.Raw;
+using Org.BouncyCastle.Security;
 
 namespace Org.BouncyCastle.Math.EC.Custom.Sec
 {
     internal class SecP384R1Field
     {
             // 2^384 - 2^128 - 2^96 + 2^32 - 1
-        internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF,
+        private static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF,
             0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
-        internal static readonly uint[] PExt = new uint[]{ 0x00000001, 0xFFFFFFFE, 0x00000000, 0x00000002, 0x00000000, 0xFFFFFFFE,
+        private static readonly uint[] PExt = new uint[]{ 0x00000001, 0xFFFFFFFE, 0x00000000, 0x00000002, 0x00000000, 0xFFFFFFFE,
             0x00000000, 0x00000002, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0xFFFFFFFE, 0x00000001, 0x00000000,
             0xFFFFFFFE, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
         private static readonly uint[] PExtInv = new uint[]{ 0xFFFFFFFF, 0x00000001, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFF, 0x00000001,
@@ -72,6 +74,81 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             }
         }
 
+        public static void Inv(uint[] x, uint[] z)
+        {
+            /*
+             * Raise this element to the exponent 2^384 - 2^128 - 2^96 + 2^32 - 3
+             *
+             * Breaking up the exponent's binary representation into "repunits", we get:
+             * { 255 1s } { 1 0s } { 32 1s } { 64 0s } { 30 1s } { 1 0s} { 1 1s}
+             *
+             * Therefore we need an addition chain containing 1, 30, 32, 255 (the lengths of the repunits)
+             * We use: [1], 2, 3, 6, 12, 24, [30], [32], 62, 124, 248, 254, [255]
+             */
+
+            if (0 != IsZero(x))
+                throw new ArgumentException("cannot be 0", "x");
+
+            uint[] x1 = x;
+            uint[] x2 = Nat.Create(12);
+            Square(x1, x2);
+            Multiply(x2, x1, x2);
+            uint[] x3 = Nat.Create(12);
+            Square(x2, x3);
+            Multiply(x3, x1, x3);
+            uint[] x6 = Nat.Create(12);
+            SquareN(x3, 3, x6);
+            Multiply(x6, x3, x6);
+            uint[] x12 = x3;
+            SquareN(x6, 6, x12);
+            Multiply(x12, x6, x12);
+            uint[] x24 = Nat.Create(12);
+            SquareN(x12, 12, x24);
+            Multiply(x24, x12, x24);
+            uint[] x30 = x12;
+            SquareN(x24, 6, x30);
+            Multiply(x30, x6, x30);
+            uint[] x32 = x24;
+            SquareN(x30, 2, x32);
+            Multiply(x32, x2, x32);
+            uint[] x62 = x2;
+            SquareN(x32, 30, x62);
+            Multiply(x62, x30, x62);
+            uint[] x124 = Nat.Create(12);
+            SquareN(x62, 62, x124);
+            Multiply(x124, x62, x124);
+            uint[] x248 = x62;
+            SquareN(x124, 124, x248);
+            Multiply(x248, x124, x248);
+            uint[] x254 = x124;
+            SquareN(x248, 6, x254);
+            Multiply(x254, x6, x254);
+            uint[] x255 = x6;
+            Square(x254, x255);
+            Multiply(x255, x1, x255);
+
+            uint[] t = x255;
+            SquareN(t, 33, t);
+            Multiply(t, x32, t);
+            SquareN(t, 94, t);
+            Multiply(t, x30, t);
+            SquareN(t, 2, t);
+
+            // NOTE that x1 and z could be the same array
+            Multiply(x1, t, z);
+        }
+
+        public static int IsZero(uint[] x)
+        {
+            uint d = 0;
+            for (int i = 0; i < 12; ++i)
+            {
+                d |= x[i];
+            }
+            d = (d >> 1) | (d & 1);
+            return ((int)d - 1) >> 31;
+        }
+
         public static void Multiply(uint[] x, uint[] y, uint[] z)
         {
             uint[] tt = Nat.Create(24);
@@ -81,9 +158,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Negate(uint[] x, uint[] z)
         {
-            if (Nat.IsZero(12, x))
+            if (0 != IsZero(x))
             {
-                Nat.Zero(12, z);
+                Nat.Sub(12, P, P, z);
             }
             else
             {
@@ -91,6 +168,26 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             }
         }
 
+        public static void Random(SecureRandom r, uint[] z)
+        {
+            byte[] bb = new byte[12 * 4];
+            do
+            {
+                r.NextBytes(bb);
+                Pack.LE_To_UInt32(bb, 0, z, 0, 12);
+            }
+            while (0 == Nat.LessThan(12, z, P));
+        }
+
+        public static void RandomMult(SecureRandom r, uint[] z)
+        {
+            do
+            {
+                Random(r, z);
+            }
+            while (0 != IsZero(z));
+        }
+
         public static void Reduce(uint[] xx, uint[] z)
         {
             long xx16 = xx[16], xx17 = xx[17], xx18 = xx[18], xx19 = xx[19];
diff --git a/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs
index 00046b495..3fc1fb851 100644
--- a/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs
@@ -94,7 +94,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             //return Multiply(b.Invert());
             uint[] z = Nat.Create(12);
-            Mod.Invert(SecP384R1Field.P, ((SecP384R1FieldElement)b).x, z);
+            SecP384R1Field.Inv(((SecP384R1FieldElement)b).x, z);
             SecP384R1Field.Multiply(z, x, z);
             return new SecP384R1FieldElement(z);
         }
@@ -117,7 +117,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             //return new SecP384R1FieldElement(ToBigInteger().ModInverse(Q));
             uint[] z = Nat.Create(12);
-            Mod.Invert(SecP384R1Field.P, x, z);
+            SecP384R1Field.Inv(x, z);
             return new SecP384R1FieldElement(z);
         }
 
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1Curve.cs b/crypto/src/math/ec/custom/sec/SecP521R1Curve.cs
index 28db11aea..143176b10 100644
--- a/crypto/src/math/ec/custom/sec/SecP521R1Curve.cs
+++ b/crypto/src/math/ec/custom/sec/SecP521R1Curve.cs
@@ -1,6 +1,7 @@
 using System;
 
 using Org.BouncyCastle.Math.Raw;
+using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities.Encoders;
 
 namespace Org.BouncyCastle.Math.EC.Custom.Sec
@@ -92,6 +93,20 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             return new SecP521R1LookupTable(this, table, len);
         }
 
+        public override ECFieldElement RandomFieldElement(SecureRandom r)
+        {
+            uint[] x = Nat.Create(17);
+            SecP521R1Field.Random(r, x);
+            return new SecP521R1FieldElement(x);
+        }
+
+        public override ECFieldElement RandomFieldElementMult(SecureRandom r)
+        {
+            uint[] x = Nat.Create(17);
+            SecP521R1Field.RandomMult(r, x);
+            return new SecP521R1FieldElement(x);
+        }
+
         private class SecP521R1LookupTable
             : AbstractECLookupTable
         {
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1Field.cs b/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
index b7f8eb146..ec81cf021 100644
--- a/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecP521R1Field.cs
@@ -1,14 +1,16 @@
 using System;
 using System.Diagnostics;
 
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Math.Raw;
+using Org.BouncyCastle.Security;
 
 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,
+        private 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;
 
@@ -51,6 +53,77 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             z[16] = (x16 >> 1) | (c >> 23);
         }
 
+        public static void Inv(uint[] x, uint[] z)
+        {
+            /*
+             * Raise this element to the exponent 2^521 - 3
+             *
+             * Breaking up the exponent's binary representation into "repunits", we get:
+             * { 519 1s } { 1 0s} { 1 1s}
+             *
+             * Therefore we need an addition chain containing 1, 519 (the lengths of the repunits)
+             * We use: [1], 2, 4, 8, 16, 32, 64, 128, 256, 512, 516, 518, [519]
+             */
+
+            if (0 != IsZero(x))
+                throw new ArgumentException("cannot be 0", "x");
+
+            uint[] x1 = x;
+            uint[] x2 = Nat.Create(17);
+            Square(x1, x2);
+            Multiply(x2, x1, x2);
+            uint[] x4 = Nat.Create(17);
+            SquareN(x2, 2, x4);
+            Multiply(x4, x2, x4);
+            uint[] x8 = Nat.Create(17);
+            SquareN(x4, 4, x8);
+            Multiply(x8, x4, x8);
+            uint[] x16 = Nat.Create(17);
+            SquareN(x8, 8, x16);
+            Multiply(x16, x8, x16);
+            uint[] x32 = x8;
+            SquareN(x16, 16, x32);
+            Multiply(x32, x16, x32);
+            uint[] x64 = x16;
+            SquareN(x32, 32, x64);
+            Multiply(x64, x32, x64);
+            uint[] x128 = x32;
+            SquareN(x64, 64, x128);
+            Multiply(x128, x64, x128);
+            uint[] x256 = x64;
+            SquareN(x128, 128, x256);
+            Multiply(x256, x128, x256);
+            uint[] x512 = x128;
+            SquareN(x256, 256, x512);
+            Multiply(x512, x256, x512);
+            uint[] x516 = x256;
+            SquareN(x512, 4, x516);
+            Multiply(x516, x4, x516);
+            uint[] x518 = x4;
+            SquareN(x516, 2, x518);
+            Multiply(x518, x2, x518);
+            uint[] x519 = x2;
+            Square(x518, x519);
+            Multiply(x519, x1, x519);
+
+            uint[] t = x519;
+            SquareN(t, 2, t);
+
+            // NOTE that x1 and z could be the same array
+            Multiply(x1, t, z);
+        }
+
+        public static int IsZero(uint[] x)
+        {
+            uint d = 0;
+            for (int i = 0; i < 17; ++i)
+            {
+                d |= x[i];
+            }
+            d = (d >> 1) | (d & 1);
+            return ((int)d - 1) >> 31;
+        }
+
         public static void Multiply(uint[] x, uint[] y, uint[] z)
         {
             uint[] tt = Nat.Create(33);
@@ -60,9 +133,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         public static void Negate(uint[] x, uint[] z)
         {
-            if (Nat.IsZero(17, x))
+            if (0 != IsZero(x))
             {
-                Nat.Zero(17, z);
+                Nat.Sub(17, P, P, z);
             }
             else
             {
@@ -70,6 +143,27 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
             }
         }
 
+        public static void Random(SecureRandom r, uint[] z)
+        {
+            byte[] bb = new byte[17 * 4];
+            do
+            {
+                r.NextBytes(bb);
+                Pack.LE_To_UInt32(bb, 0, z, 0, 17);
+                z[16] &= 0x000001FFU;
+            }
+            while (0 == Nat.LessThan(17, z, P));
+        }
+
+        public static void RandomMult(SecureRandom r, uint[] z)
+        {
+            do
+            {
+                Random(r, z);
+            }
+            while (0 != IsZero(z));
+        }
+
         public static void Reduce(uint[] xx, uint[] z)
         {
             Debug.Assert(xx[32] >> 18 == 0);
diff --git a/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs b/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
index c677b2610..adfc89e9d 100644
--- a/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
+++ b/crypto/src/math/ec/custom/sec/SecP521R1FieldElement.cs
@@ -94,7 +94,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             //return Multiply(b.Invert());
             uint[] z = Nat.Create(17);
-            Mod.Invert(SecP521R1Field.P, ((SecP521R1FieldElement)b).x, z);
+            SecP521R1Field.Inv(((SecP521R1FieldElement)b).x, z);
             SecP521R1Field.Multiply(z, x, z);
             return new SecP521R1FieldElement(z);
         }
@@ -117,7 +117,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         {
             //return new SecP521R1FieldElement(ToBigInteger().ModInverse(Q));
             uint[] z = Nat.Create(17);
-            Mod.Invert(SecP521R1Field.P, x, z);
+            SecP521R1Field.Inv(x, z);
             return new SecP521R1FieldElement(z);
         }