pub const SUBSTRATE_SIZE: usize = 58;
pub const SUBSTRATE_VERSION: u8 = 0x01;
pub const COMMITMENT_SIZE: usize = 32;
pub const TIMESTAMP_SIZE: usize = 8;
pub const NONCE_SIZE: usize = 16;
pub const COMMITMENT_OFFSET: usize = 2;
pub const TIMESTAMP_OFFSET: usize = COMMITMENT_OFFSET + COMMITMENT_SIZE;
pub const NONCE_OFFSET: usize = TIMESTAMP_OFFSET + TIMESTAMP_SIZE;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[repr(u8)]
pub enum ComputationType {
BiometricAuth = 0x01,
FraudScore = 0x02,
FedNowPayment = 0x03,
SolanaAttestation = 0x04,
HatsGovernance = 0x05,
BitcoinUtxo = 0x06,
KycVerification = 0x07,
ShareComputation = 0x08,
ArchiveSign = 0x09,
MedVaultPhi = 0x0A,
VaultKeyOp = 0x0B,
ApiResponse = 0x0C,
GenericFhe = 0xFF,
}
impl ComputationType {
#[must_use]
pub const fn from_byte(byte: u8) -> Option<Self> {
match byte {
0x01 => Some(Self::BiometricAuth),
0x02 => Some(Self::FraudScore),
0x03 => Some(Self::FedNowPayment),
0x04 => Some(Self::SolanaAttestation),
0x05 => Some(Self::HatsGovernance),
0x06 => Some(Self::BitcoinUtxo),
0x07 => Some(Self::KycVerification),
0x08 => Some(Self::ShareComputation),
0x09 => Some(Self::ArchiveSign),
0x0A => Some(Self::MedVaultPhi),
0x0B => Some(Self::VaultKeyOp),
0x0C => Some(Self::ApiResponse),
0xFF => Some(Self::GenericFhe),
_ => None,
}
}
#[must_use]
pub const fn to_byte(self) -> u8 {
self as u8
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn layout_constants_add_up() {
assert_eq!(SUBSTRATE_SIZE, 58);
assert_eq!(COMMITMENT_OFFSET, 2);
assert_eq!(TIMESTAMP_OFFSET, 34);
assert_eq!(NONCE_OFFSET, 42);
assert_eq!(NONCE_OFFSET + NONCE_SIZE, SUBSTRATE_SIZE);
}
#[test]
fn computation_type_round_trip() {
let all = [
ComputationType::BiometricAuth,
ComputationType::FraudScore,
ComputationType::FedNowPayment,
ComputationType::SolanaAttestation,
ComputationType::HatsGovernance,
ComputationType::BitcoinUtxo,
ComputationType::KycVerification,
ComputationType::ShareComputation,
ComputationType::ArchiveSign,
ComputationType::MedVaultPhi,
ComputationType::VaultKeyOp,
ComputationType::ApiResponse,
ComputationType::GenericFhe,
];
for ct in all {
assert_eq!(ComputationType::from_byte(ct.to_byte()), Some(ct));
}
}
#[test]
fn unrecognized_computation_type_bytes_are_none() {
assert_eq!(ComputationType::from_byte(0x00), None);
assert_eq!(ComputationType::from_byte(0x0D), None);
assert_eq!(ComputationType::from_byte(0x42), None);
assert_eq!(ComputationType::from_byte(0xFE), None);
}
}