penumbra_asset/asset/
id.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::Value;
use ark_ff::ToConstraintField;
use ark_serialize::CanonicalDeserialize;
use base64::Engine;
use decaf377::Fq;
use once_cell::sync::Lazy;
use penumbra_num::Amount;
use penumbra_proto::{penumbra::core::asset::v1 as pb, serializers::bech32str, DomainType};
use serde::{Deserialize, Serialize};

/// An identifier for an IBC asset type.
///
/// This is similar to, but different from, the design in [ADR001].  As in
/// ADR001, a denomination trace is hashed to a fixed-size identifier, but
/// unlike ADR001, we hash to a field element rather than a byte string.
///
/// A denomination trace looks like
///
/// - `denom` (native chain A asset)
/// - `transfer/channelToA/denom` (chain B representation of chain A asset)
/// - `transfer/channelToB/transfer/channelToA/denom` (chain C representation of chain B representation of chain A asset)
///
/// ADR001 defines the IBC asset ID as the SHA-256 hash of the denomination
/// trace.  Instead, Penumbra hashes to a field element, so that asset IDs can
/// be more easily used inside of a circuit.
///
/// [ADR001]:
/// https://github.com/cosmos/ibc-go/blob/main/docs/architecture/adr-001-coin-source-tracing.md
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(try_from = "pb::AssetId", into = "pb::AssetId")]
pub struct Id(pub Fq);

impl From<Id> for pb::AssetId {
    fn from(id: Id) -> Self {
        pb::AssetId {
            inner: id.0.to_bytes().to_vec(),
            // Never produce a proto encoding with the alt string encoding.
            alt_bech32m: String::new(),
            // Never produce a proto encoding with the alt base denom.
            alt_base_denom: String::new(),
        }
    }
}

impl TryFrom<pb::AssetId> for Id {
    type Error = anyhow::Error;
    fn try_from(value: pb::AssetId) -> Result<Self, Self::Error> {
        if !value.inner.is_empty() {
            if !value.alt_base_denom.is_empty() || !value.alt_bech32m.is_empty() {
                anyhow::bail!(
                    "AssetId proto has both inner and alt_bech32m or alt_base_denom fields set"
                );
            }
            value.inner.as_slice().try_into()
        } else if !value.alt_bech32m.is_empty() {
            value.alt_bech32m.parse()
        } else if !value.alt_base_denom.is_empty() {
            Ok(Self::from_raw_denom(&value.alt_base_denom))
        } else {
            Err(anyhow::anyhow!(
                "AssetId proto has neither inner nor alt_bech32m nor alt_base_denom fields set"
            ))
        }
    }
}

impl DomainType for Id {
    type Proto = pb::AssetId;
}

impl TryFrom<&[u8]> for Id {
    type Error = anyhow::Error;

    fn try_from(slice: &[u8]) -> Result<Id, Self::Error> {
        Ok(Id(Fq::deserialize_compressed(slice)?))
    }
}

impl TryFrom<[u8; 32]> for Id {
    type Error = anyhow::Error;

    fn try_from(bytes: [u8; 32]) -> Result<Id, Self::Error> {
        Ok(Id(Fq::from_bytes_checked(&bytes).expect("convert to bytes")))
    }
}

impl std::fmt::Debug for Id {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&bech32str::encode(
            &self.0.to_bytes(),
            bech32str::asset_id::BECH32_PREFIX,
            bech32str::Bech32m,
        ))
    }
}

impl std::fmt::Display for Id {
    // IMPORTANT: Changing this is state-breaking.
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(&bech32str::encode(
            &self.0.to_bytes(),
            bech32str::asset_id::BECH32_PREFIX,
            bech32str::Bech32m,
        ))
    }
}

impl std::str::FromStr for Id {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let inner = bech32str::decode(s, bech32str::asset_id::BECH32_PREFIX, bech32str::Bech32m)?;
        pb::AssetId {
            inner,
            alt_bech32m: String::new(),
            alt_base_denom: String::new(),
        }
        .try_into()
    }
}

impl ToConstraintField<Fq> for Id {
    fn to_field_elements(&self) -> Option<Vec<Fq>> {
        let mut elements = Vec::new();
        elements.extend_from_slice(&[self.0]);
        Some(elements)
    }
}

/// The domain separator used to hash asset ids to value generators.
pub static VALUE_GENERATOR_DOMAIN_SEP: Lazy<Fq> = Lazy::new(|| {
    Fq::from_le_bytes_mod_order(blake2b_simd::blake2b(b"penumbra.value.generator").as_bytes())
});

impl Id {
    /// Compute the value generator for this asset, used for computing balance commitments.
    pub fn value_generator(&self) -> decaf377::Element {
        decaf377::Element::encode_to_curve(&poseidon377::hash_1(
            &VALUE_GENERATOR_DOMAIN_SEP,
            self.0,
        ))
    }

    /// Convert the asset ID to bytes.
    pub fn to_bytes(&self) -> [u8; 32] {
        self.0.to_bytes()
    }

    /// Create a value of this denomination.
    pub fn value(&self, amount: Amount) -> Value {
        Value {
            amount,
            asset_id: *self,
        }
    }

    pub(super) fn from_raw_denom(base_denom: &str) -> Self {
        Id(Fq::from_le_bytes_mod_order(
            // XXX choice of hash function?
            blake2b_simd::Params::default()
                .personal(b"Penumbra_AssetID")
                .hash(base_denom.as_bytes())
                .as_bytes(),
        ))
    }

    /// Returns the base64 encoded string of the inner bytes.
    pub fn to_base64(&self) -> String {
        base64::engine::general_purpose::STANDARD.encode(self.to_bytes())
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use std::str::FromStr;

    #[test]
    fn asset_id_encoding() {
        let id = Id::from_raw_denom("upenumbra");

        let bech32m_id = format!("{id}");

        let id2 = Id::from_str(&bech32m_id).expect("can decode valid asset id");

        use penumbra_proto::Message;

        let proto = id.encode_to_vec();
        let proto2 = pb::AssetId {
            alt_bech32m: bech32m_id,
            ..Default::default()
        }
        .encode_to_vec();
        let proto3 = pb::AssetId {
            alt_base_denom: "upenumbra".to_owned(),
            ..Default::default()
        }
        .encode_to_vec();

        let id3 = Id::decode(proto.as_ref()).expect("can decode valid asset id");
        let id4 = Id::decode(proto2.as_ref()).expect("can decode valid asset id");
        let id5 = Id::decode(proto3.as_ref()).expect("can decode valid asset id");

        assert_eq!(id2, id);
        assert_eq!(id3, id);
        assert_eq!(id4, id);
        assert_eq!(id5, id);
    }
}