Esempio n. 1
0
 internal ECPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
 {
     this.m_curve = curve;
     this.m_x = x;
     this.m_y = y;
     this.m_zs = zs;
     this.m_withCompression = withCompression;
 }
Esempio n. 2
0
 public virtual bool Equals(ECFieldElement other)
 {
     if (this == other)
         return true;
     if (null == other)
         return false;
     return ToBigInteger().Equals(other.ToBigInteger());
 }
Esempio n. 3
0
		public virtual ECFieldElement SquarePlusProduct(ECFieldElement x, ECFieldElement y)
		{
			return Square().Add(x.Multiply(y));
		}
Esempio n. 4
0
		public override ECFieldElement Add(
			ECFieldElement b)
		{
			// No check performed here for performance reasons. Instead the
			// elements involved are checked in ECPoint.F2m
			// checkFieldElements(this, b);
			LongArray iarrClone = this.x.Copy();
			F2mFieldElement bF2m = (F2mFieldElement)b;
			iarrClone.AddShiftedByWords(bF2m.x, 0);
			return new F2mFieldElement(m, ks, iarrClone);
		}
Esempio n. 5
0
		private ECFieldElement CheckSqrt(ECFieldElement z)
		{
			return z.Square().Equals(this) ? z : null;
		}
Esempio n. 6
0
		public virtual ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
		{
			return Multiply(b).Add(x.Multiply(y));
		}
Esempio n. 7
0
 protected internal abstract ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression);
Esempio n. 8
0
        protected F2mCurve(int m, int k1, int k2, int k3, ECFieldElement a, ECFieldElement b, BigInteger order, BigInteger cofactor)
            : base(m, k1, k2, k3)
        {
            this.m = m;
            this.k1 = k1;
            this.k2 = k2;
            this.k3 = k3;
            this.m_order = order;
            this.m_cofactor = cofactor;

            this.m_infinity = new F2mPoint(this, null, null);
            this.m_a = a;
            this.m_b = b;
            this.m_coord = F2M_DEFAULT_COORDS;
        }
 public override ECFieldElement Divide(
     ECFieldElement b)
 {
     return(new FpFieldElement(q, r, ModMult(x, ModInverse(b.ToBigInteger()))));
 }
Esempio n. 10
0
		public override ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y)
		{
			return SquarePlusProduct(x, y);
		}
Esempio n. 11
0
        public override ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
        {
            BigInteger ax = this.x, bx = b.ToBigInteger(), xx = x.ToBigInteger(), yx = y.ToBigInteger();
            BigInteger ab  = ax.Multiply(bx);
            BigInteger xy  = xx.Multiply(yx);
            BigInteger sum = ab.Add(xy);

            if (r != null && r.SignValue < 0 && sum.BitLength > (q.BitLength << 1))
            {
                sum = sum.Subtract(q.ShiftLeft(q.BitLength));
            }
            return(new FpFieldElement(q, r, ModReduce(sum)));
        }
Esempio n. 12
0
 public abstract ECFieldElement Divide(ECFieldElement b);
Esempio n. 13
0
 public abstract ECFieldElement Multiply(ECFieldElement b);
Esempio n. 14
0
        public override ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
        {
            BigInteger ax = this.x, bx = b.ToBigInteger(), xx = x.ToBigInteger(), yx = y.ToBigInteger();
            BigInteger ab = ax.Multiply(bx);
            BigInteger xy = xx.Multiply(yx);

            return(new FpFieldElement(q, r, ModReduce(ab.Subtract(xy))));
        }
Esempio n. 15
0
 public override ECFieldElement Multiply(
     ECFieldElement b)
 {
     return(new FpFieldElement(q, r, ModMult(x, b.ToBigInteger())));
 }
