Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 086c6c6

Browse files
authored
Cleanup (#52)
1 parent f651e16 commit 086c6c6

18 files changed

+215
-1483
lines changed

.gitmodules

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
[submodule "lib/forge-std"]
22
path = lib/forge-std
33
url = https://github.com/foundry-rs/forge-std
4-
[submodule "lib/solmate"]
5-
path = lib/solmate
6-
url = https://github.com/transmissions11/solmate
74
[submodule "lib/frost-evm"]
85
path = lib/frost-evm
96
url = https://github.com/Analog-Labs/frost-evm
10-
[submodule "lib/universal-factory"]
11-
path = lib/universal-factory
12-
url = https://github.com/Analog-Labs/universal-factory
13-
[submodule "lib/evm-interpreter"]
14-
path = lib/evm-interpreter
15-
url = https://github.com/Analog-Labs/evm-interpreter

lib/evm-interpreter

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/solmate

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/universal-factory

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Gateway.sol

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,9 @@ contract Gateway is IGateway, IExecutor, IUpgradable, GatewayEIP712 {
122122
// Load shard from storage
123123
ShardStore.ShardInfo storage signer = ShardStore.getMainStorage().get(signature);
124124

125-
// Load y parity bit, it must be 27 (even), or 28 (odd)
126-
// ref: https://ethereum.github.io/yellowpaper/paper.pdf
127-
uint8 yParity = BranchlessMath.ternaryU8(signer.yParity > 0, 28, 27);
128-
129125
// Verify Signature
130126
require(
131-
Schnorr.verify(yParity, signature.xCoord, uint256(message), signature.e, signature.s),
127+
Schnorr.verify(signer.yParity, signature.xCoord, uint256(message), signature.e, signature.s),
132128
"invalid tss signature"
133129
);
134130
}

src/storage/Shards.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,23 @@ library ShardStore {
195195
*/
196196
function register(MainStorage storage store, TssKey calldata newKey) internal returns (bool) {
197197
// Check y-parity
198-
require((newKey.yParity == 2 || newKey.yParity == 3), "y parity bit must be 2 or 3, cannot register shard");
198+
require((newKey.yParity == 27 || newKey.yParity == 28), "y parity bit must be 27 or 28, cannot register shard");
199199

200200
// Read shard from storage
201201
ShardID id = ShardID.wrap(bytes32(newKey.xCoord));
202202
(bool created, ShardInfo storage stored) = getOrAdd(store, id);
203203

204204
// Check if the shard is already registered
205205
if (!created) {
206-
require(stored.nonce == 1 || newKey.yParity == (stored.yParity | 2), "tsskey.yParity mismatch");
206+
require(stored.nonce == 1 || newKey.yParity == stored.yParity, "tsskey.yParity mismatch");
207207
return false;
208208
}
209209

210210
// Get the current status and nonce
211211
ShardInfo memory shard = stored;
212212

213213
require(
214-
shard.createdAtBlock == 0 || (shard.yParity | 2) == newKey.yParity,
214+
shard.createdAtBlock == 0 || shard.yParity == newKey.yParity,
215215
"the provided y-parity doesn't match the existing y-parity, cannot register shard"
216216
);
217217

@@ -222,7 +222,7 @@ library ShardStore {
222222
stored.createdAtBlock =
223223
BranchlessMath.ternaryU64(shard.createdAtBlock > 0, shard.createdAtBlock, uint64(block.number));
224224
stored.nonce = shard.nonce;
225-
stored.yParity = newKey.yParity & 1;
225+
stored.yParity = newKey.yParity;
226226
return true;
227227
}
228228

@@ -311,7 +311,7 @@ library ShardStore {
311311

312312
if (exists) {
313313
// Check y-parity
314-
require(stored.yParity == (key.yParity & 1), "y parity mismatch, cannot revoke key");
314+
require(stored.yParity == key.yParity, "y parity mismatch, cannot revoke key");
315315
return _revoke(store, id);
316316
}
317317
return false;
@@ -374,7 +374,7 @@ library ShardStore {
374374
if (!success) {
375375
revert ShardNotExists(id);
376376
}
377-
shards[i] = TssKey(shard.yParity + 2, uint256(ShardID.unwrap(id)));
377+
shards[i] = TssKey(shard.yParity, uint256(ShardID.unwrap(id)));
378378
}
379379
return shards;
380380
}

src/utils/GasUtils.sol

Lines changed: 15 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,7 @@ library GasUtils {
4343
/**
4444
* @dev Base cost of the `IExecutor.execute` method.
4545
*/
46-
uint256 internal constant EXECUTION_BASE_COST = EXECUTION_SELECTOR_OVERHEAD + 46960 + 144 + 22;
47-
48-
/**
49-
* @dev Base cost of the `IGateway.submitMessage` method.
50-
*/
51-
uint256 internal constant SUBMIT_BASE_COST = 24064 - 66 + 133 + 44;
52-
53-
/**
54-
* @dev Extra gas cost that any account `Contract or EOA` must pay when calling `IGateway.submitMessage` method.
55-
* This cost is necessary for initialize the account's `nonce` storage slot.
56-
*/
57-
uint256 internal constant FIRST_MESSAGE_EXTRA_COST = 17100 + 6000;
46+
uint256 internal constant EXECUTION_BASE_COST = EXECUTION_SELECTOR_OVERHEAD + 46960 + 144 + 22 - 28 + 43;
5847

5948
/**
6049
* @dev Solidity's reserved location for the free memory pointer.
@@ -87,7 +76,7 @@ library GasUtils {
8776
* @dev Compute the gas cost of memory expansion.
8877
* @param words number of words, where a word is 32 bytes
8978
*/
90-
function memoryExpansionGasCost(uint256 words) internal pure returns (uint256) {
79+
function memoryExpansionGasCost(uint256 words) private pure returns (uint256) {
9180
unchecked {
9281
return (words.saturatingMul(words) >> 9).saturatingAdd(words.saturatingMul(3));
9382
}
@@ -121,67 +110,6 @@ library GasUtils {
121110
}
122111
}
123112

124-
/**
125-
* @dev Compute the gas cost of the `IGateway.submitMessage` method, without the `GatewayProxy` overhead.
126-
* @param messageSize The size of the message in bytes. This is the `gmp.data.length`.
127-
*/
128-
function _submitMessageGasCost(uint16 messageSize) private pure returns (uint256 gasCost) {
129-
unchecked {
130-
gasCost = SUBMIT_BASE_COST;
131-
132-
// `countNonZeros` gas cost
133-
uint256 words = (messageSize + 31) >> 5;
134-
gasCost += (words * 106) + (((words + 254) / 255) * 214);
135-
136-
// CALLDATACOPY
137-
gasCost += words * 3;
138-
139-
// emit GmpCreated() gas cost (8 gas per byte)
140-
gasCost += words << 8;
141-
142-
// Memory expansion cost
143-
words += 17 - 1 + 2;
144-
gasCost += ((words * words) >> 9) + (words * 3);
145-
146-
return gasCost;
147-
}
148-
}
149-
150-
/**
151-
* @dev Compute the gas cost of the `IGateway.submitMessage` method including the `GatewayProxy` overhead.
152-
* @param messageSize The size of the message in bytes. This is the `gmp.data.length`.
153-
*/
154-
function submitMessageGasCost(uint16 messageSize) internal pure returns (uint256 gasCost) {
155-
unchecked {
156-
// Compute the gas cost of the `IGateway.submitMessage` method.
157-
gasCost = _submitMessageGasCost(messageSize);
158-
159-
// Convert `gmp.data.length` to `abi.encodeCall(IGateway.submitMessage, (...)).length`.
160-
uint256 calldataSize = ((messageSize + 31) & 0xffe0) + 164;
161-
162-
// Compute the `GatewayProxy` gas overhead.
163-
gasCost += proxyOverheadGasCost(uint16(calldataSize), 32);
164-
165-
return gasCost;
166-
}
167-
}
168-
169-
/**
170-
* @dev Compute the minimal gas needed to execute the `IGateway.submitMessage` method.
171-
* @param messageSize The size of the message in bytes. This is the `gmp.data.length`.
172-
*/
173-
function submitMessageGasNeeded(uint16 messageSize) internal pure returns (uint256 gasNeeded) {
174-
unchecked {
175-
// Compute the gas cost of the `IGateway.submitMessage` method.
176-
gasNeeded = _submitMessageGasCost(messageSize);
177-
// gasNeeded = gasNeeded.saturatingSub(2114);
178-
// gasNeeded = _inverseOfAllButOne64th();
179-
uint256 calldataSize = ((messageSize + 31) & 0xffe0) + 164;
180-
gasNeeded = gasNeeded.saturatingAdd(proxyOverheadGasCost(calldataSize, 32));
181-
gasNeeded = gasNeeded.saturatingSub(36);
182-
}
183-
}
184-
185113
/**
186114
* @dev Estimate the price in wei for send an GMP message.
187115
* @param gasPrice The gas price in UFloat9x56 format.
@@ -213,7 +141,7 @@ library GasUtils {
213141
unchecked {
214142
// add execution cost
215143
uint256 gasCost =
216-
computeExecutionRefund(uint16(BranchlessMath.min(messageSize, type(uint16).max)), gasLimit);
144+
executionGasUsed(uint16(BranchlessMath.min(messageSize, type(uint16).max)), gasLimit);
217145
// add base cost
218146
gasCost = gasCost.saturatingAdd(21000);
219147

@@ -274,28 +202,12 @@ library GasUtils {
274202
}
275203
}
276204

277-
/**
278-
* @dev Compute the gas needed for the transaction, this is different from the gas used.
279-
* `gas needed > gas used` because of EIP-150
280-
*/
281-
function executionGasNeeded(uint256 messageSize, uint256 gasLimit) internal pure returns (uint256 gasNeeded) {
282-
unchecked {
283-
gasNeeded = _executionGasCost(messageSize, gasLimit);
284-
gasNeeded = gasNeeded.saturatingAdd(2300 - 184);
285-
gasNeeded = inverseOfAllButOne64th(gasNeeded);
286-
messageSize = messageSize.align32().saturatingAdd(388);
287-
gasNeeded = gasNeeded.saturatingAdd(proxyOverheadGasCost(messageSize, 64));
288-
// Remove the proxy final overhead, once the message requires (2300 - 184) extra gas.
289-
gasNeeded = gasNeeded.saturatingSub(38);
290-
}
291-
}
292-
293205
/**
294206
* @dev Compute the gas that should be refunded to the executor for the execution.
295207
* @param messageSize The size of the message.
296208
* @param gasUsed The gas used by the gmp message.
297209
*/
298-
function computeExecutionRefund(uint16 messageSize, uint256 gasUsed)
210+
function executionGasUsed(uint16 messageSize, uint256 gasUsed)
299211
internal
300212
pure
301213
returns (uint256 executionCost)
@@ -311,6 +223,17 @@ library GasUtils {
311223
}
312224
}
313225

226+
/**
227+
* @dev Compute the transaction base cost.
228+
*/
229+
function txBaseCost() internal pure returns (uint256) {
230+
unchecked {
231+
uint256 nonZeros = countNonZerosCalldata(msg.data);
232+
uint256 zeros = msg.data.length - nonZeros;
233+
return 21000 + (nonZeros * 16) + (zeros * 4);
234+
}
235+
}
236+
314237
/**
315238
* @dev Count the number of non-zero bytes in a byte sequence from memory.
316239
* gas cost = 217 + (words * 112) + ((words - 1) * 193)
@@ -398,15 +321,4 @@ library GasUtils {
398321
}
399322
}
400323
}
401-
402-
/**
403-
* @dev Compute the transaction base cost.
404-
*/
405-
function txBaseCost() internal pure returns (uint256) {
406-
unchecked {
407-
uint256 nonZeros = countNonZerosCalldata(msg.data);
408-
uint256 zeros = msg.data.length - nonZeros;
409-
return 21000 + (nonZeros * 16) + (zeros * 4);
410-
}
411-
}
412324
}

0 commit comments

Comments
 (0)