//windowed naf form of BigInt k, w is the window size sbyte[] Naf(BigInt k, byte w) { // The window NAF is at most 1 element longer than the binary // representation of the integer k. byte can be used instead of short or // int unless the window width is larger than 8. For larger width use // short or int. However, a width of more than 8 is not efficient for // m = log2(q) smaller than 2305 Bits. Note: Values for m larger than // 1000 Bits are currently not used in practice. sbyte[] wnaf = new sbyte[k.BitLength() + 1]; // 2^width as short and BigInteger short pow2wB = (short)(1 << w); BigInt pow2wBI = BigInt.ValueOf(pow2wB); int i = 0; // The actual length of the WNAF int length = 0; // while k >= 1 while (k.Signum() > 0) { // if k is odd if (k.TestBit(0)) { // k mod 2^width BigInt remainder = k.Mod(pow2wBI); // if remainder > 2^(width - 1) - 1 if (remainder.TestBit(w - 1)) { wnaf[i] = (sbyte)(remainder.IntValue() - pow2wB); } else { wnaf[i] = (sbyte)remainder.IntValue(); } // wnaf[i] is now in [-2^(width-1), 2^(width-1)-1] k = k.Subtract(BigInt.ValueOf(wnaf[i])); length = i; } else { wnaf[i] = 0; } // k = k/2 k = k.ShiftRight(1); i++; } length++; // Reduce the WNAF array to its actual length sbyte[] wnafShort = new sbyte[length]; Array.Copy(wnaf, 0, wnafShort, 0, length); return(wnafShort); }
//multiplication using Jacobian coordinates public JacobPoint JMultiplyMut(Point p, BigInt k) { if (!(this.field.IsValidElement(p.X)) || !(this.field.IsValidElement(p.Y))) { throw new ArgumentException("The input point must be taken over the field."); } if (p.IsInfinity()) { return(this.AToJ(p)); } if (k.Equals(BigInt.ZERO)) { return(JacobPoint.INFINITY); } if (k.Equals(BigInt.ONE)) { return(this.AToJ(p)); } if (k.Signum() == -1) { k = k.Abs(); p = this.Negate(p); } //byte [] ba =k.toByteArray(); int degree = k.BitLength() - 2; JacobPoint result = this.AToJ(p); for (int i = degree; i >= 0; i--) { this.JDblMut(result); if (k.TestBit(i)) ///AQUI TE QUEDASTE IMPLEMENTAR TESTBIT { this.JAddMut(result, p); } } return(result); }
public void BitLengthTest() { BigInt a = new BigInt(123456); Assert.AreEqual(17, a.BitLength()); }