Canto Identity Subprotocols contest - ReyAdmirado's results

Subprotocols for Canto Identity Protocol.

General Information

Platform: Code4rena

Start Date: 17/03/2023

Pot Size: $36,500 USDC

Total HM: 10

Participants: 98

Period: 3 days

Judge: leastwood

Total Solo HM: 5

Id: 223

League: ETH

Canto Identity Subprotocols

Findings Distribution

Researcher Performance

Rank: 38/98

Findings: 1

Award: $77.59

Gas:
grade-a

🌟 Selected for report: 0

🚀 Solo Findings: 0

1. Multiple address/ID mappings can be combined into a single mapping of an address/ID to a struct, where appropriate

Reads and subsequent writes can be cheaper when a function requires both values and they both fit in the same storage slot. can save ~42 gas per access due to not having to recalculate the key’s keccak256 hash (Gkeccak256 - 30 gas) and that calculation’s associated stack operations.

tokenToName and nftCharacters are accessed once together

2. can make the variable outside the loop to save gas

make the variable outside and only give the value to variable inside

3. Ternary over if ... else

Using ternary operator instead of the if else statement saves gas.

4. public functions not called by the contract should be declared external instead

Contracts are allowed to override their parents’ functions and change the visibility from external to public and can save gas by doing so.

5. Functions guaranteed to revert when called by normal users can be marked payable

If a function modifier or require such as onlyOwner-admin is used, the function will revert if a normal user tries to pay the function. Marking the function as payable will lower the gas cost for legitimate callers because the compiler will not include checks for whether a payment was provided. The extra opcodes avoided are CALLVALUE(2), DUP1(3), ISZERO(3), PUSH2(3), JUMPI(10), PUSH1(3), DUP1(3), REVERT(0), JUMPDEST(1), POP(2) which costs an average of about 21 gas per call to the function, in addition to the extra deployment cost.

6. Optimize names to save gas

Contracts most called functions could simply save gas by function ordering via Method ID. Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions.

See more here. you can use this tool to get the optimized version for function and properties signitures

7. Non-strict inequalities are cheaper than strict ones

In the EVM, there is no opcode for non-strict inequalities (>=, <=) and two operations are performed (> + = or < + =). consider replacing >= with the strict counterpart > and add - 1 to the second side

8. Setting the constructor to payable

You can cut out 10 opcodes in the creation-time EVM bytecode if you declare a constructor payable. Making the constructor payable eliminates the need for an initial check of msg.value == 0 and saves 13 gas on deployment with no security risks.

9. can sort the conditions from cheapest to the most expensive

this will give the possibility that the cheaper condition succeeds and there is no need to do extra operations

if (i > 0 && (i + 1) % 40 == 0 || prevByteWasContinuation || i == lengthInBytes - 1)

should be like this instead

if (prevByteWasContinuation || i == lengthInBytes - 1 || (i > 0 && (i + 1) % 40 == 0))

#0 - c4-judge

2023-04-11T05:30:42Z

0xleastwood marked the issue as grade-a

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter