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; }
public virtual bool Equals(ECFieldElement other) { if (this == other) return true; if (null == other) return false; return ToBigInteger().Equals(other.ToBigInteger()); }
public virtual ECFieldElement SquarePlusProduct(ECFieldElement x, ECFieldElement y) { return Square().Add(x.Multiply(y)); }
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); }
private ECFieldElement CheckSqrt(ECFieldElement z) { return z.Square().Equals(this) ? z : null; }
public virtual ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y) { return Multiply(b).Add(x.Multiply(y)); }
protected internal abstract ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression);
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())))); }
public override ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y) { return SquarePlusProduct(x, y); }
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))); }
public abstract ECFieldElement Divide(ECFieldElement b);
public abstract ECFieldElement Multiply(ECFieldElement b);
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)))); }
public override ECFieldElement Multiply( ECFieldElement b) { return(new FpFieldElement(q, r, ModMult(x, b.ToBigInteger()))); }
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)); }
private ECFieldElement CheckSqrt(ECFieldElement z) { return(z.Square().Equals(this) ? z : null); }
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); }
public virtual ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y) { return(Multiply(b).Subtract(x.Multiply(y))); }
public abstract ECFieldElement Subtract(ECFieldElement b);
public virtual ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y) { return(Multiply(b).Add(x.Multiply(y))); }
protected FpCurve(BigInteger q, BigInteger r, ECFieldElement a, ECFieldElement b) : this(q, r, a, b, null, null) { }
public virtual ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y) { return(Square().Subtract(x.Multiply(y))); }
public abstract ECFieldElement Add(ECFieldElement b);
public virtual ECFieldElement SquarePlusProduct(ECFieldElement x, ECFieldElement y) { return(Square().Add(x.Multiply(y))); }
public virtual ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y) { return Multiply(b).Subtract(x.Multiply(y)); }
public override ECFieldElement Subtract( ECFieldElement b) { // Addition and subtraction are the same in F2m return(Add(b)); }
public virtual ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y) { return Square().Subtract(x.Multiply(y)); }
public override ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y) { return(MultiplyPlusProduct(b, x, y)); }
/** * 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"); } }
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)); }
public override ECFieldElement Subtract( ECFieldElement b) { // Addition and subtraction are the same in F2m return Add(b); }
public override ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y) { return(SquarePlusProduct(x, y)); }
public override ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y) { return MultiplyPlusProduct(b, x, y); }
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; }
public override ECFieldElement Divide( ECFieldElement b) { // There may be more efficient implementations ECFieldElement bInv = b.Invert(); return Multiply(bInv); }
public X9FieldElement( ECFieldElement f) { this.f = f; }
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); }
public override ECFieldElement Subtract( ECFieldElement b) { return(new FpFieldElement(q, r, ModSubtract(x, b.ToBigInteger()))); }
public static int GetByteLength(ECFieldElement fe) { return (fe.FieldSize + 7) / 8; }
public abstract ECFieldElement Divide(ECFieldElement b);
/** * 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; }
public override ECFieldElement Divide( ECFieldElement b) { return new FpFieldElement(q, r, ModMult(x, ModInverse(b.ToBigInteger()))); }
/** * 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]); } }
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))); }
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; }
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)); }
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression) { return new F2mPoint(this, x, y, zs, withCompression); }
public override ECFieldElement Add( ECFieldElement b) { return(new FpFieldElement(q, r, ModAdd(x, b.ToBigInteger()))); }