// 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);
        }
Exemplo n.º 2
0
 public override ECFieldElement Sqrt()
 {
     uint[] y = x;
     if (Nat160.IsZero(y) || Nat160.IsOne(y))
     {
         return(this);
     }
     uint[] array = Nat160.Create();
     SecP160R1Field.Square(y, array);
     SecP160R1Field.Multiply(array, y, array);
     uint[] array2 = Nat160.Create();
     SecP160R1Field.SquareN(array, 2, array2);
     SecP160R1Field.Multiply(array2, array, array2);
     uint[] array3 = array;
     SecP160R1Field.SquareN(array2, 4, array3);
     SecP160R1Field.Multiply(array3, array2, array3);
     uint[] array4 = array2;
     SecP160R1Field.SquareN(array3, 8, array4);
     SecP160R1Field.Multiply(array4, array3, array4);
     uint[] array5 = array3;
     SecP160R1Field.SquareN(array4, 16, array5);
     SecP160R1Field.Multiply(array5, array4, array5);
     uint[] array6 = array4;
     SecP160R1Field.SquareN(array5, 32, array6);
     SecP160R1Field.Multiply(array6, array5, array6);
     uint[] array7 = array5;
     SecP160R1Field.SquareN(array6, 64, array7);
     SecP160R1Field.Multiply(array7, array6, array7);
     uint[] array8 = array6;
     SecP160R1Field.Square(array7, array8);
     SecP160R1Field.Multiply(array8, y, array8);
     uint[] z = array8;
     SecP160R1Field.SquareN(z, 29, z);
     uint[] array9 = array7;
     SecP160R1Field.Square(z, array9);
     if (!Nat160.Eq(y, array9))
     {
         return(null);
     }
     return(new SecP160R1FieldElement(z));
 }
Exemplo n.º 3
0
 public override ECFieldElement Sqrt()
 {
     uint[] x = this.x;
     if (Nat160.IsZero(x) || Nat160.IsOne(x))
     {
         return(this);
     }
     uint[] z = Nat160.Create();
     SecP160R1Field.Square(x, z);
     SecP160R1Field.Multiply(z, x, z);
     uint[] numArray3 = Nat160.Create();
     SecP160R1Field.SquareN(z, 2, numArray3);
     SecP160R1Field.Multiply(numArray3, z, numArray3);
     uint[] numArray4 = z;
     SecP160R1Field.SquareN(numArray3, 4, numArray4);
     SecP160R1Field.Multiply(numArray4, numArray3, numArray4);
     uint[] numArray5 = numArray3;
     SecP160R1Field.SquareN(numArray4, 8, numArray5);
     SecP160R1Field.Multiply(numArray5, numArray4, numArray5);
     uint[] numArray6 = numArray4;
     SecP160R1Field.SquareN(numArray5, 0x10, numArray6);
     SecP160R1Field.Multiply(numArray6, numArray5, numArray6);
     uint[] numArray7 = numArray5;
     SecP160R1Field.SquareN(numArray6, 0x20, numArray7);
     SecP160R1Field.Multiply(numArray7, numArray6, numArray7);
     uint[] numArray8 = numArray6;
     SecP160R1Field.SquareN(numArray7, 0x40, numArray8);
     SecP160R1Field.Multiply(numArray8, numArray7, numArray8);
     uint[] numArray9 = numArray7;
     SecP160R1Field.Square(numArray8, numArray9);
     SecP160R1Field.Multiply(numArray9, x, numArray9);
     uint[] numArray10 = numArray9;
     SecP160R1Field.SquareN(numArray10, 0x1d, numArray10);
     uint[] numArray11 = numArray8;
     SecP160R1Field.Square(numArray10, numArray11);
     return(!Nat160.Eq(x, numArray11) ? null : new SecP160R1FieldElement(numArray10));
 }