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

Nu7 implementation #65

Closed
wants to merge 73 commits into from
Closed

Nu7 implementation #65

wants to merge 73 commits into from

Conversation

alexeykoren
Copy link
Collaborator

@alexeykoren alexeykoren commented Jun 26, 2024

ZSA is implemented across all the codebase, including:

  1. TransactionData now contains 2 extra components - OrchardZsaBundle and IssueBundle

  2. Serialization/deserialization of the new components

  3. Digests of the new components are taken into account during calculation of sighash/txid.
    TODO: we are waiting for test vectors and small ordering fixes to be merged to be able to check txid calculation

  4. Nu7 network upgrade is introduced, although currently "nu6" zcash_unstable flag is used to separate ZSA from regular build. All nu7-related "nu6" flags are annotated with /* TODO nu7 */ comment

  5. Builder methods for ZSAs

  6. Wallet-related functionality (zcash_client_backend, zcash_client_sqlite) are disabled since they do not support ZSAs

@QED-it QED-it deleted a comment from what-the-diff bot Jun 26, 2024
@PaulLaux PaulLaux marked this pull request as ready for review July 1, 2024 17:18
Copy link

@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 some minor comments but I have the felling the the diff is incorrect, maybe because all the upstream commits that should not be there.

# run: rustup target add ${{ matrix.target }}
# - name: Build for target
# working-directory: ./ci-build
# run: cargo build --verbose --target ${{ matrix.target }}

Choose a reason for hiding this comment

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

why comment out?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We decided not to touch zcah_client_xxx crates for now and this target requires them

Choose a reason for hiding this comment

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

so let's remove just the relevant lines:

name: Add zcash_client_backend as a dependency of the synthetic crate
working-directory: ./ci-build
run: cargo add --features lightwalletd-tonic --path ../crates/zcash_client_backend

Seems that it can work

Cargo.toml Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
components/zcash_protocol/src/consensus.rs Outdated Show resolved Hide resolved
components/zcash_protocol/src/consensus.rs Outdated Show resolved Hide resolved
supply-chain/config.toml Show resolved Hide resolved
zcash_client_sqlite/src/testing.rs Show resolved Hide resolved
zcash_primitives/src/transaction/builder.rs Outdated Show resolved Hide resolved
dmidem and others added 3 commits July 24, 2024 12:01
This PR updates `librustzcash` to accommodate the recent refactoring
changes in the `orchard` crate. The specific changes include:

- Renamed all references from `orchard_flavors` to `orchard_flavor` to
match the new module name.
- Updated the `OrchardDomain` trait references to `OrchardDomainCommon`
to reflect the trait renaming.
- Adjusted the `Bundle` structure to use the fixed `NoteValue` type for
the `burn` field, replacing the previous generic argument.
- Refactored associated code to align with the new structure and naming
conventions in the `orchard` crate.
- Switched from the `orchardzsa-backward-compatibility-0.8.0-ak` to
`orchardzsa-backward-compatibility-0.8.0` branch in `Cargo.toml` to use
the latest version of the `orchard` crate.

---------

Co-authored-by: Dmitry Demin <[email protected]>
)

This PR makes the following changes:
- reordering of the component fields in `read_action` and `write_action`
in issuance in order to make them spec compliant.
- It sets some of the constant values for NU7 in a way that is
consistent with the generated test vectors from the Python reference
implementation (See QED-it/zcash-test-vectors#22
for the changes to the Python reference implementation).
- Some changes are made to function names to reflect the shift from
preparing ZSAs for V7 rather than V6.
- There are changes to the tests to use the `zcash_unstable` flag in
order to cover both ZSA and vanilla Orchard behavior.
- The test vectors are also expanded to include both the V5 vectors, and
the V7 vectors (behind a `zcash_unstable` flag).
@alexeykoren alexeykoren force-pushed the txv6-separate-bundles-rebased branch from 04ecb20 to f160f25 Compare July 24, 2024 16:25
Copy link

@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.

good start, added some comments, mostly around builder.
In addition:

  • fix diff and redundant commits in the list
  • rename txv7 and v7 to txv6 and v6 respectively.

components/zcash_protocol/src/consensus.rs Outdated Show resolved Hide resolved
components/zcash_protocol/src/consensus.rs Outdated Show resolved Hide resolved
components/zcash_protocol/src/local_consensus.rs Outdated Show resolved Hide resolved
zcash_client_backend/src/scanning.rs Outdated Show resolved Hide resolved
zcash_primitives/src/transaction/builder.rs Outdated Show resolved Hide resolved
zcash_primitives/src/transaction/mod.rs Outdated Show resolved Hide resolved
"Sprout components cannot be present when serializing to the V7 transaction format.",
));
}
self.write_v5_header(&mut writer)?;

Choose a reason for hiding this comment

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

rename to write_header()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's their naming, shall we change it anyway?

Choose a reason for hiding this comment

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

well yes, the functionality is now extended to v6

}
self.write_v5_header(&mut writer)?;
self.write_transparent(&mut writer)?;
self.write_v5_sapling(&mut writer)?;

Choose a reason for hiding this comment

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

rename to write_sapling()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

same here?

Choose a reason for hiding this comment

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

well yes, the functionality is now extended to v6

Comment on lines +1254 to +1255
_orchard_zsa_bundle in orchard_testing::arb_zsa_bundle_for_version(version),
_issue_bundle in issuance::testing::arb_bundle_for_version(version),

Choose a reason for hiding this comment

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

why the _?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

in these proptests I'm not sure its possible to hide it behind feature flag, so I want avoid clippy bitching at unused arguments for non-nu6 builds

