Files
zeekay 1d7cae0355 Add post-quantum cryptography precompiles and update crypto dependency
- Add post-quantum signature verification precompiles:
  - ML-DSA (FIPS 204) at address 0x0f
  - SLH-DSA (FIPS 205) at address 0x10
  - Falcon at address 0x11
  - ML-KEM key encapsulation at address 0x12

- Add api_lux.go with Lux-specific API extensions

- Add precompile infrastructure from SubnetEVM:
  - allowlist module
  - contract interfaces
  - precompile config and modules

- Update crypto dependency to v1.17.9 (with VerifySignature API)
- Remove local replace directives for CI compatibility
- Update import paths across 230+ files to use luxfi/crypto
2025-12-11 02:36:17 +00:00

49 lines
1.7 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package contract
import (
"errors"
"fmt"
"regexp"
"github.com/luxfi/geth/crypto"
)
// ErrOutOfGas is returned when there is not enough gas to execute an operation
var ErrOutOfGas = errors.New("out of gas")
// Gas costs for stateful precompiles
const (
WriteGasCostPerSlot = 20_000
ReadGasCostPerSlot = 5_000
// Per LOG operation.
LogGas uint64 = 375 // from params/protocol_params.go
// Gas cost of single topic of the LOG. Should be multiplied by the number of topics.
LogTopicGas uint64 = 375 // from params/protocol_params.go
// Per byte cost in a LOG operation's data. Should be multiplied by the byte size of the data.
LogDataGas uint64 = 8 // from params/protocol_params.go
)
var functionSignatureRegex = regexp.MustCompile(`\w+\((\w*|(\w+,)+\w+)\)`)
// CalculateFunctionSelector returns the 4 byte function selector that results from [functionSignature]
// Ex. the function setBalance(addr address, balance uint256) should be passed in as the string:
// "setBalance(address,uint256)"
func CalculateFunctionSelector(functionSignature string) []byte {
if !functionSignatureRegex.MatchString(functionSignature) {
panic(fmt.Errorf("invalid function signature: %q", functionSignature))
}
hash := crypto.Keccak256([]byte(functionSignature))
return hash[:4]
}
// DeductGas checks if [suppliedGas] is sufficient against [requiredGas] and deducts [requiredGas] from [suppliedGas].
func DeductGas(suppliedGas uint64, requiredGas uint64) (uint64, error) {
if suppliedGas < requiredGas {
return 0, ErrOutOfGas
}
return suppliedGas - requiredGas, nil
}