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.
Add Falcon signature verification precompiled contract
- Loading branch information
Showing
12 changed files
with
196 additions
and
1 deletion.
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
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
/* | ||
* 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.ethereum.core.Account; | ||
import org.hyperledger.besu.ethereum.core.Address; | ||
import org.hyperledger.besu.ethereum.mainnet.precompiles.FalconPrecompiledContract; | ||
|
||
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, | ||
final boolean quorumCompatibilityMode) { | ||
return MainnetProtocolSpecs.istanbulDefinition( | ||
chainId, | ||
contractSizeLimit, | ||
configStackSizeLimit, | ||
enableRevertReason, | ||
quorumCompatibilityMode) | ||
.precompileContractRegistryBuilder( | ||
precompiledContractConfiguration -> { | ||
PrecompileContractRegistry lacchainContractsRegistry = | ||
MainnetPrecompiledContractRegistries.istanbul(precompiledContractConfiguration); | ||
lacchainContractsRegistry.put( | ||
Address.LACCHAIN_FALCON, | ||
Account.DEFAULT_VERSION, | ||
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
93 changes: 93 additions & 0 deletions
93
...ain/java/org/hyperledger/besu/ethereum/mainnet/precompiles/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,93 @@ | ||
/* | ||
* 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.precompiles; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import org.hyperledger.besu.crypto.Hash; | ||
import org.hyperledger.besu.ethereum.core.Gas; | ||
import org.hyperledger.besu.ethereum.mainnet.AbstractPrecompiledContract; | ||
import org.hyperledger.besu.ethereum.vm.GasCalculator; | ||
import org.hyperledger.besu.ethereum.vm.MessageFrame; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.tuweni.bytes.Bytes; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.openquantumsafe.Signature; | ||
|
||
/** | ||
* note: Liboqs - random number generation defaults to /dev/urandom a better form is to use the | ||
* OQS_RAND_agl_openssl "OpenSSL" random number algorithm, then set the environment default engine | ||
* to IBRand for quantum entropy | ||
*/ | ||
public class FalconPrecompiledContract extends AbstractPrecompiledContract { | ||
|
||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
private static final Bytes METHOD_ABI = | ||
Hash.keccak256(Bytes.of("verify(bytes,bytes,bytes)".getBytes(UTF_8))).slice(0, 4); | ||
// taken from liboqs C sig.h header, OQS_SIG_alg_falcon_512 | ||
private static final String SIGNATURE_ALGORITHM = "Falcon-512"; | ||
|
||
public FalconPrecompiledContract(final GasCalculator gasCalculator) { | ||
super("Falcon", gasCalculator); | ||
} | ||
|
||
@Override | ||
public Gas gasRequirement(final Bytes input) { | ||
return gasCalculator().sha256PrecompiledContractGasCost(input); | ||
} | ||
|
||
@Override | ||
public Bytes compute(final Bytes methodInput, 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 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(); | ||
|
||
Bytes signatureSlice = input.slice(signatureOffset + 32, signatureLength); | ||
Bytes pubKeySlice = input.slice(pubKeyOffset + 32, pubKeyLength); | ||
Bytes dataSlice = input.slice(dataOffset + 32, dataLength); | ||
|
||
if (LOG.isTraceEnabled()) { | ||
LOG.trace( | ||
"{} verify: signature={}, pubKey={}, data={}", | ||
SIGNATURE_ALGORITHM, | ||
signatureSlice.toHexString(), | ||
pubKeySlice.toHexString(), | ||
dataSlice.toHexString()); | ||
} | ||
Signature verifier = new Signature(SIGNATURE_ALGORITHM); | ||
final boolean verifies = | ||
verifier.verify(dataSlice.toArray(), signatureSlice.toArray(), pubKeySlice.toArray()); | ||
|
||
if (verifies) { | ||
LOG.debug("Signature is VALID"); | ||
return Bytes32.leftPad(Bytes.of(0)); | ||
} else { | ||
LOG.debug("Signature is INVALID"); | ||
return 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