Exemplo n.º 1
0
 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 ECFieldElement Sqrt()
 {
     uint[] x = this.x;
     if (Nat256.IsZero(x) || Nat256.IsOne(x))
     {
         return(this);
     }
     uint[] z = Nat256.Create();
     SecP256K1Field.Square(x, z);
     SecP256K1Field.Multiply(z, x, z);
     uint[] numArray3 = Nat256.Create();
     SecP256K1Field.Square(z, numArray3);
     SecP256K1Field.Multiply(numArray3, x, numArray3);
     uint[] numArray4 = Nat256.Create();
     SecP256K1Field.SquareN(numArray3, 3, numArray4);
     SecP256K1Field.Multiply(numArray4, numArray3, numArray4);
     uint[] numArray5 = numArray4;
     SecP256K1Field.SquareN(numArray4, 3, numArray5);
     SecP256K1Field.Multiply(numArray5, numArray3, numArray5);
     uint[] numArray6 = numArray5;
     SecP256K1Field.SquareN(numArray5, 2, numArray6);
     SecP256K1Field.Multiply(numArray6, z, numArray6);
     uint[] numArray7 = Nat256.Create();
     SecP256K1Field.SquareN(numArray6, 11, numArray7);
     SecP256K1Field.Multiply(numArray7, numArray6, numArray7);
     uint[] numArray8 = numArray6;
     SecP256K1Field.SquareN(numArray7, 0x16, numArray8);
     SecP256K1Field.Multiply(numArray8, numArray7, numArray8);
     uint[] numArray9 = Nat256.Create();
     SecP256K1Field.SquareN(numArray8, 0x2c, numArray9);
     SecP256K1Field.Multiply(numArray9, numArray8, numArray9);
     uint[] numArray10 = Nat256.Create();
     SecP256K1Field.SquareN(numArray9, 0x58, numArray10);
     SecP256K1Field.Multiply(numArray10, numArray9, numArray10);
     uint[] numArray11 = numArray9;
     SecP256K1Field.SquareN(numArray10, 0x2c, numArray11);
     SecP256K1Field.Multiply(numArray11, numArray8, numArray11);
     uint[] numArray12 = numArray8;
     SecP256K1Field.SquareN(numArray11, 3, numArray12);
     SecP256K1Field.Multiply(numArray12, numArray3, numArray12);
     uint[] numArray13 = numArray12;
     SecP256K1Field.SquareN(numArray13, 0x17, numArray13);
     SecP256K1Field.Multiply(numArray13, numArray7, numArray13);
     SecP256K1Field.SquareN(numArray13, 6, numArray13);
     SecP256K1Field.Multiply(numArray13, z, numArray13);
     SecP256K1Field.SquareN(numArray13, 2, numArray13);
     uint[] numArray14 = z;
     SecP256K1Field.Square(numArray13, numArray14);
     return(!Nat256.Eq(x, numArray14) ? null : new SecP256K1FieldElement(numArray13));
 }
Exemplo n.º 3
0
        /**
         * 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^254 - 2^30 - 2^7 - 2^6 - 2^5 - 2^4 - 2^2
             *
             * Breaking up the exponent's binary representation into "repunits", we get:
             * { 223 1s } { 1 0s } { 22 1s } { 4 0s } { 2 1s } { 2 0s}
             *
             * Therefore we need an addition chain containing 2, 22, 223 (the lengths of the repunits)
             * We use: 1, [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
             */

            uint[] x1 = this.x;
            if (Nat256.IsZero(x1) || Nat256.IsOne(x1))
            {
                return(this);
            }

            uint[] x2 = Nat256.Create();
            SecP256K1Field.Square(x1, x2);
            SecP256K1Field.Multiply(x2, x1, x2);
            uint[] x3 = Nat256.Create();
            SecP256K1Field.Square(x2, x3);
            SecP256K1Field.Multiply(x3, x1, x3);
            uint[] x6 = Nat256.Create();
            SecP256K1Field.SquareN(x3, 3, x6);
            SecP256K1Field.Multiply(x6, x3, x6);
            uint[] x9 = x6;
            SecP256K1Field.SquareN(x6, 3, x9);
            SecP256K1Field.Multiply(x9, x3, x9);
            uint[] x11 = x9;
            SecP256K1Field.SquareN(x9, 2, x11);
            SecP256K1Field.Multiply(x11, x2, x11);
            uint[] x22 = Nat256.Create();
            SecP256K1Field.SquareN(x11, 11, x22);
            SecP256K1Field.Multiply(x22, x11, x22);
            uint[] x44 = x11;
            SecP256K1Field.SquareN(x22, 22, x44);
            SecP256K1Field.Multiply(x44, x22, x44);
            uint[] x88 = Nat256.Create();
            SecP256K1Field.SquareN(x44, 44, x88);
            SecP256K1Field.Multiply(x88, x44, x88);
            uint[] x176 = Nat256.Create();
            SecP256K1Field.SquareN(x88, 88, x176);
            SecP256K1Field.Multiply(x176, x88, x176);
            uint[] x220 = x88;
            SecP256K1Field.SquareN(x176, 44, x220);
            SecP256K1Field.Multiply(x220, x44, x220);
            uint[] x223 = x44;
            SecP256K1Field.SquareN(x220, 3, x223);
            SecP256K1Field.Multiply(x223, x3, x223);

            uint[] t1 = x223;
            SecP256K1Field.SquareN(t1, 23, t1);
            SecP256K1Field.Multiply(t1, x22, t1);
            SecP256K1Field.SquareN(t1, 6, t1);
            SecP256K1Field.Multiply(t1, x2, t1);
            SecP256K1Field.SquareN(t1, 2, t1);

            uint[] t2 = x2;
            SecP256K1Field.Square(t1, t2);

            return(Nat256.Eq(x1, t2) ? new SecP256K1FieldElement(t1) : null);
        }