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

Resolve Issues from PR #2 in zcash/zcash_note_encryption repository #10

Merged
merged 6 commits into from
Jul 30, 2024

Conversation

dmidem
Copy link

@dmidem dmidem commented Jul 30, 2024

This PR addresses the issues and suggestions raised in PR #2 of the zcash/zcash_note_encryption repository. The changes made in this branch include:

  1. Reintroduced constants such as COMPACT_NOTE_SIZE, NOTE_PLAINTEXT_SIZE, and ENC_CIPHERTEXT_SIZE.

  2. Introduced a new NoteBytes trait to handle fixed-size byte arrays used in note encryption. Added NoteBytesData generict structure and Implemented NoteBytes for NoteBytesData to encapsulate byte handling and parsing logic.

  3. Updated the Domain trait to use the NoteBytes trait for NotePlaintextBytes, NoteCiphertextBytes, CompactNotePlaintextBytes, and CompactNoteCiphertextBytes.

  4. Added new parsing methods (parse_note_plaintext_bytes, parse_note_ciphertext_bytes, parse_compact_note_plaintext_bytes) to the Domain trait to handle conversions safely. These methods replace the previous use of From traits, which did not handle possible failures due to incorrect byte slice sizes. Updated the encryption and decryption functions to use these new parsing methods.

  5. Renamed extract_memo to split_plaintext_at_memo to better reflect its functionality.

  6. Refactored the extract_tag function to work with the new NoteBytes trait and renamed it to split_ciphertext_at_tag.

Additional Notes

The ShieldedOutput trait methods enc_ciphertext and enc_ciphertext_compact still return copies instead of references due to implementation constraints. Further refactoring may be required to address this.

dmidem and others added 3 commits July 18, 2024 11:21
* Add NoteBytes

* Implement concat manually for wasm

* Fix slice bounds in concat

* Fmt

* Split interface and implementation of NoteBytes

* Add new method to NoteBytes

---------

Co-authored-by: alexeykoren <[email protected]>
@dmidem dmidem requested a review from PaulLaux July 30, 2024 07:08
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.

approved with some minor comments. Please update orchard and sapling crypto after merge

src/lib.rs Outdated
Comment on lines 370 to 372
// FIXME: we can't return a ref to NoteCiphertextBytes as it's not a member of self or
// a static object in saplic-crypto crate (but it is a mamber of self in orchard crate)
// Should we really need to return Option here?

Choose a reason for hiding this comment

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

remove comment

src/lib.rs Outdated
Comment on lines 376 to 378
// FIXME: we can't return a ref to CompactNoteCiphertextBytes as it's not a member of self or
// a static object neither in orchard nor in saplic-crypto crate
// FIXME: Should we return Option<...> instead?

Choose a reason for hiding this comment

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

replace comment with

// FIXME: Should we return `Option<D::CompactNoteCiphertextBytes>` or `&D::CompactNoteCiphertextBytes` instead? (complexity)

src/lib.rs Outdated

let (plaintext, tail) = enc_ciphertext.split_at_mut(tag_loc);
let tag: [u8; AEAD_TAG_SIZE] = tail.try_into().expect("tag is correct length");

Choose a reason for hiding this comment

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

"the length of the tag is correct"

src/lib.rs Outdated
@@ -410,7 +453,7 @@ impl<D: Domain> NoteEncryption<D> {
let tag = ChaCha20Poly1305::new(key.as_ref().into())
.encrypt_in_place_detached([0u8; 12][..].into(), &[], output)
.unwrap();
D::NoteCiphertextBytes::from((output, tag.as_ref()))
D::parse_note_ciphertext_bytes(output, tag.into()).expect("output is correct length")

Choose a reason for hiding this comment

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

"the output length is correct"

return None;
}

let mut data: [u8; N] = [0; N];

Choose a reason for hiding this comment

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

: [u8; N] is redundant

src/lib.rs Outdated
@@ -675,12 +712,17 @@ pub fn try_output_recovery_with_ock<D: Domain, Output: ShieldedOutput<D>>(
}
}

// FIXME: rename to split_ciphertext_at_tag? make it a method of ShieldedOutput trait?

Choose a reason for hiding this comment

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

yes

output: &[u8],
tag: [u8; TAG_SIZE],
) -> Option<NoteBytesData<N>> {
let expected_output_len = N.checked_sub(TAG_SIZE)?;

Choose a reason for hiding this comment

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

nice

@dmidem dmidem merged commit 319c782 into zsa1 Jul 30, 2024
23 checks passed
dmidem added a commit to QED-it/orchard that referenced this pull request Jul 31, 2024
… PR #2 issues resolve (#111)

Orchard has been synced with the changes from [PR
#10](QED-it/zcash_note_encryption#10) in the
`zcash_note_encryption` repository. This update includes the following
changes:

- Implements new `parse_note_plaintext_bytes`,
`parse_note_ciphertext_bytes`, and `parse_compact_note_plaintext_bytes`
methods of the `Domain` trait from `zcash_note_encryption`.
- Uses the `NoteBytes` trait and `NoteBytesData` structure from
`zcash_note_encryption` instead of having local definitions and
implementations.

### Note
This PR uses the `resolve_zcash_pr2_issues` branch of
`zcash_note_encryption` in `Cargo.toml`. Before merging this PR, [PR
#10](https://github.com/zcash/zcash_note_encryption/pull/10) needs to be
merged into the `zsa1` branch of `zcash_note_encryption`. Then, this
Orchard PR branch should be updated to use the `zsa1` branch of
`zcash_note_encryption` befor merging this PR.

---------

Co-authored-by: Dmitry Demin <[email protected]>
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