Esempio n. 16
0
		public override ECFieldElement Multiply(
			ECFieldElement b)
		{
			// Right-to-left comb multiplication in the LongArray
			// Input: Binary polynomials a(z) and b(z) of degree at most m-1
			// Output: c(z) = a(z) * b(z) mod f(z)

			// No check performed here for performance reasons. Instead the
			// elements involved are checked in ECPoint.F2m
			// checkFieldElements(this, b);
			return new F2mFieldElement(m, ks, x.ModMultiply(((F2mFieldElement)b).x, m, ks));
		}
Esempio n. 17
0
 private ECFieldElement CheckSqrt(ECFieldElement z)
 {
     return(z.Square().Equals(this) ? z : null);
 }
Esempio n. 18
0
		public override ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
		{
			LongArray ax = this.x, bx = ((F2mFieldElement)b).x, xx = ((F2mFieldElement)x).x, yx = ((F2mFieldElement)y).x;

			LongArray ab = ax.Multiply(bx, m, ks);
			LongArray xy = xx.Multiply(yx, m, ks);

			if(ab == ax || ab == bx)
			{
				ab = (LongArray)ab.Copy();
			}

			ab.AddShiftedByWords(xy, 0);
			ab.Reduce(m, ks);

			return new F2mFieldElement(m, ks, ab);
		}
Esempio n. 19
0
 public virtual ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
 {
     return(Multiply(b).Subtract(x.Multiply(y)));
 }
Esempio n. 20
0
 public abstract ECFieldElement Subtract(ECFieldElement b);
Esempio n. 21
0
 public virtual ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
 {
     return(Multiply(b).Add(x.Multiply(y)));
 }
Esempio n. 22
0
 protected FpCurve(BigInteger q, BigInteger r, ECFieldElement a, ECFieldElement b)
     : this(q, r, a, b, null, null)
 {
 }
Esempio n. 23
0
 public virtual ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y)
 {
     return(Square().Subtract(x.Multiply(y)));
 }
Esempio n. 24
0
 public abstract ECFieldElement Add(ECFieldElement b);
Esempio n. 25
0
 public virtual ECFieldElement SquarePlusProduct(ECFieldElement x, ECFieldElement y)
 {
     return(Square().Add(x.Multiply(y)));
 }
Esempio n. 26
0
		public virtual ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
		{
			return Multiply(b).Subtract(x.Multiply(y));
		}
Esempio n. 27
0
 public override ECFieldElement Subtract(
     ECFieldElement b)
 {
     // Addition and subtraction are the same in F2m
     return(Add(b));
 }
Esempio n. 28
0
		public virtual ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y)
		{
			return Square().Subtract(x.Multiply(y));
		}
Esempio n. 29
0
 public override ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
 {
     return(MultiplyPlusProduct(b, x, y));
 }
Esempio n. 30
0
		/**
        * Checks, if the ECFieldElements <code>a</code> and <code>b</code>
        * are elements of the same field <code>F<sub>2<sup>m</sup></sub></code>
        * (having the same representation).
        * @param a field element.
        * @param b field element to be compared.
        * @throws ArgumentException if <code>a</code> and <code>b</code>
        * are not elements of the same field
        * <code>F<sub>2<sup>m</sup></sub></code> (having the same
        * representation).
        */
		public static void CheckFieldElements(
			ECFieldElement a,
			ECFieldElement b)
		{
			if(!(a is F2mFieldElement) || !(b is F2mFieldElement))
			{
				throw new ArgumentException("Field elements are not "
					+ "both instances of F2mFieldElement");
			}

			F2mFieldElement aF2m = (F2mFieldElement)a;
			F2mFieldElement bF2m = (F2mFieldElement)b;

			if(aF2m.representation != bF2m.representation)
			{
				// Should never occur
				throw new ArgumentException("One of the F2m field elements has incorrect representation");
			}

			if((aF2m.m != bF2m.m) || !Arrays.AreEqual(aF2m.ks, bF2m.ks))
			{
				throw new ArgumentException("Field elements are not elements of the same field F2m");
			}
		}
