Esempio n. 1
0
		protected internal ECPoint(
			ECCurve			curve,
			ECFieldElement	x,
			ECFieldElement	y,
			bool			withCompression)
		{
			if (curve == null)
				throw new ArgumentNullException("curve");

			this.curve = curve;
			this.x = x;
			this.y = y;
			this.withCompression = withCompression;
		}
Esempio n. 2
0
		public override ECFieldElement Add(
			ECFieldElement b)
		{
			return new FpFieldElement(q, x.Add(b.ToBigInteger()).Mod(q));
		}
Esempio n. 3
0
		protected bool Equals(
			ECFieldElement other)
		{
			return ToBigInteger().Equals(other.ToBigInteger());
		}
Esempio n. 4
0
		public abstract ECFieldElement Divide(ECFieldElement b);
Esempio n. 5
0
		public abstract ECFieldElement Multiply(ECFieldElement b);
Esempio n. 6
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.ToBigInteger().Sign == 0)
            {
                return FromBigInteger(BigInteger.Zero);
            }

			ECFieldElement z = null;
            ECFieldElement gamma = FromBigInteger(BigInteger.Zero);

			while (gamma.ToBigInteger().Sign == 0)
            {
                ECFieldElement t = FromBigInteger(new BigInteger(m, new Random()));
				z = FromBigInteger(BigInteger.Zero);

				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.ToBigInteger().Sign != 0)
                {
                    return null;
                }
                gamma = z.Square().Add(z);
            }
            return z;
        }
Esempio n. 7
0
		/**
		 * @param curve base curve
		 * @param x x point
		 * @param y y point
		 */
		public F2mPoint(
			ECCurve			curve,
			ECFieldElement	x,
			ECFieldElement	y)
			:  this(curve, x, y, false)
		{
		}
Esempio n. 8
0
		public override ECFieldElement Divide(
			ECFieldElement b)
		{
			// There may be more efficient implementations
			ECFieldElement bInv = b.Invert();
			return Multiply(bInv);
		}
Esempio n. 9
0
		public override ECFieldElement Subtract(
			ECFieldElement b)
		{
			// Addition and subtraction are the same in F2m
			return Add(b);
		}
Esempio n. 10
0
		public override ECFieldElement Multiply(
			ECFieldElement b)
		{
			// Right-to-left comb multiplication in the IntArray
			// 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);
			F2mFieldElement bF2m = (F2mFieldElement) b;
			IntArray mult = x.Multiply(bF2m.x, m);
			mult.Reduce(m, new int[]{k1, k2, k3});
			return new F2mFieldElement(m, k1, k2, k3, mult);
		}
Esempio n. 11
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);
			IntArray iarrClone = (IntArray) this.x.Copy();
			F2mFieldElement bF2m = (F2mFieldElement) b;
			iarrClone.AddShifted(bF2m.x, 0);
			return new F2mFieldElement(m, k1, k2, k3, iarrClone);
		}
Esempio n. 12
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.m != bF2m.m) || (aF2m.k1 != bF2m.k1)
				|| (aF2m.k2 != bF2m.k2) || (aF2m.k3 != bF2m.k3))
			{
				throw new ArgumentException("Field elements are not "
					+ "elements of the same field F2m");
			}

			if (aF2m.representation != bF2m.representation)
			{
				// Should never occur
				throw new ArgumentException(
					"One of the field "
					+ "elements are not elements has incorrect representation");
			}
		}
		public static int GetByteLength(
            ECFieldElement fe)
        {
			return (fe.FieldSize + 7) / 8;
        }
Esempio n. 14
0
		public override ECFieldElement Subtract(
			ECFieldElement b)
		{
			return new FpFieldElement(q, x.Subtract(b.ToBigInteger()).Mod(q));
		}
Esempio n. 15
0
		public override ECFieldElement Divide(
			ECFieldElement b)
		{
			return new FpFieldElement(q, x.Multiply(b.ToBigInteger().ModInverse(q)).Mod(q));
		}
Esempio n. 16
0
		protected internal ECPointBase(
			ECCurve			curve,
			ECFieldElement	x,
			ECFieldElement	y,
			bool			withCompression)
			: base(curve, x, y, withCompression)
		{
		}
Esempio n. 17
0
		public abstract ECFieldElement Add(ECFieldElement b);
Esempio n. 18
0
		/**
		 * Create a point that encodes with or without point compresion.
		 *
		 * @param curve the curve to use
		 * @param x affine x co-ordinate
		 * @param y affine y co-ordinate
		 * @param withCompression if true encode with point compression
		 */
		public FpPoint(
			ECCurve			curve,
			ECFieldElement	x,
			ECFieldElement	y,
			bool			withCompression)
			: base(curve, x, y, withCompression)
		{
			if ((x != null && y == null) || (x == null && y != null))
				throw new ArgumentException("Exactly one of the field elements is null");
		}
Esempio n. 19
0
		public abstract ECFieldElement Subtract(ECFieldElement b);
Esempio n. 20
0
		/**
		 * @param curve base curve
		 * @param x x point
		 * @param y y point
		 * @param withCompression true if encode with point compression.
		 */
		public F2mPoint(
			ECCurve			curve,
			ECFieldElement	x,
			ECFieldElement	y,
			bool			withCompression)
			: base(curve, x, y, withCompression)
		{
			if ((x != null && y == null) || (x == null && y != null))
			{
				throw new ArgumentException("Exactly one of the field elements is null");
			}

			if (x != null)
			{
				// Check if x and y are elements of the same field
				F2mFieldElement.CheckFieldElements(this.x, this.y);

				// Check if x and a are elements of the same field
				F2mFieldElement.CheckFieldElements(this.x, this.curve.A);
			}
		}
Esempio n. 21
0
		public X9FieldElement(
			ECFieldElement f)
		{
			this.f = f;
		}