summary refs log tree commit diff
path: root/crypto/src/pqc/math/linearalgebra/RandUtils.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/pqc/math/linearalgebra/RandUtils.cs')
-rw-r--r--crypto/src/pqc/math/linearalgebra/RandUtils.cs27
1 files changed, 27 insertions, 0 deletions
diff --git a/crypto/src/pqc/math/linearalgebra/RandUtils.cs b/crypto/src/pqc/math/linearalgebra/RandUtils.cs
new file mode 100644
index 000000000..f7b7b8588
--- /dev/null
+++ b/crypto/src/pqc/math/linearalgebra/RandUtils.cs
@@ -0,0 +1,27 @@
+using Org.BouncyCastle.Security;
+
+namespace Org.BouncyCastle.Pqc.Math.LinearAlgebra
+{
+    public class RandUtils
+    {
+        public static int NextInt(SecureRandom rand, int n)
+        {
+
+            if ((n & -n) == n)  // i.e., n is a power of 2
+            {
+                return (int)((n * (long)(Utils.UnsignedRightBitShiftInt(rand.NextInt(), 1))) >> 31);
+            }
+
+            int bits, value;
+            do
+            {
+                bits = Utils.UnsignedRightBitShiftInt(rand.NextInt() ,1);
+                value = bits % n;
+            }
+            while (bits - value + (n - 1) < 0);
+
+            return value;
+        }
+    }
+
+}