openzeppelin-contracts icon indicating copy to clipboard operation
openzeppelin-contracts copied to clipboard

Improve gas efficiency in ECDSA

Open TheGreatHB opened this issue 3 years ago • 2 comments

Fixes #3853

This PR reduces gas usage of toTypedDataHash toEthSignedMessageHash(bytes32) toEthSignedMessageHash(bytes).

Here are some results:

[toTypedDataHash]

Input                                                                          | Current | New  | Gas saved
-------------------------------------------------------------------------------|---------|------|-----------------
0x6afaff4708d235e0fbf2e9ecd1fbfeb0326cd4801f43d3ca2c0e52d8c2967801 ,           | 198     | 126  | -72   (-36.36%)
0x07785110ea31d256c912208a2875b68e6db3099b5b1d9b3e3035a03fc513f5fa             |         |      |  
[toEthSignedMessageHash(bytes32)]

Input                                                                          | Current | New  | Gas saved
-------------------------------------------------------------------------------|---------|------|-----------------
0x6afaff4708d235e0fbf2e9ecd1fbfeb0326cd4801f43d3ca2c0e52d8c2967801             | 166     | 73   | -93   (-56.02%)
[toEthSignedMessageHash(bytes)]

Input                                                                          | Current | New  | Gas saved
-------------------------------------------------------------------------------|---------|------|-----------------
0x (0 bytes)                                                                   | 971     | 706  | -265  (-27.29%)
                                                                               |         |      | 
0xd6 (1 bytes)                                                                 | 1038    | 773  | -265  (-25.53%)
                                                                               |         |      | 
0xdd66 (2 bytes)                                                               | 1038    | 773  | -265  (-25.53%)
                                                                               |         |      | 
0xb41fcf9e (4 bytes)                                                           | 1038    | 773  | -265  (-25.53%)
                                                                               |         |      | 
0x096739f04d2a1f8f (8 bytes)                                                   | 1047    | 779  | -268  (-25.60%)
                                                                               |         |      | 
0x9e9c1d8a527a7a2c96478f2cd656e80c (16 bytes)                                  | 1124    | 856  | -268  (-23.84%)
                                                                               |         |      | 
0xc561a711bf976ba7ce3e2d4a9de2910c29343df56787ddb6580bde1d26e5250a (32 bytes)  | 1124    | 856  | -268  (-23.84%)
                                                                               |         |      | 
0xaa6a93e1d641813798556c75d1f94b8889fe7b85dc20583f7dc4b3e1a5a296eed69dec40e4be4| 1200    | 932  | -268  (-22.33%)
d25ad4445528eec261b15ee7fcb184f910d8c497403a12e22f3 (64 bytes)                 |         |      | 
                                                                               |         |      | 
0xf127b0025a8bef80a123dd132d39eca568ee23a65790e88b10e1b63e78c898331e48d28f771d6| 1439    | 1171 | -268  (-18.62%)
6bb0acb92bf504474b78c11ff1a17608c98ad010d8f4958d4bbd1d1bb5d75106d79fd1129c4ec28|         |      | 
4a95b2b5363b6dfc39cc3ebf2f4b456e172e434fd5051878276f7ca21254af6572c20b3c9a7f72a|         |      | 
8ba8fc83d5f6fb2c24ef6 (128 bytes)                                              |         |      | 
                                                                               |         |      | 
0xf96785d724671d6026400cf52bd054817d8fcb44a6ada4d049f6a2688765ac2180da016c2a6b0| 1744    | 1476 | -268  (-15.37%)
b0a4a9ac6c0ae5f7e567e7ff3a8602390fd6857894fafc9d5ebd6ad58461f6ddf9efbbb15e59c8f|         |      | 
adbf78ceed91b1042846f6aa0505d08ba78655f9d5863346d2baf4a914a0f13baf64459d3481c0f|         |      | 
9f435a7e806cdadd5ce9c6ad7d84608b76994e103dc8c8e893dad51b785de9591116b3638beefaa|         |      | 
d3d13b3580a61037b2e4ed9c054119fac5fa77eeec65ed6eb25e1fd82f7a92ba41af6f932416ebd|         |      | 
a9285902f8b340a169d471d264080ec9f17dd76bd3173c33a19bf4c684b4c58d865bc0b74f5c159|         |      | 
9f2580cd3ac24ff8fc6067abfabc2df1b1f4e98a (256 bytes)                           |         |      | 
                                                                               |         |      | 
0xaa....bb (512 bytes)                                                         | 2354    | 2086 |  -268  (-11.38%)
                                                                               |         |      | 
0xcc....dd (1024 bytes)                                                        | 3652    | 3384 |  -268  (-7.34% )
                                                                               |         |      | 
0xee....ff (2048 bytes)                                                        | 6104    | 5835 |  -269  (-4.41% )
                                                                               |         |      | 
0x00....11 (4096 bytes)                                                        | 11043   | 10773|  -270  (-2.44% )
                                                                               |         |      | 

PR Checklist

  • [x] Tests
  • [x] Documentation
  • [x] Changelog entry

TheGreatHB avatar Dec 04 '22 04:12 TheGreatHB

I agree that toEthSignedMessageHash and toTypedDataHash functions are probably the most efficient implementations written in Solidity Assembly, btw we are using the same in @1inch: https://github.com/1inch/solidity-utils/blob/95bc66ff4267ae112f97b8296c2515a6dd999449/contracts/libraries/ECDSA.sol#L260-L282

k06a avatar Dec 06 '22 14:12 k06a

I agree that toEthSignedMessageHash and toTypedDataHash functions are probably the most efficient implementations written in Solidity Assembly, btw we are using the same in @1inch: https://github.com/1inch/solidity-utils/blob/95bc66ff4267ae112f97b8296c2515a6dd999449/contracts/libraries/ECDSA.sol#L260-L282

I've never seen it before, but it's a good. Thank you letting me know about your good work.

TheGreatHB avatar Dec 06 '22 16:12 TheGreatHB

@frangio Hello? Should I do anything more for merging?

TheGreatHB avatar Jan 29 '23 14:01 TheGreatHB

🦋 Changeset detected

Latest commit: c24ead1c903809efdd1c5e55da4d0a540830dcb9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openzeppelin-solidity Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Jan 30 '23 10:01 changeset-bot[bot]

Not sure why we have changes to lib/forge-std in this PR.

Maybe this PR was created before #3885?

TheGreatHB avatar Feb 06 '23 09:02 TheGreatHB

Not sure why we have changes to lib/forge-std in this PR.

Maybe this PR was created before #3885?

at some point it was "discarded" during a merge of master into this. I cherry picked the corresponding commit to fix things.

Amxx avatar Feb 06 '23 13:02 Amxx

Congrats, your important contribution to this open-source project has earned you a GitPOAP!

GitPOAP: 2023 OpenZeppelin Contracts Contributor:

GitPOAP: 2023 OpenZeppelin Contracts Contributor GitPOAP Badge

Head to gitpoap.io & connect your GitHub account to mint!

Learn more about GitPOAPs here.

gitpoap-bot[bot] avatar Feb 08 '23 01:02 gitpoap-bot[bot]