/**
         * return a sqrt root - the routine verifies that the calculation returns the right value - if
         * none exists it returns null.
         */
        public override ECFieldElement Sqrt()
        {
            uint[] c = this.x;
            if (Nat224.IsZero(c) || Nat224.IsOne(c))
            {
                return(this);
            }

            uint[] nc = Nat224.Create();
            SecP224R1Field.Negate(c, nc);

            uint[] r = Mod.Random(SecP224R1Field.P);
            uint[] t = Nat224.Create();

            if (!IsSquare(c))
            {
                return(null);
            }

            while (!TrySqrt(nc, r, t))
            {
                SecP224R1Field.AddOne(r, r);
            }

            SecP224R1Field.Square(t, r);

            return(Nat224.Eq(c, r) ? new SecP224R1FieldElement(t) : null);
        }
 public virtual bool Equals(SecP224R1FieldElement other)
 {
     if (this == other)
     {
         return(true);
     }
     if (null == other)
     {
         return(false);
     }
     return(Nat224.Eq(x, other.x));
 }
示例#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()
        {
            /*
             * Q == 8m + 5, so we use Pocklington's method for this case.
             *
             * First, raise this element to the exponent 2^221 - 2^29 - 2^9 - 2^8 - 2^6 - 2^4 - 2^1 (i.e. m + 1)
             *
             * Breaking up the exponent's binary representation into "repunits", we get:
             * { 191 1s } { 1 0s } { 19 1s } { 2 0s } { 1 1s } { 1 0s} { 1 1s } { 1 0s} { 3 1s } { 1 0s}
             *
             * Therefore we need an addition chain containing 1, 3, 19, 191 (the lengths of the repunits)
             * We use: [1], 2, [3], 4, 8, 11, [19], 23, 42, 84, 107, [191]
             */

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

            uint[] x2 = Nat224.Create();
            SecP224K1Field.Square(x1, x2);
            SecP224K1Field.Multiply(x2, x1, x2);
            uint[] x3 = x2;
            SecP224K1Field.Square(x2, x3);
            SecP224K1Field.Multiply(x3, x1, x3);
            uint[] x4 = Nat224.Create();
            SecP224K1Field.Square(x3, x4);
            SecP224K1Field.Multiply(x4, x1, x4);
            uint[] x8 = Nat224.Create();
            SecP224K1Field.SquareN(x4, 4, x8);
            SecP224K1Field.Multiply(x8, x4, x8);
            uint[] x11 = Nat224.Create();
            SecP224K1Field.SquareN(x8, 3, x11);
            SecP224K1Field.Multiply(x11, x3, x11);
            uint[] x19 = x11;
            SecP224K1Field.SquareN(x11, 8, x19);
            SecP224K1Field.Multiply(x19, x8, x19);
            uint[] x23 = x8;
            SecP224K1Field.SquareN(x19, 4, x23);
            SecP224K1Field.Multiply(x23, x4, x23);
            uint[] x42 = x4;
            SecP224K1Field.SquareN(x23, 19, x42);
            SecP224K1Field.Multiply(x42, x19, x42);
            uint[] x84 = Nat224.Create();
            SecP224K1Field.SquareN(x42, 42, x84);
            SecP224K1Field.Multiply(x84, x42, x84);
            uint[] x107 = x42;
            SecP224K1Field.SquareN(x84, 23, x107);
            SecP224K1Field.Multiply(x107, x23, x107);
            uint[] x191 = x23;
            SecP224K1Field.SquareN(x107, 84, x191);
            SecP224K1Field.Multiply(x191, x84, x191);

            uint[] t1 = x191;
            SecP224K1Field.SquareN(t1, 20, t1);
            SecP224K1Field.Multiply(t1, x19, t1);
            SecP224K1Field.SquareN(t1, 3, t1);
            SecP224K1Field.Multiply(t1, x1, t1);
            SecP224K1Field.SquareN(t1, 2, t1);
            SecP224K1Field.Multiply(t1, x1, t1);
            SecP224K1Field.SquareN(t1, 4, t1);
            SecP224K1Field.Multiply(t1, x3, t1);
            SecP224K1Field.Square(t1, t1);

            uint[] t2 = x84;
            SecP224K1Field.Square(t1, t2);

            if (Nat224.Eq(x1, t2))
            {
                return(new SecP224K1FieldElement(t1));
            }

            /*
             * If the first guess is incorrect, we multiply by a precomputed power of 2 to get the second guess,
             * which is ((4x)^(m + 1))/2 mod Q
             */
            SecP224K1Field.Multiply(t1, PRECOMP_POW2, t1);

            SecP224K1Field.Square(t1, t2);

            if (Nat224.Eq(x1, t2))
            {
                return(new SecP224K1FieldElement(t1));
            }

            return(null);
        }
