forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Setup GitHub Actions build and Docker deploy Signed-off-by: Diego López León <[email protected]> * Add Falcon signature verification precompiled contract Signed-off-by: Diego López León <[email protected]> * add error handler for invalid format input in Falcon precompiled Signed-off-by: eum602 <[email protected]> * add benchmark for Falcon-512 Signed-off-by: eum602 <[email protected]> * add falcon512 precompiled gas cost Signed-off-by: eum602 <[email protected]> * return value '1' instead of throwing on falcon precompiled Signed-off-by: eum602 <[email protected]> * fix: return precompiled error for malformed method signature Signed-off-by: eum602 <[email protected]> * rename variables and change License details Signed-off-by: eum602 <[email protected]> * update falcon Address Signed-off-by: eum602 <[email protected]> --------- Signed-off-by: eum602 <[email protected]> Co-authored-by: Diego López León <[email protected]>
- Loading branch information
Showing
14 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...reum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/Falcon512ProtocolSpecs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.ethereum.mainnet; | ||
|
||
import org.hyperledger.besu.datatypes.Address; | ||
import org.hyperledger.besu.evm.internal.EvmConfiguration; | ||
import org.hyperledger.besu.evm.precompile.FalconPrecompiledContract; | ||
import org.hyperledger.besu.evm.precompile.PrecompileContractRegistry; | ||
|
||
import java.math.BigInteger; | ||
import java.util.Optional; | ||
import java.util.OptionalInt; | ||
|
||
public class Falcon512ProtocolSpecs { | ||
public static ProtocolSpecBuilder postQuantumDefinition( | ||
final Optional<BigInteger> chainId, | ||
final OptionalInt contractSizeLimit, | ||
final OptionalInt configStackSizeLimit, | ||
final boolean enableRevertReason) { | ||
return MainnetProtocolSpecs.istanbulDefinition( | ||
chainId, | ||
contractSizeLimit, | ||
configStackSizeLimit, | ||
enableRevertReason, | ||
EvmConfiguration.DEFAULT) | ||
.precompileContractRegistryBuilder( | ||
precompiledContractConfiguration -> { | ||
PrecompileContractRegistry falcon512ContractsRegistry = | ||
MainnetPrecompiledContractRegistries.istanbul(precompiledContractConfiguration); | ||
falcon512ContractsRegistry.put( | ||
Address.FALCON512, | ||
new FalconPrecompiledContract( | ||
precompiledContractConfiguration.getGasCalculator())); | ||
return falcon512ContractsRegistry; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
evm/src/main/java/org/hyperledger/besu/evm/precompile/FalconPrecompiledContract.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* 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.evm.precompile; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import org.hyperledger.besu.crypto.Hash; | ||
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; | ||
import org.hyperledger.besu.evm.frame.MessageFrame; | ||
import org.hyperledger.besu.evm.gascalculator.GasCalculator; | ||
|
||
import java.util.Optional; | ||
import javax.annotation.Nonnull; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.bouncycastle.pqc.crypto.falcon.FalconParameters; | ||
import org.bouncycastle.pqc.crypto.falcon.FalconPublicKeyParameters; | ||
import org.bouncycastle.pqc.crypto.falcon.FalconSigner; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** The Falcon precompiled contract. */ | ||
public class FalconPrecompiledContract extends AbstractPrecompiledContract { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(AbstractBLS12PrecompiledContract.class); | ||
|
||
private static final Bytes METHOD_ABI = | ||
Hash.keccak256(Bytes.of("verify(bytes,bytes,bytes)".getBytes(UTF_8))).slice(0, 4); | ||
private static final String SIGNATURE_ALGORITHM = "Falcon-512"; | ||
|
||
private final FalconSigner falconSigner = new FalconSigner(); | ||
|
||
/** | ||
* Instantiates a new Falcon precompiled contract. | ||
* | ||
* @param gasCalculator the gas calculator | ||
*/ | ||
public FalconPrecompiledContract(final GasCalculator gasCalculator) { | ||
super("Falcon", gasCalculator); | ||
} | ||
|
||
@Override | ||
public long gasRequirement(final Bytes input) { | ||
return gasCalculator().falconVerifyPrecompiledContractGasCost(input); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public PrecompileContractResult computePrecompile( | ||
final Bytes methodInput, @Nonnull final MessageFrame messageFrame) { | ||
Bytes methodAbi; | ||
try { | ||
methodAbi = methodInput.slice(0, METHOD_ABI.size()); | ||
if (!methodAbi.xor(METHOD_ABI).isZero()) { | ||
LOG.trace("Unexpected method ABI: " + methodAbi.toHexString()); | ||
return PrecompileContractResult.halt( | ||
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); | ||
} | ||
} catch (Exception e) { | ||
return PrecompileContractResult.halt( | ||
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); | ||
} | ||
Bytes signatureSlice; | ||
Bytes pubKeySlice; | ||
Bytes dataSlice; | ||
|
||
try { | ||
Bytes input = methodInput.slice(METHOD_ABI.size()); | ||
int signatureOffset = input.slice(0, 32).trimLeadingZeros().toInt(); | ||
int pubKeyOffset = input.slice(32, 32).trimLeadingZeros().toInt(); | ||
int dataOffset = input.slice(64, 32).trimLeadingZeros().toInt(); | ||
|
||
int signatureLength = input.slice(signatureOffset, 32).trimLeadingZeros().toInt(); | ||
int pubKeyLength = input.slice(pubKeyOffset, 32).trimLeadingZeros().toInt(); | ||
int dataLength = input.slice(dataOffset, 32).trimLeadingZeros().toInt(); | ||
|
||
signatureSlice = input.slice(signatureOffset + 32, signatureLength); | ||
pubKeySlice = | ||
input.slice( | ||
pubKeyOffset + 32 + 1, | ||
pubKeyLength - 1); // BouncyCastle omits the first byte since it is always zero | ||
dataSlice = input.slice(dataOffset + 32, dataLength); | ||
} catch (Exception e) { | ||
LOG.trace("Error executing Falcon-512 precompiled contract: '{}'", "invalid input"); | ||
return PrecompileContractResult.success(Bytes32.leftPad(Bytes.of(1))); | ||
} | ||
|
||
if (LOG.isTraceEnabled()) { | ||
LOG.trace( | ||
"{} verify: signature={}, pubKey={}, data={}", | ||
SIGNATURE_ALGORITHM, | ||
signatureSlice.toHexString(), | ||
pubKeySlice.toHexString(), | ||
dataSlice.toHexString()); | ||
} | ||
FalconPublicKeyParameters falconPublicKeyParameters = | ||
new FalconPublicKeyParameters(FalconParameters.falcon_512, pubKeySlice.toArray()); | ||
falconSigner.init(false, falconPublicKeyParameters); | ||
final boolean verifies = | ||
falconSigner.verifySignature(dataSlice.toArray(), signatureSlice.toArray()); | ||
|
||
if (verifies) { | ||
LOG.debug("Signature is VALID"); | ||
return PrecompileContractResult.success(Bytes32.leftPad(Bytes.of(0))); | ||
} else { | ||
LOG.debug("Signature is INVALID"); | ||
return PrecompileContractResult.success(Bytes32.leftPad(Bytes.of(1))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.