Esempio n. 31
0
        public override ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
        {
            LongArray ax = this.x, bx = ((F2mFieldElement)b).x, xx = ((F2mFieldElement)x).x, yx = ((F2mFieldElement)y).x;

            LongArray ab = ax.Multiply(bx, m, ks);
            LongArray xy = xx.Multiply(yx, m, ks);

            if (ab == ax || ab == bx)
            {
                ab = (LongArray)ab.Copy();
            }

            ab.AddShiftedByWords(xy, 0);
            ab.Reduce(m, ks);

            return(new F2mFieldElement(m, ks, ab));
        }
Esempio n. 32
0
		public override ECFieldElement Subtract(
			ECFieldElement b)
		{
			// Addition and subtraction are the same in F2m
			return Add(b);
		}
Esempio n. 33
0
 public override ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y)
 {
     return(SquarePlusProduct(x, y));
 }
Esempio n. 34
0
		public override ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
		{
			return MultiplyPlusProduct(b, x, y);
		}
Esempio n. 35
0
        public static void MontgomeryTrick(ECFieldElement[] zs, int off, int len)
        {
            /*
             * Uses the "Montgomery Trick" to invert many field elements, with only a single actual
             * field inversion. See e.g. the paper:
             * "Fast Multi-scalar Multiplication Methods on Elliptic Curves with Precomputation Strategy Using Montgomery Trick"
             * by Katsuyuki Okeya, Kouichi Sakurai.
             */

            ECFieldElement[] c = new ECFieldElement[len];
            c[0] = zs[off];

            int i = 0;
            while (++i < len)
            {
                c[i] = c[i - 1].Multiply(zs[off + i]);
            }

            ECFieldElement u = c[--i].Invert();

            while (i > 0)
            {
                int j = off + i--;
                ECFieldElement tmp = zs[j];
                zs[j] = c[i].Multiply(u);
                u = u.Multiply(tmp);
            }

            zs[off] = u;
        }
Esempio n. 36
0
		public override ECFieldElement Divide(
			ECFieldElement b)
		{
			// There may be more efficient implementations
			ECFieldElement bInv = b.Invert();
			return Multiply(bInv);
		}
Esempio n. 37
0
 public X9FieldElement(
     ECFieldElement f)
 {
     this.f = f;
 }
Esempio n. 38
0
		public override ECFieldElement SquarePlusProduct(ECFieldElement x, ECFieldElement y)
		{
			LongArray ax = this.x, xx = ((F2mFieldElement)x).x, yx = ((F2mFieldElement)y).x;

			LongArray aa = ax.Square(m, ks);
			LongArray xy = xx.Multiply(yx, m, ks);

			if(aa == ax)
			{
				aa = (LongArray)aa.Copy();
			}

			aa.AddShiftedByWords(xy, 0);
			aa.Reduce(m, ks);

			return new F2mFieldElement(m, ks, aa);
		}
Esempio n. 39
0
 public override ECFieldElement Subtract(
     ECFieldElement b)
 {
     return(new FpFieldElement(q, r, ModSubtract(x, b.ToBigInteger())));
 }
Esempio n. 40
0
 public static int GetByteLength(ECFieldElement fe)
 {
     return (fe.FieldSize + 7) / 8;
 }
Esempio n. 41
0
		public abstract ECFieldElement Divide(ECFieldElement b);
Esempio n. 42
0
        /**
         * Solves a quadratic equation <code>z<sup>2</sup> + z = beta</code>(X9.62
         * D.1.6) The other solution is <code>z + 1</code>.
         *
         * @param beta
         *            The value to solve the qradratic equation for.
         * @return the solution for <code>z<sup>2</sup> + z = beta</code> or
         *         <code>null</code> if no solution exists.
         */
        private ECFieldElement SolveQuadradicEquation(ECFieldElement beta)
        {
            if (beta.IsZero)
            {
                return beta;
            }

            ECFieldElement zeroElement = FromBigInteger(BigInteger.Zero);

            ECFieldElement z = null;
            ECFieldElement gamma = null;

            Random rand = new Random();
            do
            {
                ECFieldElement t = FromBigInteger(new BigInteger(m, rand));
                z = zeroElement;
                ECFieldElement w = beta;
                for (int i = 1; i <= m - 1; i++)
                {
                    ECFieldElement w2 = w.Square();
                    z = z.Square().Add(w2.Multiply(t));
                    w = w2.Add(beta);
                }
                if (!w.IsZero)
                {
                    return null;
                }
                gamma = z.Square().Add(z);
            }
            while (gamma.IsZero);

            return z;
        }
