mirror of
https://github.com/luxfi/coreth.git
synced 2026-10-30 17:00:51 +00:00
This module would not build against the warp it resolves. warp v1.24 removed the
UnsignedMessage / Message split, and the rename is a rotation, not a substitution
— every name shifted one place along:
UnsignedMessage -> Message (the signed subject)
NewUnsignedMessage -> NewMessage
ParseUnsignedMessage -> ParseMessage
Message -> Envelope (subject + lanes)
NewMessage -> NewEnvelope
ParseMessage -> ParseEnvelope
VerifyMessage -> VerifyEnvelope
.UnsignedMessage -> .Message
.Signature -> .Beam
Applying it in the obvious order silently conflates the two Message meanings, so
the rewrite goes through placeholders.
The fold is a security fix upstream, not cosmetic: Message now carries the Pulsar
lineage (SourceNebulaRoot, SourceKeyEraID, SourceGeneration, HashSuiteID) inside
the signed subject. Under the old split the BLS Beam signed only the unsigned
body and that lineage was Beam-unauthenticated.
VerifyEnvelope takes the same five arguments VerifyMessage did and is documented
as the canonical classical path, so verification semantics are unchanged here.
VerifyWithOptions is the opt-in door to the Corona and ML-DSA lanes; nothing in
this module opts in, so no PQ policy is decided by this commit.
Two shape changes fall out of Envelope holding concrete lanes:
- Beam is a BitSetSignature value, not an interface, so the signer-count
type-assertion in the gas path is unreachable and its unknown-signature-type
branch is gone. A parsed envelope always has a Beam to count.
- Envelope.Bytes reports c14n failures instead of panicking, so the RPC path
surfaces that error rather than returning a truncated message.
NewEnvelope also takes the Corona and ML-DSA cert-set lanes; the test aggregator
is Beam-only and passes nil for both.
Builds clean, go vet clean, warp + precompile + aggregator suites pass.
Co-authored-by: Hanzo Dev <[email protected]>
136 lines
4.4 KiB
Go
136 lines
4.4 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package warp
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/luxfi/coreth/precompile/contract"
|
|
"github.com/luxfi/coreth/predicate"
|
|
"github.com/luxfi/geth/common"
|
|
"github.com/luxfi/geth/core/vm"
|
|
"github.com/luxfi/math"
|
|
"github.com/luxfi/math/set"
|
|
"github.com/luxfi/warp"
|
|
"github.com/luxfi/warp/payload"
|
|
)
|
|
|
|
var (
|
|
_ messageHandler = addressedPayloadHandler{}
|
|
_ messageHandler = blockHashHandler{}
|
|
)
|
|
|
|
var (
|
|
getVerifiedWarpMessageInvalidOutput []byte
|
|
getVerifiedWarpBlockHashInvalidOutput []byte
|
|
)
|
|
|
|
func init() {
|
|
res, err := PackGetVerifiedWarpMessageOutput(GetVerifiedWarpMessageOutput{Valid: false})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
getVerifiedWarpMessageInvalidOutput = res
|
|
|
|
res, err = PackGetVerifiedWarpBlockHashOutput(GetVerifiedWarpBlockHashOutput{Valid: false})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
getVerifiedWarpBlockHashInvalidOutput = res
|
|
}
|
|
|
|
type messageHandler interface {
|
|
packFailed() []byte
|
|
handleMessage(msg *warp.Envelope) ([]byte, error)
|
|
}
|
|
|
|
func handleWarpMessage(accessibleState contract.AccessibleState, input []byte, suppliedGas uint64, handler messageHandler) ([]byte, uint64, error) {
|
|
remainingGas, err := contract.DeductGas(suppliedGas, GetVerifiedWarpMessageBaseCost)
|
|
if err != nil {
|
|
return nil, remainingGas, err
|
|
}
|
|
|
|
warpIndexInput, err := UnpackGetVerifiedWarpMessageInput(input)
|
|
if err != nil {
|
|
return nil, remainingGas, fmt.Errorf("%w: %s", errInvalidIndexInput, err)
|
|
}
|
|
if warpIndexInput > 2147483647 { // math.MaxInt32
|
|
return nil, remainingGas, fmt.Errorf("%w: larger than MaxInt32", errInvalidIndexInput)
|
|
}
|
|
warpIndex := int(warpIndexInput) // This conversion is safe even if int is 32 bits because we checked above.
|
|
state := accessibleState.GetStateDB()
|
|
predicateBytes, exists := state.GetPredicateStorageSlots(ContractAddress, warpIndex)
|
|
predicateResults := accessibleState.GetBlockContext().GetPredicateResults(state.TxHash(), ContractAddress)
|
|
valid := exists && !set.BitsFromBytes(predicateResults).Contains(warpIndex)
|
|
if !valid {
|
|
return handler.packFailed(), remainingGas, nil
|
|
}
|
|
|
|
// Note: we charge for the size of the message during both predicate verification and each time the message is read during
|
|
// EVM execution because each execution incurs an additional read cost.
|
|
msgBytesGas, overflow := math.SafeMul(GasCostPerWarpMessageBytes, uint64(len(predicateBytes)))
|
|
if overflow {
|
|
return nil, 0, vm.ErrOutOfGas
|
|
}
|
|
if remainingGas, err = contract.DeductGas(remainingGas, msgBytesGas); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
// Note: since the predicate is verified in advance of execution, the precompile should not
|
|
// hit an error during execution.
|
|
unpackedPredicateBytes, err := predicate.UnpackPredicate(predicateBytes)
|
|
if err != nil {
|
|
return nil, remainingGas, fmt.Errorf("%w: %s", errInvalidPredicateBytes, err)
|
|
}
|
|
warpMessage, err := warp.ParseEnvelope(unpackedPredicateBytes)
|
|
if err != nil {
|
|
return nil, remainingGas, fmt.Errorf("%w: %s", errInvalidWarpMsg, err)
|
|
}
|
|
res, err := handler.handleMessage(warpMessage)
|
|
if err != nil {
|
|
return nil, remainingGas, err
|
|
}
|
|
return res, remainingGas, nil
|
|
}
|
|
|
|
type addressedPayloadHandler struct{}
|
|
|
|
func (addressedPayloadHandler) packFailed() []byte {
|
|
return getVerifiedWarpMessageInvalidOutput
|
|
}
|
|
|
|
func (addressedPayloadHandler) handleMessage(warpMessage *warp.Envelope) ([]byte, error) {
|
|
addressedPayload, err := payload.ParseAddressedCall(warpMessage.Message.Payload)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %s", errInvalidAddressedPayload, err)
|
|
}
|
|
return PackGetVerifiedWarpMessageOutput(GetVerifiedWarpMessageOutput{
|
|
Message: WarpMessage{
|
|
SourceChainID: common.Hash(warpMessage.Message.SourceChainID),
|
|
OriginSenderAddress: common.BytesToAddress(addressedPayload.SourceAddress),
|
|
Payload: addressedPayload.Payload,
|
|
},
|
|
Valid: true,
|
|
})
|
|
}
|
|
|
|
type blockHashHandler struct{}
|
|
|
|
func (blockHashHandler) packFailed() []byte {
|
|
return getVerifiedWarpBlockHashInvalidOutput
|
|
}
|
|
|
|
func (blockHashHandler) handleMessage(warpMessage *warp.Envelope) ([]byte, error) {
|
|
blockHashPayload, err := payload.ParseHash(warpMessage.Message.Payload)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %s", errInvalidBlockHashPayload, err)
|
|
}
|
|
return PackGetVerifiedWarpBlockHashOutput(GetVerifiedWarpBlockHashOutput{
|
|
WarpBlockHash: WarpBlockHash{
|
|
SourceChainID: common.Hash(warpMessage.Message.SourceChainID),
|
|
BlockHash: common.BytesToHash(blockHashPayload.Hash[:]),
|
|
},
|
|
Valid: true,
|
|
})
|
|
}
|