Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signed Ethereum network and token definitions from host #2410

Merged
merged 19 commits into from Mar 24, 2023

Conversation

marnova
Copy link

@marnova marnova commented Jul 25, 2022

Enables to send signed Ethereum network and token definitions from host.
Related to #15 (high-level plan described in its comment #15 (comment)).

@marnova marnova self-assigned this Jul 25, 2022
@trezor-ci trezor-ci added this to To be reviewed in Pull Requests via automation Jul 25, 2022
@marnova marnova linked an issue Jul 25, 2022 that may be closed by this pull request
Copy link
Contributor

@matejcik matejcik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

partial review for now

common/defs/ethereum/eth_builtin_networks.json Outdated Show resolved Hide resolved
common/defs/README.md Outdated Show resolved Hide resolved
common/defs/README.md Outdated Show resolved Hide resolved
common/defs/README.md Outdated Show resolved Hide resolved
common/defs/README.md Outdated Show resolved Hide resolved
python/src/trezorlib/ethereum.py Outdated Show resolved Hide resolved
python/src/trezorlib/ethereum.py Outdated Show resolved Hide resolved
core/tests/test_apps.ethereum.tokens.py Outdated Show resolved Hide resolved
core/tests/test_apps.ethereum.layout.py Outdated Show resolved Hide resolved
common/protob/messages-ethereum.proto Outdated Show resolved Hide resolved
@grdddj
Copy link
Contributor

grdddj commented Oct 13, 2022

I have some possibly good news including binary size.

So far we have considered the size of tokens.py and networks.py definitions around 90 KB in total - that is their size connected with src/apps/ethereum/tokens.py and networks,py respectively.

Now I tried to actually remove these two files from the build and got incredible results:

...
.flash2    -8_148  1    L-8_148  D0       mp_qstr_frozen_const_pool
.flash2    -13_068 135  L-10_956 D-2_112  src/apps/ethereum/networks.py                                _networks_iterator()
.flash2    -78_727 2054 L-45_911 D-32_816 src/apps/ethereum/tokens.py                                  token_by_chain_address()
.flash2    -87_716 1    L-87_716 D0       [section .flash2]
SUMMARY: 41 rows, -193_024 bytes in total (L-156_640 D-36_384).

There is that ~90 kb size decrease in src/apps/ethereum, but also other big decrease in mp_qstr_frozen_const_pool and mostly .flash2 section - str1 symbol in particular.

This means that if we can get rid of the most information in them, we are looking at a HUGE size decrease.

Holding the thumbs we can really replace it!

@grdddj
Copy link
Contributor

grdddj commented Oct 13, 2022

I also tried to replace tokens.py with tokens.rs and exposing trezorui2.token_by_chain_address(chain_id: int, address: bytes) - it seems to be around 70 kB (say 45 %) smaller.

We could theoretically try to use this with coininfo.py, could save some kilobytes.

@marnova marnova marked this pull request as ready for review November 24, 2022 13:28
Copy link
Contributor

@matejcik matejcik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

legacy part review done.

feel free to rebase before you start working on these changes

legacy/firmware/ethereum_definitions.c Outdated Show resolved Hide resolved
legacy/firmware/ethereum_definitions.c Outdated Show resolved Hide resolved
legacy/firmware/ethereum_definitions.c Outdated Show resolved Hide resolved
legacy/firmware/ethereum_definitions.c Outdated Show resolved Hide resolved
legacy/firmware/ethereum_definitions.c Outdated Show resolved Hide resolved
legacy/firmware/ethereum_networks.c.mako Outdated Show resolved Hide resolved
legacy/firmware/ethereum_definitions.h Outdated Show resolved Hide resolved
legacy/firmware/fsm_msg_ethereum.h Outdated Show resolved Hide resolved
legacy/firmware/fsm_msg_ethereum.h Outdated Show resolved Hide resolved
legacy/firmware/fsm_msg_ethereum.h Outdated Show resolved Hide resolved
Copy link
Contributor

@matejcik matejcik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have rebased on master and squashed the commit history somewhat.
Resolved all comments from before, most of them seem to have been resolved, the others we'll find and fix again. Only thing I'm leaving up is the most recent pending review.

common/protob/protocol.md Show resolved Hide resolved
core/.changelog.d/15.removed Outdated Show resolved Hide resolved
core/src/apps/ethereum/definitions.py Show resolved Hide resolved
core/src/apps/ethereum/definitions.py Outdated Show resolved Hide resolved
core/src/apps/ethereum/definitions.py Outdated Show resolved Hide resolved
core/src/apps/ethereum/keychain.py Outdated Show resolved Hide resolved
core/src/apps/ethereum/layout.py Outdated Show resolved Hide resolved
core/tests/test_apps.ethereum.keychain.py Outdated Show resolved Hide resolved
core/tests/test_apps.ethereum.definitions.py Outdated Show resolved Hide resolved
common/defs/README.md Outdated Show resolved Hide resolved
common/tools/coin_info.py Outdated Show resolved Hide resolved
Copy link
Contributor

@grdddj grdddj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments from reviewing core and python changes

