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
use decaf377::FieldExt;
use penumbra_proto::{penumbra::crypto::tct::v1 as pb, DomainType};
use poseidon377::Fq;

/// A commitment to a note or swap.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(into = "pb::StateCommitment", try_from = "pb::StateCommitment")]
pub struct StateCommitment(pub Fq);

/// An error when decoding a commitment from a hex string.
#[derive(Clone, Debug, thiserror::Error)]
pub enum ParseCommitmentError {
    /// The string was not a hex string.
    #[error(transparent)]
    InvalidHex(#[from] hex::FromHexError),
    /// The bytes did not encode a valid commitment.
    #[error(transparent)]
    InvalidCommitment(#[from] InvalidStateCommitment),
}

impl StateCommitment {
    /// Parse a hex string as a [`Commitment`].
    pub fn parse_hex(str: &str) -> Result<StateCommitment, ParseCommitmentError> {
        let bytes = hex::decode(str)?;
        Ok(StateCommitment::try_from(&bytes[..])?)
    }
}

impl DomainType for StateCommitment {
    type Proto = pb::StateCommitment;
}

#[cfg(test)]
mod test_serde {
    use super::StateCommitment;

    #[test]
    fn roundtrip_json_zero() {
        let commitment = StateCommitment::try_from([0; 32]).unwrap();
        let bytes = serde_json::to_vec(&commitment).unwrap();
        println!("{bytes:?}");
        let deserialized: StateCommitment = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(commitment, deserialized);
    }

    /*
    Disabled; pbjson_build derived implementations don't play well with bincode,
    because of the issue described here: https://github.com/bincode-org/bincode/issues/276
    #[test]
    fn roundtrip_bincode_zero() {
        let commitment = Commitment::try_from([0; 32]).unwrap();
        let bytes = bincode::serialize(&commitment).unwrap();
        println!("{:?}", bytes);
        let deserialized: Commitment = bincode::deserialize(&bytes).unwrap();
        assert_eq!(commitment, deserialized);
    }
     */
}

impl From<StateCommitment> for pb::StateCommitment {
    fn from(nc: StateCommitment) -> Self {
        Self {
            inner: nc.0.to_bytes().to_vec(),
        }
    }
}

/// Error returned when a note commitment cannot be deserialized because it is not in range.
#[derive(thiserror::Error, Debug, Clone, Copy)]
#[error("Invalid note commitment")]
pub struct InvalidStateCommitment;

impl TryFrom<pb::StateCommitment> for StateCommitment {
    type Error = InvalidStateCommitment;

    fn try_from(value: pb::StateCommitment) -> Result<Self, Self::Error> {
        let bytes: [u8; 32] = value.inner[..]
            .try_into()
            .map_err(|_| InvalidStateCommitment)?;

        let inner = Fq::from_bytes(bytes).map_err(|_| InvalidStateCommitment)?;

        Ok(StateCommitment(inner))
    }
}

impl std::fmt::Display for StateCommitment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&hex::encode(&self.0.to_bytes()[..]))
    }
}

impl std::fmt::Debug for StateCommitment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!(
            "note::Commitment({})",
            hex::encode(&self.0.to_bytes()[..])
        ))
    }
}

impl From<StateCommitment> for [u8; 32] {
    fn from(commitment: StateCommitment) -> [u8; 32] {
        commitment.0.to_bytes()
    }
}

impl TryFrom<[u8; 32]> for StateCommitment {
    type Error = InvalidStateCommitment;

    fn try_from(bytes: [u8; 32]) -> Result<StateCommitment, Self::Error> {
        let inner = Fq::from_bytes(bytes).map_err(|_| InvalidStateCommitment)?;

        Ok(StateCommitment(inner))
    }
}

// TODO: remove? aside from sqlx is there a use case for non-proto conversion from byte slices?
impl TryFrom<&[u8]> for StateCommitment {
    type Error = InvalidStateCommitment;

    fn try_from(slice: &[u8]) -> Result<StateCommitment, Self::Error> {
        let bytes: [u8; 32] = slice[..].try_into().map_err(|_| InvalidStateCommitment)?;

        let inner = Fq::from_bytes(bytes).map_err(|_| InvalidStateCommitment)?;

        Ok(StateCommitment(inner))
    }
}

#[cfg(feature = "arbitrary")]
pub use arbitrary::FqStrategy;

#[cfg(feature = "arbitrary")]
mod arbitrary {
    use ark_ed_on_bls12_377::Fq;
    use ark_ff::{BigInteger256, PrimeField};
    use proptest::strategy::Strategy;

    use super::StateCommitment;

    // Arbitrary implementation for [`Commitment`]s.
    impl proptest::arbitrary::Arbitrary for StateCommitment {
        type Parameters = Vec<StateCommitment>;

        fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
            FqStrategy(args.into_iter().map(|commitment| commitment.0).collect())
                .prop_map(StateCommitment)
        }

        type Strategy = proptest::strategy::Map<FqStrategy, fn(Fq) -> StateCommitment>;
    }

    /// A [`proptest`] [`Strategy`](proptest::strategy::Strategy) for generating [`Fq`]s.
    #[derive(Clone, Debug, PartialEq, Eq, Default)]
    pub struct FqStrategy(Vec<Fq>);

    impl FqStrategy {
        /// Create a new [`FqStrategy`] that will generate arbitrary [`Commitment`]s.
        pub fn arbitrary() -> Self {
            Self::one_of(vec![])
        }

        /// Create a new [`FqStrategy`] that will only produce the given [`Fq`]s.
        ///
        /// If the given vector is empty, this will generate arbitrary [`Fq`]s instead.
        pub fn one_of(commitments: Vec<Fq>) -> Self {
            FqStrategy(commitments)
        }
    }

    impl proptest::strategy::Strategy for FqStrategy {
        type Tree = proptest::strategy::Filter<proptest::strategy::Just<Fq>, fn(&Fq) -> bool>;

        type Value = Fq;

        fn new_tree(
            &self,
            runner: &mut proptest::test_runner::TestRunner,
        ) -> proptest::strategy::NewTree<Self> {
            use proptest::prelude::{Rng, RngCore};
            let rng = runner.rng();
            Ok(if !self.0.is_empty() {
                proptest::strategy::Just(
                    *rng.sample(rand::distributions::Slice::new(&self.0).expect("empty vector")),
                )
            } else {
                let parts = [
                    rng.next_u64(),
                    rng.next_u64(),
                    rng.next_u64(),
                    rng.next_u64(),
                ];
                proptest::strategy::Just(decaf377::Fq::new(BigInteger256::new(parts)))
            }
            .prop_filter("bigger than modulus", |fq| fq.0 < Fq::MODULUS))
        }
    }
}