Skip to content

Zsa Swap Utils #141

Merged
alexeykoren merged 20 commits intozsa_swapfrom
zsa_swap_spendauth_clone
Mar 9, 2025
Merged

Zsa Swap Utils #141
alexeykoren merged 20 commits intozsa_swapfrom
zsa_swap_spendauth_clone

Conversation

@alexeykoren
Copy link

@alexeykoren alexeykoren commented Feb 19, 2025

This PR contains a set of additions/changes that allow ZSA Swaps to be implemented in librustzcash:

  • Adds Swap BundleType and Flag
  • Adds AuthorizedWithProof trait that defines proof() method for both Authorized and ActionGroupAuthorized
  • Adds utility methods e.g. from_parts() for SwapBundle, is_empty() in Builder
  • Changes visibility of some methods e.g. dummy() for testing in librustzcash
  • Removes ActionGroup structure

Copy link
Collaborator

@PaulLaux PaulLaux left a comment

Choose a reason for hiding this comment

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

added comments.
AuthorizedWithProof is critical

const FLAG_OUTPUTS_ENABLED: u8 = 0b0000_0010;
const FLAG_ZSA_ENABLED: u8 = 0b0000_0100;
const FLAGS_EXPECTED_UNSET: u8 = !(FLAG_SPENDS_ENABLED | FLAG_OUTPUTS_ENABLED | FLAG_ZSA_ENABLED);
const FLAG_SWAPS_ENABLED: u8 = 0b0000_1000;
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this compliant with the Swaps zip?

Copy link
Author

Choose a reason for hiding this comment

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

Flag for swaps is not mentioned in the zip, probably should ask Vivek to add it

Comment on lines +7 to +12
use super::{
orchard_domain::OrchardDomainCommon,
zcash_note_encryption_domain::{
build_base_note_plaintext_bytes, Memo, COMPACT_NOTE_SIZE_VANILLA, NOTE_VERSION_BYTE_V2,
},
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should not move existing imports

Copy link
Author

Choose a reason for hiding this comment

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

it's cargo fmt

Copy link
Collaborator

@PaulLaux PaulLaux Feb 20, 2025

Choose a reason for hiding this comment

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

  1. Preserve Import Order
    Add this to your rustfmt.toml:
reorder_imports = false

This prevents Rust from reordering imports.
(This is needed for everything we do.)

Copy link
Author

Choose a reason for hiding this comment

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

Done

src/note.rs Outdated
///
/// [orcharddummynotes]: https://zips.z.cash/protocol/nu5.pdf#orcharddummynotes
pub(crate) fn dummy(
pub fn dummy(
Copy link
Collaborator

Choose a reason for hiding this comment

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

why?
generaly we do not want to expose this.
Consider a soution where the dummy padding is happaning inside orchard (add relevent function)

Copy link
Author

Choose a reason for hiding this comment

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

I need dummy notes and keys to build a tx in librustzcash tests

Copy link
Collaborator

Choose a reason for hiding this comment

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

exposing an unneeded api publicly for the sake of external tests will not fly.
Can we reproduce the conntent of dummy() inside librustzcash?

Copy link
Author

Choose a reason for hiding this comment

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

I would need to duplicate a lot, not sure its the best way. Perhaps we play with test-dependencies and features instead, what do you think?

Copy link
Author

Choose a reason for hiding this comment

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

Dummy methods have been copied to librustzcash

Copy link
Collaborator

Choose a reason for hiding this comment

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

just make sure it is inside #[Test]

src/tree.rs Outdated
impl MerklePath {
/// Generates a dummy Merkle path for use in dummy spent notes.
pub(crate) fn dummy(mut rng: &mut impl RngCore) -> Self {
pub fn dummy(mut rng: &mut impl RngCore) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

same for dummy()

Copy link
Author

Choose a reason for hiding this comment

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

same

Copy link
Author

Choose a reason for hiding this comment

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

done

src/bundle.rs Outdated
}

/// Defines the authorization type of an Orchard bundle with a proof.
pub trait AuthorizedWithProof: Authorization<SpendAuth = redpallas::Signature<SpendAuth>> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do we need a new trait?

Why not add proof() to impl ActionGroupAuthorized { ?

Copy link
Author

Choose a reason for hiding this comment

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

In serailization normal ZSA bundle and ZSA bundle that is inside ActionGroup are almost identical, except Authorization. It's Authorized and ActionGroupAuthorized. To depuplicate serialization I need them both to implement a trait that has a proof() method

Copy link
Collaborator

@PaulLaux PaulLaux Feb 20, 2025

Choose a reason for hiding this comment

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

not sure I follow.
AuthorizedWithProof adds no new information to the content. There is already a proof inside ActionGroupAuthorized. Why do we need a new generic AuthorizedWithProof again?

Copy link
Author

@alexeykoren alexeykoren Feb 20, 2025

Choose a reason for hiding this comment

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

Both ActionGroup and OrchardZSA budnles are Bundle<A, V, D>. They are identical, but in first case A=Authorized, in second A=ActionGroupAuthorized. During serialization we need to write a proof which is a part of both, but it is not a part of any common trait, it is just 2 unrelated fields in 2 unrelated structures. So to deduplicate the code I added this common interface that describes something that is Authorized and has proof method

Copy link
Collaborator

Choose a reason for hiding this comment

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

image

In both cases A is Authorization
let's add fn proof(&self) -> Option<&Proof>; to Authorization and implement it 4 times (InProgress and Unauthorized will return None).

Better than more adding traits with partial intersection.

@QED-it QED-it deleted a comment from what-the-diff bot Feb 27, 2025
Copy link
Collaborator

@PaulLaux PaulLaux left a comment

Choose a reason for hiding this comment

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

much much better. Minor comments + fix Ci to merge

Also, please update description of the Swap PR #116 to reflect the changes and the missing Zip detail comment.

rustfmt.toml Outdated
@@ -0,0 +1 @@
reorder_imports = false No newline at end of file
Copy link
Collaborator

Choose a reason for hiding this comment

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

new line
(consider updating ide setup to be automatic)

zcash_note_encryption_domain::{
build_base_note_plaintext_bytes, Memo, COMPACT_NOTE_SIZE_VANILLA, NOTE_VERSION_BYTE_V2,
},
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

any reason moving the imports?

@alexeykoren alexeykoren merged commit bd81617 into zsa_swap Mar 9, 2025
28 checks passed
@alexeykoren alexeykoren deleted the zsa_swap_spendauth_clone branch March 9, 2025 17:45
alexeykoren added a commit that referenced this pull request Oct 22, 2025
This PR contains a set of additions/changes that allow ZSA Swaps to be
implemented in librustzcash:
- Adds Swap BundleType and Flag
- Adds AuthorizedWithProof trait that defines proof() method for both
Authorized and ActionGroupAuthorized
- Adds utility methods e.g. from_parts() for SwapBundle, is_empty() in
Builder
- Changes visibility of some methods e.g. dummy() for testing in
librustzcash
- Removes ActionGroup structure
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