secp256k1_zkp/zkp/
tag.rs

1use crate::ffi;
2use core::fmt;
3
4/// Represents a tag.
5///
6/// Tags are 32-byte data structures used in surjection proofs. Usually, tags are created from hashes.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
8pub struct Tag(ffi::Tag);
9
10impl Tag {
11    pub(crate) fn into_inner(self) -> ffi::Tag {
12        self.0
13    }
14
15    #[cfg(all(feature = "actual-rand", feature = "std"))]
16    pub(crate) fn as_inner(&self) -> &ffi::Tag {
17        &self.0
18    }
19}
20
21impl AsRef<[u8]> for Tag {
22    fn as_ref(&self) -> &[u8] {
23        self.0.as_ref()
24    }
25}
26
27impl fmt::Display for Tag {
28    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
29        write!(f, "{:x}", self)
30    }
31}
32
33impl fmt::LowerHex for Tag {
34    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
35        for i in self.0.as_ref() {
36            write!(f, "{:02x}", i)?;
37        }
38        Ok(())
39    }
40}
41
42#[cfg(all(test, feature = "rand-std"))]
43impl Tag {
44    pub fn random() -> Self {
45        use rand::thread_rng;
46        use rand::RngCore;
47
48        let mut bytes = [0u8; 32];
49        thread_rng().fill_bytes(&mut bytes);
50
51        Self::from(bytes)
52    }
53}
54
55impl From<[u8; 32]> for Tag {
56    fn from(bytes: [u8; 32]) -> Self {
57        Tag(ffi::Tag::from(bytes))
58    }
59}
60
61impl From<Tag> for [u8; 32] {
62    fn from(tag: Tag) -> Self {
63        tag.0.into()
64    }
65}