|
| 1 | +// The FinderOuter |
| 2 | +// Copyright (c) 2020 Coding Enthusiast |
| 3 | +// Distributed under the MIT software license, see the accompanying |
| 4 | +// file LICENCE or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Numerics; |
| 8 | + |
| 9 | +namespace FinderOuter.Backend.Cryptography.Arithmetic |
| 10 | +{ |
| 11 | + public static class SquareRoot |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Finds N such that N % P = A using Tonelli-Shanks algorithm. |
| 15 | + /// </summary> |
| 16 | + public static BigInteger FindSquareRoot(BigInteger a, BigInteger p) |
| 17 | + { |
| 18 | + return TonelliShanks(a, p); |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | + private static BigInteger TonelliShanks(BigInteger a, BigInteger p) |
| 23 | + { |
| 24 | + if (a >= p) |
| 25 | + { |
| 26 | + throw new Exception("The residue, 'a' cannot be greater than the modulus 'p'!"); |
| 27 | + } |
| 28 | + if (Legendre.Symbol(a, p) != 1) // a^(p-1 / 2) % p == p-1 |
| 29 | + { |
| 30 | + throw new ArithmeticException($"Parameter 'a' is not a quadratic residue, mod 'p'"); |
| 31 | + } |
| 32 | + // This will be true for secp256k1 curve prime |
| 33 | + if (p % 4 == 3) |
| 34 | + { |
| 35 | + return BigInteger.ModPow(a, (p + 1) / 4, p); |
| 36 | + } |
| 37 | + |
| 38 | + //Initialize |
| 39 | + BigInteger s = p - 1; |
| 40 | + BigInteger e = 0; |
| 41 | + while (s % 2 == 0) |
| 42 | + { |
| 43 | + s /= 2; |
| 44 | + e += 1; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + BigInteger n = FindGenerator(p); |
| 49 | + |
| 50 | + BigInteger x = BigInteger.ModPow(a, (s + 1) / 2, p); |
| 51 | + BigInteger b = BigInteger.ModPow(a, s, p); |
| 52 | + BigInteger g = BigInteger.ModPow(n, s, p); |
| 53 | + BigInteger r = e; |
| 54 | + BigInteger m = Order(b, p); |
| 55 | + if (m == 0) |
| 56 | + { |
| 57 | + return x; |
| 58 | + } |
| 59 | + |
| 60 | + while (m > 0) |
| 61 | + { |
| 62 | + x = (x * BigInteger.ModPow(g, TwoExp(r - m - 1), p)) % p; |
| 63 | + b = (b * BigInteger.ModPow(g, TwoExp(r - m), p)) % p; |
| 64 | + g = BigInteger.ModPow(g, TwoExp(r - m), p); |
| 65 | + r = m; |
| 66 | + m = Order(b, p); |
| 67 | + } |
| 68 | + |
| 69 | + return x; |
| 70 | + } |
| 71 | + |
| 72 | + private static BigInteger FindGenerator(BigInteger p) |
| 73 | + { |
| 74 | + BigInteger n = 2; |
| 75 | + while (BigInteger.ModPow(n, (p - 1) / 2, p) == 1) |
| 76 | + { |
| 77 | + n++; |
| 78 | + } |
| 79 | + |
| 80 | + return n; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + private static BigInteger Order(BigInteger b, BigInteger p) |
| 87 | + { |
| 88 | + BigInteger m = 1; |
| 89 | + BigInteger e = 0; |
| 90 | + |
| 91 | + while (BigInteger.ModPow(b, m, p) != 1) |
| 92 | + { |
| 93 | + m *= 2; |
| 94 | + e++; |
| 95 | + } |
| 96 | + |
| 97 | + return e; |
| 98 | + } |
| 99 | + |
| 100 | + private static BigInteger TwoExp(BigInteger exp) |
| 101 | + { |
| 102 | + return BigInteger.Pow(2, (int)exp); |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | +} |
0 commit comments