Skip to content

Commit

Permalink
7702 validation checks v2 (#7653)
Browse files Browse the repository at this point in the history
* yParity is valid  up to 2**256 as well

Signed-off-by: Daniel Lehrner <daniel.lehrner@consensys.net>
  • Loading branch information
daniellehrner committed Sep 21, 2024
1 parent 6e246ca commit 1751a77
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public SECPSignature createSignature(final BigInteger r, final BigInteger s, fin

@Override
public CodeDelegationSignature createCodeDelegationSignature(
final BigInteger r, final BigInteger s, final long yParity) {
final BigInteger r, final BigInteger s, final BigInteger yParity) {
return CodeDelegationSignature.create(r, s, yParity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,25 @@ public CodeDelegationSignature(final BigInteger r, final BigInteger s, final byt
* @return the new CodeDelegationSignature
*/
public static CodeDelegationSignature create(
final BigInteger r, final BigInteger s, final long yParity) {
final BigInteger r, final BigInteger s, final BigInteger yParity) {
checkNotNull(r);
checkNotNull(s);

if (r.compareTo(TWO_POW_256) >= 0) {
throw new IllegalArgumentException("Invalid 'r' value, should be < 2^256 but got " + r);
throw new IllegalArgumentException(
"Invalid 'r' value, should be < 2^256 but got " + r.toString(16));
}

if (s.compareTo(TWO_POW_256) >= 0) {
throw new IllegalArgumentException("Invalid 's' value, should be < 2^256 but got " + s);
throw new IllegalArgumentException(
"Invalid 's' value, should be < 2^256 but got " + s.toString(16));
}

return new CodeDelegationSignature(r, s, (byte) yParity);
if (yParity.compareTo(TWO_POW_256) >= 0) {
throw new IllegalArgumentException(
"Invalid 'yParity' value, should be < 2^256 but got " + yParity.toString(16));
}

return new CodeDelegationSignature(r, s, yParity.byteValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ Optional<SECPPublicKey> recoverPublicKeyFromSignature(
* @return the code delegation signature
*/
CodeDelegationSignature createCodeDelegationSignature(
final BigInteger r, final BigInteger s, final long yParity);
final BigInteger r, final BigInteger s, final BigInteger yParity);

/**
* Decode secp signature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ class CodeDelegationSignatureTest {
void testValidInputs() {
BigInteger r = BigInteger.ONE;
BigInteger s = BigInteger.TEN;
long yParity = 1L;
BigInteger yParity = BigInteger.ONE;

CodeDelegationSignature result = CodeDelegationSignature.create(r, s, yParity);

assertThat(r).isEqualTo(result.getR());
assertThat(s).isEqualTo(result.getS());
assertThat((byte) yParity).isEqualTo(result.getRecId());
assertThat(yParity.byteValue()).isEqualTo(result.getRecId());
}

@Test
void testNullRValue() {
BigInteger s = BigInteger.TEN;
long yParity = 0L;
BigInteger yParity = BigInteger.ZERO;

assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> CodeDelegationSignature.create(null, s, yParity));
Expand All @@ -50,7 +50,7 @@ void testNullRValue() {
@Test
void testNullSValue() {
BigInteger r = BigInteger.ONE;
long yParity = 0L;
BigInteger yParity = BigInteger.ZERO;

assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> CodeDelegationSignature.create(r, null, yParity));
Expand All @@ -60,7 +60,7 @@ void testNullSValue() {
void testRValueExceedsTwoPow256() {
BigInteger r = TWO_POW_256;
BigInteger s = BigInteger.TEN;
long yParity = 0L;
BigInteger yParity = BigInteger.ZERO;

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> CodeDelegationSignature.create(r, s, yParity))
Expand All @@ -71,23 +71,34 @@ void testRValueExceedsTwoPow256() {
void testSValueExceedsTwoPow256() {
BigInteger r = BigInteger.ONE;
BigInteger s = TWO_POW_256;
long yParity = 0L;
BigInteger yParity = BigInteger.ZERO;

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> CodeDelegationSignature.create(r, s, yParity))
.withMessageContainingAll("Invalid 's' value, should be < 2^256");
}

@Test
void testYParityExceedsTwoPow256() {
BigInteger r = BigInteger.ONE;
BigInteger s = BigInteger.TWO;
BigInteger yParity = TWO_POW_256;

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> CodeDelegationSignature.create(r, s, yParity))
.withMessageContainingAll("Invalid 'yParity' value, should be < 2^256");
}

@Test
void testValidYParityZero() {
BigInteger r = BigInteger.ONE;
BigInteger s = BigInteger.TEN;
long yParity = 0L;
BigInteger yParity = BigInteger.ZERO;

CodeDelegationSignature result = CodeDelegationSignature.create(r, s, yParity);

assertThat(r).isEqualTo(result.getR());
assertThat(s).isEqualTo(result.getS());
assertThat((byte) yParity).isEqualTo(result.getRecId());
assertThat(yParity.byteValue()).isEqualTo(result.getRecId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static CodeDelegation decodeInnerPayload(final RLPInput input) {
final Address address = Address.wrap(input.readBytes());
final long nonce = input.readLongScalar();

final long yParity = input.readUnsignedIntScalar();
final BigInteger yParity = input.readUInt256Scalar().toUnsignedBigInteger();
final BigInteger r = input.readUInt256Scalar().toUnsignedBigInteger();
final BigInteger s = input.readUInt256Scalar().toUnsignedBigInteger();

Expand Down

0 comments on commit 1751a77

Please sign in to comment.