1use crate::ValidationError;
2
3#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
5pub struct Digest([u8; Self::LEN]);
6
7impl Digest {
8 pub const LEN: usize = 32;
10
11 #[must_use]
13 pub const fn new(bytes: [u8; Self::LEN]) -> Self {
14 Self(bytes)
15 }
16
17 #[must_use]
19 pub const fn as_bytes(&self) -> &[u8; Self::LEN] {
20 &self.0
21 }
22
23 #[must_use]
25 pub const fn is_zero(&self) -> bool {
26 let mut accumulated = 0;
27 let mut index = 0;
28 while index < Self::LEN {
29 accumulated |= self.0[index];
30 index += 1;
31 }
32 accumulated == 0
33 }
34
35 #[must_use]
40 pub const fn ct_eq(&self, other: &Self) -> bool {
41 let mut accumulated = 0;
42 let mut index = 0;
43 while index < Self::LEN {
44 accumulated |= self.0[index] ^ other.0[index];
45 index += 1;
46 }
47 accumulated == 0
48 }
49}
50
51impl core::fmt::Debug for Digest {
52 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
53 formatter.write_str("Digest(..)")
54 }
55}
56
57#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
59pub struct EventId(Digest);
60
61impl EventId {
62 pub const fn new(digest: Digest) -> Result<Self, ValidationError> {
64 if digest.is_zero() {
65 Err(ValidationError::ZeroValue)
66 } else {
67 Ok(Self(digest))
68 }
69 }
70
71 #[must_use]
73 pub const fn digest(&self) -> Digest {
74 self.0
75 }
76}
77
78#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
80pub struct CapabilityRef(Digest);
81
82impl CapabilityRef {
83 pub const fn new(digest: Digest) -> Result<Self, ValidationError> {
85 if digest.is_zero() {
86 Err(ValidationError::ZeroValue)
87 } else {
88 Ok(Self(digest))
89 }
90 }
91
92 #[must_use]
94 pub const fn digest(&self) -> Digest {
95 self.0
96 }
97}
98
99#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
101pub struct PolicyEpoch(Digest);
102
103impl PolicyEpoch {
104 pub const fn new(digest: Digest) -> Result<Self, ValidationError> {
106 if digest.is_zero() {
107 Err(ValidationError::ZeroValue)
108 } else {
109 Ok(Self(digest))
110 }
111 }
112
113 #[must_use]
115 pub const fn digest(&self) -> Digest {
116 self.0
117 }
118}
119
120#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
122pub struct OperationSequence(u64);
123
124impl OperationSequence {
125 pub const fn new(value: u64) -> Result<Self, ValidationError> {
127 if value == 0 {
128 Err(ValidationError::ZeroValue)
129 } else {
130 Ok(Self(value))
131 }
132 }
133
134 #[must_use]
136 pub const fn get(self) -> u64 {
137 self.0
138 }
139}
140
141#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
143pub struct Nonce([u8; Self::LEN]);
144
145impl Nonce {
146 pub const LEN: usize = 16;
148
149 #[must_use]
151 pub const fn new(bytes: [u8; Self::LEN]) -> Self {
152 Self(bytes)
153 }
154
155 #[must_use]
157 pub const fn as_bytes(&self) -> &[u8; Self::LEN] {
158 &self.0
159 }
160}
161
162impl core::fmt::Debug for Nonce {
163 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
164 formatter.write_str("Nonce(..)")
165 }
166}