From 6ed9455e00831dfda035949bb4bfa35027b6dc4f Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Wed, 27 Sep 2023 00:20:21 +0530 Subject: [PATCH 01/11] refactoring and renaming sk_iss to imk throughout the project --- src/bundle/burn_validation.rs | 4 ++-- src/issuance.rs | 8 ++++---- src/keys.rs | 22 +++++++++++----------- src/note/asset_base.rs | 12 ++++++------ src/supply_info.rs | 4 ++-- tests/zsa.rs | 4 ++-- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/bundle/burn_validation.rs b/src/bundle/burn_validation.rs index c0cfb84f1..1116a7743 100644 --- a/src/bundle/burn_validation.rs +++ b/src/bundle/burn_validation.rs @@ -83,8 +83,8 @@ mod tests { pub fn get_burn_tuple(asset_desc: &str, value: i64) -> (AssetBase, i64) { use crate::keys::{IssuanceAuthorizingKey, IssuanceKey, IssuanceValidatingKey}; - let sk_iss = IssuanceKey::from_bytes([0u8; 32]).unwrap(); - let isk: IssuanceAuthorizingKey = (&sk_iss).into(); + let imk = IssuanceKey::from_bytes([0u8; 32]).unwrap(); + let isk: IssuanceAuthorizingKey = (&imk).into(); ( AssetBase::derive(&IssuanceValidatingKey::from(&isk), asset_desc), diff --git a/src/issuance.rs b/src/issuance.rs index 29a95fda3..36b779eb4 100644 --- a/src/issuance.rs +++ b/src/issuance.rs @@ -629,8 +629,8 @@ mod tests { ) { let mut rng = OsRng; - let sk_iss = IssuanceKey::random(&mut rng); - let isk: IssuanceAuthorizingKey = (&sk_iss).into(); + let imk = IssuanceKey::random(&mut rng); + let isk: IssuanceAuthorizingKey = (&imk).into(); let ik: IssuanceValidatingKey = (&isk).into(); let fvk = FullViewingKey::from(&SpendingKey::random(&mut rng)); @@ -1278,8 +1278,8 @@ mod tests { let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); - let incorrect_sk_iss = IssuanceKey::random(&mut rng); - let incorrect_isk: IssuanceAuthorizingKey = (&incorrect_sk_iss).into(); + let incorrect_imk = IssuanceKey::random(&mut rng); + let incorrect_isk: IssuanceAuthorizingKey = (&incorrect_imk).into(); let incorrect_ik: IssuanceValidatingKey = (&incorrect_isk).into(); // Add "bad" note diff --git a/src/keys.rs b/src/keys.rs index 7853d4920..9f2a0a865 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -255,11 +255,11 @@ impl IssuanceKey { /// Constructs an Orchard issuance key from uniformly-random bytes. /// /// Returns `None` if the bytes do not correspond to a valid Orchard issuance key. - pub fn from_bytes(sk_iss: [u8; 32]) -> CtOption { - let sk_iss = IssuanceKey(sk_iss); + pub fn from_bytes(imk: [u8; 32]) -> CtOption { + let imk = IssuanceKey(imk); // If isk = 0 (A scalar value), discard this key. - let isk = IssuanceAuthorizingKey::derive_inner(&sk_iss); - CtOption::new(sk_iss, !isk.is_zero()) + let isk = IssuanceAuthorizingKey::derive_inner(&imk); + CtOption::new(imk, !isk.is_zero()) } /// Returns the raw bytes of the issuance key. @@ -295,9 +295,9 @@ impl IssuanceKey { pub struct IssuanceAuthorizingKey(redpallas::SigningKey); impl IssuanceAuthorizingKey { - /// Derives isk from sk_iss. Internal use only, does not enforce all constraints. - fn derive_inner(sk_iss: &IssuanceKey) -> pallas::Scalar { - to_scalar(PrfExpand::ZsaIsk.expand(&sk_iss.0)) + /// Derives isk from imk. Internal use only, does not enforce all constraints. + fn derive_inner(imk: &IssuanceKey) -> pallas::Scalar { + to_scalar(PrfExpand::ZsaIsk.expand(&imk.0)) } /// Sign the provided message using the `IssuanceAuthorizingKey`. @@ -311,8 +311,8 @@ impl IssuanceAuthorizingKey { } impl From<&IssuanceKey> for IssuanceAuthorizingKey { - fn from(sk_iss: &IssuanceKey) -> Self { - let isk = IssuanceAuthorizingKey::derive_inner(sk_iss); + fn from(imk: &IssuanceKey) -> Self { + let isk = IssuanceAuthorizingKey::derive_inner(imk); // IssuanceAuthorizingKey cannot be constructed such that this assertion would fail. assert!(!bool::from(isk.is_zero())); IssuanceAuthorizingKey(conditionally_negate(isk)) @@ -1267,9 +1267,9 @@ mod tests { let ask: SpendAuthorizingKey = (&sk).into(); assert_eq!(<[u8; 32]>::from(&ask.0), tv.ask); - let sk_iss = IssuanceKey::from_bytes(tv.sk).unwrap(); + let imk = IssuanceKey::from_bytes(tv.sk).unwrap(); - let isk: IssuanceAuthorizingKey = (&sk_iss).into(); + let isk: IssuanceAuthorizingKey = (&imk).into(); assert_eq!(<[u8; 32]>::from(&isk.0), tv.isk); let ak: SpendValidatingKey = (&ask).into(); diff --git a/src/note/asset_base.rs b/src/note/asset_base.rs index 41a284a8a..0da770aab 100644 --- a/src/note/asset_base.rs +++ b/src/note/asset_base.rs @@ -102,8 +102,8 @@ impl AssetBase { /// /// This is only used in tests. pub(crate) fn random(rng: &mut impl RngCore) -> Self { - let sk_iss = IssuanceKey::random(rng); - let isk = IssuanceAuthorizingKey::from(&sk_iss); + let imk = IssuanceKey::random(rng); + let isk = IssuanceAuthorizingKey::from(&imk); let ik = IssuanceValidatingKey::from(&isk); let asset_descr = "zsa_asset"; AssetBase::derive(&ik, asset_descr) @@ -165,10 +165,10 @@ pub mod testing { prop_compose! { /// Generate an asset ID pub fn arb_zsa_asset_id()( - sk_iss in arb_issuance_key(), + imk in arb_issuance_key(), str in "[A-Za-z]{255}" ) -> AssetBase { - let isk = IssuanceAuthorizingKey::from(&sk_iss); + let isk = IssuanceAuthorizingKey::from(&imk); AssetBase::derive(&IssuanceValidatingKey::from(&isk), &str) } } @@ -176,10 +176,10 @@ pub mod testing { prop_compose! { /// Generate an asset ID using a specific description pub fn zsa_asset_id(asset_desc: String)( - sk_iss in arb_issuance_key(), + imk in arb_issuance_key(), ) -> AssetBase { assert!(super::is_asset_desc_of_valid_size(&asset_desc)); - let isk = IssuanceAuthorizingKey::from(&sk_iss); + let isk = IssuanceAuthorizingKey::from(&imk); AssetBase::derive(&IssuanceValidatingKey::from(&isk), &asset_desc) } } diff --git a/src/supply_info.rs b/src/supply_info.rs index 1c2f346b1..5b66324ba 100644 --- a/src/supply_info.rs +++ b/src/supply_info.rs @@ -82,8 +82,8 @@ mod tests { fn create_test_asset(asset_desc: &str) -> AssetBase { use crate::keys::{IssuanceAuthorizingKey, IssuanceKey, IssuanceValidatingKey}; - let sk_iss = IssuanceKey::from_bytes([0u8; 32]).unwrap(); - let isk: IssuanceAuthorizingKey = (&sk_iss).into(); + let imk = IssuanceKey::from_bytes([0u8; 32]).unwrap(); + let isk: IssuanceAuthorizingKey = (&imk).into(); AssetBase::derive(&IssuanceValidatingKey::from(&isk), asset_desc) } diff --git a/tests/zsa.rs b/tests/zsa.rs index d3dd1b922..104f8a652 100644 --- a/tests/zsa.rs +++ b/tests/zsa.rs @@ -61,8 +61,8 @@ fn prepare_keys() -> Keychain { let fvk = FullViewingKey::from(&sk); let recipient = fvk.address_at(0u32, Scope::External); - let sk_iss = IssuanceKey::from_bytes([0; 32]).unwrap(); - let isk = IssuanceAuthorizingKey::from(&sk_iss); + let imk = IssuanceKey::from_bytes([0; 32]).unwrap(); + let isk = IssuanceAuthorizingKey::from(&imk); let ik = IssuanceValidatingKey::from(&isk); Keychain { pk, From 09a1d05b3d503e3977cf63f97777e01366e7dce5 Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Tue, 17 Oct 2023 11:56:43 +0530 Subject: [PATCH 02/11] renaming IssuanceKey to IssuanceMasterKey --- src/bundle/burn_validation.rs | 6 +++--- src/issuance.rs | 10 +++++----- src/keys.rs | 28 ++++++++++++++-------------- src/note/asset_base.rs | 4 ++-- src/supply_info.rs | 4 ++-- tests/zsa.rs | 4 ++-- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/bundle/burn_validation.rs b/src/bundle/burn_validation.rs index 1116a7743..6bf59e9e3 100644 --- a/src/bundle/burn_validation.rs +++ b/src/bundle/burn_validation.rs @@ -69,7 +69,7 @@ mod tests { /// Creates an item of bundle burn list for a given asset description and value. /// /// This function is deterministic and guarantees that each call with the same parameters - /// will return the same result. It achieves determinism by using a static `IssuanceKey`. + /// will return the same result. It achieves determinism by using a static `IssuanceMasterKey`. /// /// # Arguments /// @@ -81,9 +81,9 @@ mod tests { /// A tuple `(AssetBase, Amount)` representing the burn list item. /// pub fn get_burn_tuple(asset_desc: &str, value: i64) -> (AssetBase, i64) { - use crate::keys::{IssuanceAuthorizingKey, IssuanceKey, IssuanceValidatingKey}; + use crate::keys::{IssuanceAuthorizingKey, IssuanceMasterKey, IssuanceValidatingKey}; - let imk = IssuanceKey::from_bytes([0u8; 32]).unwrap(); + let imk = IssuanceMasterKey::from_bytes([0u8; 32]).unwrap(); let isk: IssuanceAuthorizingKey = (&imk).into(); ( diff --git a/src/issuance.rs b/src/issuance.rs index 36b779eb4..8c2c1d17d 100644 --- a/src/issuance.rs +++ b/src/issuance.rs @@ -606,7 +606,7 @@ mod tests { }; use crate::issuance::{verify_issue_bundle, IssueAction, Signed, Unauthorized}; use crate::keys::{ - FullViewingKey, IssuanceAuthorizingKey, IssuanceKey, IssuanceValidatingKey, Scope, + FullViewingKey, IssuanceAuthorizingKey, IssuanceMasterKey, IssuanceValidatingKey, Scope, SpendingKey, }; use crate::note::{AssetBase, Nullifier}; @@ -629,7 +629,7 @@ mod tests { ) { let mut rng = OsRng; - let imk = IssuanceKey::random(&mut rng); + let imk = IssuanceMasterKey::random(&mut rng); let isk: IssuanceAuthorizingKey = (&imk).into(); let ik: IssuanceValidatingKey = (&isk).into(); @@ -951,7 +951,7 @@ mod tests { ) .unwrap(); - let wrong_isk: IssuanceAuthorizingKey = (&IssuanceKey::random(&mut OsRng)).into(); + let wrong_isk: IssuanceAuthorizingKey = (&IssuanceMasterKey::random(&mut OsRng)).into(); let err = bundle .prepare([0; 32]) @@ -1183,7 +1183,7 @@ mod tests { ) .unwrap(); - let wrong_isk: IssuanceAuthorizingKey = (&IssuanceKey::random(&mut rng)).into(); + let wrong_isk: IssuanceAuthorizingKey = (&IssuanceMasterKey::random(&mut rng)).into(); let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); @@ -1278,7 +1278,7 @@ mod tests { let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); - let incorrect_imk = IssuanceKey::random(&mut rng); + let incorrect_imk = IssuanceMasterKey::random(&mut rng); let incorrect_isk: IssuanceAuthorizingKey = (&incorrect_imk).into(); let incorrect_ik: IssuanceValidatingKey = (&incorrect_isk).into(); diff --git a/src/keys.rs b/src/keys.rs index 9f2a0a865..091009731 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -227,21 +227,21 @@ type IssuanceAuth = SpendAuth; /// /// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents #[derive(Debug, Copy, Clone)] -pub struct IssuanceKey([u8; 32]); +pub struct IssuanceMasterKey([u8; 32]); -impl From for IssuanceKey { +impl From for IssuanceMasterKey { fn from(sk: SpendingKey) -> Self { - IssuanceKey(*sk.to_bytes()) + IssuanceMasterKey(*sk.to_bytes()) } } -impl ConstantTimeEq for IssuanceKey { +impl ConstantTimeEq for IssuanceMasterKey { fn ct_eq(&self, other: &Self) -> Choice { self.to_bytes().ct_eq(other.to_bytes()) } } -impl IssuanceKey { +impl IssuanceMasterKey { /// Generates a random issuance key. /// /// This is only used when generating a random AssetBase. @@ -256,7 +256,7 @@ impl IssuanceKey { /// /// Returns `None` if the bytes do not correspond to a valid Orchard issuance key. pub fn from_bytes(imk: [u8; 32]) -> CtOption { - let imk = IssuanceKey(imk); + let imk = IssuanceMasterKey(imk); // If isk = 0 (A scalar value), discard this key. let isk = IssuanceAuthorizingKey::derive_inner(&imk); CtOption::new(imk, !isk.is_zero()) @@ -296,7 +296,7 @@ pub struct IssuanceAuthorizingKey(redpallas::SigningKey); impl IssuanceAuthorizingKey { /// Derives isk from imk. Internal use only, does not enforce all constraints. - fn derive_inner(imk: &IssuanceKey) -> pallas::Scalar { + fn derive_inner(imk: &IssuanceMasterKey) -> pallas::Scalar { to_scalar(PrfExpand::ZsaIsk.expand(&imk.0)) } @@ -310,8 +310,8 @@ impl IssuanceAuthorizingKey { } } -impl From<&IssuanceKey> for IssuanceAuthorizingKey { - fn from(imk: &IssuanceKey) -> Self { +impl From<&IssuanceMasterKey> for IssuanceAuthorizingKey { + fn from(imk: &IssuanceMasterKey) -> Self { let isk = IssuanceAuthorizingKey::derive_inner(imk); // IssuanceAuthorizingKey cannot be constructed such that this assertion would fail. assert!(!bool::from(isk.is_zero())); @@ -1116,7 +1116,7 @@ impl SharedSecret { #[cfg_attr(docsrs, doc(cfg(feature = "test-dependencies")))] pub mod testing { use super::{ - DiversifierIndex, DiversifierKey, EphemeralSecretKey, IssuanceAuthorizingKey, IssuanceKey, + DiversifierIndex, DiversifierKey, EphemeralSecretKey, IssuanceAuthorizingKey, IssuanceMasterKey, IssuanceValidatingKey, SpendingKey, }; use proptest::prelude::*; @@ -1140,12 +1140,12 @@ pub mod testing { /// Generate a uniformly distributed Orchard issuance key. pub fn arb_issuance_key()( key in prop::array::uniform32(prop::num::u8::ANY) - .prop_map(IssuanceKey::from_bytes) + .prop_map(IssuanceMasterKey::from_bytes) .prop_filter( "Values must correspond to valid Orchard issuance keys.", |opt| bool::from(opt.is_some()) ) - ) -> IssuanceKey { + ) -> IssuanceMasterKey { key.unwrap() } } @@ -1186,7 +1186,7 @@ pub mod testing { /// Generate a uniformly distributed RedDSA issuance authorizing key. pub fn arb_issuance_authorizing_key()(rng_seed in prop::array::uniform32(prop::num::u8::ANY)) -> IssuanceAuthorizingKey { let mut rng = StdRng::from_seed(rng_seed); - IssuanceAuthorizingKey::from(&IssuanceKey::random(&mut rng)) + IssuanceAuthorizingKey::from(&IssuanceMasterKey::random(&mut rng)) } } @@ -1267,7 +1267,7 @@ mod tests { let ask: SpendAuthorizingKey = (&sk).into(); assert_eq!(<[u8; 32]>::from(&ask.0), tv.ask); - let imk = IssuanceKey::from_bytes(tv.sk).unwrap(); + let imk = IssuanceMasterKey::from_bytes(tv.sk).unwrap(); let isk: IssuanceAuthorizingKey = (&imk).into(); assert_eq!(<[u8; 32]>::from(&isk.0), tv.isk); diff --git a/src/note/asset_base.rs b/src/note/asset_base.rs index 0da770aab..6e9050f85 100644 --- a/src/note/asset_base.rs +++ b/src/note/asset_base.rs @@ -10,7 +10,7 @@ use subtle::{Choice, ConstantTimeEq, CtOption}; use crate::constants::fixed_bases::{ NATIVE_ASSET_BASE_V_BYTES, VALUE_COMMITMENT_PERSONALIZATION, ZSA_ASSET_BASE_PERSONALIZATION, }; -use crate::keys::{IssuanceAuthorizingKey, IssuanceKey, IssuanceValidatingKey}; +use crate::keys::{IssuanceAuthorizingKey, IssuanceMasterKey, IssuanceValidatingKey}; /// Note type identifier. #[derive(Clone, Copy, Debug, Eq)] @@ -102,7 +102,7 @@ impl AssetBase { /// /// This is only used in tests. pub(crate) fn random(rng: &mut impl RngCore) -> Self { - let imk = IssuanceKey::random(rng); + let imk = IssuanceMasterKey::random(rng); let isk = IssuanceAuthorizingKey::from(&imk); let ik = IssuanceValidatingKey::from(&isk); let asset_descr = "zsa_asset"; diff --git a/src/supply_info.rs b/src/supply_info.rs index 5b66324ba..2039ce1ef 100644 --- a/src/supply_info.rs +++ b/src/supply_info.rs @@ -80,9 +80,9 @@ mod tests { use super::*; fn create_test_asset(asset_desc: &str) -> AssetBase { - use crate::keys::{IssuanceAuthorizingKey, IssuanceKey, IssuanceValidatingKey}; + use crate::keys::{IssuanceAuthorizingKey, IssuanceMasterKey, IssuanceValidatingKey}; - let imk = IssuanceKey::from_bytes([0u8; 32]).unwrap(); + let imk = IssuanceMasterKey::from_bytes([0u8; 32]).unwrap(); let isk: IssuanceAuthorizingKey = (&imk).into(); AssetBase::derive(&IssuanceValidatingKey::from(&isk), asset_desc) diff --git a/tests/zsa.rs b/tests/zsa.rs index 104f8a652..9b5585288 100644 --- a/tests/zsa.rs +++ b/tests/zsa.rs @@ -14,7 +14,7 @@ use orchard::{ bundle::Flags, circuit::{ProvingKey, VerifyingKey}, keys::{ - FullViewingKey, IssuanceKey, PreparedIncomingViewingKey, Scope, SpendAuthorizingKey, + FullViewingKey, IssuanceMasterKey, PreparedIncomingViewingKey, Scope, SpendAuthorizingKey, SpendingKey, }, value::NoteValue, @@ -61,7 +61,7 @@ fn prepare_keys() -> Keychain { let fvk = FullViewingKey::from(&sk); let recipient = fvk.address_at(0u32, Scope::External); - let imk = IssuanceKey::from_bytes([0; 32]).unwrap(); + let imk = IssuanceMasterKey::from_bytes([0; 32]).unwrap(); let isk = IssuanceAuthorizingKey::from(&imk); let ik = IssuanceValidatingKey::from(&isk); Keychain { From 9f53f1d649483a45aabd976dcd9075a2c3173c7c Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Wed, 25 Oct 2023 19:44:17 +0530 Subject: [PATCH 03/11] applying fmt suggestions --- src/keys.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/keys.rs b/src/keys.rs index 091009731..c4e5b7dd3 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -1116,8 +1116,8 @@ impl SharedSecret { #[cfg_attr(docsrs, doc(cfg(feature = "test-dependencies")))] pub mod testing { use super::{ - DiversifierIndex, DiversifierKey, EphemeralSecretKey, IssuanceAuthorizingKey, IssuanceMasterKey, - IssuanceValidatingKey, SpendingKey, + DiversifierIndex, DiversifierKey, EphemeralSecretKey, IssuanceAuthorizingKey, + IssuanceMasterKey, IssuanceValidatingKey, SpendingKey, }; use proptest::prelude::*; use rand::{rngs::StdRng, SeedableRng}; From 6ade7ade00123275f4517ed8ba77629351ed6e1b Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Fri, 27 Oct 2023 18:35:18 +0530 Subject: [PATCH 04/11] documentation update for IssuanceMasterKey --- src/keys.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/keys.rs b/src/keys.rs index c4e5b7dd3..4dc04b4ed 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -223,9 +223,9 @@ type IssuanceAuth = SpendAuth; /// An issuance key, from which all key material is derived. /// -/// $\mathsf{sk}$ as defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents]. +/// $\mathsf{imk}$ as defined in [ZIP 227][issuancekeycomponents]. /// -/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents +/// [issuancekeycomponents]: https://qed-it.github.io/zips/zip-0227#issuance-key-derivation #[derive(Debug, Copy, Clone)] pub struct IssuanceMasterKey([u8; 32]); From 8b3df6fe3e9da626e531a2761a9002e346d83a57 Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Mon, 30 Oct 2023 09:40:47 +0530 Subject: [PATCH 05/11] adding functionality to IssuanceMasterKey as a first step to removing IssuanceAuthorizingKey --- src/keys.rs | 65 +++++++++++++++++++++++++++++------------- src/note/asset_base.rs | 8 +++--- 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/keys.rs b/src/keys.rs index 4dc04b4ed..c0f147053 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -29,6 +29,7 @@ use crate::{ ZIP32_ORCHARD_PERSONALIZATION_FOR_ISSUANCE, }, }; +use crate::primitives::redpallas::SigningKey; const KDF_ORCHARD_PERSONALIZATION: &[u8; 16] = b"Zcash_OrchardKDF"; const ZIP32_PURPOSE: u32 = 32; @@ -255,11 +256,14 @@ impl IssuanceMasterKey { /// Constructs an Orchard issuance key from uniformly-random bytes. /// /// Returns `None` if the bytes do not correspond to a valid Orchard issuance key. - pub fn from_bytes(imk: [u8; 32]) -> CtOption { - let imk = IssuanceMasterKey(imk); - // If isk = 0 (A scalar value), discard this key. - let isk = IssuanceAuthorizingKey::derive_inner(&imk); - CtOption::new(imk, !isk.is_zero()) + pub fn from_bytes(imk_bytes: [u8; 32]) -> CtOption { + let imk = IssuanceMasterKey(imk_bytes); + CtOption::new(imk, imk.is_valid()) + } + + /// Checks whether the Orchard-ZSA issuance key is valid + pub fn is_valid(self) -> Choice { + 1u8.into() } /// Returns the raw bytes of the issuance key. @@ -282,8 +286,23 @@ impl IssuanceMasterKey { ExtendedSpendingKey::from_path(seed, path, ZIP32_ORCHARD_PERSONALIZATION_FOR_ISSUANCE) .map(|esk| esk.sk().into()) } + + /// Derives the RedPallas signing key from imk. Internal use only, does not enforce all constraints. + fn derive_inner(&self) -> pallas::Scalar { + to_scalar(PrfExpand::ZsaIsk.expand(&self.0)) + } + + /// Sign the provided message using the `IssuanceMasterKey`. + pub fn sign( + &self, + rng: &mut (impl RngCore + CryptoRng), + msg: &[u8], + ) -> redpallas::Signature { + conditionally_negate(self.derive_inner()).sign(rng, msg) + } } +// TOREMOVE: /// An issuance authorizing key, used to create issuance authorization signatures. /// This type enforces that the corresponding public point (ik^ℙ) has ỹ = 0. /// @@ -292,16 +311,16 @@ impl IssuanceMasterKey { /// /// [IssuanceZSA]: https://qed-it.github.io/zips/draft-ZIP-0227.html#asset-identifier-generation #[derive(Clone, Debug)] -pub struct IssuanceAuthorizingKey(redpallas::SigningKey); +pub struct IssuanceAuthorizingKey(redpallas::SigningKey); //TOREMOVE impl IssuanceAuthorizingKey { /// Derives isk from imk. Internal use only, does not enforce all constraints. - fn derive_inner(imk: &IssuanceMasterKey) -> pallas::Scalar { + fn derive_inner(imk: &IssuanceMasterKey) -> pallas::Scalar { //TOREMOVE to_scalar(PrfExpand::ZsaIsk.expand(&imk.0)) } /// Sign the provided message using the `IssuanceAuthorizingKey`. - pub fn sign( + pub fn sign( //TOREMOVE &self, rng: &mut (impl RngCore + CryptoRng), msg: &[u8], @@ -311,7 +330,7 @@ impl IssuanceAuthorizingKey { } impl From<&IssuanceMasterKey> for IssuanceAuthorizingKey { - fn from(imk: &IssuanceMasterKey) -> Self { + fn from(imk: &IssuanceMasterKey) -> Self { //TOREMOVE let isk = IssuanceAuthorizingKey::derive_inner(imk); // IssuanceAuthorizingKey cannot be constructed such that this assertion would fail. assert!(!bool::from(isk.is_zero())); @@ -321,20 +340,26 @@ impl From<&IssuanceMasterKey> for IssuanceAuthorizingKey { /// A key used to validate issuance authorization signatures. /// -/// Defined in [Issuance of Zcash Shielded Assets ZIP-0227 § Asset Identifier Generation (DRAFT PR)][IssuanceZSA]. +/// Defined in [ZIP 227: Issuance of Zcash Shielded Assets § Issuance Key Generation][IssuanceZSA]. /// Note that this is $\mathsf{ik}^\mathbb{P}$, which by construction is equivalent to /// $\mathsf{ik}$ but stored here as a RedPallas verification key. /// -/// [IssuanceZSA]: https://qed-it.github.io/zips/draft-ZIP-0227.html#asset-identifier-generation +/// [IssuanceZSA]: https://qed-it.github.io/zips/zip-0227#issuance-key-derivation #[derive(Debug, Clone, PartialOrd, Ord)] pub struct IssuanceValidatingKey(VerificationKey); -impl From<&IssuanceAuthorizingKey> for IssuanceValidatingKey { +impl From<&IssuanceAuthorizingKey> for IssuanceValidatingKey { //TOREMOVE fn from(isk: &IssuanceAuthorizingKey) -> Self { IssuanceValidatingKey((&isk.0).into()) } } +impl From<&IssuanceMasterKey> for IssuanceValidatingKey { + fn from(imk: &IssuanceMasterKey) -> Self { + IssuanceValidatingKey((&(conditionally_negate(imk.derive_inner()))).into()) + } +} + impl From<&IssuanceValidatingKey> for pallas::Point { fn from(issuance_validating_key: &IssuanceValidatingKey) -> pallas::Point { pallas::Point::from_bytes(&(&issuance_validating_key.0).into()).unwrap() @@ -1137,8 +1162,8 @@ pub mod testing { } prop_compose! { - /// Generate a uniformly distributed Orchard issuance key. - pub fn arb_issuance_key()( + /// Generate a uniformly distributed Orchard issuance master key. + pub fn arb_issuance_master_key()( key in prop::array::uniform32(prop::num::u8::ANY) .prop_map(IssuanceMasterKey::from_bytes) .prop_filter( @@ -1184,7 +1209,7 @@ pub mod testing { prop_compose! { /// Generate a uniformly distributed RedDSA issuance authorizing key. - pub fn arb_issuance_authorizing_key()(rng_seed in prop::array::uniform32(prop::num::u8::ANY)) -> IssuanceAuthorizingKey { + pub fn arb_issuance_authorizing_key()(rng_seed in prop::array::uniform32(prop::num::u8::ANY)) -> IssuanceAuthorizingKey { //TOREMOVE let mut rng = StdRng::from_seed(rng_seed); IssuanceAuthorizingKey::from(&IssuanceMasterKey::random(&mut rng)) } @@ -1192,8 +1217,8 @@ pub mod testing { prop_compose! { /// Generate a uniformly distributed RedDSA issuance validating key. - pub fn arb_issuance_validating_key()(isk in arb_issuance_authorizing_key()) -> IssuanceValidatingKey { - IssuanceValidatingKey::from(&isk) + pub fn arb_issuance_validating_key()(imk in arb_issuance_master_key()) -> IssuanceValidatingKey { + IssuanceValidatingKey::from(&imk) } } } @@ -1269,13 +1294,13 @@ mod tests { let imk = IssuanceMasterKey::from_bytes(tv.sk).unwrap(); - let isk: IssuanceAuthorizingKey = (&imk).into(); - assert_eq!(<[u8; 32]>::from(&isk.0), tv.isk); + let isk: IssuanceAuthorizingKey = (&imk).into(); // TOREMOVE + assert_eq!(<[u8; 32]>::from(&isk.0), tv.isk); // TOREMOVE let ak: SpendValidatingKey = (&ask).into(); assert_eq!(<[u8; 32]>::from(ak.0), tv.ak); - let ik: IssuanceValidatingKey = (&isk).into(); + let ik: IssuanceValidatingKey = (&imk).into(); assert_eq!(<[u8; 32]>::from(ik.0), tv.ik); let nk: NullifierDerivingKey = (&sk).into(); diff --git a/src/note/asset_base.rs b/src/note/asset_base.rs index 6e9050f85..b61ba3f62 100644 --- a/src/note/asset_base.rs +++ b/src/note/asset_base.rs @@ -136,13 +136,13 @@ pub mod testing { use proptest::prelude::*; - use crate::keys::{testing::arb_issuance_key, IssuanceAuthorizingKey, IssuanceValidatingKey}; + use crate::keys::{testing::arb_issuance_master_key, IssuanceAuthorizingKey, IssuanceValidatingKey}; prop_compose! { /// Generate a uniformly distributed note type pub fn arb_asset_id()( is_native in prop::bool::ANY, - sk in arb_issuance_key(), + sk in arb_issuance_master_key(), str in "[A-Za-z]{255}", ) -> AssetBase { if is_native { @@ -165,7 +165,7 @@ pub mod testing { prop_compose! { /// Generate an asset ID pub fn arb_zsa_asset_id()( - imk in arb_issuance_key(), + imk in arb_issuance_master_key(), str in "[A-Za-z]{255}" ) -> AssetBase { let isk = IssuanceAuthorizingKey::from(&imk); @@ -176,7 +176,7 @@ pub mod testing { prop_compose! { /// Generate an asset ID using a specific description pub fn zsa_asset_id(asset_desc: String)( - imk in arb_issuance_key(), + imk in arb_issuance_master_key(), ) -> AssetBase { assert!(super::is_asset_desc_of_valid_size(&asset_desc)); let isk = IssuanceAuthorizingKey::from(&imk); From b88c08e30fd031f14bc5358b102b5839576f6062 Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Mon, 30 Oct 2023 12:58:16 +0530 Subject: [PATCH 06/11] removing IssuanceAuthorizingKey --- src/bundle/burn_validation.rs | 5 +- src/issuance.rs | 94 +++++++++++++++++------------------ src/keys.rs | 51 +------------------ src/note/asset_base.rs | 18 +++---- src/supply_info.rs | 5 +- tests/zsa.rs | 21 ++++---- 6 files changed, 68 insertions(+), 126 deletions(-) diff --git a/src/bundle/burn_validation.rs b/src/bundle/burn_validation.rs index 6bf59e9e3..f00688db8 100644 --- a/src/bundle/burn_validation.rs +++ b/src/bundle/burn_validation.rs @@ -81,13 +81,12 @@ mod tests { /// A tuple `(AssetBase, Amount)` representing the burn list item. /// pub fn get_burn_tuple(asset_desc: &str, value: i64) -> (AssetBase, i64) { - use crate::keys::{IssuanceAuthorizingKey, IssuanceMasterKey, IssuanceValidatingKey}; + use crate::keys::{IssuanceMasterKey, IssuanceValidatingKey}; let imk = IssuanceMasterKey::from_bytes([0u8; 32]).unwrap(); - let isk: IssuanceAuthorizingKey = (&imk).into(); ( - AssetBase::derive(&IssuanceValidatingKey::from(&isk), asset_desc), + AssetBase::derive(&IssuanceValidatingKey::from(&imk), asset_desc), value, ) } diff --git a/src/issuance.rs b/src/issuance.rs index 8c2c1d17d..49f1e1669 100644 --- a/src/issuance.rs +++ b/src/issuance.rs @@ -12,7 +12,7 @@ use crate::issuance::Error::{ IssueActionWithoutNoteNotFinalized, IssueBundleIkMismatchAssetBase, IssueBundleInvalidSignature, ValueSumOverflow, WrongAssetDescSize, }; -use crate::keys::{IssuanceAuthorizingKey, IssuanceValidatingKey}; +use crate::keys::{IssuanceMasterKey, IssuanceValidatingKey}; use crate::note::asset_base::is_asset_desc_of_valid_size; use crate::note::{AssetBase, Nullifier}; use crate::primitives::redpallas::Signature; @@ -408,13 +408,13 @@ impl IssueBundle { impl IssueBundle { /// Sign the `IssueBundle`. - /// The call makes sure that the provided `isk` matches the `ik` and the driven `asset` for each note in the bundle. + /// The call makes sure that the provided `imk` matches the `ik` and the driven `asset` for each note in the bundle. pub fn sign( self, mut rng: R, - isk: &IssuanceAuthorizingKey, + imk: &IssuanceMasterKey, ) -> Result, Error> { - let expected_ik: IssuanceValidatingKey = (isk).into(); + let expected_ik: IssuanceValidatingKey = (imk).into(); // Make sure the `expected_ik` matches the `asset` for all notes. self.actions.iter().try_for_each(|action| { @@ -426,7 +426,7 @@ impl IssueBundle { ik: self.ik, actions: self.actions, authorization: Signed { - signature: isk.sign(&mut rng, &self.authorization.sighash), + signature: imk.sign(&mut rng, &self.authorization.sighash), }, }) } @@ -532,7 +532,7 @@ pub fn verify_issue_bundle( pub enum Error { /// The requested IssueAction not exists in the bundle. IssueActionNotFound, - /// The provided `isk` and the driven `ik` does not match at least one note type. + /// The provided `imk` and the driven `ik` does not match at least one note type. IssueBundleIkMismatchAssetBase, /// `asset_desc` should be between 1 and 512 bytes. WrongAssetDescSize, @@ -562,7 +562,7 @@ impl fmt::Display for Error { IssueBundleIkMismatchAssetBase => { write!( f, - "the provided `isk` and the driven `ik` does not match at least one note type" + "the provided `imk` and the derived `ik` do not match at least one note type" ) } WrongAssetDescSize => { @@ -606,7 +606,7 @@ mod tests { }; use crate::issuance::{verify_issue_bundle, IssueAction, Signed, Unauthorized}; use crate::keys::{ - FullViewingKey, IssuanceAuthorizingKey, IssuanceMasterKey, IssuanceValidatingKey, Scope, + FullViewingKey, IssuanceMasterKey, IssuanceValidatingKey, Scope, SpendingKey, }; use crate::note::{AssetBase, Nullifier}; @@ -622,7 +622,7 @@ mod tests { fn setup_params() -> ( OsRng, - IssuanceAuthorizingKey, + IssuanceMasterKey, IssuanceValidatingKey, Address, [u8; 32], @@ -630,8 +630,7 @@ mod tests { let mut rng = OsRng; let imk = IssuanceMasterKey::random(&mut rng); - let isk: IssuanceAuthorizingKey = (&imk).into(); - let ik: IssuanceValidatingKey = (&isk).into(); + let ik: IssuanceValidatingKey = (&imk).into(); let fvk = FullViewingKey::from(&SpendingKey::random(&mut rng)); let recipient = fvk.address_at(0u32, Scope::External); @@ -639,7 +638,7 @@ mod tests { let mut sighash = [0u8; 32]; rng.fill_bytes(&mut sighash); - (rng, isk, ik, recipient, sighash) + (rng, imk, ik, recipient, sighash) } fn setup_verify_supply_test_params( @@ -688,11 +687,11 @@ mod tests { note2_value: u64, ) -> ( OsRng, - IssuanceAuthorizingKey, + IssuanceMasterKey, IssueBundle, [u8; 32], ) { - let (mut rng, isk, ik, recipient, sighash) = setup_params(); + let (mut rng, imk, ik, recipient, sighash) = setup_params(); let note1 = Note::new( recipient, @@ -715,7 +714,7 @@ mod tests { let bundle = IssueBundle::from_parts(ik, NonEmpty::new(action), Unauthorized); - (rng, isk, bundle, sighash) + (rng, imk, bundle, sighash) } #[test] @@ -917,7 +916,7 @@ mod tests { #[test] fn issue_bundle_sign() { - let (rng, isk, ik, recipient, sighash) = setup_params(); + let (rng, imk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik.clone(), @@ -930,14 +929,14 @@ mod tests { ) .unwrap(); - let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); ik.verify(&sighash, &signed.authorization.signature) .expect("signature should be valid"); } #[test] - fn issue_bundle_invalid_isk_for_signature() { + fn issue_bundle_invalid_imk_for_signature() { let (rng, _, ik, recipient, _) = setup_params(); let (bundle, _) = IssueBundle::new( @@ -951,11 +950,11 @@ mod tests { ) .unwrap(); - let wrong_isk: IssuanceAuthorizingKey = (&IssuanceMasterKey::random(&mut OsRng)).into(); + let wrong_imk: IssuanceMasterKey = IssuanceMasterKey::random(&mut OsRng); let err = bundle .prepare([0; 32]) - .sign(rng, &wrong_isk) + .sign(rng, &wrong_imk) .expect_err("should not be able to sign"); assert_eq!(err, IssueBundleIkMismatchAssetBase); @@ -963,7 +962,7 @@ mod tests { #[test] fn issue_bundle_incorrect_asset_for_signature() { - let (mut rng, isk, ik, recipient, _) = setup_params(); + let (mut rng, imk, ik, recipient, _) = setup_params(); // Create a bundle with "normal" note let (mut bundle, _) = IssueBundle::new( @@ -989,7 +988,7 @@ mod tests { let err = bundle .prepare([0; 32]) - .sign(rng, &isk) + .sign(rng, &imk) .expect_err("should not be able to sign"); assert_eq!(err, IssueBundleIkMismatchAssetBase); @@ -997,7 +996,7 @@ mod tests { #[test] fn issue_bundle_verify() { - let (rng, isk, ik, recipient, sighash) = setup_params(); + let (rng, imk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, @@ -1010,7 +1009,7 @@ mod tests { ) .unwrap(); - let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); let prev_finalized = &mut HashSet::new(); let supply_info = verify_issue_bundle(&signed, sighash, prev_finalized).unwrap(); @@ -1022,7 +1021,7 @@ mod tests { #[test] fn issue_bundle_verify_with_finalize() { - let (rng, isk, ik, recipient, sighash) = setup_params(); + let (rng, imk, ik, recipient, sighash) = setup_params(); let (mut bundle, _) = IssueBundle::new( ik.clone(), @@ -1039,7 +1038,7 @@ mod tests { .finalize_action(String::from("Verify with finalize")) .unwrap(); - let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); let prev_finalized = &mut HashSet::new(); let supply_info = verify_issue_bundle(&signed, sighash, prev_finalized).unwrap(); @@ -1052,7 +1051,7 @@ mod tests { #[test] fn issue_bundle_verify_with_supply_info() { - let (rng, isk, ik, recipient, sighash) = setup_params(); + let (rng, imk, ik, recipient, sighash) = setup_params(); let asset1_desc = "Verify with supply info 1"; let asset2_desc = "Verify with supply info 2"; @@ -1104,7 +1103,7 @@ mod tests { ) .unwrap(); - let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); let prev_finalized = &mut HashSet::new(); let supply_info = verify_issue_bundle(&signed, sighash, prev_finalized).unwrap(); @@ -1135,7 +1134,7 @@ mod tests { #[test] fn issue_bundle_verify_fail_previously_finalized() { - let (rng, isk, ik, recipient, sighash) = setup_params(); + let (rng, imk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik.clone(), @@ -1148,7 +1147,7 @@ mod tests { ) .unwrap(); - let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); let prev_finalized = &mut HashSet::new(); let final_type = AssetBase::derive(&ik, &String::from("already final")); @@ -1170,7 +1169,7 @@ mod tests { } } - let (mut rng, isk, ik, recipient, sighash) = setup_params(); + let (mut rng, imk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, @@ -1183,12 +1182,12 @@ mod tests { ) .unwrap(); - let wrong_isk: IssuanceAuthorizingKey = (&IssuanceMasterKey::random(&mut rng)).into(); + let wrong_imk: IssuanceMasterKey = IssuanceMasterKey::random(&mut rng); - let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); + let mut signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); signed.set_authorization(Signed { - signature: wrong_isk.sign(&mut rng, &sighash), + signature: wrong_imk.sign(&mut rng, &sighash), }); let prev_finalized = &HashSet::new(); @@ -1201,7 +1200,7 @@ mod tests { #[test] fn issue_bundle_verify_fail_wrong_sighash() { - let (rng, isk, ik, recipient, random_sighash) = setup_params(); + let (rng, imk, ik, recipient, random_sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, String::from("Asset description"), @@ -1214,7 +1213,7 @@ mod tests { .unwrap(); let sighash: [u8; 32] = bundle.commitment().into(); - let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); let prev_finalized = &HashSet::new(); assert_eq!( @@ -1225,7 +1224,7 @@ mod tests { #[test] fn issue_bundle_verify_fail_incorrect_asset_description() { - let (mut rng, isk, ik, recipient, sighash) = setup_params(); + let (mut rng, imk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, @@ -1238,7 +1237,7 @@ mod tests { ) .unwrap(); - let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); + let mut signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); // Add "bad" note let note = Note::new( @@ -1263,7 +1262,7 @@ mod tests { fn issue_bundle_verify_fail_incorrect_ik() { let asset_description = "Asset"; - let (mut rng, isk, ik, recipient, sighash) = setup_params(); + let (mut rng, imk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, @@ -1276,11 +1275,10 @@ mod tests { ) .unwrap(); - let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); + let mut signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); let incorrect_imk = IssuanceMasterKey::random(&mut rng); - let incorrect_isk: IssuanceAuthorizingKey = (&incorrect_imk).into(); - let incorrect_ik: IssuanceValidatingKey = (&incorrect_isk).into(); + let incorrect_ik: IssuanceValidatingKey = (&incorrect_imk).into(); // Add "bad" note let note = Note::new( @@ -1310,7 +1308,7 @@ mod tests { } } - let (rng, isk, ik, recipient, sighash) = setup_params(); + let (rng, imk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, @@ -1323,7 +1321,7 @@ mod tests { ) .unwrap(); - let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); + let mut signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); let prev_finalized = HashSet::new(); // 1. Try too long description @@ -1348,23 +1346,23 @@ mod tests { #[test] fn issue_bundle_cannot_be_signed_with_asset_base_identity_point() { - let (rng, isk, bundle, sighash) = identity_point_test_params(10, 20); + let (rng, imk, bundle, sighash) = identity_point_test_params(10, 20); assert_eq!( - bundle.prepare(sighash).sign(rng, &isk).unwrap_err(), + bundle.prepare(sighash).sign(rng, &imk).unwrap_err(), AssetBaseCannotBeIdentityPoint ); } #[test] fn issue_bundle_verify_fail_asset_base_identity_point() { - let (mut rng, isk, bundle, sighash) = identity_point_test_params(10, 20); + let (mut rng, imk, bundle, sighash) = identity_point_test_params(10, 20); let signed = IssueBundle { ik: bundle.ik, actions: bundle.actions, authorization: Signed { - signature: isk.sign(&mut rng, &sighash), + signature: imk.sign(&mut rng, &sighash), }, }; diff --git a/src/keys.rs b/src/keys.rs index c0f147053..91165cbc6 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -302,41 +302,6 @@ impl IssuanceMasterKey { } } -// TOREMOVE: -/// An issuance authorizing key, used to create issuance authorization signatures. -/// This type enforces that the corresponding public point (ik^ℙ) has ỹ = 0. -/// -/// $\mathsf{isk}$ as defined in -/// [Issuance of Zcash Shielded Assets ZIP-0227 § Asset Identifier Generation (DRAFT ZIP)][IssuanceZSA]. -/// -/// [IssuanceZSA]: https://qed-it.github.io/zips/draft-ZIP-0227.html#asset-identifier-generation -#[derive(Clone, Debug)] -pub struct IssuanceAuthorizingKey(redpallas::SigningKey); //TOREMOVE - -impl IssuanceAuthorizingKey { - /// Derives isk from imk. Internal use only, does not enforce all constraints. - fn derive_inner(imk: &IssuanceMasterKey) -> pallas::Scalar { //TOREMOVE - to_scalar(PrfExpand::ZsaIsk.expand(&imk.0)) - } - - /// Sign the provided message using the `IssuanceAuthorizingKey`. - pub fn sign( //TOREMOVE - &self, - rng: &mut (impl RngCore + CryptoRng), - msg: &[u8], - ) -> redpallas::Signature { - self.0.sign(rng, msg) - } -} - -impl From<&IssuanceMasterKey> for IssuanceAuthorizingKey { - fn from(imk: &IssuanceMasterKey) -> Self { //TOREMOVE - let isk = IssuanceAuthorizingKey::derive_inner(imk); - // IssuanceAuthorizingKey cannot be constructed such that this assertion would fail. - assert!(!bool::from(isk.is_zero())); - IssuanceAuthorizingKey(conditionally_negate(isk)) - } -} /// A key used to validate issuance authorization signatures. /// @@ -348,12 +313,6 @@ impl From<&IssuanceMasterKey> for IssuanceAuthorizingKey { #[derive(Debug, Clone, PartialOrd, Ord)] pub struct IssuanceValidatingKey(VerificationKey); -impl From<&IssuanceAuthorizingKey> for IssuanceValidatingKey { //TOREMOVE - fn from(isk: &IssuanceAuthorizingKey) -> Self { - IssuanceValidatingKey((&isk.0).into()) - } -} - impl From<&IssuanceMasterKey> for IssuanceValidatingKey { fn from(imk: &IssuanceMasterKey) -> Self { IssuanceValidatingKey((&(conditionally_negate(imk.derive_inner()))).into()) @@ -1141,7 +1100,7 @@ impl SharedSecret { #[cfg_attr(docsrs, doc(cfg(feature = "test-dependencies")))] pub mod testing { use super::{ - DiversifierIndex, DiversifierKey, EphemeralSecretKey, IssuanceAuthorizingKey, + DiversifierIndex, DiversifierKey, EphemeralSecretKey, IssuanceMasterKey, IssuanceValidatingKey, SpendingKey, }; use proptest::prelude::*; @@ -1207,14 +1166,6 @@ pub mod testing { } } - prop_compose! { - /// Generate a uniformly distributed RedDSA issuance authorizing key. - pub fn arb_issuance_authorizing_key()(rng_seed in prop::array::uniform32(prop::num::u8::ANY)) -> IssuanceAuthorizingKey { //TOREMOVE - let mut rng = StdRng::from_seed(rng_seed); - IssuanceAuthorizingKey::from(&IssuanceMasterKey::random(&mut rng)) - } - } - prop_compose! { /// Generate a uniformly distributed RedDSA issuance validating key. pub fn arb_issuance_validating_key()(imk in arb_issuance_master_key()) -> IssuanceValidatingKey { diff --git a/src/note/asset_base.rs b/src/note/asset_base.rs index b61ba3f62..a151b7f53 100644 --- a/src/note/asset_base.rs +++ b/src/note/asset_base.rs @@ -10,7 +10,7 @@ use subtle::{Choice, ConstantTimeEq, CtOption}; use crate::constants::fixed_bases::{ NATIVE_ASSET_BASE_V_BYTES, VALUE_COMMITMENT_PERSONALIZATION, ZSA_ASSET_BASE_PERSONALIZATION, }; -use crate::keys::{IssuanceAuthorizingKey, IssuanceMasterKey, IssuanceValidatingKey}; +use crate::keys::{IssuanceMasterKey, IssuanceValidatingKey}; /// Note type identifier. #[derive(Clone, Copy, Debug, Eq)] @@ -103,8 +103,7 @@ impl AssetBase { /// This is only used in tests. pub(crate) fn random(rng: &mut impl RngCore) -> Self { let imk = IssuanceMasterKey::random(rng); - let isk = IssuanceAuthorizingKey::from(&imk); - let ik = IssuanceValidatingKey::from(&isk); + let ik = IssuanceValidatingKey::from(&imk); let asset_descr = "zsa_asset"; AssetBase::derive(&ik, asset_descr) } @@ -136,20 +135,19 @@ pub mod testing { use proptest::prelude::*; - use crate::keys::{testing::arb_issuance_master_key, IssuanceAuthorizingKey, IssuanceValidatingKey}; + use crate::keys::{testing::arb_issuance_master_key, IssuanceMasterKey, IssuanceValidatingKey}; prop_compose! { /// Generate a uniformly distributed note type pub fn arb_asset_id()( is_native in prop::bool::ANY, - sk in arb_issuance_master_key(), + imk in arb_issuance_master_key(), str in "[A-Za-z]{255}", ) -> AssetBase { if is_native { AssetBase::native() } else { - let isk = IssuanceAuthorizingKey::from(&sk); - AssetBase::derive(&IssuanceValidatingKey::from(&isk), &str) + AssetBase::derive(&IssuanceValidatingKey::from(&imk), &str) } } } @@ -168,8 +166,7 @@ pub mod testing { imk in arb_issuance_master_key(), str in "[A-Za-z]{255}" ) -> AssetBase { - let isk = IssuanceAuthorizingKey::from(&imk); - AssetBase::derive(&IssuanceValidatingKey::from(&isk), &str) + AssetBase::derive(&IssuanceValidatingKey::from(&imk), &str) } } @@ -179,8 +176,7 @@ pub mod testing { imk in arb_issuance_master_key(), ) -> AssetBase { assert!(super::is_asset_desc_of_valid_size(&asset_desc)); - let isk = IssuanceAuthorizingKey::from(&imk); - AssetBase::derive(&IssuanceValidatingKey::from(&isk), &asset_desc) + AssetBase::derive(&IssuanceValidatingKey::from(&imk), &asset_desc) } } diff --git a/src/supply_info.rs b/src/supply_info.rs index 2039ce1ef..63a2117f9 100644 --- a/src/supply_info.rs +++ b/src/supply_info.rs @@ -80,12 +80,11 @@ mod tests { use super::*; fn create_test_asset(asset_desc: &str) -> AssetBase { - use crate::keys::{IssuanceAuthorizingKey, IssuanceMasterKey, IssuanceValidatingKey}; + use crate::keys::{IssuanceMasterKey, IssuanceValidatingKey}; let imk = IssuanceMasterKey::from_bytes([0u8; 32]).unwrap(); - let isk: IssuanceAuthorizingKey = (&imk).into(); - AssetBase::derive(&IssuanceValidatingKey::from(&isk), asset_desc) + AssetBase::derive(&IssuanceValidatingKey::from(&imk), asset_desc) } fn sum<'a, T: IntoIterator>(supplies: T) -> Option { diff --git a/tests/zsa.rs b/tests/zsa.rs index 9b5585288..f1385a5f7 100644 --- a/tests/zsa.rs +++ b/tests/zsa.rs @@ -5,7 +5,7 @@ use bridgetree::BridgeTree; use incrementalmerkletree::Hashable; use orchard::bundle::Authorized; use orchard::issuance::{verify_issue_bundle, IssueBundle, IssueInfo, Signed, Unauthorized}; -use orchard::keys::{IssuanceAuthorizingKey, IssuanceValidatingKey}; +use orchard::keys::{IssuanceMasterKey, IssuanceValidatingKey}; use orchard::note::{AssetBase, ExtractedNoteCommitment}; use orchard::note_encryption_v3::OrchardDomainV3; use orchard::tree::{MerkleHashOrchard, MerklePath}; @@ -14,7 +14,7 @@ use orchard::{ bundle::Flags, circuit::{ProvingKey, VerifyingKey}, keys::{ - FullViewingKey, IssuanceMasterKey, PreparedIncomingViewingKey, Scope, SpendAuthorizingKey, + FullViewingKey, PreparedIncomingViewingKey, Scope, SpendAuthorizingKey, SpendingKey, }, value::NoteValue, @@ -30,7 +30,7 @@ struct Keychain { vk: VerifyingKey, sk: SpendingKey, fvk: FullViewingKey, - isk: IssuanceAuthorizingKey, + imk: IssuanceMasterKey, ik: IssuanceValidatingKey, recipient: Address, } @@ -45,8 +45,8 @@ impl Keychain { fn fvk(&self) -> &FullViewingKey { &self.fvk } - fn isk(&self) -> &IssuanceAuthorizingKey { - &self.isk + fn imk(&self) -> &IssuanceMasterKey { + &self.imk } fn ik(&self) -> &IssuanceValidatingKey { &self.ik @@ -62,14 +62,13 @@ fn prepare_keys() -> Keychain { let recipient = fvk.address_at(0u32, Scope::External); let imk = IssuanceMasterKey::from_bytes([0; 32]).unwrap(); - let isk = IssuanceAuthorizingKey::from(&imk); - let ik = IssuanceValidatingKey::from(&isk); + let ik = IssuanceValidatingKey::from(&imk); Keychain { pk, vk, sk, fvk, - isk, + imk, ik, recipient, } @@ -78,11 +77,11 @@ fn prepare_keys() -> Keychain { fn sign_issue_bundle( unauthorized: IssueBundle, rng: OsRng, - isk: &IssuanceAuthorizingKey, + imk: &IssuanceMasterKey, ) -> IssueBundle { let sighash = unauthorized.commitment().into(); let proven = unauthorized.prepare(sighash); - proven.sign(rng, isk).unwrap() + proven.sign(rng, imk).unwrap() } fn build_and_sign_bundle( @@ -165,7 +164,7 @@ fn issue_zsa_notes(asset_descr: &str, keys: &Keychain) -> (Note, Note) { ) .is_ok()); - let issue_bundle = sign_issue_bundle(unauthorized, rng, keys.isk()); + let issue_bundle = sign_issue_bundle(unauthorized, rng, keys.imk()); // Take notes from first action let notes = issue_bundle.get_all_notes(); From b5404db6bbb95b88ea03c50394356e8f6acee67c Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Mon, 30 Oct 2023 13:02:42 +0530 Subject: [PATCH 07/11] fmt fixes --- src/issuance.rs | 3 +-- src/keys.rs | 7 +++---- tests/zsa.rs | 5 +---- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/issuance.rs b/src/issuance.rs index 49f1e1669..710cef8ec 100644 --- a/src/issuance.rs +++ b/src/issuance.rs @@ -606,8 +606,7 @@ mod tests { }; use crate::issuance::{verify_issue_bundle, IssueAction, Signed, Unauthorized}; use crate::keys::{ - FullViewingKey, IssuanceMasterKey, IssuanceValidatingKey, Scope, - SpendingKey, + FullViewingKey, IssuanceMasterKey, IssuanceValidatingKey, Scope, SpendingKey, }; use crate::note::{AssetBase, Nullifier}; use crate::value::{NoteValue, ValueSum}; diff --git a/src/keys.rs b/src/keys.rs index 91165cbc6..756a4cdea 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -16,6 +16,7 @@ use rand::{CryptoRng, RngCore}; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; use zcash_note_encryption::EphemeralKeyBytes; +use crate::primitives::redpallas::SigningKey; use crate::{ address::Address, primitives::redpallas::{self, SpendAuth, VerificationKey}, @@ -29,7 +30,6 @@ use crate::{ ZIP32_ORCHARD_PERSONALIZATION_FOR_ISSUANCE, }, }; -use crate::primitives::redpallas::SigningKey; const KDF_ORCHARD_PERSONALIZATION: &[u8; 16] = b"Zcash_OrchardKDF"; const ZIP32_PURPOSE: u32 = 32; @@ -302,7 +302,6 @@ impl IssuanceMasterKey { } } - /// A key used to validate issuance authorization signatures. /// /// Defined in [ZIP 227: Issuance of Zcash Shielded Assets § Issuance Key Generation][IssuanceZSA]. @@ -1100,8 +1099,8 @@ impl SharedSecret { #[cfg_attr(docsrs, doc(cfg(feature = "test-dependencies")))] pub mod testing { use super::{ - DiversifierIndex, DiversifierKey, EphemeralSecretKey, - IssuanceMasterKey, IssuanceValidatingKey, SpendingKey, + DiversifierIndex, DiversifierKey, EphemeralSecretKey, IssuanceMasterKey, + IssuanceValidatingKey, SpendingKey, }; use proptest::prelude::*; use rand::{rngs::StdRng, SeedableRng}; diff --git a/tests/zsa.rs b/tests/zsa.rs index f1385a5f7..d81bfa95f 100644 --- a/tests/zsa.rs +++ b/tests/zsa.rs @@ -13,10 +13,7 @@ use orchard::{ builder::Builder, bundle::Flags, circuit::{ProvingKey, VerifyingKey}, - keys::{ - FullViewingKey, PreparedIncomingViewingKey, Scope, SpendAuthorizingKey, - SpendingKey, - }, + keys::{FullViewingKey, PreparedIncomingViewingKey, Scope, SpendAuthorizingKey, SpendingKey}, value::NoteValue, Address, Anchor, Bundle, Note, }; From 3898f6a8b5bd246950d037866df65053e3c0f035 Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Mon, 30 Oct 2023 13:43:36 +0530 Subject: [PATCH 08/11] cleaning up and fixing tests --- src/keys.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/keys.rs b/src/keys.rs index 756a4cdea..0a5df6fc7 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -16,7 +16,6 @@ use rand::{CryptoRng, RngCore}; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; use zcash_note_encryption::EphemeralKeyBytes; -use crate::primitives::redpallas::SigningKey; use crate::{ address::Address, primitives::redpallas::{self, SpendAuth, VerificationKey}, @@ -1244,9 +1243,6 @@ mod tests { let imk = IssuanceMasterKey::from_bytes(tv.sk).unwrap(); - let isk: IssuanceAuthorizingKey = (&imk).into(); // TOREMOVE - assert_eq!(<[u8; 32]>::from(&isk.0), tv.isk); // TOREMOVE - let ak: SpendValidatingKey = (&ask).into(); assert_eq!(<[u8; 32]>::from(ak.0), tv.ak); From e546eeb9e77374a6ef3317b44c3757915b9b50d9 Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Mon, 30 Oct 2023 14:35:09 +0530 Subject: [PATCH 09/11] fixing more Clippy issues --- src/keys.rs | 3 +-- src/note/asset_base.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/keys.rs b/src/keys.rs index 0a5df6fc7..7e25e3071 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -270,7 +270,7 @@ impl IssuanceMasterKey { &self.0 } - /// Derives the Orchard issuance key for the given seed, coin type, and account. + /// Derives the Orchard-ZSA issuance key for the given seed, coin type, and account. pub fn from_zip32_seed( seed: &[u8], coin_type: u32, @@ -1102,7 +1102,6 @@ pub mod testing { IssuanceValidatingKey, SpendingKey, }; use proptest::prelude::*; - use rand::{rngs::StdRng, SeedableRng}; prop_compose! { /// Generate a uniformly distributed Orchard spending key. diff --git a/src/note/asset_base.rs b/src/note/asset_base.rs index a151b7f53..ca80b9f54 100644 --- a/src/note/asset_base.rs +++ b/src/note/asset_base.rs @@ -135,7 +135,7 @@ pub mod testing { use proptest::prelude::*; - use crate::keys::{testing::arb_issuance_master_key, IssuanceMasterKey, IssuanceValidatingKey}; + use crate::keys::{testing::arb_issuance_master_key, IssuanceValidatingKey}; prop_compose! { /// Generate a uniformly distributed note type From 273b01d58ad880fb89a54242fcd809cbe0efeb04 Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Wed, 1 Nov 2023 18:26:01 +0530 Subject: [PATCH 10/11] change imk: IssuanceMasterKey to isk: IssuanceAuthorizingKey --- src/bundle/burn_validation.rs | 8 +-- src/issuance.rs | 96 +++++++++++++++++------------------ src/keys.rs | 46 ++++++++--------- src/note/asset_base.rs | 20 ++++---- src/supply_info.rs | 6 +-- tests/zsa.rs | 20 ++++---- 6 files changed, 98 insertions(+), 98 deletions(-) diff --git a/src/bundle/burn_validation.rs b/src/bundle/burn_validation.rs index f00688db8..0fdf35dd1 100644 --- a/src/bundle/burn_validation.rs +++ b/src/bundle/burn_validation.rs @@ -69,7 +69,7 @@ mod tests { /// Creates an item of bundle burn list for a given asset description and value. /// /// This function is deterministic and guarantees that each call with the same parameters - /// will return the same result. It achieves determinism by using a static `IssuanceMasterKey`. + /// will return the same result. It achieves determinism by using a static `IssuanceAuthorizingKey`. /// /// # Arguments /// @@ -81,12 +81,12 @@ mod tests { /// A tuple `(AssetBase, Amount)` representing the burn list item. /// pub fn get_burn_tuple(asset_desc: &str, value: i64) -> (AssetBase, i64) { - use crate::keys::{IssuanceMasterKey, IssuanceValidatingKey}; + use crate::keys::{IssuanceAuthorizingKey, IssuanceValidatingKey}; - let imk = IssuanceMasterKey::from_bytes([0u8; 32]).unwrap(); + let isk = IssuanceAuthorizingKey::from_bytes([0u8; 32]).unwrap(); ( - AssetBase::derive(&IssuanceValidatingKey::from(&imk), asset_desc), + AssetBase::derive(&IssuanceValidatingKey::from(&isk), asset_desc), value, ) } diff --git a/src/issuance.rs b/src/issuance.rs index 710cef8ec..26a4b5cc8 100644 --- a/src/issuance.rs +++ b/src/issuance.rs @@ -12,7 +12,7 @@ use crate::issuance::Error::{ IssueActionWithoutNoteNotFinalized, IssueBundleIkMismatchAssetBase, IssueBundleInvalidSignature, ValueSumOverflow, WrongAssetDescSize, }; -use crate::keys::{IssuanceMasterKey, IssuanceValidatingKey}; +use crate::keys::{IssuanceAuthorizingKey, IssuanceValidatingKey}; use crate::note::asset_base::is_asset_desc_of_valid_size; use crate::note::{AssetBase, Nullifier}; use crate::primitives::redpallas::Signature; @@ -408,13 +408,13 @@ impl IssueBundle { impl IssueBundle { /// Sign the `IssueBundle`. - /// The call makes sure that the provided `imk` matches the `ik` and the driven `asset` for each note in the bundle. + /// The call makes sure that the provided `isk` matches the `ik` and the driven `asset` for each note in the bundle. pub fn sign( self, mut rng: R, - imk: &IssuanceMasterKey, + isk: &IssuanceAuthorizingKey, ) -> Result, Error> { - let expected_ik: IssuanceValidatingKey = (imk).into(); + let expected_ik: IssuanceValidatingKey = (isk).into(); // Make sure the `expected_ik` matches the `asset` for all notes. self.actions.iter().try_for_each(|action| { @@ -426,7 +426,7 @@ impl IssueBundle { ik: self.ik, actions: self.actions, authorization: Signed { - signature: imk.sign(&mut rng, &self.authorization.sighash), + signature: isk.sign(&mut rng, &self.authorization.sighash), }, }) } @@ -532,7 +532,7 @@ pub fn verify_issue_bundle( pub enum Error { /// The requested IssueAction not exists in the bundle. IssueActionNotFound, - /// The provided `imk` and the driven `ik` does not match at least one note type. + /// The provided `isk` and the derived `ik` does not match at least one note type. IssueBundleIkMismatchAssetBase, /// `asset_desc` should be between 1 and 512 bytes. WrongAssetDescSize, @@ -562,7 +562,7 @@ impl fmt::Display for Error { IssueBundleIkMismatchAssetBase => { write!( f, - "the provided `imk` and the derived `ik` do not match at least one note type" + "the provided `isk` and the derived `ik` do not match at least one note type" ) } WrongAssetDescSize => { @@ -606,7 +606,7 @@ mod tests { }; use crate::issuance::{verify_issue_bundle, IssueAction, Signed, Unauthorized}; use crate::keys::{ - FullViewingKey, IssuanceMasterKey, IssuanceValidatingKey, Scope, SpendingKey, + FullViewingKey, IssuanceAuthorizingKey, IssuanceValidatingKey, Scope, SpendingKey, }; use crate::note::{AssetBase, Nullifier}; use crate::value::{NoteValue, ValueSum}; @@ -621,15 +621,15 @@ mod tests { fn setup_params() -> ( OsRng, - IssuanceMasterKey, + IssuanceAuthorizingKey, IssuanceValidatingKey, Address, [u8; 32], ) { let mut rng = OsRng; - let imk = IssuanceMasterKey::random(&mut rng); - let ik: IssuanceValidatingKey = (&imk).into(); + let isk = IssuanceAuthorizingKey::random(&mut rng); + let ik: IssuanceValidatingKey = (&isk).into(); let fvk = FullViewingKey::from(&SpendingKey::random(&mut rng)); let recipient = fvk.address_at(0u32, Scope::External); @@ -637,7 +637,7 @@ mod tests { let mut sighash = [0u8; 32]; rng.fill_bytes(&mut sighash); - (rng, imk, ik, recipient, sighash) + (rng, isk, ik, recipient, sighash) } fn setup_verify_supply_test_params( @@ -686,11 +686,11 @@ mod tests { note2_value: u64, ) -> ( OsRng, - IssuanceMasterKey, + IssuanceAuthorizingKey, IssueBundle, [u8; 32], ) { - let (mut rng, imk, ik, recipient, sighash) = setup_params(); + let (mut rng, isk, ik, recipient, sighash) = setup_params(); let note1 = Note::new( recipient, @@ -713,7 +713,7 @@ mod tests { let bundle = IssueBundle::from_parts(ik, NonEmpty::new(action), Unauthorized); - (rng, imk, bundle, sighash) + (rng, isk, bundle, sighash) } #[test] @@ -915,7 +915,7 @@ mod tests { #[test] fn issue_bundle_sign() { - let (rng, imk, ik, recipient, sighash) = setup_params(); + let (rng, isk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik.clone(), @@ -928,14 +928,14 @@ mod tests { ) .unwrap(); - let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); ik.verify(&sighash, &signed.authorization.signature) .expect("signature should be valid"); } #[test] - fn issue_bundle_invalid_imk_for_signature() { + fn issue_bundle_invalid_isk_for_signature() { let (rng, _, ik, recipient, _) = setup_params(); let (bundle, _) = IssueBundle::new( @@ -949,11 +949,11 @@ mod tests { ) .unwrap(); - let wrong_imk: IssuanceMasterKey = IssuanceMasterKey::random(&mut OsRng); + let wrong_isk: IssuanceAuthorizingKey = IssuanceAuthorizingKey::random(&mut OsRng); let err = bundle .prepare([0; 32]) - .sign(rng, &wrong_imk) + .sign(rng, &wrong_isk) .expect_err("should not be able to sign"); assert_eq!(err, IssueBundleIkMismatchAssetBase); @@ -961,7 +961,7 @@ mod tests { #[test] fn issue_bundle_incorrect_asset_for_signature() { - let (mut rng, imk, ik, recipient, _) = setup_params(); + let (mut rng, isk, ik, recipient, _) = setup_params(); // Create a bundle with "normal" note let (mut bundle, _) = IssueBundle::new( @@ -987,7 +987,7 @@ mod tests { let err = bundle .prepare([0; 32]) - .sign(rng, &imk) + .sign(rng, &isk) .expect_err("should not be able to sign"); assert_eq!(err, IssueBundleIkMismatchAssetBase); @@ -995,7 +995,7 @@ mod tests { #[test] fn issue_bundle_verify() { - let (rng, imk, ik, recipient, sighash) = setup_params(); + let (rng, isk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, @@ -1008,7 +1008,7 @@ mod tests { ) .unwrap(); - let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); let prev_finalized = &mut HashSet::new(); let supply_info = verify_issue_bundle(&signed, sighash, prev_finalized).unwrap(); @@ -1020,7 +1020,7 @@ mod tests { #[test] fn issue_bundle_verify_with_finalize() { - let (rng, imk, ik, recipient, sighash) = setup_params(); + let (rng, isk, ik, recipient, sighash) = setup_params(); let (mut bundle, _) = IssueBundle::new( ik.clone(), @@ -1037,7 +1037,7 @@ mod tests { .finalize_action(String::from("Verify with finalize")) .unwrap(); - let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); let prev_finalized = &mut HashSet::new(); let supply_info = verify_issue_bundle(&signed, sighash, prev_finalized).unwrap(); @@ -1050,7 +1050,7 @@ mod tests { #[test] fn issue_bundle_verify_with_supply_info() { - let (rng, imk, ik, recipient, sighash) = setup_params(); + let (rng, isk, ik, recipient, sighash) = setup_params(); let asset1_desc = "Verify with supply info 1"; let asset2_desc = "Verify with supply info 2"; @@ -1102,7 +1102,7 @@ mod tests { ) .unwrap(); - let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); let prev_finalized = &mut HashSet::new(); let supply_info = verify_issue_bundle(&signed, sighash, prev_finalized).unwrap(); @@ -1133,7 +1133,7 @@ mod tests { #[test] fn issue_bundle_verify_fail_previously_finalized() { - let (rng, imk, ik, recipient, sighash) = setup_params(); + let (rng, isk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik.clone(), @@ -1146,7 +1146,7 @@ mod tests { ) .unwrap(); - let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); let prev_finalized = &mut HashSet::new(); let final_type = AssetBase::derive(&ik, &String::from("already final")); @@ -1168,7 +1168,7 @@ mod tests { } } - let (mut rng, imk, ik, recipient, sighash) = setup_params(); + let (mut rng, isk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, @@ -1181,12 +1181,12 @@ mod tests { ) .unwrap(); - let wrong_imk: IssuanceMasterKey = IssuanceMasterKey::random(&mut rng); + let wrong_isk: IssuanceAuthorizingKey = IssuanceAuthorizingKey::random(&mut rng); - let mut signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); + let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); signed.set_authorization(Signed { - signature: wrong_imk.sign(&mut rng, &sighash), + signature: wrong_isk.sign(&mut rng, &sighash), }); let prev_finalized = &HashSet::new(); @@ -1199,7 +1199,7 @@ mod tests { #[test] fn issue_bundle_verify_fail_wrong_sighash() { - let (rng, imk, ik, recipient, random_sighash) = setup_params(); + let (rng, isk, ik, recipient, random_sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, String::from("Asset description"), @@ -1212,7 +1212,7 @@ mod tests { .unwrap(); let sighash: [u8; 32] = bundle.commitment().into(); - let signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); + let signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); let prev_finalized = &HashSet::new(); assert_eq!( @@ -1223,7 +1223,7 @@ mod tests { #[test] fn issue_bundle_verify_fail_incorrect_asset_description() { - let (mut rng, imk, ik, recipient, sighash) = setup_params(); + let (mut rng, isk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, @@ -1236,7 +1236,7 @@ mod tests { ) .unwrap(); - let mut signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); + let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); // Add "bad" note let note = Note::new( @@ -1261,7 +1261,7 @@ mod tests { fn issue_bundle_verify_fail_incorrect_ik() { let asset_description = "Asset"; - let (mut rng, imk, ik, recipient, sighash) = setup_params(); + let (mut rng, isk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, @@ -1274,10 +1274,10 @@ mod tests { ) .unwrap(); - let mut signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); + let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); - let incorrect_imk = IssuanceMasterKey::random(&mut rng); - let incorrect_ik: IssuanceValidatingKey = (&incorrect_imk).into(); + let incorrect_isk = IssuanceAuthorizingKey::random(&mut rng); + let incorrect_ik: IssuanceValidatingKey = (&incorrect_isk).into(); // Add "bad" note let note = Note::new( @@ -1307,7 +1307,7 @@ mod tests { } } - let (rng, imk, ik, recipient, sighash) = setup_params(); + let (rng, isk, ik, recipient, sighash) = setup_params(); let (bundle, _) = IssueBundle::new( ik, @@ -1320,7 +1320,7 @@ mod tests { ) .unwrap(); - let mut signed = bundle.prepare(sighash).sign(rng, &imk).unwrap(); + let mut signed = bundle.prepare(sighash).sign(rng, &isk).unwrap(); let prev_finalized = HashSet::new(); // 1. Try too long description @@ -1345,23 +1345,23 @@ mod tests { #[test] fn issue_bundle_cannot_be_signed_with_asset_base_identity_point() { - let (rng, imk, bundle, sighash) = identity_point_test_params(10, 20); + let (rng, isk, bundle, sighash) = identity_point_test_params(10, 20); assert_eq!( - bundle.prepare(sighash).sign(rng, &imk).unwrap_err(), + bundle.prepare(sighash).sign(rng, &isk).unwrap_err(), AssetBaseCannotBeIdentityPoint ); } #[test] fn issue_bundle_verify_fail_asset_base_identity_point() { - let (mut rng, imk, bundle, sighash) = identity_point_test_params(10, 20); + let (mut rng, isk, bundle, sighash) = identity_point_test_params(10, 20); let signed = IssueBundle { ik: bundle.ik, actions: bundle.actions, authorization: Signed { - signature: imk.sign(&mut rng, &sighash), + signature: isk.sign(&mut rng, &sighash), }, }; diff --git a/src/keys.rs b/src/keys.rs index 7e25e3071..b7c42fc6d 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -223,25 +223,25 @@ type IssuanceAuth = SpendAuth; /// An issuance key, from which all key material is derived. /// -/// $\mathsf{imk}$ as defined in [ZIP 227][issuancekeycomponents]. +/// $\mathsf{isk}$ as defined in [ZIP 227][issuancekeycomponents]. /// /// [issuancekeycomponents]: https://qed-it.github.io/zips/zip-0227#issuance-key-derivation #[derive(Debug, Copy, Clone)] -pub struct IssuanceMasterKey([u8; 32]); +pub struct IssuanceAuthorizingKey([u8; 32]); -impl From for IssuanceMasterKey { +impl From for IssuanceAuthorizingKey { fn from(sk: SpendingKey) -> Self { - IssuanceMasterKey(*sk.to_bytes()) + IssuanceAuthorizingKey(*sk.to_bytes()) } } -impl ConstantTimeEq for IssuanceMasterKey { +impl ConstantTimeEq for IssuanceAuthorizingKey { fn ct_eq(&self, other: &Self) -> Choice { self.to_bytes().ct_eq(other.to_bytes()) } } -impl IssuanceMasterKey { +impl IssuanceAuthorizingKey { /// Generates a random issuance key. /// /// This is only used when generating a random AssetBase. @@ -255,12 +255,12 @@ impl IssuanceMasterKey { /// Constructs an Orchard issuance key from uniformly-random bytes. /// /// Returns `None` if the bytes do not correspond to a valid Orchard issuance key. - pub fn from_bytes(imk_bytes: [u8; 32]) -> CtOption { - let imk = IssuanceMasterKey(imk_bytes); - CtOption::new(imk, imk.is_valid()) + pub fn from_bytes(isk_bytes: [u8; 32]) -> CtOption { + let isk = IssuanceAuthorizingKey(isk_bytes); + CtOption::new(isk, isk.is_valid()) } - /// Checks whether the Orchard-ZSA issuance key is valid + /// Checks whether the Orchard-ZSA issuance key is valid //TODO: What are the criteria? pub fn is_valid(self) -> Choice { 1u8.into() } @@ -286,12 +286,12 @@ impl IssuanceMasterKey { .map(|esk| esk.sk().into()) } - /// Derives the RedPallas signing key from imk. Internal use only, does not enforce all constraints. + /// Derives the RedPallas signing key from isk. Internal use only, does not enforce all constraints. fn derive_inner(&self) -> pallas::Scalar { to_scalar(PrfExpand::ZsaIsk.expand(&self.0)) } - /// Sign the provided message using the `IssuanceMasterKey`. + /// Sign the provided message using the `IssuanceAuthorizingKey`. pub fn sign( &self, rng: &mut (impl RngCore + CryptoRng), @@ -311,9 +311,9 @@ impl IssuanceMasterKey { #[derive(Debug, Clone, PartialOrd, Ord)] pub struct IssuanceValidatingKey(VerificationKey); -impl From<&IssuanceMasterKey> for IssuanceValidatingKey { - fn from(imk: &IssuanceMasterKey) -> Self { - IssuanceValidatingKey((&(conditionally_negate(imk.derive_inner()))).into()) +impl From<&IssuanceAuthorizingKey> for IssuanceValidatingKey { + fn from(isk: &IssuanceAuthorizingKey) -> Self { + IssuanceValidatingKey((&(conditionally_negate(isk.derive_inner()))).into()) } } @@ -1098,7 +1098,7 @@ impl SharedSecret { #[cfg_attr(docsrs, doc(cfg(feature = "test-dependencies")))] pub mod testing { use super::{ - DiversifierIndex, DiversifierKey, EphemeralSecretKey, IssuanceMasterKey, + DiversifierIndex, DiversifierKey, EphemeralSecretKey, IssuanceAuthorizingKey, IssuanceValidatingKey, SpendingKey, }; use proptest::prelude::*; @@ -1119,14 +1119,14 @@ pub mod testing { prop_compose! { /// Generate a uniformly distributed Orchard issuance master key. - pub fn arb_issuance_master_key()( + pub fn arb_issuance_authorizing_key()( key in prop::array::uniform32(prop::num::u8::ANY) - .prop_map(IssuanceMasterKey::from_bytes) + .prop_map(IssuanceAuthorizingKey::from_bytes) .prop_filter( "Values must correspond to valid Orchard issuance keys.", |opt| bool::from(opt.is_some()) ) - ) -> IssuanceMasterKey { + ) -> IssuanceAuthorizingKey { key.unwrap() } } @@ -1165,8 +1165,8 @@ pub mod testing { prop_compose! { /// Generate a uniformly distributed RedDSA issuance validating key. - pub fn arb_issuance_validating_key()(imk in arb_issuance_master_key()) -> IssuanceValidatingKey { - IssuanceValidatingKey::from(&imk) + pub fn arb_issuance_validating_key()(isk in arb_issuance_authorizing_key()) -> IssuanceValidatingKey { + IssuanceValidatingKey::from(&isk) } } } @@ -1240,12 +1240,12 @@ mod tests { let ask: SpendAuthorizingKey = (&sk).into(); assert_eq!(<[u8; 32]>::from(&ask.0), tv.ask); - let imk = IssuanceMasterKey::from_bytes(tv.sk).unwrap(); + let isk = IssuanceAuthorizingKey::from_bytes(tv.sk).unwrap(); let ak: SpendValidatingKey = (&ask).into(); assert_eq!(<[u8; 32]>::from(ak.0), tv.ak); - let ik: IssuanceValidatingKey = (&imk).into(); + let ik: IssuanceValidatingKey = (&isk).into(); assert_eq!(<[u8; 32]>::from(ik.0), tv.ik); let nk: NullifierDerivingKey = (&sk).into(); diff --git a/src/note/asset_base.rs b/src/note/asset_base.rs index ca80b9f54..ba8c80825 100644 --- a/src/note/asset_base.rs +++ b/src/note/asset_base.rs @@ -10,7 +10,7 @@ use subtle::{Choice, ConstantTimeEq, CtOption}; use crate::constants::fixed_bases::{ NATIVE_ASSET_BASE_V_BYTES, VALUE_COMMITMENT_PERSONALIZATION, ZSA_ASSET_BASE_PERSONALIZATION, }; -use crate::keys::{IssuanceMasterKey, IssuanceValidatingKey}; +use crate::keys::{IssuanceAuthorizingKey, IssuanceValidatingKey}; /// Note type identifier. #[derive(Clone, Copy, Debug, Eq)] @@ -102,8 +102,8 @@ impl AssetBase { /// /// This is only used in tests. pub(crate) fn random(rng: &mut impl RngCore) -> Self { - let imk = IssuanceMasterKey::random(rng); - let ik = IssuanceValidatingKey::from(&imk); + let isk = IssuanceAuthorizingKey::random(rng); + let ik = IssuanceValidatingKey::from(&isk); let asset_descr = "zsa_asset"; AssetBase::derive(&ik, asset_descr) } @@ -135,19 +135,19 @@ pub mod testing { use proptest::prelude::*; - use crate::keys::{testing::arb_issuance_master_key, IssuanceValidatingKey}; + use crate::keys::{testing::arb_issuance_authorizing_key, IssuanceValidatingKey}; prop_compose! { /// Generate a uniformly distributed note type pub fn arb_asset_id()( is_native in prop::bool::ANY, - imk in arb_issuance_master_key(), + isk in arb_issuance_authorizing_key(), str in "[A-Za-z]{255}", ) -> AssetBase { if is_native { AssetBase::native() } else { - AssetBase::derive(&IssuanceValidatingKey::from(&imk), &str) + AssetBase::derive(&IssuanceValidatingKey::from(&isk), &str) } } } @@ -163,20 +163,20 @@ pub mod testing { prop_compose! { /// Generate an asset ID pub fn arb_zsa_asset_id()( - imk in arb_issuance_master_key(), + isk in arb_issuance_authorizing_key(), str in "[A-Za-z]{255}" ) -> AssetBase { - AssetBase::derive(&IssuanceValidatingKey::from(&imk), &str) + AssetBase::derive(&IssuanceValidatingKey::from(&isk), &str) } } prop_compose! { /// Generate an asset ID using a specific description pub fn zsa_asset_id(asset_desc: String)( - imk in arb_issuance_master_key(), + isk in arb_issuance_authorizing_key(), ) -> AssetBase { assert!(super::is_asset_desc_of_valid_size(&asset_desc)); - AssetBase::derive(&IssuanceValidatingKey::from(&imk), &asset_desc) + AssetBase::derive(&IssuanceValidatingKey::from(&isk), &asset_desc) } } diff --git a/src/supply_info.rs b/src/supply_info.rs index 63a2117f9..1752fbd64 100644 --- a/src/supply_info.rs +++ b/src/supply_info.rs @@ -80,11 +80,11 @@ mod tests { use super::*; fn create_test_asset(asset_desc: &str) -> AssetBase { - use crate::keys::{IssuanceMasterKey, IssuanceValidatingKey}; + use crate::keys::{IssuanceAuthorizingKey, IssuanceValidatingKey}; - let imk = IssuanceMasterKey::from_bytes([0u8; 32]).unwrap(); + let isk = IssuanceAuthorizingKey::from_bytes([0u8; 32]).unwrap(); - AssetBase::derive(&IssuanceValidatingKey::from(&imk), asset_desc) + AssetBase::derive(&IssuanceValidatingKey::from(&isk), asset_desc) } fn sum<'a, T: IntoIterator>(supplies: T) -> Option { diff --git a/tests/zsa.rs b/tests/zsa.rs index d81bfa95f..c49905859 100644 --- a/tests/zsa.rs +++ b/tests/zsa.rs @@ -5,7 +5,7 @@ use bridgetree::BridgeTree; use incrementalmerkletree::Hashable; use orchard::bundle::Authorized; use orchard::issuance::{verify_issue_bundle, IssueBundle, IssueInfo, Signed, Unauthorized}; -use orchard::keys::{IssuanceMasterKey, IssuanceValidatingKey}; +use orchard::keys::{IssuanceAuthorizingKey, IssuanceValidatingKey}; use orchard::note::{AssetBase, ExtractedNoteCommitment}; use orchard::note_encryption_v3::OrchardDomainV3; use orchard::tree::{MerkleHashOrchard, MerklePath}; @@ -27,7 +27,7 @@ struct Keychain { vk: VerifyingKey, sk: SpendingKey, fvk: FullViewingKey, - imk: IssuanceMasterKey, + isk: IssuanceAuthorizingKey, ik: IssuanceValidatingKey, recipient: Address, } @@ -42,8 +42,8 @@ impl Keychain { fn fvk(&self) -> &FullViewingKey { &self.fvk } - fn imk(&self) -> &IssuanceMasterKey { - &self.imk + fn isk(&self) -> &IssuanceAuthorizingKey { + &self.isk } fn ik(&self) -> &IssuanceValidatingKey { &self.ik @@ -58,14 +58,14 @@ fn prepare_keys() -> Keychain { let fvk = FullViewingKey::from(&sk); let recipient = fvk.address_at(0u32, Scope::External); - let imk = IssuanceMasterKey::from_bytes([0; 32]).unwrap(); - let ik = IssuanceValidatingKey::from(&imk); + let isk = IssuanceAuthorizingKey::from_bytes([0; 32]).unwrap(); + let ik = IssuanceValidatingKey::from(&isk); Keychain { pk, vk, sk, fvk, - imk, + isk, ik, recipient, } @@ -74,11 +74,11 @@ fn prepare_keys() -> Keychain { fn sign_issue_bundle( unauthorized: IssueBundle, rng: OsRng, - imk: &IssuanceMasterKey, + isk: &IssuanceAuthorizingKey, ) -> IssueBundle { let sighash = unauthorized.commitment().into(); let proven = unauthorized.prepare(sighash); - proven.sign(rng, imk).unwrap() + proven.sign(rng, isk).unwrap() } fn build_and_sign_bundle( @@ -161,7 +161,7 @@ fn issue_zsa_notes(asset_descr: &str, keys: &Keychain) -> (Note, Note) { ) .is_ok()); - let issue_bundle = sign_issue_bundle(unauthorized, rng, keys.imk()); + let issue_bundle = sign_issue_bundle(unauthorized, rng, keys.isk()); // Take notes from first action let notes = issue_bundle.get_all_notes(); From bf81f98ff3984f31157670d1c65db77c82831a18 Mon Sep 17 00:00:00 2001 From: Vivek Arte Date: Tue, 7 Nov 2023 16:13:49 +0530 Subject: [PATCH 11/11] adding changes based on comments --- src/keys.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/keys.rs b/src/keys.rs index b7c42fc6d..a1a81eb4c 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -257,12 +257,7 @@ impl IssuanceAuthorizingKey { /// Returns `None` if the bytes do not correspond to a valid Orchard issuance key. pub fn from_bytes(isk_bytes: [u8; 32]) -> CtOption { let isk = IssuanceAuthorizingKey(isk_bytes); - CtOption::new(isk, isk.is_valid()) - } - - /// Checks whether the Orchard-ZSA issuance key is valid //TODO: What are the criteria? - pub fn is_valid(self) -> Choice { - 1u8.into() + CtOption::new(isk, 1u8.into()) } /// Returns the raw bytes of the issuance key.