Ejemplo n.º 1
0
 public override ECFieldElement Sqrt()
 {
     uint[] y = x;
     if (Nat256.IsZero(y) || Nat256.IsOne(y))
     {
         return(this);
     }
     uint[] array  = Nat256.Create();
     uint[] array2 = Nat256.Create();
     SecP256R1Field.Square(y, array);
     SecP256R1Field.Multiply(array, y, array);
     SecP256R1Field.SquareN(array, 2, array2);
     SecP256R1Field.Multiply(array2, array, array2);
     SecP256R1Field.SquareN(array2, 4, array);
     SecP256R1Field.Multiply(array, array2, array);
     SecP256R1Field.SquareN(array, 8, array2);
     SecP256R1Field.Multiply(array2, array, array2);
     SecP256R1Field.SquareN(array2, 16, array);
     SecP256R1Field.Multiply(array, array2, array);
     SecP256R1Field.SquareN(array, 32, array);
     SecP256R1Field.Multiply(array, y, array);
     SecP256R1Field.SquareN(array, 96, array);
     SecP256R1Field.Multiply(array, y, array);
     SecP256R1Field.SquareN(array, 94, array);
     SecP256R1Field.Multiply(array, array, array2);
     return((!Nat256.Eq(y, array2)) ? null : new SecP256R1FieldElement(array));
 }
 public override ECFieldElement Sqrt()
 {
     uint[] x = this.x;
     if (Nat256.IsZero(x) || Nat256.IsOne(x))
     {
         return(this);
     }
     uint[] z         = Nat256.Create();
     uint[] numArray3 = Nat256.Create();
     SecP256R1Field.Square(x, z);
     SecP256R1Field.Multiply(z, x, z);
     SecP256R1Field.SquareN(z, 2, numArray3);
     SecP256R1Field.Multiply(numArray3, z, numArray3);
     SecP256R1Field.SquareN(numArray3, 4, z);
     SecP256R1Field.Multiply(z, numArray3, z);
     SecP256R1Field.SquareN(z, 8, numArray3);
     SecP256R1Field.Multiply(numArray3, z, numArray3);
     SecP256R1Field.SquareN(numArray3, 0x10, z);
     SecP256R1Field.Multiply(z, numArray3, z);
     SecP256R1Field.SquareN(z, 0x20, z);
     SecP256R1Field.Multiply(z, x, z);
     SecP256R1Field.SquareN(z, 0x60, z);
     SecP256R1Field.Multiply(z, x, z);
     SecP256R1Field.SquareN(z, 0x5e, z);
     SecP256R1Field.Multiply(z, z, numArray3);
     return(!Nat256.Eq(x, numArray3) ? null : new SecP256R1FieldElement(z));
 }
Ejemplo 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^222 + 2^190 + 2^94

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

            uint[] t1 = Nat256.Create();
            uint[] t2 = Nat256.Create();

            SecP256R1Field.Square(x1, t1);
            SecP256R1Field.Multiply(t1, x1, t1);

            SecP256R1Field.SquareN(t1, 2, t2);
            SecP256R1Field.Multiply(t2, t1, t2);

            SecP256R1Field.SquareN(t2, 4, t1);
            SecP256R1Field.Multiply(t1, t2, t1);

            SecP256R1Field.SquareN(t1, 8, t2);
            SecP256R1Field.Multiply(t2, t1, t2);

            SecP256R1Field.SquareN(t2, 16, t1);
            SecP256R1Field.Multiply(t1, t2, t1);

            SecP256R1Field.SquareN(t1, 32, t1);
            SecP256R1Field.Multiply(t1, x1, t1);

            SecP256R1Field.SquareN(t1, 96, t1);
            SecP256R1Field.Multiply(t1, x1, t1);

            SecP256R1Field.SquareN(t1, 94, t1);
            SecP256R1Field.Multiply(t1, t1, t2);

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