| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | |
| | | 4 | | using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Utilities; |
| | | 5 | | using Renci.SshNet.Security.Org.BouncyCastle.Security; |
| | | 6 | | |
| | | 7 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Math.Raw |
| | | 8 | | { |
| | | 9 | | internal abstract class Mod |
| | | 10 | | { |
| | 0 | 11 | | private static readonly SecureRandom RandomSource = new SecureRandom(); |
| | | 12 | | |
| | | 13 | | public static void Invert(uint[] p, uint[] x, uint[] z) |
| | 33 | 14 | | { |
| | 33 | 15 | | int len = p.Length; |
| | 33 | 16 | | if (Nat.IsZero(len, x)) |
| | 0 | 17 | | throw new ArgumentException("cannot be 0", "x"); |
| | 33 | 18 | | if (Nat.IsOne(len, x)) |
| | 0 | 19 | | { |
| | 0 | 20 | | Array.Copy(x, 0, z, 0, len); |
| | 0 | 21 | | return; |
| | | 22 | | } |
| | | 23 | | |
| | 33 | 24 | | uint[] u = Nat.Copy(len, x); |
| | 33 | 25 | | uint[] a = Nat.Create(len); |
| | 33 | 26 | | a[0] = 1; |
| | 33 | 27 | | int ac = 0; |
| | | 28 | | |
| | 33 | 29 | | if ((u[0] & 1) == 0) |
| | 16 | 30 | | { |
| | 16 | 31 | | InversionStep(p, u, len, a, ref ac); |
| | 16 | 32 | | } |
| | 33 | 33 | | if (Nat.IsOne(len, u)) |
| | 0 | 34 | | { |
| | 0 | 35 | | InversionResult(p, ac, a, z); |
| | 0 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | 33 | 39 | | uint[] v = Nat.Copy(len, p); |
| | 33 | 40 | | uint[] b = Nat.Create(len); |
| | 33 | 41 | | int bc = 0; |
| | | 42 | | |
| | 33 | 43 | | int uvLen = len; |
| | | 44 | | |
| | | 45 | | for (;;) |
| | 8947 | 46 | | { |
| | 9321 | 47 | | while (u[uvLen - 1] == 0 && v[uvLen - 1] == 0) |
| | 374 | 48 | | { |
| | 374 | 49 | | --uvLen; |
| | 374 | 50 | | } |
| | | 51 | | |
| | 8947 | 52 | | if (Nat.Gte(len, u, v)) |
| | 4393 | 53 | | { |
| | 4393 | 54 | | Nat.SubFrom(len, v, u); |
| | 4393 | 55 | | Debug.Assert((u[0] & 1) == 0); |
| | 4393 | 56 | | ac += Nat.SubFrom(len, b, a) - bc; |
| | 4393 | 57 | | InversionStep(p, u, uvLen, a, ref ac); |
| | 4393 | 58 | | if (Nat.IsOne(len, u)) |
| | 20 | 59 | | { |
| | 20 | 60 | | InversionResult(p, ac, a, z); |
| | 20 | 61 | | return; |
| | | 62 | | } |
| | 4373 | 63 | | } |
| | | 64 | | else |
| | 4554 | 65 | | { |
| | 4554 | 66 | | Nat.SubFrom(len, u, v); |
| | 4554 | 67 | | Debug.Assert((v[0] & 1) == 0); |
| | 4554 | 68 | | bc += Nat.SubFrom(len, a, b) - ac; |
| | 4554 | 69 | | InversionStep(p, v, uvLen, b, ref bc); |
| | 4554 | 70 | | if (Nat.IsOne(len, v)) |
| | 13 | 71 | | { |
| | 13 | 72 | | InversionResult(p, bc, b, z); |
| | 13 | 73 | | return; |
| | | 74 | | } |
| | 4541 | 75 | | } |
| | 8914 | 76 | | } |
| | 33 | 77 | | } |
| | | 78 | | |
| | | 79 | | public static uint[] Random(uint[] p) |
| | 0 | 80 | | { |
| | 0 | 81 | | int len = p.Length; |
| | 0 | 82 | | uint[] s = Nat.Create(len); |
| | | 83 | | |
| | 0 | 84 | | uint m = p[len - 1]; |
| | 0 | 85 | | m |= m >> 1; |
| | 0 | 86 | | m |= m >> 2; |
| | 0 | 87 | | m |= m >> 4; |
| | 0 | 88 | | m |= m >> 8; |
| | 0 | 89 | | m |= m >> 16; |
| | | 90 | | |
| | | 91 | | do |
| | 0 | 92 | | { |
| | 0 | 93 | | byte[] bytes = new byte[len << 2]; |
| | 0 | 94 | | RandomSource.NextBytes(bytes); |
| | 0 | 95 | | Pack.BE_To_UInt32(bytes, 0, s); |
| | 0 | 96 | | s[len - 1] &= m; |
| | 0 | 97 | | } |
| | 0 | 98 | | while (Nat.Gte(len, s, p)); |
| | | 99 | | |
| | 0 | 100 | | return s; |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | public static void Add(uint[] p, uint[] x, uint[] y, uint[] z) |
| | 0 | 104 | | { |
| | 0 | 105 | | int len = p.Length; |
| | 0 | 106 | | uint c = Nat.Add(len, x, y, z); |
| | 0 | 107 | | if (c != 0) |
| | 0 | 108 | | { |
| | 0 | 109 | | Nat.SubFrom(len, p, z); |
| | 0 | 110 | | } |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | public static void Subtract(uint[] p, uint[] x, uint[] y, uint[] z) |
| | 0 | 114 | | { |
| | 0 | 115 | | int len = p.Length; |
| | 0 | 116 | | int c = Nat.Sub(len, x, y, z); |
| | 0 | 117 | | if (c != 0) |
| | 0 | 118 | | { |
| | 0 | 119 | | Nat.AddTo(len, p, z); |
| | 0 | 120 | | } |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | private static void InversionResult(uint[] p, int ac, uint[] a, uint[] z) |
| | 33 | 124 | | { |
| | 33 | 125 | | if (ac < 0) |
| | 15 | 126 | | { |
| | 15 | 127 | | Nat.Add(p.Length, a, p, z); |
| | 15 | 128 | | } |
| | | 129 | | else |
| | 18 | 130 | | { |
| | 18 | 131 | | Array.Copy(a, 0, z, 0, p.Length); |
| | 18 | 132 | | } |
| | 33 | 133 | | } |
| | | 134 | | |
| | | 135 | | private static void InversionStep(uint[] p, uint[] u, int uLen, uint[] x, ref int xc) |
| | 8963 | 136 | | { |
| | 8963 | 137 | | int len = p.Length; |
| | 8963 | 138 | | int count = 0; |
| | 8963 | 139 | | while (u[0] == 0) |
| | 0 | 140 | | { |
| | 0 | 141 | | Nat.ShiftDownWord(uLen, u, 0); |
| | 0 | 142 | | count += 32; |
| | 0 | 143 | | } |
| | | 144 | | |
| | 8963 | 145 | | { |
| | 8963 | 146 | | int zeroes = GetTrailingZeroes(u[0]); |
| | 8963 | 147 | | if (zeroes > 0) |
| | 8963 | 148 | | { |
| | 8963 | 149 | | Nat.ShiftDownBits(uLen, u, zeroes, 0); |
| | 8963 | 150 | | count += zeroes; |
| | 8963 | 151 | | } |
| | 8963 | 152 | | } |
| | | 153 | | |
| | 53744 | 154 | | for (int i = 0; i < count; ++i) |
| | 17909 | 155 | | { |
| | 17909 | 156 | | if ((x[0] & 1) != 0) |
| | 5552 | 157 | | { |
| | 5552 | 158 | | if (xc < 0) |
| | 2759 | 159 | | { |
| | 2759 | 160 | | xc += (int)Nat.AddTo(len, p, x); |
| | 2759 | 161 | | } |
| | | 162 | | else |
| | 2793 | 163 | | { |
| | 2793 | 164 | | xc += Nat.SubFrom(len, p, x); |
| | 2793 | 165 | | } |
| | 5552 | 166 | | } |
| | | 167 | | |
| | 17909 | 168 | | Debug.Assert(xc == 0 || xc == -1); |
| | 17909 | 169 | | Nat.ShiftDownBit(len, x, (uint)xc); |
| | 17909 | 170 | | } |
| | 8963 | 171 | | } |
| | | 172 | | |
| | | 173 | | private static int GetTrailingZeroes(uint x) |
| | 8963 | 174 | | { |
| | 8963 | 175 | | Debug.Assert(x != 0); |
| | 8963 | 176 | | int count = 0; |
| | 26872 | 177 | | while ((x & 1) == 0) |
| | 17909 | 178 | | { |
| | 17909 | 179 | | x >>= 1; |
| | 17909 | 180 | | ++count; |
| | 17909 | 181 | | } |
| | 8963 | 182 | | return count; |
| | 8963 | 183 | | } |
| | | 184 | | } |
| | | 185 | | } |