org.spongycastle.math.ec.ECCurve类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(92)

本文整理了Java中org.spongycastle.math.ec.ECCurve类的一些代码示例,展示了ECCurve类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ECCurve类的具体详情如下:
包路径:org.spongycastle.math.ec.ECCurve
类名称:ECCurve

ECCurve介绍

[英]base class for an elliptic curve
[中]椭圆曲线的基类

代码示例

代码示例来源:origin: ethereum/ethereumj

static AuthResponseMessage decode(byte[] wire) {
  int offset = 0;
  AuthResponseMessage message = new AuthResponseMessage();
  byte[] bytes = new byte[65];
  System.arraycopy(wire, offset, bytes, 1, 64);
  offset += 64;
  bytes[0] = 0x04; // uncompressed
  message.ephemeralPublicKey = ECKey.CURVE.getCurve().decodePoint(bytes);
  message.nonce = new byte[32];
  System.arraycopy(wire, offset, message.nonce, 0, 32);
  offset += message.nonce.length;
  byte tokenUsed = wire[offset];
  offset += 1;
  if (tokenUsed != 0x00 && tokenUsed != 0x01)
    throw new RuntimeException("invalid boolean"); // TODO specific exception
  message.isTokenUsed = (tokenUsed == 0x01);
  return message;
}

代码示例来源:origin: ethereum/ethereumj

private static ECPoint extractPublicKey(final ECPublicKey ecPublicKey) {
 final java.security.spec.ECPoint publicPointW = ecPublicKey.getW();
 final BigInteger xCoord = publicPointW.getAffineX();
 final BigInteger yCoord = publicPointW.getAffineY();
 return CURVE.getCurve().createPoint(xCoord, yCoord);
}

代码示例来源:origin: ethereum/ethereumj

public static byte[] decrypt(BigInteger privKey, byte[] cipher, byte[] macData) throws IOException, InvalidCipherTextException {
  byte[] plaintext;
  ByteArrayInputStream is = new ByteArrayInputStream(cipher);
  byte[] ephemBytes = new byte[2*((CURVE.getCurve().getFieldSize()+7)/8) + 1];
  is.read(ephemBytes);
  ECPoint ephem = CURVE.getCurve().decodePoint(ephemBytes);
  byte[] IV = new byte[KEY_SIZE /8];
  is.read(IV);
  byte[] cipherBody = new byte[is.available()];
  is.read(cipherBody);
  plaintext = decrypt(ephem, privKey, IV, cipherBody, macData);
  return plaintext;
}

代码示例来源:origin: com.madgag.spongycastle/core

public boolean equals(ECCurve other)
{
  return this == other
    || (null != other
      && getField().equals(other.getField())
      && getA().toBigInteger().equals(other.getA().toBigInteger())
      && getB().toBigInteger().equals(other.getB().toBigInteger()));
}

代码示例来源:origin: com.madgag/scprov-jdk15on

