Skip to content

refactor(standards): clean up signature.masm and optimize verify_signatures#3230

Open
onurinanc wants to merge 7 commits into
nextfrom
refactor-signature
Open

refactor(standards): clean up signature.masm and optimize verify_signatures#3230
onurinanc wants to merge 7 commits into
nextfrom
refactor-signature

Conversation

@onurinanc

Copy link
Copy Markdown
Collaborator

This PR cleans up signature.masm and optimizes verify_signatures:

  • Removes the redundant scheme-id validation in authenticate_transaction, relying solely on the internal trap in verify_signature_by_scheme.
  • Drops the redundant scheme_id duplication in verify_signature_by_scheme so the value is consumed cleanly on each branch.
  • Removes the neq.0 double-negation prefixes in assert_supported_scheme_word, saving six VM cycles per call.
  • Deletes the unused NUM_OF_APPROVERS_LOC local slot and shrinks verify_signatures locals accordingly.
  • Eliminates the SIGNER_INDEX_LOC store/load by reusing the signer index already held on the stack via dup.12.
  • Keeps each approver's public key on the operand stack instead of round-tripping it through CURRENT_PK_LOC, removing one word store and one word load per loop iteration and dropping the now-unused local.

@PhilippGackstatter PhilippGackstatter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, left a few suggestions.

Comment on lines +225 to +229
# Duplicate the public key and message so that SIG_KEY can be computed by `merge` while a
# copy of the public key is kept on the stack for the signature verification below. This
# avoids round-tripping the public key through local memory.
dupw.1 dupw.1
# => [OWNER_PUB_KEY, MSG, OWNER_PUB_KEY, MSG, i-1]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
# Duplicate the public key and message so that SIG_KEY can be computed by `merge` while a
# copy of the public key is kept on the stack for the signature verification below. This
# avoids round-tripping the public key through local memory.
dupw.1 dupw.1
# => [OWNER_PUB_KEY, MSG, OWNER_PUB_KEY, MSG, i-1]
dupw.1 dupw.1
# => [OWNER_PUB_KEY, MSG, OWNER_PUB_KEY, MSG, i-1]

nit: unnecessary explanation of code

Comment on lines +234 to +235
adv.has_mapkey
# => [SIG_KEY, MSG, i-1]
# => [SIG_KEY, OWNER_PUB_KEY, MSG, i-1]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like neither branch uses SIG_KEY below, so we can drop it right after this instruction.

Comment on lines 216 to 217
exec.active_account::get_initial_map_item
# => [OWNER_PUB_KEY, i-1, MSG]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Btw. why do we call this OWNER_PUB_KEY? Why "owner"? Should we call it APPROVER_PUB_KEY, which would be consistent with APPROVER_MAP_KEY above?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

OWNER_PUB_KEY doesn't make sense to me as well. APPROVER_PUB_KEY seems better.

Comment on lines +244 to +247
# SIG_KEY is no longer needed; drop it and duplicate the message so one copy survives
# verification.
dropw
# => [OWNER_PUB_KEY, MSG, i-1]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: In general, I would avoid commenting drops or duplications since it's typically not hard to figure out why we need these and adding a comment to these adds unnecessary verbosity.

Comment on lines +262 to +264
# Build map key from the signer index, still held on the stack as the loop counter.
dup.12 exec.create_approver_map_key
# => [APPROVER_MAP_KEY, OWNER_PUB_KEY, MSG, MSG, i-1]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: another improvement here would be giving i-1 a more precise name like signer_idx, so we wouldn't need to explain that the signer index is the loop counter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants