1use sha2::{Digest, Sha256};
8
9pub fn compute_discriminator(name: &str) -> [u8; 8] {
13 let mut hasher = Sha256::new();
14 hasher.update(format!("global:{name}"));
15 let hash = hasher.finalize();
16 let mut disc = [0u8; 8];
17 disc.copy_from_slice(&hash[..8]);
18 disc
19}
20
21macro_rules! disc {
22 ($(#[$meta:meta])* $name:ident, $ix:expr) => {
23 $(#[$meta])*
24 pub static $name: [u8; 8] = {
25 const PREIMAGE: &[u8] = concat!("global:", $ix).as_bytes();
26 sha256_first8(PREIMAGE)
27 };
28 };
29}
30
31const SHA256_K: [u32; 64] = [
32 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
33 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
34 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
35 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
36 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
37 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
38 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
39 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
40];
41
42const fn compress_block(h: [u32; 8], block: [u8; 64]) -> [u32; 8] {
44 let mut w = [0u32; 64];
45 let mut i = 0;
46 while i < 16 {
47 w[i] = ((block[i * 4] as u32) << 24)
48 | ((block[i * 4 + 1] as u32) << 16)
49 | ((block[i * 4 + 2] as u32) << 8)
50 | (block[i * 4 + 3] as u32);
51 i += 1;
52 }
53 i = 16;
54 while i < 64 {
55 let s0 = w[i - 15].rotate_right(7) ^ w[i - 15].rotate_right(18) ^ (w[i - 15] >> 3);
56 let s1 = w[i - 2].rotate_right(17) ^ w[i - 2].rotate_right(19) ^ (w[i - 2] >> 10);
57 w[i] = w[i - 16]
58 .wrapping_add(s0)
59 .wrapping_add(w[i - 7])
60 .wrapping_add(s1);
61 i += 1;
62 }
63
64 let (mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut hh) =
65 (h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]);
66
67 i = 0;
68 while i < 64 {
69 let s1 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);
70 let ch = (e & f) ^ ((!e) & g);
71 let temp1 = hh
72 .wrapping_add(s1)
73 .wrapping_add(ch)
74 .wrapping_add(SHA256_K[i])
75 .wrapping_add(w[i]);
76 let s0 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);
77 let maj = (a & b) ^ (a & c) ^ (b & c);
78 let temp2 = s0.wrapping_add(maj);
79
80 hh = g;
81 g = f;
82 f = e;
83 e = d.wrapping_add(temp1);
84 d = c;
85 c = b;
86 b = a;
87 a = temp1.wrapping_add(temp2);
88 i += 1;
89 }
90
91 [
92 h[0].wrapping_add(a),
93 h[1].wrapping_add(b),
94 h[2].wrapping_add(c),
95 h[3].wrapping_add(d),
96 h[4].wrapping_add(e),
97 h[5].wrapping_add(f),
98 h[6].wrapping_add(g),
99 h[7].wrapping_add(hh),
100 ]
101}
102
103pub const fn sha256_first8(msg: &[u8]) -> [u8; 8] {
106 let h: [u32; 8] = [
107 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
108 0x5be0cd19,
109 ];
110
111 let bit_len = (msg.len() as u64) * 8;
112 let padded_len = if msg.len() + 9 <= 64 { 64 } else { 128 };
113
114 let mut padded = [0u8; 128];
115 let mut i = 0;
116 while i < msg.len() {
117 padded[i] = msg[i];
118 i += 1;
119 }
120 padded[msg.len()] = 0x80;
121 padded[padded_len - 8] = (bit_len >> 56) as u8;
122 padded[padded_len - 7] = (bit_len >> 48) as u8;
123 padded[padded_len - 6] = (bit_len >> 40) as u8;
124 padded[padded_len - 5] = (bit_len >> 32) as u8;
125 padded[padded_len - 4] = (bit_len >> 24) as u8;
126 padded[padded_len - 3] = (bit_len >> 16) as u8;
127 padded[padded_len - 2] = (bit_len >> 8) as u8;
128 padded[padded_len - 1] = bit_len as u8;
129
130 let mut block0 = [0u8; 64];
132 i = 0;
133 while i < 64 {
134 block0[i] = padded[i];
135 i += 1;
136 }
137 let h = compress_block(h, block0);
138
139 let h = if padded_len > 64 {
141 let mut block1 = [0u8; 64];
142 i = 0;
143 while i < 64 {
144 block1[i] = padded[64 + i];
145 i += 1;
146 }
147 compress_block(h, block1)
148 } else {
149 h
150 };
151
152 [
153 (h[0] >> 24) as u8,
154 (h[0] >> 16) as u8,
155 (h[0] >> 8) as u8,
156 h[0] as u8,
157 (h[1] >> 24) as u8,
158 (h[1] >> 16) as u8,
159 (h[1] >> 8) as u8,
160 h[1] as u8,
161 ]
162}
163
164disc!(
165 DEPOSIT,
167 "deposit"
168);
169disc!(
170 BUY,
172 "buy"
173);
174
175disc!(
176 WITHDRAW,
178 "withdraw"
179);
180disc!(
181 SELL,
183 "sell"
184);
185disc!(
186 WITHDRAW_FROM_AVAILABLE,
188 "withdraw_from_available"
189);
190
191disc!(
192 INVEST,
194 "invest"
195);
196disc!(
197 INVEST_WITH_MAX_AMOUNT,
199 "invest_with_max_amount"
200);
201
202disc!(
203 REDEEM_IN_KIND,
205 "redeem_in_kind"
206);
207
208#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
210#[non_exhaustive]
211pub enum KvaultInstruction {
212 Deposit,
214 Buy,
216 Withdraw,
218 Sell,
220 WithdrawFromAvailable,
222 Invest,
224 InvestWithMaxAmount,
226 RedeemInKind,
228}
229
230impl core::fmt::Display for KvaultInstruction {
231 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
232 write!(f, "{self:?}")
233 }
234}
235
236pub fn identify_instruction(data: &[u8]) -> Option<KvaultInstruction> {
241 if data.len() < 8 {
242 return None;
243 }
244 let mut disc = [0u8; 8];
245 disc.copy_from_slice(&data[..8]);
246
247 match disc {
248 d if d == DEPOSIT => Some(KvaultInstruction::Deposit),
249 d if d == BUY => Some(KvaultInstruction::Buy),
250 d if d == WITHDRAW => Some(KvaultInstruction::Withdraw),
251 d if d == SELL => Some(KvaultInstruction::Sell),
252 d if d == WITHDRAW_FROM_AVAILABLE => Some(KvaultInstruction::WithdrawFromAvailable),
253 d if d == INVEST => Some(KvaultInstruction::Invest),
254 d if d == INVEST_WITH_MAX_AMOUNT => Some(KvaultInstruction::InvestWithMaxAmount),
255 d if d == REDEEM_IN_KIND => Some(KvaultInstruction::RedeemInKind),
256 _ => None,
257 }
258}
259
260#[cfg(test)]
261mod tests {
262 use super::*;
263
264 macro_rules! check_disc {
265 ($name:expr, $constant:ident) => {
266 assert_eq!(
267 compute_discriminator($name),
268 $constant,
269 "Discriminator mismatch for {}",
270 $name
271 );
272 };
273 }
274
275 #[test]
276 fn verify_all_discriminators() {
277 check_disc!("deposit", DEPOSIT);
278 check_disc!("buy", BUY);
279 check_disc!("withdraw", WITHDRAW);
280 check_disc!("sell", SELL);
281 check_disc!("withdraw_from_available", WITHDRAW_FROM_AVAILABLE);
282 check_disc!("invest", INVEST);
283 check_disc!("invest_with_max_amount", INVEST_WITH_MAX_AMOUNT);
284 check_disc!("redeem_in_kind", REDEEM_IN_KIND);
285 }
286}