// D.1.4 91 /** * return a sqrt root - the routine verifies that the calculation returns the right value - if * none exists it returns null. */ public override ECFieldElement Sqrt() { /* * Raise this element to the exponent 2^158 - 2^29 * * Breaking up the exponent's binary representation into "repunits", we get: * { 129 1s } { 29 0s } * * Therefore we need an addition chain containing 129 (the length of the repunit) We use: * 1, 2, 4, 8, 16, 32, 64, 128, [129] */ uint[] x1 = this.x; if (Nat160.IsZero(x1) || Nat160.IsOne(x1)) { return(this); } uint[] x2 = Nat160.Create(); SecP160R1Field.Square(x1, x2); SecP160R1Field.Multiply(x2, x1, x2); uint[] x4 = Nat160.Create(); SecP160R1Field.SquareN(x2, 2, x4); SecP160R1Field.Multiply(x4, x2, x4); uint[] x8 = x2; SecP160R1Field.SquareN(x4, 4, x8); SecP160R1Field.Multiply(x8, x4, x8); uint[] x16 = x4; SecP160R1Field.SquareN(x8, 8, x16); SecP160R1Field.Multiply(x16, x8, x16); uint[] x32 = x8; SecP160R1Field.SquareN(x16, 16, x32); SecP160R1Field.Multiply(x32, x16, x32); uint[] x64 = x16; SecP160R1Field.SquareN(x32, 32, x64); SecP160R1Field.Multiply(x64, x32, x64); uint[] x128 = x32; SecP160R1Field.SquareN(x64, 64, x128); SecP160R1Field.Multiply(x128, x64, x128); uint[] x129 = x64; SecP160R1Field.Square(x128, x129); SecP160R1Field.Multiply(x129, x1, x129); uint[] t1 = x129; SecP160R1Field.SquareN(t1, 29, t1); uint[] t2 = x128; SecP160R1Field.Square(t1, t2); return(Nat160.Eq(x1, t2) ? new SecP160R1FieldElement(t1) : null); }