示例#4
0
 public override ECFieldElement Sqrt()
 {
     uint[] y = x;
     if (Nat224.IsZero(y) || Nat224.IsOne(y))
     {
         return(this);
     }
     uint[] array = Nat224.Create();
     SecP224K1Field.Square(y, array);
     SecP224K1Field.Multiply(array, y, array);
     uint[] array2 = array;
     SecP224K1Field.Square(array, array2);
     SecP224K1Field.Multiply(array2, y, array2);
     uint[] array3 = Nat224.Create();
     SecP224K1Field.Square(array2, array3);
     SecP224K1Field.Multiply(array3, y, array3);
     uint[] array4 = Nat224.Create();
     SecP224K1Field.SquareN(array3, 4, array4);
     SecP224K1Field.Multiply(array4, array3, array4);
     uint[] array5 = Nat224.Create();
     SecP224K1Field.SquareN(array4, 3, array5);
     SecP224K1Field.Multiply(array5, array2, array5);
     uint[] array6 = array5;
     SecP224K1Field.SquareN(array5, 8, array6);
     SecP224K1Field.Multiply(array6, array4, array6);
     uint[] array7 = array4;
     SecP224K1Field.SquareN(array6, 4, array7);
     SecP224K1Field.Multiply(array7, array3, array7);
     uint[] array8 = array3;
     SecP224K1Field.SquareN(array7, 19, array8);
     SecP224K1Field.Multiply(array8, array6, array8);
     uint[] array9 = Nat224.Create();
     SecP224K1Field.SquareN(array8, 42, array9);
     SecP224K1Field.Multiply(array9, array8, array9);
     uint[] z = array8;
     SecP224K1Field.SquareN(array9, 23, z);
     SecP224K1Field.Multiply(z, array7, z);
     uint[] array10 = array7;
     SecP224K1Field.SquareN(z, 84, array10);
     SecP224K1Field.Multiply(array10, array9, array10);
     uint[] z2 = array10;
     SecP224K1Field.SquareN(z2, 20, z2);
     SecP224K1Field.Multiply(z2, array6, z2);
     SecP224K1Field.SquareN(z2, 3, z2);
     SecP224K1Field.Multiply(z2, y, z2);
     SecP224K1Field.SquareN(z2, 2, z2);
     SecP224K1Field.Multiply(z2, y, z2);
     SecP224K1Field.SquareN(z2, 4, z2);
     SecP224K1Field.Multiply(z2, array2, z2);
     SecP224K1Field.Square(z2, z2);
     uint[] array11 = array9;
     SecP224K1Field.Square(z2, array11);
     if (Nat224.Eq(y, array11))
     {
         return(new SecP224K1FieldElement(z2));
     }
     SecP224K1Field.Multiply(z2, PRECOMP_POW2, z2);
     SecP224K1Field.Square(z2, array11);
     if (Nat224.Eq(y, array11))
     {
         return(new SecP224K1FieldElement(z2));
     }
     return(null);
 }