zcash_primitives/src/transaction/mod.rs Show resolved Hide resolved
Copy link

@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 some comments. Most impotently try to stick to orchard::issuance api

#[allow(unused_mut)]
let mut upper = None;
#[cfg(zcash_unstable = "nu6")]
{

Choose a reason for hiding this comment

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

sure we need the scope? (x3)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, looks ugly af, but I'm not sure how to do it better. Open to ideas...

Comment on lines -51 to -52
use super::components::amount::NonNegativeAmount;
use super::components::sapling::zip212_enforcement;

Choose a reason for hiding this comment

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

Let's try to avoid moving (to line 41) if not needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Found them a better place

@@ -345,7 +403,13 @@ impl<A: Authorization> TransactionData<A> {
transparent_bundle: Option<transparent::Bundle<A::TransparentAuth>>,
sprout_bundle: Option<sprout::Bundle>,
sapling_bundle: Option<sapling::Bundle<A::SaplingAuth, Amount>>,
orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, Amount>>,
orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, Amount, OrchardVanilla>>,
#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )] orchard_zsa_bundle: Option<

Choose a reason for hiding this comment

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

please make sure a new line after the flag (all occurrences )

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'd be happy but cargo fmt disagrees

Comment on lines 630 to 631
#[cfg(zcash_unstable = "nu6")]
/* TODO nu7 */

Choose a reason for hiding this comment

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

please be consistent about the flag syntax

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yup, weird, fixed

A::OrchardZsaAuth,
B::OrchardZsaAuth,
>,
#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )] f_issue: impl issuance::MapIssueAuth<

Choose a reason for hiding this comment

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

new line

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cargo fmt

Comment on lines +913 to +916
let (bundle, meta) = builder
.build(&mut rng)
.map_err(Error::OrchardBuild)?
.unwrap();

Choose a reason for hiding this comment

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

should be outside of if (before)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we do that Rust will infer OrchardVanilla for the bundle

zcash_primitives/src/transaction/components/issuance.rs Outdated Show resolved Hide resolved
Comment on lines +98 to +108
let flags = read_flags(&mut reader)?;
let value_balance = Transaction::read_amount(&mut reader)?;
let anchor = read_anchor(&mut reader)?;
let proof_bytes = Vector::read(&mut reader, |r| r.read_u8())?;
let actions = NonEmpty::from_vec(
actions_without_auth
.into_iter()
.map(|act| act.try_map(|_| read_signature::<_, redpallas::SpendAuth>(&mut reader)))
.collect::<Result<Vec<_>, _>>()?,
)
.expect("A nonzero number of actions was read from the transaction data.");
Copy link

@PaulLaux PaulLaux Aug 12, 2024

Choose a reason for hiding this comment

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

not sure I understand the question (replay to previous comment)

@@ -134,21 +181,43 @@ pub fn read_cmx<R: Read>(mut reader: R) -> io::Result<ExtractedNoteCommitment> {
})
}

pub fn read_note_ciphertext<R: Read>(mut reader: R) -> io::Result<TransmittedNoteCiphertext> {
let mut tnc = TransmittedNoteCiphertext {
pub fn read_note_ciphertext<R: Read>(

Choose a reason for hiding this comment

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

pub fn read_note_ciphertext<R: Read, D: OrchardDomainCommon>(
    mut reader: R,
) -> io::Result<TransmittedNoteCiphertext<D>> {

    let mut epk = [0; 32];
    let mut enc = vec![0u8; D::ENC_CIPHERTEXT_SIZE];
    let mut out = [0;80];
    
    reader.read_exact(&mut epk)?;
    reader.read_exact(&mut enc)?;
    reader.read_exact(&mut out)?;

    Ok(TransmittedNoteCiphertext::<D> {
        epk_bytes: epk,
        enc_ciphertext: <D>::NoteCiphertextBytes::from_slice(&enc).unwrap(),
        out_ciphertext: out,
    })
}

Ok(tnc)
}

pub fn read_action_without_auth<R: Read>(mut reader: R) -> io::Result<Action<(), OrchardVanilla>> {

Choose a reason for hiding this comment

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

pub fn read_action<R: Read, D: OrchardDomainCommon>(mut reader: R) -> io::Result<Action<(), D>> {
    let cv_net = read_value_commitment(&mut reader)?;
    let nf_old = read_nullifier(&mut reader)?;
    let rk = read_verification_key(&mut reader)?;
    let cmx = read_cmx(&mut reader)?;
      let encrypted_note = read_note_ciphertext(&mut reader)?; // the generic function

    Ok(...)
}

dmidem and others added 7 commits August 20, 2024 10:26
This PR updates the `zcash_primitives` crate to align with the recent
changes in the `sapling-crypto` crate, which was updated to sync with
the latest changes in `zcash_note_encryption`.

### Changes:
- Updated the usage of `enc_ciphertext` to call `.as_ref()` to match the
new return type.
- Adjusted the import of the `ENC_CIPHERTEXT_SIZE` constant to use the
definition from `sapling-crypto` instead of `zcash_note_encryption`, as
the constant has been moved to `sapling-crypto`.

Co-authored-by: Dmitry Demin <[email protected]>
This updates the test vectors to be of version 6 (as opposed to
version 7).

It also provides a new file for the V6 test vectors, an improvement to
having them in the same file but behind feature flags.

It provides a error for when the asset description string is not a UTF8
encoding.
@alexeykoren
Copy link
Collaborator Author

Closed in favor of #73

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.

4 participants