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

Serialize/Deserialize sapling data in V5 #1860

Closed
wants to merge 3 commits into from

Conversation

oxarbitrage
Copy link
Contributor

Motivation

Parse sapling data in transaction v5: #1829

Solution

It is very similar to V4 but the order of the fields is different in the 2 versions. This complicates a bit the de-duplication efforts.

For the serialize a new type SigShieldedData was added and the ZcashSerialize implementation is done against this new type that haves the shielded data and a flag to control if the serializing of the binding_sig field will be done here or not. V4 needs to serialize the binding_sig later so the flag for it is false while in V5 everything can be done in the same place so this flag is true.

For deserialization the idea is similar but we cant implement a type as zcash_deserialize cant access the instance(no self is available). So by now i implemented this as a function.

The rest is pretty straightforward however i am not sure if this is the best approach, open to options.

Review

Related Issues

If merged this will close #1829

Comment on lines +69 to +72
struct SigShieldedData<'a> {
sig: bool,
shielded_data: &'a Option<ShieldedData>,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

transaction::ShieldedData includes binding_sig, for the Sapling data I'm not sure why this type is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is needed for the order of the fields inside a transaction. In V4 transactions we have the spends and outputs for sapling then the joinsplits and then the binding sig. This is explained in https://github.com/ZcashFoundation/zebra/blob/main/zebra-chain/src/transaction/serialize.rs#L147-L153 and can be verified by checking the fields of the V4 transaction in the spec.

In the other hand in the V5 transactions the binding sig is intermediately after the sapling spends and outputs so it can be serialized right there as there is nothing in the middle.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a doc comment for this type, that explains why it's needed, and how it should be used.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, it looks like we can't use this type, because ShieldedData is different in V4 and V5 transactions.

} else {
None
};
let shielded_data =
Copy link
Contributor

Choose a reason for hiding this comment

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

Transaction::v4 isn't changing so I don't think we need to change this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is to use the same code in V4 and V5 as they are doing almost the same for sapling stuff(except for the order of the fields).

Copy link
Contributor

Choose a reason for hiding this comment

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

Even though they have the same names, the underlying types of these fields are different, so it might be best to just duplicate some code.

@@ -222,21 +226,26 @@ impl Transaction {
// This function returns a boxed iterator because the different
// transaction variants end up having different iterator types
match self {
// JoinSplits with Groth Proofs
// Transactions with ShieldedData
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

/// The shielded data for this transaction, if any.
shielded_data: Option<ShieldedData>,
/// The net value of Sapling spend transfers minus output transfers.
value_balance: Amount,
Copy link
Contributor

@teor2345 teor2345 Mar 7, 2021

Choose a reason for hiding this comment

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

v5 transactions also include the Orchard value balance.

So we'll need to rename shielded_data, ShieldedData, and value_balance to include sapling. We should make this change across V4 and V5 transactions, for consistency.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, we'll need to make this change in this PR, because the v5 transaction format changes the fields inside ShieldedData. So we'll need to:

  • rename ShieldedData to SaplingV4ShieldedData
  • add SaplingV5ShieldedData
  • in a later PR, add OrchardActionData

@dconnolly what's a good name for the new SaplingV5ShieldedData type?
It doesn't have any proofs or signatures, just value commitments, nullifiers, keys, and ciphertext.

Screen Shot 2021-03-08 at 09 02 55

Screen Shot 2021-03-08 at 09 02 39

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about having ShieldedData::V4 and ShieldedData::V5 instead ?

Copy link
Contributor

Choose a reason for hiding this comment

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

What about having ShieldedData::V4 and ShieldedData::V5 instead ?

That's still easily confused with Orchard shielded data. (And a bit confusing with Sprout shielded data as well.)

let shielded_data = self.shielded_data;
match shielded_data {
None => {
// Signal no shielded spends and no shielded outputs.
Copy link
Contributor

@teor2345 teor2345 Mar 7, 2021

Choose a reason for hiding this comment

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

Suggested change
// Signal no shielded spends and no shielded outputs.
// There are no shielded spends and no shielded outputs.
// Since all other fields are omitted, the serialization is the same in V4 and V5 transactions.

Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

The contents of the vSpendsSapling and vOutputsSapling fields has changed in V5 transactions: they contain much less data. So we won't be able to parse them using the existing ShieldedData type.

This isn't quite clear from the spec, because the field sizes are wrong. I've made some comments on the draft spec PR.

Screen Shot 2021-03-08 at 08 58 49

Screen Shot 2021-03-08 at 09 02 39

Screen Shot 2021-03-08 at 09 02 55

@oxarbitrage
Copy link
Contributor Author

Blocked on #1863

@oxarbitrage oxarbitrage added NU-5 Network Upgrade: NU5 specific tasks S-blocked Status: Blocked on other tasks labels Mar 9, 2021
Comment on lines +360 to +364
let shielded_spends = Vec::zcash_deserialize(&mut reader)?;
let shielded_outputs = Vec::zcash_deserialize(&mut reader)?;
let shielded_data =
deserialize_shielded_data(&mut reader, shielded_spends, shielded_outputs)?;
let sapling_value_balance = (&mut reader).zcash_deserialize_into()?;
Copy link
Contributor

Choose a reason for hiding this comment

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

If inputs and outputs are None, these fields do not exist in V5:
#1829 (comment)

I will add a test for this bug to the design in #1863.

After #1863 is completed, please revise all the code in this PR, based on the latest spec.

Screen Shot 2021-03-17 at 11 07 55

@oxarbitrage
Copy link
Contributor Author

This PR was replaced by the already approved and merged design for V5 (#1886), an initial refactor based on the design and implementation in V4 (#1946) and a PR that will be submitted for V5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NU-5 Network Upgrade: NU5 specific tasks S-blocked Status: Blocked on other tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parse Sapling data in Transaction Version 5
3 participants