Skip to content

Commit a1accc0

Browse files
committed
antimev: privnet: add gas limit field to envelope data and verify in dBFT
1 parent 098d6d5 commit a1accc0

File tree

8 files changed

+36
-13
lines changed

8 files changed

+36
-13
lines changed

antimev/envelope.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package antimev
33
import (
44
"bytes"
55
"crypto/aes"
6+
"encoding/binary"
67

78
"github.com/ethereum/go-ethereum/common"
89
"github.com/ethereum/go-ethereum/core/systemcontracts"
@@ -21,9 +22,13 @@ var (
2122
EncryptedDataPrefixLen = len(EncryptedDataPrefix)
2223

2324
// EncryptedDataRoundLen is the amount of bytes that encoded DKG round of transaction
24-
// encryption takes in the Envelope's data byte slice (the size of Uint64).
25+
// encryption takes in the Envelope's data byte slice (the size of Uint32).
2526
EncryptedDataRoundLen = 4
2627

28+
// EncryptedDataGasLen is the amount of bytes that encoded gas space to reserve for
29+
// the decrypted transaction in the block (the size of Uint32).
30+
EncryptedDataGasLen = 4
31+
2732
// EncryptedDataHashLen is the amount of bytes that represents the hash of an
2833
// encrypted transaction.
2934
EncryptedDataHashLen = common.HashLength
@@ -34,7 +39,7 @@ var (
3439
// a simple gas transfer with 1 gwei (105 bytes) is taken as a reference point
3540
// for evaluation of variable-length part; it is padded to be even to the AES
3641
// block size as required by AES encryption rules.
37-
minEncryptedDataSize = EncryptedDataPrefixLen + EncryptedDataRoundLen + EncryptedDataHashLen + tpke.CipherTextSize + 105 + (aes.BlockSize - 105%aes.BlockSize)
42+
minEncryptedDataSize = EncryptedDataPrefixLen + EncryptedDataRoundLen + EncryptedDataGasLen + EncryptedDataHashLen + tpke.CipherTextSize + 105 + (aes.BlockSize - 105%aes.BlockSize)
3843
)
3944

4045
// IsEnvelope checks whether a transaction is an Envelope transaction. The criteria
@@ -64,6 +69,13 @@ func IsEnvelopeData(data []byte) bool {
6469
// GetEncryptedHash returns the hash of inner encrypted transaction specified in an
6570
// unencrypted part of Envelope data. Passing non-Envelope as an argument is a no-op.
6671
func GetEncryptedHash(envelope *types.Transaction) common.Hash {
67-
hashOffset := EncryptedDataPrefixLen + EncryptedDataRoundLen
72+
hashOffset := EncryptedDataPrefixLen + EncryptedDataRoundLen + EncryptedDataGasLen
6873
return common.Hash(envelope.Data()[hashOffset : hashOffset+EncryptedDataHashLen])
6974
}
75+
76+
// GetEncryptedGas returns the gas limit of inner encrypted transaction specified in an
77+
// unencrypted part of Envelope data. Passing non-Envelope as an argument is a no-op.
78+
func GetEncryptedGas(envelope *types.Transaction) uint32 {
79+
gasOffset := EncryptedDataPrefixLen + EncryptedDataRoundLen
80+
return binary.BigEndian.Uint32(envelope.Data()[gasOffset : gasOffset+EncryptedDataGasLen])
81+
}

antimev/tpke_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ func TestTPKE(t *testing.T) {
8282

8383
// Generate an example envelope for privnet verification
8484
var envelopeData = EncryptedDataPrefix
85-
envelopeData = binary.LittleEndian.AppendUint32(envelopeData, 0)
85+
envelopeData = binary.BigEndian.AppendUint32(envelopeData, 0)
86+
envelopeData = binary.BigEndian.AppendUint32(envelopeData, 0)
8687
envelopeData = append(envelopeData, common.MaxHash[:]...)
8788
envelopeData = append(envelopeData, encryptedKey.ToBytes()...)
8889
envelopeData = append(envelopeData, encryptedMsg...)
@@ -154,7 +155,8 @@ func TestGenerateEncryptedTx(t *testing.T) {
154155
}
155156
// Generate envelope.
156157
var envelopeData = EncryptedDataPrefix
157-
envelopeData = binary.LittleEndian.AppendUint32(envelopeData, epoch)
158+
envelopeData = binary.BigEndian.AppendUint32(envelopeData, epoch)
159+
envelopeData = binary.BigEndian.AppendUint32(envelopeData, uint32(tx.Gas()))
158160
envelopeData = append(envelopeData, tx.Hash().Bytes()...)
159161
envelopeData = append(envelopeData, encryptedKey.ToBytes()...)
160162
envelopeData = append(envelopeData, encryptedMsg...)

consensus/dbft/amev.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ type envelopeData struct {
3131
func decodeEnvelopeData(buf []byte) (envelopeData, error) {
3232
var (
3333
key = new(tpke.CipherText)
34-
keyOffset = antimev.EncryptedDataPrefixLen + antimev.EncryptedDataRoundLen + antimev.EncryptedDataHashLen
34+
keyOffset = antimev.EncryptedDataPrefixLen + antimev.EncryptedDataRoundLen + antimev.EncryptedDataGasLen + antimev.EncryptedDataHashLen
3535
cipherTextOffset = keyOffset + tpke.CipherTextSize
3636
)
3737
// It's guaranteed by Envelope definition that buf has a proper length.
3838
_, err := key.FromBytes(buf[keyOffset:cipherTextOffset])
3939
if err != nil {
4040
return envelopeData{}, fmt.Errorf("failed to decode TPKE cipher text: %w", err)
4141
}
42-
round := binary.LittleEndian.Uint32(buf[antimev.EncryptedDataPrefixLen:keyOffset])
42+
round := binary.BigEndian.Uint32(buf[antimev.EncryptedDataPrefixLen:keyOffset])
4343
if round == 0 {
4444
return envelopeData{}, fmt.Errorf("invalid TPKE cipher text: invalid round %d", round)
4545
}

consensus/dbft/dbft.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ func (c *DBFT) processPreBlockCb(b dbft.PreBlock[common.Hash]) error {
11421142
break
11431143
}
11441144
}
1145-
err = c.validateDecryptedTx(parent, decryptedTx, pre.transactions[i])
1145+
err = c.validateDecryptedTx(parent, decryptedTx, pre.transactions[i], pre.finalReceipts[i])
11461146
if err != nil {
11471147
if fallbackToEnvelope(i, true, fmt.Sprintf("decrypted transaction verification failed: %s", err)) {
11481148
continue
@@ -1232,7 +1232,7 @@ func (c *DBFT) newLocalPool(parent *types.Header) *legacypool.LegacyPool {
12321232
}
12331233

12341234
// validateDecryptedTx checks the validity of the transaction to determine whether the outer envelope transaction should be replaced.
1235-
func (c *DBFT) validateDecryptedTx(head *types.Header, decryptedTx *types.Transaction, envelope *types.Transaction) error {
1235+
func (c *DBFT) validateDecryptedTx(head *types.Header, decryptedTx *types.Transaction, envelope *types.Transaction, envelopeReceipt *types.Receipt) error {
12361236
// Make sure the transaction is signed properly and has the same sender and nonce with envelope
12371237
if decryptedTx.Nonce() != envelope.Nonce() {
12381238
return fmt.Errorf("decryptedTx nonce mismatch: decryptedNonce %v, envelopeNonce %v", decryptedTx.Nonce(), envelope.Nonce())
@@ -1262,6 +1262,15 @@ func (c *DBFT) validateDecryptedTx(head *types.Header, decryptedTx *types.Transa
12621262
if decryptedTx.Hash().Cmp(expectedH) != 0 {
12631263
return fmt.Errorf("decryptedTx hash mismatch: expected %s, got %s", expectedH, decryptedTx.Hash())
12641264
}
1265+
// Ensure decrypted gas limit is the same as the envelope declared
1266+
expectedG := antimev.GetEncryptedGas(envelope)
1267+
if decryptedTx.Gas() != uint64(expectedG) {
1268+
return fmt.Errorf("decryptedTx gas limit mismatch: expected %v, got %v", expectedG, decryptedTx.Gas())
1269+
}
1270+
// Ensure decrypted gas limit has been allocated by Envelope
1271+
if decryptedTx.Gas() > envelopeReceipt.GasUsed {
1272+
return fmt.Errorf("decryptedTx gas limit not allocated: needed %v, got %v", decryptedTx.Gas(), envelopeReceipt.GasUsed)
1273+
}
12651274

12661275
return nil
12671276
}

privnet/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ from: '5e6D9680428e6fe62a09BBb6AC23Df5bFE069AE8',
342342
to: '1212000000000000000000000000000000000003',
343343
value: 250,
344344
gas_price: 10,
345-
data: '0xffffffff01000000777bbe0bb1e4c34310c50160c1a22e66bfab8531c54b9bc87739a17eff6fd15a902eeed7615e060e8620866d05e73c41f864150a545954f908e3e9d4be7c46eeeefd7c41c6be8e88aa01bd241df090b1ab1c0cb95a5ec5b8a70ff23d8918849ed6f9c74d6ee8a43a560e4b8fb4326cbf1ea71900119d5713f1a25286e0f48321ab1c4d37d03ed991191ea2d9d94198c9761b65fae7a755bfba1d6a2794ddffaa21d0e98f5f7e93620ab43343b43f50b916813972a31c4c0cc34a7eac7e48d7835b92c36b5e0b962a5c95d42f574349705eda3f70282160c308049967e39b5e179fc3f4d750b429b3a75c0a3932e801d044721da5a88e2a77a14ff1f916578202c3af4f960d0554c30fa1df3e8f1061a9630d8646db5a84c50dc2dd5c47a18dcc434756e0be2116c42872619c8065424b68a223fdb7e9666fbdc59ee8325ec4ab9d7e9e97c704feefaac54f8bbd1c7a2b'
345+
data: '0xffffffff0000000100005208777bbe0bb1e4c34310c50160c1a22e66bfab8531c54b9bc87739a17eff6fd15a80f8c8c22d6379ff80f22d0b5df932c9333ed77854fecb1adab2fb3028a11235c44f7fc0a5c5167782142bdcc904816fab28a18214e2a10cd4ea9dbed21a232d4ae1bbcf9657ce37524931e4f416392e16e2bf70af30d0fefc42effa7c4b1a53a59cc49002c3dc9af6f438095376500b26e6e3878a290078b5ce9ce76bc40c990dc8c3e9bcee8261ac67f1b6a924d6630798b02e12d235524266e1fcba1a2e56b3095266057fc431dd8285f61f49f784d80ab3bedb8b5935f818d3caa1f1f787615e3bd33e3d980902da98b9960b11a2fda7df415d3ebdee5c0bf77eaf803e382b1a9bca13e21bde75e06d4884217f24d6f0652839d61703932c1eb9b038e49e6be3c4c20d21ee63548e036e51525eec63d4a516bf487a5b6d8462aacd72f24d83a71640a0752fbe534d5111fa6a1810'
346346
});
347347
```
348348

privnet/four/genesis_privnet.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@
244244
},
245245
"0x1212100000000000000000000000000000000003": {
246246
"balance": "0",
247-
"code": "0x60806040526004361061007e575f3560e01c80634f1ef2861161004d5780634f1ef2861461015757806352d1902d1461016a578063ad3cb1cc1461018c578063f13c03af146101c957610085565b80631633da6e146100ca578063180cb47f146100f457806326bd0c4e146101275780633ccfd60b1461014357610085565b3661008557005b7fffffffff000000000000000000000000000000000000000000000000000000005f358116146100c857604051631cd4b64760e21b815260040160405180910390fd5b005b3480156100d5575f80fd5b506100de6101e1565b6040516100eb91906106fd565b60405180910390f35b3480156100ff575f80fd5b5061010f600161090960911b0181565b6040516001600160a01b0390911681526020016100eb565b348015610132575f80fd5b5061010f600362012121608c1b0181565b34801561014e575f80fd5b506100c8610252565b6100c86101653660046107a2565b610294565b348015610175575f80fd5b5061017e6102b3565b6040519081526020016100eb565b348015610197575f80fd5b506101bc604051806040016040528060058152602001640352e302e360dc1b81525081565b6040516100eb9190610866565b3480156101d4575f80fd5b5061010f61090960911b81565b6060600161090960911b016001600160a01b0316639f9d7f816040518163ffffffff1660e01b81526004015f60405180830381865afa158015610226573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f1916820160405261024d9190810190610898565b905090565b33600161090960911b011461027a57604051632d5be4cb60e21b815260040160405180910390fd5b471561029257610292600161090960911b01476102e1565b565b61029c610370565b6102a5826103e7565b6102af828261040f565b5050565b5f6102bc6104e3565b507f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc90565b604080515f808252602082019092526001600160a01b03841690839060405161030a9190610945565b5f6040518083038185875af1925050503d805f8114610344576040519150601f19603f3d011682016040523d82523d5f602084013e610349565b606091505b505090508061036b576040516312171d8360e31b815260040160405180910390fd5b505050565b30600362012121608c1b0114806103c95750600362012121608c1b016103bd7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc546001600160a01b031690565b6001600160a01b031614155b156102925760405163703e46dd60e11b815260040160405180910390fd5b3361090960911b1461040c57604051637bfa4b9f60e01b815260040160405180910390fd5b50565b816001600160a01b03166352d1902d6040518163ffffffff1660e01b8152600401602060405180830381865afa925050508015610469575060408051601f3d908101601f1916820190925261046691810190610960565b60015b61049657604051634c9c8ce360e01b81526001600160a01b03831660048201526024015b60405180910390fd5b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc81146104d957604051632a87526960e21b81526004810182905260240161048d565b61036b838361050c565b30600362012121608c1b01146102925760405163703e46dd60e11b815260040160405180910390fd5b61051582610561565b6040516001600160a01b038316907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b905f90a28051156105595761036b82826105e4565b6102af610656565b806001600160a01b03163b5f0361059657604051634c9c8ce360e01b81526001600160a01b038216600482015260240161048d565b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc805473ffffffffffffffffffffffffffffffffffffffff19166001600160a01b0392909216919091179055565b60605f80846001600160a01b0316846040516106009190610945565b5f60405180830381855af49150503d805f8114610638576040519150601f19603f3d011682016040523d82523d5f602084013e61063d565b606091505b509150915061064d858383610675565b95945050505050565b34156102925760405163b398979f60e01b815260040160405180910390fd5b60608261068a57610685826106d4565b6106cd565b81511580156106a157506001600160a01b0384163b155b156106ca57604051639996b31560e01b81526001600160a01b038516600482015260240161048d565b50805b9392505050565b8051156106e45780518082602001fd5b604051630a12f52160e11b815260040160405180910390fd5b602080825282518282018190525f9190848201906040850190845b8181101561073d5783516001600160a01b031683529284019291840191600101610718565b50909695505050505050565b6001600160a01b038116811461040c575f80fd5b634e487b7160e01b5f52604160045260245ffd5b604051601f8201601f1916810167ffffffffffffffff8111828210171561079a5761079a61075d565b604052919050565b5f80604083850312156107b3575f80fd5b82356107be81610749565b915060208381013567ffffffffffffffff808211156107db575f80fd5b818601915086601f8301126107ee575f80fd5b8135818111156108005761080061075d565b610812601f8201601f19168501610771565b91508082528784828501011115610827575f80fd5b80848401858401375f848284010152508093505050509250929050565b5f5b8381101561085e578181015183820152602001610846565b50505f910152565b602081525f8251806020840152610884816040850160208701610844565b601f01601f19169190910160400192915050565b5f60208083850312156108a9575f80fd5b825167ffffffffffffffff808211156108c0575f80fd5b818501915085601f8301126108d3575f80fd5b8151818111156108e5576108e561075d565b8060051b91506108f6848301610771565b818152918301840191848101908884111561090f575f80fd5b938501935b83851015610939578451925061092983610749565b8282529385019390850190610914565b98975050505050505050565b5f8251610956818460208701610844565b9190910192915050565b5f60208284031215610970575f80fd5b505191905056fea2646970667358221220706dfb7aaeb9fb5030c8246e1f649cf9583ae4783d73e7699ce35c14d28c4e1c64736f6c63430008190033"
247+
"code": "0x60806040818152600491823610156101e8575b5036156101e6577fffffffff00000000000000000000000000000000000000000000000000000000600160e01b5f358216016101d85736600c116101d45781519061005c826107b6565b83825260089060209085600860208601376024925f6024860152868551036101a657939291905f945f945b8886106101275788888860e01c61520881106101195763ffffffff90615207190181811161010657825163192f575d60e31b602082019081528582525f93849390926100d2816107b6565b5192309116fa6100e0610824565b81156100fb575b506100ee57005b5163384042ed60e11b8152fd5b9050511515836100e7565b601184634e487b7160e01b5f525260245ffd5b50516304c5ed9760e51b8152fd5b9091929394958151871015610194578187018501517fff0000000000000000000000000000000000000000000000000000000000000016600388901b881589820486141715610182571c841617956001019493929190610087565b8760118c634e487b7160e01b5f52525ffd5b8560328a634e487b7160e01b5f52525ffd5b855162461bcd60e51b81526020818901526008602482015267426164206461746160c01b6044820152606490fd5b5f80fd5b5051631cd4b64760e21b8152fd5b005b5f3560e01c9081631633da6e1461066c57508063180cb47f1461064a57806326bd0c4e146106275780633ccfd60b146105985780634f1ef2861461035c57806352d1902d14610305578063ad3cb1cc1461028b578063c97abae8146102755763f13c03af14610257575f610012565b346101d4575f3660031901126101d4576020905161090960911b8152f35b346101d4575f3660031901126101d4575b610286565b50346101d4575f3660031901126101d4578051906102a8826107b6565b600582526020640352e302e360dc1b60208401528151928391602083528151918260208501525f5b8381106102ef5750505f83830185015250601f01601f19168101030190f35b81810183015187820187015286945082016102d0565b50346101d4575f3660031901126101d457600362012121608c1b01300361034f57602090517f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc8152f35b5163703e46dd60e11b8152fd5b5090816003193601126101d4578035916001600160a01b0390818416918285036101d45760249485359167ffffffffffffffff83116101d457366023840112156101d457828601356103ad81610808565b916103ba865193846107e6565b81835260209182840195368b83830101116101d457815f928c869301893785010152600362012121608c1b0180301491821561056a575b505061055a5761090960911b330361054a5784516352d1902d60e01b815290808289818a5afa9182915f9361051a575b505061043c57505050505191634c9c8ce360e01b8352820152fd5b858588928a7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc918281036105055750863b156104f15750805473ffffffffffffffffffffffffffffffffffffffff19168317905551907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b5f80a28251156104d95750505f6101e69381925190845af46104d3610824565b91610853565b9250925050346104e557005b63b398979f60e01b8152fd5b9450505191634c9c8ce360e01b8352820152fd5b8351632a87526960e21b815280870191909152fd5b9080929350813d8311610543575b61053281836107e6565b810103126101d45751905f80610421565b503d610528565b8451637bfa4b9f60e01b81528790fd5b845163703e46dd60e11b81528790fd5b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5416141590505f806103f1565b50346101d4575f3660031901126101d457600161090960911b0180330361061957476105c057005b479082516020810181811067ffffffffffffffff821117610606575f93849384809481948952525af16105f1610824565b50156105f957005b516312171d8360e31b8152fd5b604186634e487b7160e01b5f525260245ffd5b5051632d5be4cb60e21b8152fd5b50346101d4575f3660031901126101d45760209051600362012121608c1b018152f35b50346101d4575f3660031901126101d45760209051600161090960911b018152f35b839150346101d4575f3660031901126101d457639f9d7f8160e01b81525f818381600161090960911b015afa9182156107ac575f926106f5575b5050908051918291602090602080850191818652845180935285019301915f5b8281106106d557505050500390f35b83516001600160a01b0316855286955093810193928101926001016106c6565b9091503d805f843e61070781846107e6565b820160209182848303126101d457835167ffffffffffffffff948582116101d457019082601f830112156101d457815194851161079957508360051b90855194610753858401876107e6565b855283808601928201019283116101d4578301905b82821061077a575050505082806106a6565b81516001600160a01b03811681036101d4578152908301908301610768565b604190634e487b7160e01b5f525260245ffd5b83513d5f823e3d90fd5b6040810190811067ffffffffffffffff8211176107d257604052565b634e487b7160e01b5f52604160045260245ffd5b90601f8019910116810190811067ffffffffffffffff8211176107d257604052565b67ffffffffffffffff81116107d257601f01601f191660200190565b3d1561084e573d9061083582610808565b9161084360405193846107e6565b82523d5f602084013e565b606090565b9061087a575080511561086857805190602001fd5b604051630a12f52160e11b8152600490fd5b815115806108ad575b61088b575090565b604051639996b31560e01b81526001600160a01b039091166004820152602490fd5b50803b1561088356fea26469706673582212208542001ff2676057e70521f389cad26fc14d14d4f7b6b4a6d4b6abe817cd1c8a64736f6c63430008190033"
248248
},
249249
"0x1212100000000000000000000000000000000004": {
250250
"balance": "0",

0 commit comments

Comments
 (0)