Exemplo 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;
		}
Exemplo n.º 2
0
		public override ECFieldElement Add(
			ECFieldElement b)
		{
			return new FpFieldElement(q, x.Add(b.ToBigInteger()).Mod(q));
		}
Exemplo n.º 3
0
		protected bool Equals(
			ECFieldElement other)
		{
			return ToBigInteger().Equals(other.ToBigInteger());
		}
Exemplo n.º 4
0
		public abstract ECFieldElement Divide(ECFieldElement b);
Exemplo n.º 5
0
		public abstract ECFieldElement Multiply(ECFieldElement b);
Exemplo 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;
        }
Exemplo 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)
		{
		}
Exemplo n.º 8
0
		public override ECFieldElement Divide(
			ECFieldElement b)
		{
			// There may be more efficient implementations
			ECFieldElement bInv = b.Invert();
			return Multiply(bInv);
		}
Exemplo n.º 9
0
		public override ECFieldElement Subtract(
			ECFieldElement b)
		{
			// Addition and subtraction are the same in F2m
			return Add(b);
		}
Exemplo 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);
		}
Exemplo 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);
		}
Exemplo 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");
			}
		}
Exemplo n.º 13
0
		public static int GetByteLength(
            ECFieldElement fe)
        {
			return (fe.FieldSize + 7) / 8;
        }
Exemplo n.º 14
0
		public override ECFieldElement Subtract(
			ECFieldElement b)
		{
			return new FpFieldElement(q, x.Subtract(b.ToBigInteger()).Mod(q));
		}
Exemplo n.º 15
0
		public override ECFieldElement Divide(
			ECFieldElement b)
		{
			return new FpFieldElement(q, x.Multiply(b.ToBigInteger().ModInverse(q)).Mod(q));
		}
Exemplo n.º 16
0
		protected internal ECPointBase(
			ECCurve			curve,
			ECFieldElement	x,
			ECFieldElement	y,
			bool			withCompression)
			: base(curve, x, y, withCompression)
		{
		}
Exemplo n.º 17
0
		public abstract ECFieldElement Add(ECFieldElement b);
Exemplo 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");
		}
Exemplo n.º 19
0
		public abstract ECFieldElement Subtract(ECFieldElement b);
Exemplo 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);
			}
		}
Exemplo n.º 21
0
		public X9FieldElement(
			ECFieldElement f)
		{
			this.f = f;
		}