Merge branch 'main' into zkbesu

This commit is contained in:
Fabio Di Fabio
2024-09-23 17:11:35 +02:00
77 changed files with 927 additions and 1311 deletions

View File

@@ -212,6 +212,12 @@ public abstract class AbstractSECP256 implements SignatureAlgorithm {
return SECPSignature.create(r, s, recId, curveOrder);
}
@Override
public CodeDelegationSignature createCodeDelegationSignature(
final BigInteger r, final BigInteger s, final BigInteger yParity) {
return CodeDelegationSignature.create(r, s, yParity);
}
@Override
public SECPSignature decodeSignature(final Bytes bytes) {
return SECPSignature.decode(bytes, curveOrder);

View File

@@ -0,0 +1,66 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.crypto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.math.BigInteger;
/** Secp signature with code delegation. */
public class CodeDelegationSignature extends SECPSignature {
private static final BigInteger TWO_POW_256 = BigInteger.TWO.pow(256);
/**
* Instantiates a new SECPSignature.
*
* @param r the r part of the signature
* @param s the s part of the signature
* @param yParity the parity of the y coordinate of the public key
*/
public CodeDelegationSignature(final BigInteger r, final BigInteger s, final byte yParity) {
super(r, s, yParity);
}
/**
* Create a new CodeDelegationSignature.
*
* @param r the r part of the signature
* @param s the s part of the signature
* @param yParity the parity of the y coordinate of the public key
* @return the new CodeDelegationSignature
*/
public static CodeDelegationSignature create(
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.toString(16));
}
if (s.compareTo(TWO_POW_256) >= 0) {
throw new IllegalArgumentException(
"Invalid 's' value, should be < 2^256 but got " + s.toString(16));
}
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());
}
}

View File

@@ -215,6 +215,17 @@ public interface SignatureAlgorithm {
*/
SECPSignature createSignature(final BigInteger r, final BigInteger s, final byte recId);
/**
* Create code delegation signature which have different bounds than transaction signatures.
*
* @param r the r part of the signature
* @param s the s part of the signature
* @param yParity the parity of the y coordinate of the public key
* @return the code delegation signature
*/
CodeDelegationSignature createCodeDelegationSignature(
final BigInteger r, final BigInteger s, final BigInteger yParity);
/**
* Decode secp signature.
*

View File

@@ -0,0 +1,104 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.crypto;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.math.BigInteger;
import org.junit.jupiter.api.Test;
class CodeDelegationSignatureTest {
private static final BigInteger TWO_POW_256 = BigInteger.valueOf(2).pow(256);
@Test
void testValidInputs() {
BigInteger r = BigInteger.ONE;
BigInteger s = BigInteger.TEN;
BigInteger yParity = BigInteger.ONE;
CodeDelegationSignature result = CodeDelegationSignature.create(r, s, yParity);
assertThat(r).isEqualTo(result.getR());
assertThat(s).isEqualTo(result.getS());
assertThat(yParity.byteValue()).isEqualTo(result.getRecId());
}
@Test
void testNullRValue() {
BigInteger s = BigInteger.TEN;
BigInteger yParity = BigInteger.ZERO;
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> CodeDelegationSignature.create(null, s, yParity));
}
@Test
void testNullSValue() {
BigInteger r = BigInteger.ONE;
BigInteger yParity = BigInteger.ZERO;
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> CodeDelegationSignature.create(r, null, yParity));
}
@Test
void testRValueExceedsTwoPow256() {
BigInteger r = TWO_POW_256;
BigInteger s = BigInteger.TEN;
BigInteger yParity = BigInteger.ZERO;
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> CodeDelegationSignature.create(r, s, yParity))
.withMessageContainingAll("Invalid 'r' value, should be < 2^256");
}
@Test
void testSValueExceedsTwoPow256() {
BigInteger r = BigInteger.ONE;
BigInteger s = TWO_POW_256;
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;
BigInteger yParity = BigInteger.ZERO;
CodeDelegationSignature result = CodeDelegationSignature.create(r, s, yParity);
assertThat(r).isEqualTo(result.getR());
assertThat(s).isEqualTo(result.getS());
assertThat(yParity.byteValue()).isEqualTo(result.getRecId());
}
}