Skip to content

ZSA burn functionality#28

Closed
alexeykoren wants to merge 28 commits intozsa1from
zsa1-burn
Closed

ZSA burn functionality#28
alexeykoren wants to merge 28 commits intozsa1from
zsa1-burn

Conversation

@alexeykoren
Copy link

@alexeykoren alexeykoren commented Nov 22, 2022

  • Added a method to add assets to burn to the Builder

  • bvk computation now includes the burnt assets

  • Added Tests for bsk/bvk consistency for burning

  • Added E2E tests for assets burning

PaulLaux and others added 15 commits July 18, 2022 12:03
* Added .circleci/config.yml
Implements the issuer keys as

    IssuerAuthorizingKey -> isk
    IssuerVerifyingKey -> ik

Test vectors generated with zcash_test_vectors repo
* Added NoteType to Notes
* Added NoteType to value commitment derivation
* Circleci project setup (#1)

* Added .circleci/config.yml

* Added NoteType to Notes

* reformated file

* updated `derive` for NoteType

* added note_type to value commit derivation

* rustfmt

* updated ci config

* updated ci config

* updated ci config

* updated derive for note_type

* added test for arb note_type

* added test for `native` note type

* zsa-note-encryption: introduce AssetType and encode and decode it in note plaintexts

* zsa-note-encryption: extend the size of compact notes to include asset_type

* fixed clippy warrnings

* rustfmt

* zsa-note-encryption: document parsing requirement

* zsa-note-encryption: revert support of ZSA compact action

* zsa_value: add NoteType method is_native

* zsa-note-encryption: remove dependency on changes in the other crate

* zsa-note-encryption: extract memo of ZSA notes

* zsa-note-encryption: tests (zcash_test_vectors 77c73492)

* zsa-note-encryption: simplify roundtrip test

* zsa-note-encryption: more test vectors (zcash_test_vectors c10da464)

* Circleci project setup (#1)

* Added .circleci/config.yml

* issuer keys implementation (#5)

Implements the issuer keys as

    IssuerAuthorizingKey -> isk
    IssuerVerifyingKey -> ik

Test vectors generated with zcash_test_vectors repo

* Added NoteType to Notes (#2)

* Added NoteType to Notes
* Added NoteType to value commitment derivation

* zsa-note-encryption: use both native and ZSA in proptests

* zsa-note-encryption: test vector commit 51398c93

* zsa-note-encryption: fix after merge

Co-authored-by: Paul <3682187+PaulLaux@users.noreply.github.com>
Co-authored-by: Paul <lauxpaul@protonmail.com>
Co-authored-by: Aurélien Nicolas <info@nau.re>
Co-authored-by: Daniel Benarroch <danielbenarroch92@gmail.com>
+ Updated test bsk_consistent_with_bvk to verify mixed note types.
+ Added NoteType support to the builder and the bundle.
+ added split_flag to SpentInfo and as input to the Circuit (currently commented out)
+ added conditional cv_sum calculation (currently commented out)
+ added padding to actions
- added IssueBundle and IssueAction
- added a builder for IssueBundle
- added verify_issue_bundle() for consensus verification.
- unit tests.
added tests in `tests/zsa.rs`
* disabled split notes and proof check for zsa transfer
* fixes and suggestions

* changed "issuer" to "issuance" as per zcash#356 (comment)

* terminology fixes

* updated naming
* rename 2 note_type -> asset as per  zcash#356 (comment)

* added a dedicated type for "IssuanceAuth"

* disabled codecov github action due to bad behavior. 

* extracted "is_asset_desc_of_valid_size()" into asset_id.rs
* improved `verify_issue_bundle()`
@alexeykoren alexeykoren requested a review from PaulLaux November 22, 2022 21:32
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.

Good first run, added the comments. In addition:

  • Avoid the term burnt since it is past tense. Use burn or for_burn or to_burn.

src/builder.rs Outdated
recipients: Vec<RecipientInfo>,
flags: Flags,
anchor: Anchor,
assets_burnt: Vec<(AssetId, i64)>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Vec seems inappropriate since we can have multiple burns for the same asset. I would go with:

pub struct Builder {
    spends: Vec<SpendInfo>,
    recipients: Vec<RecipientInfo>,
    burn: HashMap<AssetId, ValueSum>,
    flags: Flags,
    anchor: Anchor,
}

Order is also important, let's have flags and anchor at the end.

Copy link
Author

Choose a reason for hiding this comment

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

I was thinking its more of a feature that you can burn different amounts separately, but I agree that use case for that will be pretty artificial

Regarding ValueSum - yeah, actually first I implemented with it and then decided to simplify

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
value_balance: V,
anchor: Anchor,
authorization: T,
assets_burnt: Vec<(AssetId, i64)>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's keep similar terminology and order:

/// A bundle of actions to be applied to the ledger.
#[derive(Clone)]
pub struct Bundle<T: Authorization, V> {
    /// The list of actions that make up this bundle.
    actions: NonEmpty<Action<T::SpendAuth>>,
    /// Orchard-specific transaction-level flags for this bundle.
    flags: Flags,
    /// The net value moved out of the Orchard shielded pool.
    ///
    /// This is the sum of Orchard spends minus the sum of Orchard outputs.
    value_balance: V,
    /// Assets intended for burning.
    burn: Vec<(AssetId, V)>,
    /// The root of the Orchard commitment tree that this bundle commits to.
    anchor: Anchor,
    /// The authorization for this bundle.
    authorization: T,
}

Copy link
Author

Choose a reason for hiding this comment

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

done

tests/zsa.rs Outdated
)
});
assets_to_burn
.iter()
Copy link
Collaborator

Choose a reason for hiding this comment

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

try into_iter() to avoid dereferance.

Copy link
Author

Choose a reason for hiding this comment

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

done

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.

This is better, we are getting there.

src/builder.rs Outdated

/// Add an instruction to burn a given amount of a specific asset.
pub fn add_burn(&mut self, asset: AssetId, value: NoteValue) -> Result<(), &'static str> {
if asset == AssetId::native() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if asset.is_native().into()

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/builder.rs Outdated
Comment on lines +354 to +355
let sum = (*self.burn.get(&asset).unwrap_or(&ValueSum::zero()) + value)
.ok_or("Orchard value operation overflowed")?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's have

        let cur = *self.burn.get(&asset).unwrap_or(&ValueSum::zero());

        let sum = (cur + value).ok_or("Orchard ValueSum operation overflowed")?;

for clarity.

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/builder.rs Outdated
let value_balance = pre_actions
let native_value_balance = pre_actions
.iter()
.filter(|action| action.spend.note.asset() == AssetId::native())
Copy link
Collaborator

Choose a reason for hiding this comment

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

asset().is_native().into()

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/builder.rs Outdated
.and_then(|i| {
V::try_from(i).map_err(|_| Error::ValueSum(value::OverflowError))
})
.unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

unwrap() might panic, you should check and propagate with ? or similar.

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/builder.rs Outdated
Comment on lines +447 to +451
let v_value: V = i64::try_from(value)
.map_err(Error::ValueSum)
.and_then(|i| {
V::try_from(i).map_err(|_| Error::ValueSum(value::OverflowError))
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

extract functionality here and in result_native_value_balance into somthing like

    fn into<V: TryFrom<V>>(v: ValueSum) -> Result<V, Error> {
        i64::try_from(v)
            .map_err(Error::ValueSum)
            .and_then(|i| V::try_from(i).map_err(Error::ValueSum(OverflowError)))
    }

Located as part of the ValueSum functionality.

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/value.rs Outdated
AssetId::native(),
))
)
- arb_values_burnt
Copy link
Collaborator

