private void TestAddModFn() { Random rng = new Random(); int n = 5 + rng.Next(10); int len = 1 << (n + 1 - 5); int[] aArr = new int[len]; for (int i = 0; i < aArr.Length; i++) { aArr[i] = rng.Next(); } BigInteger a = SchonhageStrassen.ToBigInteger(aArr); int[] bArr = new int[len]; for (int i = 0; i < bArr.Length; i++) { bArr[i] = rng.Next(); } BigInteger b = SchonhageStrassen.ToBigInteger(bArr); SchonhageStrassen.AddModFn(aArr, bArr); SchonhageStrassen.ModFn(aArr); BigInteger Fn = BigInteger.ValueOf(2).Pow(1 << n).Add(BigInteger.One); BigInteger c = a.Add(b).Mod(Fn); if (!Compare.Equals(c, SchonhageStrassen.ToBigInteger(aArr))) { throw new Exception("SchönhageStrassen:AddModFn test has failed!"); } }
private void TestModFn() { int[] a = new int[] { 50593286, 151520511 }; SchonhageStrassen.ModFn(a); if (!Compare.AreEqual(new int[] { -100927224, 0 }, a)) { throw new Exception("SchönhageStrassen:TestModFn test has failed!"); } a = new int[] { 1157041776, -1895306073, -1094584616, -218513495 }; SchonhageStrassen.ModFn(a); if (!Compare.AreEqual(new int[] { -2043340903, -1676792579, 0, 0 }, a)) { throw new Exception("SchönhageStrassen:TestModFn test has failed!"); } }
//verifies idft(dft(a)) = a private void TestInversion() { Random rng = new Random(); int m = 7 + rng.Next(10); int n = m / 2 + 1; int numElements = m % 2 == 0 ? 1 << n : 1 << (n + 1); numElements /= 2; int[][] a = ArrayUtils.CreateJagged <int[][]>(numElements, 1 << (n + 1 - 5)); for (int i = 0; i < a.Length; i++) { for (int j = 0; j < a[i].Length; j++) { a[i][j] = rng.Next(); } } SchonhageStrassen.ModFn(a); int[][] aOrig = new int[a.Length][]; for (int i = 0; i < a.Length; i++) { aOrig[i] = (int[])a[i].Clone(); } SchonhageStrassen.Dft(a, m, n); SchonhageStrassen.Idft(a, m, n); SchonhageStrassen.ModFn(a); for (int j = 0; j < aOrig.Length; j++) { if (!Compare.AreEqual(aOrig[j], a[j])) { throw new Exception("SchönhageStrassen:Inversion test has failed!"); } } }