14 protected readonly BigInteger
p;
31 public BigInteger
Add(BigInteger a, BigInteger b)
33 BigInteger Sum = a + b;
51 public BigInteger
Subtract(BigInteger a, BigInteger b)
53 BigInteger Diff = a - b;
64 else if (Diff >= this.p)
80 public BigInteger
Multiply(BigInteger a, BigInteger b)
82 return BigInteger.Remainder(a * b, this.p);
91 public BigInteger
Divide(BigInteger a, BigInteger b)
94 return BigInteger.Remainder(a * b, this.p);
116 x = BigInteger.Remainder(x,
p);
121 x = BigInteger.Remainder(x,
p);
123 BigInteger i = this.
p;
125 BigInteger y1 = BigInteger.One;
126 BigInteger y2 = BigInteger.Zero;
131 q = BigInteger.DivRem(i, j, out BigInteger r);
141 throw new ArgumentException(
"Number not invertible.", nameof(x));
143 BigInteger Result = BigInteger.Remainder(y2, this.p);
155 public BigInteger
Sqrt(BigInteger N)
166 public static BigInteger
SqrtModP(BigInteger N, BigInteger
p)
172 N = BigInteger.Remainder(N,
p);
177 N = BigInteger.Remainder(N,
p);
179 BigInteger pm1d2 = (
p - 1) / 2;
180 if (BigInteger.ModPow(N, pm1d2,
p) != BigInteger.One)
181 throw new ArgumentException(
"No root available.", nameof(N));
183 BigInteger z =
p - 1;
193 while (BigInteger.ModPow(z, pm1d2,
p) == BigInteger.One)
197 throw new InvalidOperationException(
"Nonresidue not found.");
200 BigInteger c = BigInteger.ModPow(z, m,
p);
201 BigInteger c2 = BigInteger.Remainder(c * c,
p);
202 BigInteger u = BigInteger.ModPow(N, m,
p);
203 BigInteger v = BigInteger.ModPow(N, (m + 1) / 2,
p);
207 if (BigInteger.ModPow(u, BigInteger.Pow(2, s - 1),
p) != BigInteger.One)
209 u = BigInteger.Remainder(u * c2,
p);
210 v = BigInteger.Remainder(v * c,
p);
214 c2 = BigInteger.Remainder(c * c,
p);
240 int c = A.Length - 1;
Integer arithmetic, modulus a prime.
BigInteger Sqrt(BigInteger N)
Computes sqrt(N) mod p.
ModulusP(BigInteger Prime)
Integer arithmetic, modulus a prime.
readonly BigInteger p
Base prime.
BigInteger Invert(BigInteger x)
Inverts a number in the field Z[p].
BigInteger Multiply(BigInteger a, BigInteger b)
Multiplies two numbers, modulus p
static int CalcBits(BigInteger n)
Calculates the number of bits used.
BigInteger Add(BigInteger a, BigInteger b)
Adds two numbers, modulus p
static BigInteger SqrtModP(BigInteger N, BigInteger p)
Computes sqrt(N) mod p.
BigInteger Divide(BigInteger a, BigInteger b)
Divides two numbers, modulus p
BigInteger Subtract(BigInteger a, BigInteger b)
Subtracts two numbers, modulus p
BigInteger Negate(BigInteger x)
Negates a number in the field Z[p].
static int CalcBits(byte[] A)
Calculates the number of bits used in a binary encoded big integer.