public override ECPoint Twice() { if (base.IsInfinity) { return(this); } ECCurve curve = Curve; SecP256K1FieldElement secP256K1FieldElement = (SecP256K1FieldElement)base.RawYCoord; if (secP256K1FieldElement.IsZero) { return(curve.Infinity); } SecP256K1FieldElement secP256K1FieldElement2 = (SecP256K1FieldElement)base.RawXCoord; SecP256K1FieldElement secP256K1FieldElement3 = (SecP256K1FieldElement)base.RawZCoords[0]; uint[] array = Nat256.Create(); SecP256K1Field.Square(secP256K1FieldElement.x, array); uint[] array2 = Nat256.Create(); SecP256K1Field.Square(array, array2); uint[] array3 = Nat256.Create(); SecP256K1Field.Square(secP256K1FieldElement2.x, array3); uint x = Nat256.AddBothTo(array3, array3, array3); SecP256K1Field.Reduce32(x, array3); uint[] array4 = array; SecP256K1Field.Multiply(array, secP256K1FieldElement2.x, array4); x = Nat.ShiftUpBits(8, array4, 2, 0u); SecP256K1Field.Reduce32(x, array4); uint[] array5 = Nat256.Create(); x = Nat.ShiftUpBits(8, array2, 3, 0u, array5); SecP256K1Field.Reduce32(x, array5); SecP256K1FieldElement secP256K1FieldElement4 = new SecP256K1FieldElement(array2); SecP256K1Field.Square(array3, secP256K1FieldElement4.x); SecP256K1Field.Subtract(secP256K1FieldElement4.x, array4, secP256K1FieldElement4.x); SecP256K1Field.Subtract(secP256K1FieldElement4.x, array4, secP256K1FieldElement4.x); SecP256K1FieldElement secP256K1FieldElement5 = new SecP256K1FieldElement(array4); SecP256K1Field.Subtract(array4, secP256K1FieldElement4.x, secP256K1FieldElement5.x); SecP256K1Field.Multiply(secP256K1FieldElement5.x, array3, secP256K1FieldElement5.x); SecP256K1Field.Subtract(secP256K1FieldElement5.x, array5, secP256K1FieldElement5.x); SecP256K1FieldElement secP256K1FieldElement6 = new SecP256K1FieldElement(array3); SecP256K1Field.Twice(secP256K1FieldElement.x, secP256K1FieldElement6.x); if (!secP256K1FieldElement3.IsOne) { SecP256K1Field.Multiply(secP256K1FieldElement6.x, secP256K1FieldElement3.x, secP256K1FieldElement6.x); } return(new SecP256K1Point(curve, secP256K1FieldElement4, secP256K1FieldElement5, new ECFieldElement[1] { secP256K1FieldElement6 }, base.IsCompressed)); }
public override ECFieldElement Sqrt() { uint[] y = x; if (Nat256.IsZero(y) || Nat256.IsOne(y)) { return(this); } uint[] array = Nat256.Create(); SecP256K1Field.Square(y, array); SecP256K1Field.Multiply(array, y, array); uint[] array2 = Nat256.Create(); SecP256K1Field.Square(array, array2); SecP256K1Field.Multiply(array2, y, array2); uint[] array3 = Nat256.Create(); SecP256K1Field.SquareN(array2, 3, array3); SecP256K1Field.Multiply(array3, array2, array3); uint[] array4 = array3; SecP256K1Field.SquareN(array3, 3, array4); SecP256K1Field.Multiply(array4, array2, array4); uint[] array5 = array4; SecP256K1Field.SquareN(array4, 2, array5); SecP256K1Field.Multiply(array5, array, array5); uint[] array6 = Nat256.Create(); SecP256K1Field.SquareN(array5, 11, array6); SecP256K1Field.Multiply(array6, array5, array6); uint[] array7 = array5; SecP256K1Field.SquareN(array6, 22, array7); SecP256K1Field.Multiply(array7, array6, array7); uint[] array8 = Nat256.Create(); SecP256K1Field.SquareN(array7, 44, array8); SecP256K1Field.Multiply(array8, array7, array8); uint[] z = Nat256.Create(); SecP256K1Field.SquareN(array8, 88, z); SecP256K1Field.Multiply(z, array8, z); uint[] z2 = array8; SecP256K1Field.SquareN(z, 44, z2); SecP256K1Field.Multiply(z2, array7, z2); uint[] array9 = array7; SecP256K1Field.SquareN(z2, 3, array9); SecP256K1Field.Multiply(array9, array2, array9); uint[] z3 = array9; SecP256K1Field.SquareN(z3, 23, z3); SecP256K1Field.Multiply(z3, array6, z3); SecP256K1Field.SquareN(z3, 6, z3); SecP256K1Field.Multiply(z3, array, z3); SecP256K1Field.SquareN(z3, 2, z3); uint[] array10 = array; SecP256K1Field.Square(z3, array10); if (!Nat256.Eq(y, array10)) { return(null); } return(new SecP256K1FieldElement(z3)); }
public override ECPoint Add(ECPoint b) { if (base.IsInfinity) { return(b); } if (b.IsInfinity) { return(this); } if (this == b) { return(Twice()); } ECCurve curve = Curve; SecP256K1FieldElement secP256K1FieldElement = (SecP256K1FieldElement)base.RawXCoord; SecP256K1FieldElement secP256K1FieldElement2 = (SecP256K1FieldElement)base.RawYCoord; SecP256K1FieldElement secP256K1FieldElement3 = (SecP256K1FieldElement)b.RawXCoord; SecP256K1FieldElement secP256K1FieldElement4 = (SecP256K1FieldElement)b.RawYCoord; SecP256K1FieldElement secP256K1FieldElement5 = (SecP256K1FieldElement)base.RawZCoords[0]; SecP256K1FieldElement secP256K1FieldElement6 = (SecP256K1FieldElement)b.RawZCoords[0]; uint[] array = Nat256.CreateExt(); uint[] array2 = Nat256.Create(); uint[] array3 = Nat256.Create(); uint[] array4 = Nat256.Create(); bool isOne = secP256K1FieldElement5.IsOne; uint[] array5; uint[] array6; if (isOne) { array5 = secP256K1FieldElement3.x; array6 = secP256K1FieldElement4.x; } else { array6 = array3; SecP256K1Field.Square(secP256K1FieldElement5.x, array6); array5 = array2; SecP256K1Field.Multiply(array6, secP256K1FieldElement3.x, array5); SecP256K1Field.Multiply(array6, secP256K1FieldElement5.x, array6); SecP256K1Field.Multiply(array6, secP256K1FieldElement4.x, array6); } bool isOne2 = secP256K1FieldElement6.IsOne; uint[] array7; uint[] array8; if (isOne2) { array7 = secP256K1FieldElement.x; array8 = secP256K1FieldElement2.x; } else { array8 = array4; SecP256K1Field.Square(secP256K1FieldElement6.x, array8); array7 = array; SecP256K1Field.Multiply(array8, secP256K1FieldElement.x, array7); SecP256K1Field.Multiply(array8, secP256K1FieldElement6.x, array8); SecP256K1Field.Multiply(array8, secP256K1FieldElement2.x, array8); } uint[] array9 = Nat256.Create(); SecP256K1Field.Subtract(array7, array5, array9); uint[] array10 = array2; SecP256K1Field.Subtract(array8, array6, array10); if (Nat256.IsZero(array9)) { if (Nat256.IsZero(array10)) { return(Twice()); } return(curve.Infinity); } uint[] array11 = array3; SecP256K1Field.Square(array9, array11); uint[] array12 = Nat256.Create(); SecP256K1Field.Multiply(array11, array9, array12); uint[] array13 = array3; SecP256K1Field.Multiply(array11, array7, array13); SecP256K1Field.Negate(array12, array12); Nat256.Mul(array8, array12, array); uint x = Nat256.AddBothTo(array13, array13, array12); SecP256K1Field.Reduce32(x, array12); SecP256K1FieldElement secP256K1FieldElement7 = new SecP256K1FieldElement(array4); SecP256K1Field.Square(array10, secP256K1FieldElement7.x); SecP256K1Field.Subtract(secP256K1FieldElement7.x, array12, secP256K1FieldElement7.x); SecP256K1FieldElement secP256K1FieldElement8 = new SecP256K1FieldElement(array12); SecP256K1Field.Subtract(array13, secP256K1FieldElement7.x, secP256K1FieldElement8.x); SecP256K1Field.MultiplyAddToExt(secP256K1FieldElement8.x, array10, array); SecP256K1Field.Reduce(array, secP256K1FieldElement8.x); SecP256K1FieldElement secP256K1FieldElement9 = new SecP256K1FieldElement(array9); if (!isOne) { SecP256K1Field.Multiply(secP256K1FieldElement9.x, secP256K1FieldElement5.x, secP256K1FieldElement9.x); } if (!isOne2) { SecP256K1Field.Multiply(secP256K1FieldElement9.x, secP256K1FieldElement6.x, secP256K1FieldElement9.x); } ECFieldElement[] zs = new ECFieldElement[1] { secP256K1FieldElement9 }; return(new SecP256K1Point(curve, secP256K1FieldElement7, secP256K1FieldElement8, zs, base.IsCompressed)); }
public override ECFieldElement Square() { uint[] z = Nat256.Create(); SecP256K1Field.Square(x, z); return(new SecP256K1FieldElement(z)); }