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.
falcon signature verification with public key digest result
- Loading branch information
Showing
21 changed files
with
286 additions
and
12 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
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
ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/LacchainProtocolSpecs.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 IADB. | ||
* | ||
* 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 LacchainProtocolSpecs { | ||
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 lacchainContractsRegistry = | ||
MainnetPrecompiledContractRegistries.istanbul(precompiledContractConfiguration); | ||
lacchainContractsRegistry.put( | ||
Address.LACCHAIN_FALCON, | ||
new FalconPrecompiledContract( | ||
precompiledContractConfiguration.getGasCalculator())); | ||
return lacchainContractsRegistry; | ||
}); | ||
} | ||
} |
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
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
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
/* | ||
* Copyright IADB. | ||
* | ||
* 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.MessageFrame; | ||
import org.hyperledger.besu.evm.gascalculator.GasCalculator; | ||
|
||
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; | ||
|
||
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,bytes32)".getBytes(UTF_8))).slice(0, 4); | ||
private static final String SIGNATURE_ALGORITHM = "Falcon-512"; | ||
|
||
private final FalconSigner falconSigner = new FalconSigner(); | ||
|
||
public FalconPrecompiledContract(final GasCalculator gasCalculator) { | ||
super("Falcon", gasCalculator); | ||
} | ||
|
||
@Override | ||
public long gasRequirement(final Bytes input) { | ||
long value = gasCalculator().getFrecPrecompiledContractGasCost(); | ||
LOG.debug("Gas requirement calculation for Falcon has been called {}", value); | ||
return value; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public PrecompileContractResult computePrecompile( | ||
final Bytes methodInput, @Nonnull final MessageFrame messageFrame) { | ||
Bytes methodAbi = methodInput.slice(0, METHOD_ABI.size()); | ||
if (!methodAbi.xor(METHOD_ABI).isZero()) { | ||
throw new IllegalArgumentException("Unexpected method ABI: " + methodAbi.toHexString()); | ||
} | ||
Bytes input = methodInput.slice(METHOD_ABI.size()); | ||
int signatureOffset = input.slice(0, 32).trimLeadingZeros().toInt(); | ||
int pubKeyOffset = input.slice(32, 32).trimLeadingZeros().toInt(); | ||
|
||
int signatureLength = input.slice(signatureOffset, 32).trimLeadingZeros().toInt(); | ||
int pubKeyLength = input.slice(pubKeyOffset, 32).trimLeadingZeros().toInt(); | ||
int dataLength = 32; | ||
|
||
Bytes signatureSlice = input.slice(signatureOffset + 32, signatureLength); | ||
Bytes pubKeySlice = input.slice(pubKeyOffset + 32 + 1, pubKeyLength - 1); | ||
Bytes dataSlice = input.slice(64, dataLength); | ||
|
||
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) { | ||
Bytes digest = Hash.shake256(input.slice(pubKeyOffset + 32, pubKeyLength), 32); | ||
return PrecompileContractResult.success(Bytes32.leftPad(digest)); | ||
} else { | ||
return PrecompileContractResult.success(Bytes32.leftPad(Bytes.of(0))); | ||
} | ||
} | ||
} |
Oops, something went wrong.