BigIntegers
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.Raw;
using Org.BouncyCastle.Security;
using System;
namespace Org.BouncyCastle.Utilities
{
public static class BigIntegers
{
public static readonly BigInteger Zero = BigInteger.Zero;
public static readonly BigInteger One = BigInteger.One;
private const int MaxIterations = 1000;
[CLSCompliant(false)]
public static void AsUint32ArrayLittleEndian(BigInteger n, Span<uint> buf)
{
int lengthofUInt32Array = n.GetLengthofUInt32Array();
if (lengthofUInt32Array > buf.Length)
throw new ArgumentException("standard length exceeded", "n");
n.ToUInt32ArrayLittleEndian(buf);
int value = (int)buf[lengthofUInt32Array - 1] >> 31;
int num = lengthofUInt32Array;
buf.Slice(num, buf.Length - num).Fill((uint)value);
}
public static byte[] AsUnsignedByteArray(BigInteger n)
{
return n.ToByteArrayUnsigned();
}
public static byte[] AsUnsignedByteArray(int length, BigInteger n)
{
int lengthofByteArrayUnsigned = n.GetLengthofByteArrayUnsigned();
if (lengthofByteArrayUnsigned > length)
throw new ArgumentException("standard length exceeded", "n");
byte[] array = new byte[length];
n.ToByteArrayUnsigned(array.AsSpan(length - lengthofByteArrayUnsigned));
return array;
}
public static void AsUnsignedByteArray(BigInteger n, byte[] buf, int off, int len)
{
AsUnsignedByteArray(n, buf.AsSpan(off, len));
}
public static void AsUnsignedByteArray(BigInteger n, Span<byte> buf)
{
int lengthofByteArrayUnsigned = n.GetLengthofByteArrayUnsigned();
if (lengthofByteArrayUnsigned > buf.Length)
throw new ArgumentException("standard length exceeded", "n");
int num = lengthofByteArrayUnsigned;
buf.Slice(0, buf.Length - num).Fill(0);
num = lengthofByteArrayUnsigned;
int length = buf.Length;
int num2 = length - num;
n.ToByteArrayUnsigned(buf.Slice(num2, length - num2));
}
public static BigInteger CreateRandomBigInteger(int bitLength, SecureRandom secureRandom)
{
return new BigInteger(bitLength, secureRandom);
}
public static BigInteger CreateRandomInRange(BigInteger min, BigInteger max, SecureRandom random)
{
int num = min.CompareTo(max);
if (num >= 0) {
if (num > 0)
throw new ArgumentException("'min' may not be greater than 'max'");
return min;
}
if (min.BitLength > max.BitLength / 2)
return CreateRandomInRange(BigInteger.Zero, max.Subtract(min), random).Add(min);
for (int i = 0; i < 1000; i++) {
BigInteger bigInteger = new BigInteger(max.BitLength, random);
if (bigInteger.CompareTo(min) >= 0 && bigInteger.CompareTo(max) <= 0)
return bigInteger;
}
return new BigInteger(max.Subtract(min).BitLength - 1, random).Add(min);
}
public static BigInteger FromUnsignedByteArray(byte[] buf)
{
return new BigInteger(1, buf);
}
public static BigInteger FromUnsignedByteArray(byte[] buf, int off, int length)
{
return new BigInteger(1, buf, off, length);
}
public static int GetByteLength(BigInteger n)
{
return n.GetLengthofByteArray();
}
public static int GetUnsignedByteLength(BigInteger n)
{
return n.GetLengthofByteArrayUnsigned();
}
public unsafe static BigInteger ModOddInverse(BigInteger M, BigInteger X)
{
if (!M.TestBit(0))
throw new ArgumentException("must be odd", "M");
if (M.SignValue != 1)
throw new ArithmeticException("BigInteger: modulus not positive");
if (X.SignValue < 0 || X.BitLength > M.BitLength)
X = X.Mod(M);
int bitLength = M.BitLength;
if (bitLength <= 2048) {
int lengthForBits = Nat.GetLengthForBits(bitLength);
int num = lengthForBits;
Span<uint> span = new Span<uint>(stackalloc byte[(int)checked(unchecked((ulong)(uint)num) * 4)], num);
num = lengthForBits;
Span<uint> span2 = new Span<uint>(stackalloc byte[(int)checked(unchecked((ulong)(uint)num) * 4)], num);
num = lengthForBits;
Span<uint> span3 = new Span<uint>(stackalloc byte[(int)checked(unchecked((ulong)(uint)num) * 4)], num);
Nat.FromBigInteger(bitLength, M, span);
Nat.FromBigInteger(bitLength, X, span2);
if (Mod.ModOddInverse(span, span2, span3) == 0)
throw new ArithmeticException("BigInteger not invertible");
return Nat.ToBigInteger(lengthForBits, span3);
}
uint[] array = Nat.FromBigInteger(bitLength, M);
uint[] array2 = Nat.FromBigInteger(bitLength, X);
int len = array.Length;
uint[] array3 = Nat.Create(len);
if (Mod.ModOddInverse(array, array2, array3) == 0)
throw new ArithmeticException("BigInteger not invertible");
return Nat.ToBigInteger(len, array3);
}
public unsafe static BigInteger ModOddInverseVar(BigInteger M, BigInteger X)
{
if (!M.TestBit(0))
throw new ArgumentException("must be odd", "M");
if (M.SignValue != 1)
throw new ArithmeticException("BigInteger: modulus not positive");
if (M.Equals(One))
return Zero;
if (X.SignValue < 0 || X.BitLength > M.BitLength)
X = X.Mod(M);
if (X.Equals(One))
return One;
int bitLength = M.BitLength;
if (bitLength <= 2048) {
int lengthForBits = Nat.GetLengthForBits(bitLength);
int num = lengthForBits;
Span<uint> span = new Span<uint>(stackalloc byte[(int)checked(unchecked((ulong)(uint)num) * 4)], num);
num = lengthForBits;
Span<uint> span2 = new Span<uint>(stackalloc byte[(int)checked(unchecked((ulong)(uint)num) * 4)], num);
num = lengthForBits;
Span<uint> span3 = new Span<uint>(stackalloc byte[(int)checked(unchecked((ulong)(uint)num) * 4)], num);
Nat.FromBigInteger(bitLength, M, span);
Nat.FromBigInteger(bitLength, X, span2);
if (!Mod.ModOddInverseVar(span, span2, span3))
throw new ArithmeticException("BigInteger not invertible");
return Nat.ToBigInteger(lengthForBits, span3);
}
uint[] array = Nat.FromBigInteger(bitLength, M);
uint[] array2 = Nat.FromBigInteger(bitLength, X);
int len = array.Length;
uint[] array3 = Nat.Create(len);
if (!Mod.ModOddInverseVar(array, array2, array3))
throw new ArithmeticException("BigInteger not invertible");
return Nat.ToBigInteger(len, array3);
}
public unsafe static bool ModOddIsCoprime(BigInteger M, BigInteger X)
{
if (!M.TestBit(0))
throw new ArgumentException("must be odd", "M");
if (M.SignValue != 1)
throw new ArithmeticException("BigInteger: modulus not positive");
if (X.SignValue < 0 || X.BitLength > M.BitLength)
X = X.Mod(M);
int bitLength = M.BitLength;
if (bitLength <= 2048) {
int lengthForBits = Nat.GetLengthForBits(bitLength);
int num = lengthForBits;
Span<uint> span = new Span<uint>(stackalloc byte[(int)checked(unchecked((ulong)(uint)num) * 4)], num);
num = lengthForBits;
Span<uint> span2 = new Span<uint>(stackalloc byte[(int)checked(unchecked((ulong)(uint)num) * 4)], num);
Nat.FromBigInteger(bitLength, M, span);
Nat.FromBigInteger(bitLength, X, span2);
return Mod.ModOddIsCoprime(span, span2) != 0;
}
uint[] array = Nat.FromBigInteger(bitLength, M);
uint[] array2 = Nat.FromBigInteger(bitLength, X);
return Mod.ModOddIsCoprime(array, array2) != 0;
}
public unsafe static bool ModOddIsCoprimeVar(BigInteger M, BigInteger X)
{
if (!M.TestBit(0))
throw new ArgumentException("must be odd", "M");
if (M.SignValue != 1)
throw new ArithmeticException("BigInteger: modulus not positive");
if (X.SignValue < 0 || X.BitLength > M.BitLength)
X = X.Mod(M);
if (X.Equals(One))
return true;
int bitLength = M.BitLength;
if (bitLength <= 2048) {
int lengthForBits = Nat.GetLengthForBits(bitLength);
int num = lengthForBits;
Span<uint> span = new Span<uint>(stackalloc byte[(int)checked(unchecked((ulong)(uint)num) * 4)], num);
num = lengthForBits;
Span<uint> span2 = new Span<uint>(stackalloc byte[(int)checked(unchecked((ulong)(uint)num) * 4)], num);
Nat.FromBigInteger(bitLength, M, span);
Nat.FromBigInteger(bitLength, X, span2);
return Mod.ModOddIsCoprimeVar(span, span2);
}
uint[] array = Nat.FromBigInteger(bitLength, M);
uint[] array2 = Nat.FromBigInteger(bitLength, X);
return Mod.ModOddIsCoprimeVar(array, array2);
}
}
}