@Test void succeedsWhenAttemptToDeserializeXImEqualToModulusThrowsIllegalArgumentException() { String x = "0x9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab" + "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; assertThrows( IllegalArgumentException.class, () -> G2Point.fromBytesCompressed(Bytes.fromHexString(x))); }
public static G2Point fromBytesCompressed(Bytes bytes) { checkArgument( bytes.size() == 2 * fpPointSize, "Expected %s bytes but received %s", 2 * fpPointSize, bytes.size()); byte[] xImBytes = bytes.slice(0, fpPointSize).toArray(); byte[] xReBytes = bytes.slice(fpPointSize, fpPointSize).toArray(); boolean aIn = (xImBytes[0] & (byte) (1 << 5)) != 0; boolean bIn = (xImBytes[0] & (byte) (1 << 6)) != 0; boolean cIn = (xImBytes[0] & (byte) (1 << 7)) != 0; xImBytes[0] &= (byte) 31; if ((xReBytes[0] & (byte) 224) != 0) { throw new IllegalArgumentException("The input has non-zero a2, b2 or c2 flag on xRe"); } if (!cIn) { throw new IllegalArgumentException("The serialized input does not have the C flag set."); } if (bIn) { if (!aIn && Bytes.wrap(xImBytes).isZero() && Bytes.wrap(xReBytes).isZero()) { return new G2Point(); } else { throw new IllegalArgumentException( "The serialized input has B flag set, but A flag is set, or X is non-zero."); } } BIG xImBig = BIG.fromBytes(xImBytes); BIG xReBig = BIG.fromBytes(xReBytes); BIG modulus = new BIG(ROM.Modulus); if (BIG.comp(modulus, xReBig) <= 0 || BIG.comp(modulus, xImBig) <= 0) { throw new IllegalArgumentException( "The deserialized X real or imaginary coordinate is too large."); } ECP2 point = new ECP2(new FP2(xReBig, xImBig)); if (point.is_infinity()) { throw new IllegalArgumentException("X coordinate is not on the curve."); } if (!isInGroup(point)) { throw new IllegalArgumentException("The deserialized point is not in the G2 subgroup."); } if (aIn != Util.calculateYFlag(point.getY().getB())) { FP2 x = point.getX(); FP2 yneg = point.getY(); yneg.neg(); point = new ECP2(x, yneg); } return new G2Point(point); }
G2Point implements Group<G2Point> { public static G2Point fromBytesCompressed(Bytes bytes) { checkArgument( bytes.size() == 2 * fpPointSize, "Expected %s bytes but received %s", 2 * fpPointSize, bytes.size()); byte[] xImBytes = bytes.slice(0, fpPointSize).toArray(); byte[] xReBytes = bytes.slice(fpPointSize, fpPointSize).toArray(); boolean aIn = (xImBytes[0] & (byte) (1 << 5)) != 0; boolean bIn = (xImBytes[0] & (byte) (1 << 6)) != 0; boolean cIn = (xImBytes[0] & (byte) (1 << 7)) != 0; xImBytes[0] &= (byte) 31; if ((xReBytes[0] & (byte) 224) != 0) { throw new IllegalArgumentException("The input has non-zero a2, b2 or c2 flag on xRe"); } if (!cIn) { throw new IllegalArgumentException("The serialized input does not have the C flag set."); } if (bIn) { if (!aIn && Bytes.wrap(xImBytes).isZero() && Bytes.wrap(xReBytes).isZero()) { return new G2Point(); } else { throw new IllegalArgumentException( "The serialized input has B flag set, but A flag is set, or X is non-zero."); } } BIG xImBig = BIG.fromBytes(xImBytes); BIG xReBig = BIG.fromBytes(xReBytes); BIG modulus = new BIG(ROM.Modulus); if (BIG.comp(modulus, xReBig) <= 0 || BIG.comp(modulus, xImBig) <= 0) { throw new IllegalArgumentException( "The deserialized X real or imaginary coordinate is too large."); } ECP2 point = new ECP2(new FP2(xReBig, xImBig)); if (point.is_infinity()) { throw new IllegalArgumentException("X coordinate is not on the curve."); } if (!isInGroup(point)) { throw new IllegalArgumentException("The deserialized point is not in the G2 subgroup."); } if (aIn != Util.calculateYFlag(point.getY().getB())) { FP2 x = point.getX(); FP2 yneg = point.getY(); yneg.neg(); point = new ECP2(x, yneg); } return new G2Point(point); } }
G2Point implements Group<G2Point> { public static G2Point fromBytesCompressed(Bytes bytes) { checkArgument( bytes.size() == 2 * fpPointSize, "Expected %s bytes but received %s", 2 * fpPointSize, bytes.size()); byte[] xImBytes = bytes.slice(0, fpPointSize).toArray(); byte[] xReBytes = bytes.slice(fpPointSize, fpPointSize).toArray(); boolean aIn = (xImBytes[0] & (byte) (1 << 5)) != 0; boolean bIn = (xImBytes[0] & (byte) (1 << 6)) != 0; boolean cIn = (xImBytes[0] & (byte) (1 << 7)) != 0; xImBytes[0] &= (byte) 31; if ((xReBytes[0] & (byte) 224) != 0) { throw new IllegalArgumentException("The input has non-zero a2, b2 or c2 flag on xRe"); } if (!cIn) { throw new IllegalArgumentException("The serialized input does not have the C flag set."); } if (bIn) { if (!aIn && Bytes.wrap(xImBytes).isZero() && Bytes.wrap(xReBytes).isZero()) { return new G2Point(); } else { throw new IllegalArgumentException( "The serialized input has B flag set, but A flag is set, or X is non-zero."); } } BIG xImBig = BIG.fromBytes(xImBytes); BIG xReBig = BIG.fromBytes(xReBytes); BIG modulus = new BIG(ROM.Modulus); if (BIG.comp(modulus, xReBig) <= 0 || BIG.comp(modulus, xImBig) <= 0) { throw new IllegalArgumentException( "The deserialized X real or imaginary coordinate is too large."); } ECP2 point = new ECP2(new FP2(xReBig, xImBig)); if (point.is_infinity()) { throw new IllegalArgumentException("X coordinate is not on the curve."); } if (!isInGroup(point)) { throw new IllegalArgumentException("The deserialized point is not in the G2 subgroup."); } if (aIn != Util.calculateYFlag(point.getY().getB())) { FP2 x = point.getX(); FP2 yneg = point.getY(); yneg.neg(); point = new ECP2(x, yneg); } return new G2Point(point); } G2Point();  G2Point(ECP2 point); }
G2Point implements Group<G2Point> { public static G2Point fromBytesCompressed(Bytes bytes) { checkArgument( bytes.size() == 2 * fpPointSize, "Expected %s bytes but received %s", 2 * fpPointSize, bytes.size()); byte[] xImBytes = bytes.slice(0, fpPointSize).toArray(); byte[] xReBytes = bytes.slice(fpPointSize, fpPointSize).toArray(); boolean aIn = (xImBytes[0] & (byte) (1 << 5)) != 0; boolean bIn = (xImBytes[0] & (byte) (1 << 6)) != 0; boolean cIn = (xImBytes[0] & (byte) (1 << 7)) != 0; xImBytes[0] &= (byte) 31; if ((xReBytes[0] & (byte) 224) != 0) { throw new IllegalArgumentException("The input has non-zero a2, b2 or c2 flag on xRe"); } if (!cIn) { throw new IllegalArgumentException("The serialized input does not have the C flag set."); } if (bIn) { if (!aIn && Bytes.wrap(xImBytes).isZero() && Bytes.wrap(xReBytes).isZero()) { return new G2Point(); } else { throw new IllegalArgumentException( "The serialized input has B flag set, but A flag is set, or X is non-zero."); } } BIG xImBig = BIG.fromBytes(xImBytes); BIG xReBig = BIG.fromBytes(xReBytes); BIG modulus = new BIG(ROM.Modulus); if (BIG.comp(modulus, xReBig) <= 0 || BIG.comp(modulus, xImBig) <= 0) { throw new IllegalArgumentException( "The deserialized X real or imaginary coordinate is too large."); } ECP2 point = new ECP2(new FP2(xReBig, xImBig)); if (point.is_infinity()) { throw new IllegalArgumentException("X coordinate is not on the curve."); } if (!isInGroup(point)) { throw new IllegalArgumentException("The deserialized point is not in the G2 subgroup."); } if (aIn != Util.calculateYFlag(point.getY().getB())) { FP2 x = point.getX(); FP2 yneg = point.getY(); yneg.neg(); point = new ECP2(x, yneg); } return new G2Point(point); } G2Point();  G2Point(ECP2 point); static G2Point random(long seed); static G2Point hashToG2(Bytes message); @Override G2Point add(G2Point other); @Override G2Point mul(Scalar scalar); ECP2 getPoint(); Bytes toBytes(); @VisibleForTesting Bytes toBytesCompressed(); static G2Point fromBytes(Bytes bytes); static G2Point fromBytesCompressed(Bytes bytes); static boolean isInGroup(ECP2 point); ECP2 ecp2Point(); @Override String toString(); @Override boolean equals(Object obj); @Override int hashCode(); }
G2Point implements Group<G2Point> { public static G2Point fromBytesCompressed(Bytes bytes) { checkArgument( bytes.size() == 2 * fpPointSize, "Expected %s bytes but received %s", 2 * fpPointSize, bytes.size()); byte[] xImBytes = bytes.slice(0, fpPointSize).toArray(); byte[] xReBytes = bytes.slice(fpPointSize, fpPointSize).toArray(); boolean aIn = (xImBytes[0] & (byte) (1 << 5)) != 0; boolean bIn = (xImBytes[0] & (byte) (1 << 6)) != 0; boolean cIn = (xImBytes[0] & (byte) (1 << 7)) != 0; xImBytes[0] &= (byte) 31; if ((xReBytes[0] & (byte) 224) != 0) { throw new IllegalArgumentException("The input has non-zero a2, b2 or c2 flag on xRe"); } if (!cIn) { throw new IllegalArgumentException("The serialized input does not have the C flag set."); } if (bIn) { if (!aIn && Bytes.wrap(xImBytes).isZero() && Bytes.wrap(xReBytes).isZero()) { return new G2Point(); } else { throw new IllegalArgumentException( "The serialized input has B flag set, but A flag is set, or X is non-zero."); } } BIG xImBig = BIG.fromBytes(xImBytes); BIG xReBig = BIG.fromBytes(xReBytes); BIG modulus = new BIG(ROM.Modulus); if (BIG.comp(modulus, xReBig) <= 0 || BIG.comp(modulus, xImBig) <= 0) { throw new IllegalArgumentException( "The deserialized X real or imaginary coordinate is too large."); } ECP2 point = new ECP2(new FP2(xReBig, xImBig)); if (point.is_infinity()) { throw new IllegalArgumentException("X coordinate is not on the curve."); } if (!isInGroup(point)) { throw new IllegalArgumentException("The deserialized point is not in the G2 subgroup."); } if (aIn != Util.calculateYFlag(point.getY().getB())) { FP2 x = point.getX(); FP2 yneg = point.getY(); yneg.neg(); point = new ECP2(x, yneg); } return new G2Point(point); } G2Point();  G2Point(ECP2 point); static G2Point random(long seed); static G2Point hashToG2(Bytes message); @Override G2Point add(G2Point other); @Override G2Point mul(Scalar scalar); ECP2 getPoint(); Bytes toBytes(); @VisibleForTesting Bytes toBytesCompressed(); static G2Point fromBytes(Bytes bytes); static G2Point fromBytesCompressed(Bytes bytes); static boolean isInGroup(ECP2 point); ECP2 ecp2Point(); @Override String toString(); @Override boolean equals(Object obj); @Override int hashCode();  }