core/src/apps/ethereum/definitions.py Show resolved Hide resolved
core/src/apps/ethereum/definitions.py Outdated Show resolved Hide resolved
core/src/apps/ethereum/definitions.py Show resolved Hide resolved
core/src/apps/ethereum/definitions.py Outdated Show resolved Hide resolved
core/src/apps/ethereum/get_public_key.py Show resolved Hide resolved
python/src/trezorlib/ethereum.py Outdated Show resolved Hide resolved
python/src/trezorlib/ethereum.py Outdated Show resolved Hide resolved
python/src/trezorlib/ethereum.py Outdated Show resolved Hide resolved
python/src/trezorlib/ethereum.py Outdated Show resolved Hide resolved
python/tools/eth_defs_unpack.py Outdated Show resolved Hide resolved
@matejcik matejcik force-pushed the marnova/ethereum_defs_from_host branch from 8c6850c to 0813e48 Compare March 15, 2023 11:42
@matejcik matejcik requested review from mmilata and removed request for prusnak March 15, 2023 13:07
@matejcik
Copy link
Contributor

@mmilata please review mainly, in order of priority:

  • legacy implementation
  • core implementation
  • documentation

device tests are currently broken, that is a known problem

@matejcik matejcik force-pushed the marnova/ethereum_defs_from_host branch from facbadc to d73247c Compare March 16, 2023 15:39
`<TOKEN_ADDRESS>` is all lowercase token address hex without the `0x` prefix.

E.g., this is the URL for Görli TST token:
[https://data.trezor.io/firmware/eth-definitions/chain-id/5/token-7af963cf6d228e564e2a0aa0ddbf06210b38615d.dat]
Copy link
Contributor

@mroz22 mroz22 Mar 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected behavior. also the version on your old-style URL will be rejected by current code in this branch.
i'll try to get the new links up ASAP

@onvej-sl
Copy link
Contributor

By the way, what is the motivation in this case for implementing a k-out-of-n signature using aggregated public keys and aggregated signatures instead of verifying each of k signatures separately?

@matejcik
Copy link
Contributor

By the way, what is the motivation in this case for implementing a k-out-of-n signature using aggregated public keys and aggregated signatures instead of verifying each of k signatures separately?

the main motivation is that CoSi is a thing we know well and we can piggy-back on the same functionality as firmware signing; n.b., it's the only way to sign arbitrary data on Trezor, otherwise we'd need to go with something like T1's SignMessage method.
the other (smaller) reason is space saving, because it's more practical to make the definition blobs as small as possible

@matejcik matejcik force-pushed the marnova/ethereum_defs_from_host branch from 4aa7289 to 8609f48 Compare March 24, 2023 10:47
@matejcik
Copy link
Contributor

For now i am leaving the signing format as is and I'd like to merge, unless you disagree @andrewkozlik @onvej-sl ?
If you feel like we should change it, please file an issue and we'll do that separately.

@andrewkozlik
Copy link
Contributor

By the way, what is the motivation in this case for implementing a k-out-of-n signature using aggregated public keys and aggregated signatures instead of verifying each of k signatures separately?

the main motivation is that CoSi is a thing we know well and we can piggy-back on the same functionality as firmware signing; n.b., it's the only way to sign arbitrary data on Trezor, otherwise we'd need to go with something like T1's SignMessage method. the other (smaller) reason is space saving, because it's more practical to make the definition blobs as small as possible

The problem with CoSi is that it's difficult to use correctly. There is the already mentioned rogue public key attack. In addition, the process of combining the "commitments" is also prone to incorrect usage, which can be exploited to leak the private key.

For now i am leaving the signing format as is and I'd like to merge, unless you disagree @andrewkozlik @onvej-sl ?
If you feel like we should change it, please file an issue and we'll do that separately.

Based on @onvej-sl's evaluation, I don't see any obvious problem with not signing the sigmask.

Previously, an amount whose decimal representation with suffix does not
fit in buffer would not be rendered at all. This looks weird in
"Send                          to address 0xblabla".

Enlarging the buffer lets the amount be stringified. Then it won't fit
on screen so this is not a perfect fix, but "Send 25000000... to
address" is better than before.
In a very weird situation, our declaration of `shutdown()` shadows a
function `shutdown(int, int)` from sys/socket, which _just happens_ to
be called by libxcb when closing the sdl window. This calls
`main_clean_exit` which calls into micropython and causes at best an
uncaught NLR and at worst an outright segfault because by that time the
micropython environment doesn't exist anymore.

I didn't think this sort of thing would be possible but here we are??

Fixed by removing `__shutdown()` and replacing `shutdown` with
`trezor_shutdown`
@matejcik matejcik force-pushed the marnova/ethereum_defs_from_host branch from 8609f48 to 5300790 Compare March 24, 2023 11:24
@prusnak
Copy link
Member

prusnak commented Mar 24, 2023

By the way, what is the motivation in this case for implementing a k-out-of-n signature using aggregated public keys and aggregated signatures instead of verifying each of k signatures separately?

The main motivation was speed of verification. It is faster to verify just one signature instead of verifying 3 signatures. This is very much pronounced in the original T1 bootloader where it takes around 1 second to verify 3 signatures prolonging the boot time, while TT bootloader phase is "instant" (according to naked eye).

@matejcik matejcik merged commit 9244522 into master Mar 24, 2023
7 checks passed
Pull Requests automation moved this from Finalizing to Merged Mar 24, 2023
@matejcik matejcik deleted the marnova/ethereum_defs_from_host branch March 24, 2023 12:24
@bosomt
Copy link

bosomt commented Apr 18, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Approved
Development

Successfully merging this pull request may close these issues.

SignTx should contain the signed definition of the used coin/token
9 participants