public void TestClearBit() { Assert.AreEqual(zero, zero.ClearBit(0)); Assert.AreEqual(zero, one.ClearBit(0)); Assert.AreEqual(two, two.ClearBit(0)); Assert.AreEqual(zero, zero.ClearBit(1)); Assert.AreEqual(one, one.ClearBit(1)); Assert.AreEqual(zero, two.ClearBit(1)); // TODO Tests for clearing bits in negative numbers // TODO Tests for clearing extended bits for (int i = 0; i < 10; ++i) { IBigInteger n = new BigInteger(128, _random); for (int j = 0; j < 10; ++j) { int pos = _random.Next(128); IBigInteger m = n.ClearBit(pos); bool test = m.ShiftRight(pos).Remainder(two).Equals(one); Assert.IsFalse(test); } } for (int i = 0; i < 100; ++i) { IBigInteger pow2 = one.ShiftLeft(i); IBigInteger minusPow2 = pow2.Negate(); Assert.AreEqual(zero, pow2.ClearBit(i)); Assert.AreEqual(minusPow2.ShiftLeft(1), minusPow2.ClearBit(i)); IBigInteger bigI = BigInteger.ValueOf(i); IBigInteger negI = bigI.Negate(); for (int j = 0; j < 10; ++j) { string data = "i=" + i + ", j=" + j; Assert.AreEqual(bigI.AndNot(one.ShiftLeft(j)), bigI.ClearBit(j), data); Assert.AreEqual(negI.AndNot(one.ShiftLeft(j)), negI.ClearBit(j), data); } } }
public void TestFlipBit() { for (int i = 0; i < 10; ++i) { IBigInteger a = new BigInteger(128, 0, _random); IBigInteger b = a; for (int x = 0; x < 100; ++x) { // Note: Intentionally greater than initial size int pos = _random.Next(256); a = a.FlipBit(pos); b = b.TestBit(pos) ? b.ClearBit(pos) : b.SetBit(pos); } Assert.AreEqual(a, b); } for (int i = 0; i < 100; ++i) { IBigInteger pow2 = one.ShiftLeft(i); IBigInteger minusPow2 = pow2.Negate(); Assert.AreEqual(zero, pow2.FlipBit(i)); Assert.AreEqual(minusPow2.ShiftLeft(1), minusPow2.FlipBit(i)); IBigInteger bigI = BigInteger.ValueOf(i); IBigInteger negI = bigI.Negate(); for (int j = 0; j < 10; ++j) { string data = "i=" + i + ", j=" + j; Assert.AreEqual(bigI.Xor(one.ShiftLeft(j)), bigI.FlipBit(j), data); Assert.AreEqual(negI.Xor(one.ShiftLeft(j)), negI.FlipBit(j), data); } } }
/** * Computes the <code>τ</code>-adic NAF (non-adjacent form) of an * element <code>λ</code> of <code><b>Z</b>[τ]</code>. * @param mu The parameter <code>μ</code> of the elliptic curve. * @param lambda The element <code>λ</code> of * <code><b>Z</b>[τ]</code>. * @return The <code>τ</code>-adic NAF of <code>λ</code>. */ public static sbyte[] TauAdicNaf(sbyte mu, ZTauElement lambda) { if (!((mu == 1) || (mu == -1))) { throw new ArgumentException("mu must be 1 or -1"); } IBigInteger norm = Norm(mu, lambda); // Ceiling of log2 of the norm int log2Norm = norm.BitLength; // If length(TNAF) > 30, then length(TNAF) < log2Norm + 3.52 int maxLength = log2Norm > 30 ? log2Norm + 4 : 34; // The array holding the TNAF sbyte[] u = new sbyte[maxLength]; int i = 0; // The actual length of the TNAF int length = 0; IBigInteger r0 = lambda.u; IBigInteger r1 = lambda.v; while (!((r0.Equals(BigInteger.Zero)) && (r1.Equals(BigInteger.Zero)))) { // If r0 is odd if (r0.TestBit(0)) { u[i] = (sbyte)BigInteger.Two.Subtract((r0.Subtract(r1.ShiftLeft(1))).Mod(Four)).IntValue; // r0 = r0 - u[i] if (u[i] == 1) { r0 = r0.ClearBit(0); } else { // u[i] == -1 r0 = r0.Add(BigInteger.One); } length = i; } else { u[i] = 0; } IBigInteger t = r0; IBigInteger s = r0.ShiftRight(1); if (mu == 1) { r0 = r1.Add(s); } else { // mu == -1 r0 = r1.Subtract(s); } r1 = t.ShiftRight(1).Negate(); i++; } length++; // Reduce the TNAF array to its actual length sbyte[] tnaf = new sbyte[length]; Array.Copy(u, 0, tnaf, 0, length); return(tnaf); }