summary refs log tree commit diff
path: root/crypto/src/math/ec/Mod.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2014-02-27 19:55:53 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2014-02-27 19:55:53 +0700
commitdffd5540d611f199901fe3a5ee64e7553963b7c1 (patch)
treeccf79f5a90b0f145ae2a28ba419b4d1e5fe1b6b2 /crypto/src/math/ec/Mod.cs
parentEquality/hashcode should ignore "excess" words (diff)
downloadBouncyCastle.NET-ed25519-dffd5540d611f199901fe3a5ee64e7553963b7c1.tar.xz
Optimized Sqrt() for custom secp224r1
Diffstat (limited to 'crypto/src/math/ec/Mod.cs')
-rw-r--r--crypto/src/math/ec/Mod.cs26
1 files changed, 26 insertions, 0 deletions
diff --git a/crypto/src/math/ec/Mod.cs b/crypto/src/math/ec/Mod.cs
index a05ff77aa..37958e57e 100644
--- a/crypto/src/math/ec/Mod.cs
+++ b/crypto/src/math/ec/Mod.cs
@@ -1,6 +1,7 @@
 using System;
 using System.Diagnostics;
 
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Math.EC
@@ -73,6 +74,31 @@ namespace Org.BouncyCastle.Math.EC
             }
         }
 
+        public static uint[] Random(uint[] p)
+        {
+            int len = p.Length;
+            Random rand = new Random();
+            uint[] s = Nat.Create(len);
+
+            uint m = p[len - 1];
+            m |= m >> 1;
+            m |= m >> 2;
+            m |= m >> 4;
+            m |= m >> 8;
+            m |= m >> 16;
+
+            do
+            {
+                byte[] bytes = new byte[len << 2];
+                rand.NextBytes(bytes);
+                Pack.BE_To_UInt32(bytes, 0, s);
+                s[len - 1] &= m;
+            }
+            while (Nat.Gte(len, s, p));
+
+            return s;
+        }
+
         public static void Subtract(uint[] p, uint[] x, uint[] y, uint[] z)
         {
             int len = p.Length;