Choose a reason for hiding this comment

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

arb_values_to_burn

Copy link
Collaborator

Choose a reason for hiding this comment

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

Replace burnt into to_burn across the entire PR.

Copy link
Author

Choose a reason for hiding this comment

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

done


proptest! {
#[test]
fn bsk_consistent_with_bvk_native_with_zsa_transfer_and_burning(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's unify everything into one test that include 4 calls

_bsk_consistent_with_bvk(&native_values, &[], &[], &[]);
_bsk_consistent_with_bvk(&native_values, &[], &[], &assets_burnt);
_bsk_consistent_with_bvk(&native_values, &arb_values, &neg_trapdoors, &[]);
_bsk_consistent_with_bvk(&native_values, &arb_values, &neg_trapdoors, &assets_burnt);

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please rename:
_bsk_consistent_with_bvk into check_binding_signature()
arb_values into asset_values
assets_burnt into burn_values

Copy link
Author

Choose a reason for hiding this comment

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

done

.unwrap();
});
assert!(result.is_err());

Copy link
Collaborator

Choose a reason for hiding this comment

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

use consistent style: zsa_note1 or zsa_spend_1 not both.

Copy link
Author

Choose a reason for hiding this comment

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

done

tests/zsa.rs Outdated
Comment on lines +476 to +477
let value_to_burn: u64 = 3;
let value_to_transfer: u64 = zsa_spend_1.note.value().inner() - value_to_burn;
Copy link
Collaborator

Choose a reason for hiding this comment

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

redundant :u64 (all occurrences)

Copy link
Author

Choose a reason for hiding this comment

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

done

tests/zsa.rs Outdated
)
.unwrap();

// 10. Burn a part of ZSA assets
Copy link
Collaborator

Choose a reason for hiding this comment

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

Burn a partial amount of the ZSA assets

Copy link
Author

Choose a reason for hiding this comment

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

done

@PaulLaux PaulLaux force-pushed the zsa1 branch 2 times, most recently from 5ec6aee to 9405f80 Compare December 6, 2022 19:04
# Conflicts:
#	.circleci/config.yml
#	.github/workflows/bench.yml
#	.github/workflows/ci.yml
#	.github/workflows/lints-stable.yml
#	Cargo.toml
#	README.md
#	benches/note_decryption.rs
#	rust-toolchain
#	src/builder.rs
#	src/bundle.rs
#	src/note_encryption.rs
#	src/value.rs
#	tests/zsa.rs
@alexeykoren alexeykoren closed this Dec 7, 2022
@PaulLaux
Copy link
Collaborator

Closed in favor of #35

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