|
| 1 | +use group::GroupEncoding; |
| 2 | +use halo2_proofs::arithmetic::CurveExt; |
| 3 | +use pasta_curves::pallas; |
| 4 | +use subtle::CtOption; |
| 5 | + |
| 6 | +use crate::constants::fixed_bases::{VALUE_COMMITMENT_PERSONALIZATION, VALUE_COMMITMENT_V_BYTES}; |
| 7 | +use crate::keys::IssuerValidatingKey; |
| 8 | + |
| 9 | +/// Note type identifier. |
| 10 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 11 | +pub struct NoteType(pub(crate) pallas::Point); |
| 12 | + |
| 13 | +const MAX_ASSET_DESCRIPTION_SIZE: usize = 512; |
| 14 | + |
| 15 | +// the hasher used to derive the assetID |
| 16 | +#[allow(non_snake_case)] |
| 17 | +fn assetID_hasher(msg: Vec<u8>) -> pallas::Point { |
| 18 | + // TODO(zsa) replace personalization, will require circuit change? |
| 19 | + pallas::Point::hash_to_curve(VALUE_COMMITMENT_PERSONALIZATION)(&msg) |
| 20 | +} |
| 21 | + |
| 22 | +impl NoteType { |
| 23 | + /// Deserialize the note_type from a byte array. |
| 24 | + pub fn from_bytes(bytes: &[u8; 32]) -> CtOption<Self> { |
| 25 | + pallas::Point::from_bytes(bytes).map(NoteType) |
| 26 | + } |
| 27 | + |
| 28 | + /// Serialize the note_type to its canonical byte representation. |
| 29 | + pub fn to_bytes(self) -> [u8; 32] { |
| 30 | + self.0.to_bytes() |
| 31 | + } |
| 32 | + |
| 33 | + /// $DeriveNoteType$. |
| 34 | + /// |
| 35 | + /// Defined in [Zcash Protocol Spec § TBD: Note Types][notetypes]. |
| 36 | + /// |
| 37 | + /// [notetypes]: https://zips.z.cash/protocol/nu5.pdf#notetypes |
| 38 | + #[allow(non_snake_case)] |
| 39 | + pub fn derive(ik: &IssuerValidatingKey, assetDesc: Vec<u8>) -> Self { |
| 40 | + assert!(assetDesc.len() < MAX_ASSET_DESCRIPTION_SIZE); |
| 41 | + |
| 42 | + let mut s = vec![]; |
| 43 | + s.extend(ik.to_bytes()); |
| 44 | + s.extend(assetDesc); |
| 45 | + |
| 46 | + NoteType(assetID_hasher(s)) |
| 47 | + } |
| 48 | + |
| 49 | + /// Note type for the "native" currency (zec), maintains backward compatibility with Orchard untyped notes. |
| 50 | + pub fn native() -> Self { |
| 51 | + NoteType(assetID_hasher(VALUE_COMMITMENT_V_BYTES.to_vec())) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Generators for property testing. |
| 56 | +#[cfg(any(test, feature = "test-dependencies"))] |
| 57 | +#[cfg_attr(docsrs, doc(cfg(feature = "test-dependencies")))] |
| 58 | +pub mod testing { |
| 59 | + use proptest::prelude::*; |
| 60 | + |
| 61 | + use super::NoteType; |
| 62 | + |
| 63 | + use crate::keys::{testing::arb_spending_key, IssuerAuthorizingKey, IssuerValidatingKey}; |
| 64 | + |
| 65 | + prop_compose! { |
| 66 | + /// Generate a uniformly distributed note type |
| 67 | + pub fn arb_note_type()( |
| 68 | + sk in arb_spending_key(), |
| 69 | + bytes32a in prop::array::uniform32(prop::num::u8::ANY), |
| 70 | + bytes32b in prop::array::uniform32(prop::num::u8::ANY), |
| 71 | + ) -> NoteType { |
| 72 | + let bytes64 = [bytes32a, bytes32b].concat(); |
| 73 | + let isk = IssuerAuthorizingKey::from(&sk); |
| 74 | + NoteType::derive(&IssuerValidatingKey::from(&isk), bytes64) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments