public override ECFieldElement Divide(ECFieldElement b) { uint[] z = Nat256.Create(); Mod.Invert(Curve25519Field.P, ((Curve25519FieldElement)b).x, z); Curve25519Field.Multiply(z, x, z); return(new Curve25519FieldElement(z)); }
public override ECFieldElement Sqrt() { uint[] y = x; if (Nat256.IsZero(y) || Nat256.IsOne(y)) { return(this); } uint[] array = Nat256.Create(); Curve25519Field.Square(y, array); Curve25519Field.Multiply(array, y, array); uint[] array2 = array; Curve25519Field.Square(array, array2); Curve25519Field.Multiply(array2, y, array2); uint[] array3 = Nat256.Create(); Curve25519Field.Square(array2, array3); Curve25519Field.Multiply(array3, y, array3); uint[] array4 = Nat256.Create(); Curve25519Field.SquareN(array3, 3, array4); Curve25519Field.Multiply(array4, array2, array4); uint[] array5 = array2; Curve25519Field.SquareN(array4, 4, array5); Curve25519Field.Multiply(array5, array3, array5); uint[] array6 = array4; Curve25519Field.SquareN(array5, 4, array6); Curve25519Field.Multiply(array6, array3, array6); uint[] array7 = array3; Curve25519Field.SquareN(array6, 15, array7); Curve25519Field.Multiply(array7, array6, array7); uint[] array8 = array6; Curve25519Field.SquareN(array7, 30, array8); Curve25519Field.Multiply(array8, array7, array8); uint[] array9 = array7; Curve25519Field.SquareN(array8, 60, array9); Curve25519Field.Multiply(array9, array8, array9); uint[] z = array8; Curve25519Field.SquareN(array9, 11, z); Curve25519Field.Multiply(z, array5, z); uint[] array10 = array5; Curve25519Field.SquareN(z, 120, array10); Curve25519Field.Multiply(array10, array9, array10); uint[] z2 = array10; Curve25519Field.Square(z2, z2); uint[] array11 = array9; Curve25519Field.Square(z2, array11); if (Nat256.Eq(y, array11)) { return(new Curve25519FieldElement(z2)); } Curve25519Field.Multiply(z2, PRECOMP_POW2, z2); Curve25519Field.Square(z2, array11); if (Nat256.Eq(y, array11)) { return(new Curve25519FieldElement(z2)); } return(null); }
protected virtual Curve25519Point TwiceJacobianModified(bool calculateW) { Curve25519FieldElement curve25519FieldElement = (Curve25519FieldElement)base.RawXCoord; Curve25519FieldElement curve25519FieldElement2 = (Curve25519FieldElement)base.RawYCoord; Curve25519FieldElement curve25519FieldElement3 = (Curve25519FieldElement)base.RawZCoords[0]; Curve25519FieldElement jacobianModifiedW = GetJacobianModifiedW(); uint[] array = Nat256.Create(); Curve25519Field.Square(curve25519FieldElement.x, array); uint num = Nat256.AddBothTo(array, array, array); num += Nat256.AddTo(jacobianModifiedW.x, array); Curve25519Field.Reduce27(num, array); uint[] array2 = Nat256.Create(); Curve25519Field.Twice(curve25519FieldElement2.x, array2); uint[] array3 = Nat256.Create(); Curve25519Field.Multiply(array2, curve25519FieldElement2.x, array3); uint[] array4 = Nat256.Create(); Curve25519Field.Multiply(array3, curve25519FieldElement.x, array4); Curve25519Field.Twice(array4, array4); uint[] array5 = Nat256.Create(); Curve25519Field.Square(array3, array5); Curve25519Field.Twice(array5, array5); Curve25519FieldElement curve25519FieldElement4 = new Curve25519FieldElement(array3); Curve25519Field.Square(array, curve25519FieldElement4.x); Curve25519Field.Subtract(curve25519FieldElement4.x, array4, curve25519FieldElement4.x); Curve25519Field.Subtract(curve25519FieldElement4.x, array4, curve25519FieldElement4.x); Curve25519FieldElement curve25519FieldElement5 = new Curve25519FieldElement(array4); Curve25519Field.Subtract(array4, curve25519FieldElement4.x, curve25519FieldElement5.x); Curve25519Field.Multiply(curve25519FieldElement5.x, array, curve25519FieldElement5.x); Curve25519Field.Subtract(curve25519FieldElement5.x, array5, curve25519FieldElement5.x); Curve25519FieldElement curve25519FieldElement6 = new Curve25519FieldElement(array2); if (!Nat256.IsOne(curve25519FieldElement3.x)) { Curve25519Field.Multiply(curve25519FieldElement6.x, curve25519FieldElement3.x, curve25519FieldElement6.x); } Curve25519FieldElement curve25519FieldElement7 = null; if (calculateW) { curve25519FieldElement7 = new Curve25519FieldElement(array5); Curve25519Field.Multiply(curve25519FieldElement7.x, jacobianModifiedW.x, curve25519FieldElement7.x); Curve25519Field.Twice(curve25519FieldElement7.x, curve25519FieldElement7.x); } return(new Curve25519Point(Curve, curve25519FieldElement4, curve25519FieldElement5, new ECFieldElement[2] { curve25519FieldElement6, curve25519FieldElement7 }, base.IsCompressed)); }
protected virtual Curve25519FieldElement CalculateJacobianModifiedW(Curve25519FieldElement Z, uint[] ZSquared) { Curve25519FieldElement curve25519FieldElement = (Curve25519FieldElement)Curve.A; if (Z.IsOne) { return(curve25519FieldElement); } Curve25519FieldElement curve25519FieldElement2 = new Curve25519FieldElement(); if (ZSquared == null) { ZSquared = curve25519FieldElement2.x; Curve25519Field.Square(Z.x, ZSquared); } Curve25519Field.Square(ZSquared, curve25519FieldElement2.x); Curve25519Field.Multiply(curve25519FieldElement2.x, curve25519FieldElement.x, curve25519FieldElement2.x); return(curve25519FieldElement2); }
public override ECPoint Add(ECPoint b) { if (base.IsInfinity) { return(b); } if (b.IsInfinity) { return(this); } if (this == b) { return(Twice()); } ECCurve curve = Curve; Curve25519FieldElement curve25519FieldElement = (Curve25519FieldElement)base.RawXCoord; Curve25519FieldElement curve25519FieldElement2 = (Curve25519FieldElement)base.RawYCoord; Curve25519FieldElement curve25519FieldElement3 = (Curve25519FieldElement)base.RawZCoords[0]; Curve25519FieldElement curve25519FieldElement4 = (Curve25519FieldElement)b.RawXCoord; Curve25519FieldElement curve25519FieldElement5 = (Curve25519FieldElement)b.RawYCoord; Curve25519FieldElement curve25519FieldElement6 = (Curve25519FieldElement)b.RawZCoords[0]; uint[] array = Nat256.CreateExt(); uint[] array2 = Nat256.Create(); uint[] array3 = Nat256.Create(); uint[] array4 = Nat256.Create(); bool isOne = curve25519FieldElement3.IsOne; uint[] array5; uint[] array6; if (isOne) { array5 = curve25519FieldElement4.x; array6 = curve25519FieldElement5.x; } else { array6 = array3; Curve25519Field.Square(curve25519FieldElement3.x, array6); array5 = array2; Curve25519Field.Multiply(array6, curve25519FieldElement4.x, array5); Curve25519Field.Multiply(array6, curve25519FieldElement3.x, array6); Curve25519Field.Multiply(array6, curve25519FieldElement5.x, array6); } bool isOne2 = curve25519FieldElement6.IsOne; uint[] array7; uint[] array8; if (isOne2) { array7 = curve25519FieldElement.x; array8 = curve25519FieldElement2.x; } else { array8 = array4; Curve25519Field.Square(curve25519FieldElement6.x, array8); array7 = array; Curve25519Field.Multiply(array8, curve25519FieldElement.x, array7); Curve25519Field.Multiply(array8, curve25519FieldElement6.x, array8); Curve25519Field.Multiply(array8, curve25519FieldElement2.x, array8); } uint[] array9 = Nat256.Create(); Curve25519Field.Subtract(array7, array5, array9); uint[] array10 = array2; Curve25519Field.Subtract(array8, array6, array10); if (Nat256.IsZero(array9)) { if (Nat256.IsZero(array10)) { return(Twice()); } return(curve.Infinity); } uint[] array11 = Nat256.Create(); Curve25519Field.Square(array9, array11); uint[] array12 = Nat256.Create(); Curve25519Field.Multiply(array11, array9, array12); uint[] array13 = array3; Curve25519Field.Multiply(array11, array7, array13); Curve25519Field.Negate(array12, array12); Nat256.Mul(array8, array12, array); uint x = Nat256.AddBothTo(array13, array13, array12); Curve25519Field.Reduce27(x, array12); Curve25519FieldElement curve25519FieldElement7 = new Curve25519FieldElement(array4); Curve25519Field.Square(array10, curve25519FieldElement7.x); Curve25519Field.Subtract(curve25519FieldElement7.x, array12, curve25519FieldElement7.x); Curve25519FieldElement curve25519FieldElement8 = new Curve25519FieldElement(array12); Curve25519Field.Subtract(array13, curve25519FieldElement7.x, curve25519FieldElement8.x); Curve25519Field.MultiplyAddToExt(curve25519FieldElement8.x, array10, array); Curve25519Field.Reduce(array, curve25519FieldElement8.x); Curve25519FieldElement curve25519FieldElement9 = new Curve25519FieldElement(array9); if (!isOne) { Curve25519Field.Multiply(curve25519FieldElement9.x, curve25519FieldElement3.x, curve25519FieldElement9.x); } if (!isOne2) { Curve25519Field.Multiply(curve25519FieldElement9.x, curve25519FieldElement6.x, curve25519FieldElement9.x); } uint[] zSquared = (isOne && isOne2) ? array11 : null; Curve25519FieldElement curve25519FieldElement10 = CalculateJacobianModifiedW(curve25519FieldElement9, zSquared); ECFieldElement[] zs = new ECFieldElement[2] { curve25519FieldElement9, curve25519FieldElement10 }; return(new Curve25519Point(curve, curve25519FieldElement7, curve25519FieldElement8, zs, base.IsCompressed)); }
public override ECFieldElement Multiply(ECFieldElement b) { uint[] z = Nat256.Create(); Curve25519Field.Multiply(x, ((Curve25519FieldElement)b).x, z); return(new Curve25519FieldElement(z)); }