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

MASP nullifier uniqueness #2240

Merged
merged 7 commits into from
Dec 29, 2023
Merged

MASP nullifier uniqueness #2240

merged 7 commits into from
Dec 29, 2023

Conversation

grarco
Copy link
Collaborator

@grarco grarco commented Nov 30, 2023

Describe your changes

Addresses #1373.

Implements a nullifier set to prevent double spending in masp transactions. Updates the masp VP to check that the transactions writes the correct nullifiers and that these haven't already been revealed.

NOTE: testing this with an integration or e2e test is impossible cause we cannot force the submission of the tx if the builder fails to construct the shielded Transaction. We'd need to test this with a unit test (which I think would be the correct way regardless of the force issue) but at the moment we don't have the tools to write unit tests for the masp vp (at least to my knowledge)

Indicate on which release or other PRs this topic is based on

v0.27.0

Checklist before merging to draft

  • I have added a changelog
  • Git history is in acceptable state

@grarco grarco marked this pull request as ready for review November 30, 2023 18:25
@grarco grarco requested a review from murisi November 30, 2023 18:25
@murisi murisi requested a review from juped December 1, 2023 06:23
Copy link
Collaborator

@murisi murisi left a comment

Choose a reason for hiding this comment

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

Do you know why the unit and integration tests are failing in the CI? They seem to be timing out for some reason... Do they pass on your machine? Have you tried regenerating the MASP test fixtures maybe?

shared/src/ledger/native_vp/masp.rs Show resolved Hide resolved
@@ -237,6 +239,19 @@ where
);
self.write(&current_tx_key, record.serialize_to_vec())?;
self.write(&head_tx_key, (current_tx_idx + 1).serialize_to_vec())?;
for description in shielded
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see a bit of duplication in how we update the nullifier set in the transaction codes. But maybe getting rid of it is easier said than done and can be deferred to a later PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've tried to improve the thing with a shared function now

@grarco
Copy link
Collaborator Author

grarco commented Dec 1, 2023

Do you know why the unit and integration tests are failing in the CI? They seem to be timing out for some reason... Do they pass on your machine? Have you tried regenerating the MASP test fixtures maybe?

Both unit and integration tests pass on my machine without the need to rebuild the proofs

murisi
murisi previously approved these changes Dec 4, 2023
Copy link
Collaborator

@murisi murisi left a comment

Choose a reason for hiding this comment

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

Nice. Could you please just check out the serialize_to_vec issue in the comments to see if there is anything to it? And if you get the chance (but it's not strictly necessary), could you please try seeing if moving handle_masp_tx from tx_prelude/src/token.rs into core/src/ledger/masp_utils.rs is trivial or not?

shared/src/ledger/native_vp/masp.rs Show resolved Hide resolved
@@ -237,6 +238,7 @@ where
);
self.write(&current_tx_key, record.serialize_to_vec())?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is a bug? We call serialize_to_vec on (Epoch, BlockHeight, TxIndex, Transfer, Transaction) to obtain a Vec<u8>. And then StorageWrite::write itself calls serialize_to_vec again on the Vec<u8> argument it receives producing another Vec<u8>, but this time prefixed with 4 bytes indicating the length of record.serialize_to_vec(). We should probably change this line to self.write(&current_tx_key, record)?; so that we remain compatible with how we write to the same key in /tx_prelude/src/token.rs. No?

@@ -237,6 +238,7 @@ where
);
self.write(&current_tx_key, record.serialize_to_vec())?;
self.write(&head_tx_key, (current_tx_idx + 1).serialize_to_vec())?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same serialize_to_vec issue as above?

@@ -60,6 +61,7 @@ pub fn handle_masp_tx(
);
ctx.write(&current_tx_key, record)?;
ctx.write(&head_tx_key, current_tx_idx + 1)?;
masp_utils::reveal_nullifiers(ctx, shielded)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This deduplication will do. But note that the contexts in which masp_utils::reveal_nullifiers is called are essentially identical. So I am wondering if there is a way to reuse handle_masp_tx wholesale? Like can't we move this whole handle_masp_tx function into core/src/ledger/masp_utils.rs and call it like namada_core::ledger::masp_utils::handle_masp_tx(ctx, transfer, shielded) here and namada_core::ledger::masp_utils::handle_masp_tx(self, shielded.transfer, shielded.masp_tx) from shared/src/vm/host_env.rs and shared/src/ledger/native_vp/ibc/context.rs?

@grarco grarco force-pushed the grarco/nullifier-uniqueness branch from 65d031a to 39e00c5 Compare December 4, 2023 12:03
@grarco grarco requested a review from murisi December 4, 2023 12:59
@grarco
Copy link
Collaborator Author

grarco commented Dec 4, 2023

Nice. Could you please just check out the serialize_to_vec issue in the comments to see if there is anything to it? And if you get the chance (but it's not strictly necessary), could you please try seeing if moving handle_masp_tx from tx_prelude/src/token.rs into core/src/ledger/masp_utils.rs is trivial or not?

Ok, I've moved the entire handle_masp_tx to the masp_utils file. This has also solved the double serialization issue (which I believe like you it was a bug). To be able to share this function also with the IBC context I had to remove the call to insert_verifier(masp_addr) from the function (otherwise I would have needed to implement TxEnv on the IBC context), which shouldn't be a problem because the tx writes some masp keys anyway so those should already trigger the vp.

I had to keep a wrapper inside the IbcStorageContext trait cause otherwise we would have needed to implement StorageRead for IbcActions which I think might be more work

Copy link
Collaborator

@murisi murisi left a comment

Choose a reason for hiding this comment

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

Thanks for updating again. Looks good to me!

This was referenced Dec 4, 2023
@tzemanovic tzemanovic mentioned this pull request Dec 7, 2023
tzemanovic added a commit that referenced this pull request Dec 8, 2023
* origin/grarco/nullifier-uniqueness:
  Refactors `handle_masp_tx`
  Reduce code duplication to reveal masp nullifiers
  Fixes masp vp nullifier validation
  Changelog #2240
  Removes wrong comment
  Masp VP checks uniqueness of nullifiers
  Updates masp tx to reveal nullifiers
brentstone added a commit that referenced this pull request Dec 29, 2023
* origin/grarco/nullifier-uniqueness:
  Refactors `handle_masp_tx`
  Reduce code duplication to reveal masp nullifiers
  Fixes masp vp nullifier validation
  Changelog #2240
  Removes wrong comment
  Masp VP checks uniqueness of nullifiers
  Updates masp tx to reveal nullifiers
@brentstone brentstone merged commit 48d5e32 into main Dec 29, 2023
12 of 14 checks passed
@brentstone brentstone deleted the grarco/nullifier-uniqueness branch December 29, 2023 19:37
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.

3 participants