Esempio n. 43
0
		public override ECFieldElement Divide(
			ECFieldElement b)
		{
			return new FpFieldElement(q, r, ModMult(x, ModInverse(b.ToBigInteger())));
		}
Esempio n. 44
0
        /**
         * Normalization ensures that any projective coordinate is 1, and therefore that the x, y
         * coordinates reflect those of the equivalent point in an affine coordinate system. Where more
         * than one point is to be normalized, this method will generally be more efficient than
         * normalizing each point separately.
         * 
         * @param points
         *            An array of points that will be updated in place with their normalized versions,
         *            where necessary
         */
        public virtual void NormalizeAll(ECPoint[] points)
        {
            CheckPoints(points);

            if (this.CoordinateSystem == ECCurve.COORD_AFFINE)
            {
                return;
            }

            /*
             * Figure out which of the points actually need to be normalized
             */
            ECFieldElement[] zs = new ECFieldElement[points.Length];
            int[] indices = new int[points.Length];
            int count = 0;
            for (int i = 0; i < points.Length; ++i)
            {
                ECPoint p = points[i];
                if (null != p && !p.IsNormalized())
                {
                    zs[count] = p.GetZCoord(0);
                    indices[count++] = i;
                }
            }

            if (count == 0)
            {
                return;
            }

            ECAlgorithms.MontgomeryTrick(zs, 0, count);

            for (int j = 0; j < count; ++j)
            {
                int index = indices[j];
                points[index] = points[index].Normalize(zs[j]);
            }
        }
Esempio n. 45
0
		public override ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y)
		{
			BigInteger ax = this.x, xx = x.ToBigInteger(), yx = y.ToBigInteger();
			BigInteger aa = ax.Multiply(ax);
			BigInteger xy = xx.Multiply(yx);
			return new FpFieldElement(q, r, ModReduce(aa.Subtract(xy)));
		}
Esempio n. 46
0
        protected FpCurve(BigInteger q, BigInteger r, ECFieldElement a, ECFieldElement b, BigInteger order, BigInteger cofactor)
            : base(q)
        {
            this.m_q = q;
            this.m_r = r;
            this.m_infinity = new FpPoint(this, null, null);

            this.m_a = a;
            this.m_b = b;
            this.m_order = order;
            this.m_cofactor = cofactor;
            this.m_coord = FP_DEFAULT_COORDS;
        }
Esempio n. 47
0
		public override ECFieldElement SquarePlusProduct(ECFieldElement x, ECFieldElement y)
		{
			BigInteger ax = this.x, xx = x.ToBigInteger(), yx = y.ToBigInteger();
			BigInteger aa = ax.Multiply(ax);
			BigInteger xy = xx.Multiply(yx);
			BigInteger sum = aa.Add(xy);
			if(r != null && r.SignValue < 0 && sum.BitLength > (q.BitLength << 1))
			{
				sum = sum.Subtract(q.ShiftLeft(q.BitLength));
			}
			return new FpFieldElement(q, r, ModReduce(sum));
		}
Esempio n. 48
0
 protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
 {
     return new F2mPoint(this, x, y, zs, withCompression);
 }
Esempio n. 49
0
 public override ECFieldElement Add(
     ECFieldElement b)
 {
     return(new FpFieldElement(q, r, ModAdd(x, b.ToBigInteger())));
 }