private static EllipticCurve convertCurve(
  ECCurve  curve,
  byte[]   seed)
{
  if (curve instanceof ECCurve.Fp)
  {
    return new EllipticCurve(new ECFieldFp(((ECCurve.Fp)curve).getQ()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
  }
  else
  {
    ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
    int ks[];
    
    if (curveF2m.isTrinomial())
    {
      ks = new int[] { curveF2m.getK1() };
      
      return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
    }
    else
    {
      ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
      return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
    } 
  }
}

代码示例来源:origin: com.madgag.spongycastle/core

/**
 * Return the curve's field size in bytes.
 *
 * @param c the curve of interest.
 * @return the field size in bytes (rounded up).
 */
public int getByteLength(
  ECCurve c)
{
  return (c.getFieldSize() + 7) / 8;
}

代码示例来源:origin: com.madgag/sc-light-jdk15on

/**
 * Multiplies this <code>ECPoint</code> by the given number.
 * @param k The multiplicator.
 * @return <code>k * this</code>.
 */
public ECPoint multiply(BigInteger k)
{
  if (k.signum() < 0)
  {
    throw new IllegalArgumentException("The multiplicator cannot be negative");
  }
  if (this.isInfinity())
  {
    return this;
  }
  if (k.signum() == 0)
  {
    return this.curve.getInfinity();
  }
  assertECMultiplier();
  return this.multiplier.multiply(this, k, preCompInfo);
}

代码示例来源:origin: es.gob.afirma.jmulticard/jmulticard

private static BigInteger computeAffineY(final BigInteger affineX, final ECParameterSpec params) {
  final ECCurve bcCurve = toSpongyCastleECCurve(params);
  final ECFieldElement a = bcCurve.getA();
  final ECFieldElement b = bcCurve.getB();
  final ECFieldElement x = bcCurve.fromBigInteger(affineX);
  final ECFieldElement y = x.multiply(x).add(a).multiply(x).add(b).sqrt();
  return y.toBigInteger();
}

代码示例来源:origin: com.madgag/sc-light-jdk15on

public ECPoint twice()
{
  if (this.isInfinity()) 
  {
    // Twice identity element (point at infinity) is identity
    return this;
  }
  if (this.x.toBigInteger().signum() == 0) 
  {
    // if x1 == 0, then (x1, y1) == (x1, x1 + y1)
    // and hence this = -this and thus 2(x1, y1) == infinity
    return this.curve.getInfinity();
  }
  ECFieldElement.F2m lambda
    = (ECFieldElement.F2m)this.x.add(this.y.divide(this.x));
  ECFieldElement.F2m x3
    = (ECFieldElement.F2m)lambda.square().add(lambda).
      add(this.curve.getA());
  ECFieldElement ONE = this.curve.fromBigInteger(ECConstants.ONE);
  ECFieldElement.F2m y3
    = (ECFieldElement.F2m)this.x.square().add(
      x3.multiply(lambda.add(ONE)));
  return new ECPoint.F2m(this.curve, x3, y3, withCompression);
}

代码示例来源:origin: com.madgag.spongycastle/core

public static void discoverEndomorphisms(X9ECParameters x9)
{
  if (x9 == null)
  {
    throw new NullPointerException("x9");
  }
  ECCurve c = x9.getCurve();
  if (ECAlgorithms.isFpCurve(c))
  {
    BigInteger characteristic = c.getField().getCharacteristic();
    if (c.getA().isZero() && characteristic.mod(ECConstants.THREE).equals(ECConstants.ONE))
    {
      System.out.println("Curve has a 'GLV Type B' endomorphism with these parameters:");
      printGLVTypeBParameters(x9);
    }
  }
}

代码示例来源:origin: com.madgag.spongycastle/core

private static ECFieldElement hash2FieldElement(ECCurve curve, byte[] hash)
{
  byte[] data = Arrays.reverse(hash);
  return curve.fromBigInteger(truncate(new BigInteger(1, data), curve.getFieldSize()));
}

代码示例来源:origin: com.madgag.spongycastle/core

protected static ECFieldElement[] getInitialZCoords(ECCurve curve)
{
  // Cope with null curve, most commonly used by implicitlyCa
  int coord = null == curve ? ECCurve.COORD_AFFINE : curve.getCoordinateSystem();
  switch (coord)
  {
  case ECCurve.COORD_AFFINE:
  case ECCurve.COORD_LAMBDA_AFFINE:
    return EMPTY_ZS;
  default:
    break;
  }
  ECFieldElement one = curve.fromBigInteger(ECConstants.ONE);
  switch (coord)
  {
  case ECCurve.COORD_HOMOGENEOUS:
  case ECCurve.COORD_JACOBIAN:
  case ECCurve.COORD_LAMBDA_PROJECTIVE:
    return new ECFieldElement[]{ one };
  case ECCurve.COORD_JACOBIAN_CHUDNOVSKY:
    return new ECFieldElement[]{ one, one, one };
  case ECCurve.COORD_JACOBIAN_MODIFIED:
    return new ECFieldElement[]{ one, curve.getA() };
  default:
    throw new IllegalArgumentException("unknown coordinate system");
  }
}

代码示例来源:origin: com.madgag.spongycastle/core

public ECPoint twice()
{
  if (this.isInfinity())
  {
    return this;
  }
  ECCurve curve = this.getCurve();
  ECFieldElement X1 = this.x;
  if (X1.isZero())
  {
    // A point with X == 0 is it's own additive inverse
    return curve.getInfinity();
  }
  ECFieldElement L1 = this.y, Z1 = this.zs[0];
  boolean Z1IsOne = Z1.isOne();
  ECFieldElement L1Z1 = Z1IsOne ? L1 : L1.multiply(Z1);
  ECFieldElement Z1Sq = Z1IsOne ? Z1 : Z1.square();
  ECFieldElement T = L1.square().add(L1Z1).add(Z1Sq);
  if (T.isZero())
  {
    return new SecT233R1Point(curve, T, curve.getB().sqrt(), withCompression);
  }
  ECFieldElement X3 = T.square();
  ECFieldElement Z3 = Z1IsOne ? T : T.multiply(Z1Sq);
  ECFieldElement X1Z1 = Z1IsOne ? X1 : X1.multiply(Z1);
  ECFieldElement L3 = X1Z1.squarePlusProduct(T, L1Z1).add(X3).add(Z3);
  return new SecT233R1Point(curve, X3, L3, new ECFieldElement[]{ Z3 }, this.withCompression);
}

代码示例来源:origin: com.madgag.spongycastle/core

public static int getCombSize(ECCurve c)
{
  BigInteger order = c.getOrder();
  return order == null ? c.getFieldSize() + 1 : order.bitLength(); 
}

代码示例来源:origin: com.madgag/sc-light-jdk15on

/**
 * @param curve base curve
 * @param x x point
 * @param y y point
 * @param withCompression true if encode with point compression.
 */
public F2m(ECCurve curve, ECFieldElement x, ECFieldElement y, boolean withCompression)
{
  super(curve, x, y);
  if ((x != null && y == null) || (x == null && y != null))
  {
    throw new IllegalArgumentException("Exactly one of the field elements is null");
  }
  
  if (x != null)
  {
    // Check if x and y are elements of the same field
    ECFieldElement.F2m.checkFieldElements(this.x, this.y);
    // Check if x and a are elements of the same field
    if (curve != null)
    {
      ECFieldElement.F2m.checkFieldElements(this.x, this.curve.getA());
    }
  }
  
  this.withCompression = withCompression;
}

代码示例来源:origin: com.madgag/sc-light-jdk15on

public ECPoint twice()
{
  if (this.isInfinity())
  {
    // Twice identity element (point at infinity) is identity
    return this;
  }
  if (this.y.toBigInteger().signum() == 0) 
  {
    // if y1 == 0, then (x1, y1) == (x1, -y1)
    // and hence this = -this and thus 2(x1, y1) == infinity
    return this.curve.getInfinity();
  }
  ECFieldElement TWO = this.curve.fromBigInteger(BigInteger.valueOf(2));
  ECFieldElement THREE = this.curve.fromBigInteger(BigInteger.valueOf(3));
  ECFieldElement gamma = this.x.square().multiply(THREE).add(curve.a).divide(y.multiply(TWO));
  ECFieldElement x3 = gamma.square().subtract(this.x.multiply(TWO));
  ECFieldElement y3 = gamma.multiply(this.x.subtract(x3)).subtract(this.y);
    
  return new ECPoint.Fp(curve, x3, y3, this.withCompression);
}

代码示例来源:origin: com.madgag.spongycastle/core

public ECPoint importPoint(ECPoint p)
{
  if (this == p.getCurve())
  {
    return p;
  }
  if (p.isInfinity())
  {
    return getInfinity();
  }
  // TODO Default behaviour could be improved if the two curves have the same coordinate system by copying any Z coordinates.
  p = p.normalize();
  return validatePoint(p.getXCoord().toBigInteger(), p.getYCoord().toBigInteger(), p.withCompression);
}

代码示例来源:origin: com.madgag.spongycastle/core

static ECPoint implShamirsTrickJsf(ECPoint P, BigInteger k,
  ECPoint Q, BigInteger l)
{
  ECCurve curve = P.getCurve();
  ECPoint infinity = curve.getInfinity();
  // TODO conjugate co-Z addition (ZADDC) can return both of these
  ECPoint PaddQ = P.add(Q);
  ECPoint PsubQ = P.subtract(Q);
  ECPoint[] points = new ECPoint[]{ Q, PsubQ, P, PaddQ };
  curve.normalizeAll(points);
  ECPoint[] table = new ECPoint[] {
    points[3].negate(), points[2].negate(), points[1].negate(),
    points[0].negate(), infinity, points[0],
    points[1], points[2], points[3] };
  byte[] jsf = WNafUtil.generateJSF(k, l);
  ECPoint R = infinity;
  int i = jsf.length;
  while (--i >= 0)
  {
    int jsfi = jsf[i];
    // NOTE: The shifting ensures the sign is extended correctly
    int kDigit = ((jsfi << 24) >> 28), lDigit = ((jsfi << 28) >> 28);
    int index = 4 + (kDigit * 3) + lDigit;
    R = R.twicePlus(table[index]);
  }
  return R;
}

代码示例来源:origin: com.madgag.spongycastle/prov

public static EllipticCurve convertCurve(
  ECCurve curve, 
  byte[]  seed)
{
  ECField field = convertField(curve.getField());
  BigInteger a = curve.getA().toBigInteger(), b = curve.getB().toBigInteger();
  // TODO: the Sun EC implementation doesn't currently handle the seed properly
  // so at the moment it's set to null. Should probably look at making this configurable
  return new EllipticCurve(field, a, b, null);
}

代码示例来源:origin: com.madgag/scprov-jdk15on

public static EllipticCurve convertCurve(
  ECCurve curve, 
  byte[]  seed)
{
  // TODO: the Sun EC implementation doesn't currently handle the seed properly
  // so at the moment it's set to null. Should probably look at making this configurable
  if (curve instanceof ECCurve.Fp)
  {
    return new EllipticCurve(new ECFieldFp(((ECCurve.Fp)curve).getQ()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
  }
  else
  {
    ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
    int ks[];
    
    if (curveF2m.isTrinomial())
    {
      ks = new int[] { curveF2m.getK1() };
      
      return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
    }
    else
    {
      ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
      
      return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
    } 
  }
}

相关文章