whitelist.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphavm library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 //! Type whitelist for AlphaVM. 20 //! 21 //! AlphaVM restricts the available types to a minimal set required for 22 //! credit operations and governance. This module defines the whitelisted 23 //! types that can be used in AlphaVM programs. 24 //! 25 //! ## Whitelisted Types (5 total) 26 //! - `Address` - Alpha network addresses for account identification 27 //! - `U64` - 64-bit unsigned integer for credit amounts 28 //! - `U32` - 32-bit unsigned integer for counters and timestamps 29 //! - `U8` - 8-bit unsigned integer for flags and small values 30 //! - `Boolean` - Boolean values for conditions and flags 31 32 use core::fmt::{self, Display, Formatter}; 33 34 /// The whitelisted types available in AlphaVM. 35 /// 36 /// AlphaVM uses a restricted type system compared to the full SnarkVM type set. 37 /// Only these 5 types are permitted in AlphaVM programs. 38 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] 39 pub enum WhitelistedType { 40 /// The Alpha address type for account identification. 41 Address, 42 /// The 64-bit unsigned integer type for credit amounts. 43 U64, 44 /// The 32-bit unsigned integer type for counters and timestamps. 45 U32, 46 /// The 8-bit unsigned integer type for flags and small values. 47 U8, 48 /// The boolean type for conditions and flags. 49 Boolean, 50 } 51 52 impl WhitelistedType { 53 /// The total number of whitelisted types. 54 pub const COUNT: usize = 5; 55 56 /// Returns all whitelisted types as a slice. 57 pub const fn all() -> &'static [WhitelistedType] { 58 &[ 59 WhitelistedType::Address, 60 WhitelistedType::U64, 61 WhitelistedType::U32, 62 WhitelistedType::U8, 63 WhitelistedType::Boolean, 64 ] 65 } 66 67 /// Returns the type name as a string. 68 pub const fn type_name(&self) -> &'static str { 69 match self { 70 Self::Address => "address", 71 Self::U64 => "u64", 72 Self::U32 => "u32", 73 Self::U8 => "u8", 74 Self::Boolean => "boolean", 75 } 76 } 77 78 /// Returns true if the given type name is whitelisted. 79 pub fn is_whitelisted(type_name: &str) -> bool { 80 matches!(type_name, "address" | "u64" | "u32" | "u8" | "boolean") 81 } 82 83 /// Attempts to parse a type name into a WhitelistedType. 84 pub fn from_type_name(type_name: &str) -> Option<Self> { 85 match type_name { 86 "address" => Some(Self::Address), 87 "u64" => Some(Self::U64), 88 "u32" => Some(Self::U32), 89 "u8" => Some(Self::U8), 90 "boolean" => Some(Self::Boolean), 91 _ => None, 92 } 93 } 94 } 95 96 impl Display for WhitelistedType { 97 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 98 write!(f, "{}", self.type_name()) 99 } 100 } 101 102 #[cfg(test)] 103 mod tests { 104 use super::*; 105 106 #[test] 107 fn test_whitelist_count() { 108 // Verify exactly 5 types are whitelisted 109 assert_eq!(WhitelistedType::COUNT, 5); 110 assert_eq!(WhitelistedType::all().len(), 5); 111 } 112 113 #[test] 114 fn test_whitelist_contains_required_types() { 115 let types = WhitelistedType::all(); 116 117 // Verify all required types are present 118 assert!(types.contains(&WhitelistedType::Address)); 119 assert!(types.contains(&WhitelistedType::U64)); 120 assert!(types.contains(&WhitelistedType::U32)); 121 assert!(types.contains(&WhitelistedType::U8)); 122 assert!(types.contains(&WhitelistedType::Boolean)); 123 } 124 125 #[test] 126 fn test_type_names() { 127 assert_eq!(WhitelistedType::Address.type_name(), "address"); 128 assert_eq!(WhitelistedType::U64.type_name(), "u64"); 129 assert_eq!(WhitelistedType::U32.type_name(), "u32"); 130 assert_eq!(WhitelistedType::U8.type_name(), "u8"); 131 assert_eq!(WhitelistedType::Boolean.type_name(), "boolean"); 132 } 133 134 #[test] 135 fn test_is_whitelisted() { 136 // Whitelisted types 137 assert!(WhitelistedType::is_whitelisted("address")); 138 assert!(WhitelistedType::is_whitelisted("u64")); 139 assert!(WhitelistedType::is_whitelisted("u32")); 140 assert!(WhitelistedType::is_whitelisted("u8")); 141 assert!(WhitelistedType::is_whitelisted("boolean")); 142 143 // Non-whitelisted types (from full SnarkVM) 144 assert!(!WhitelistedType::is_whitelisted("field")); 145 assert!(!WhitelistedType::is_whitelisted("group")); 146 assert!(!WhitelistedType::is_whitelisted("scalar")); 147 assert!(!WhitelistedType::is_whitelisted("i8")); 148 assert!(!WhitelistedType::is_whitelisted("i16")); 149 assert!(!WhitelistedType::is_whitelisted("i32")); 150 assert!(!WhitelistedType::is_whitelisted("i64")); 151 assert!(!WhitelistedType::is_whitelisted("i128")); 152 assert!(!WhitelistedType::is_whitelisted("u16")); 153 assert!(!WhitelistedType::is_whitelisted("u128")); 154 assert!(!WhitelistedType::is_whitelisted("string")); 155 assert!(!WhitelistedType::is_whitelisted("signature")); 156 } 157 158 #[test] 159 fn test_from_type_name() { 160 assert_eq!(WhitelistedType::from_type_name("address"), Some(WhitelistedType::Address)); 161 assert_eq!(WhitelistedType::from_type_name("u64"), Some(WhitelistedType::U64)); 162 assert_eq!(WhitelistedType::from_type_name("u32"), Some(WhitelistedType::U32)); 163 assert_eq!(WhitelistedType::from_type_name("u8"), Some(WhitelistedType::U8)); 164 assert_eq!(WhitelistedType::from_type_name("boolean"), Some(WhitelistedType::Boolean)); 165 166 // Non-whitelisted types return None 167 assert_eq!(WhitelistedType::from_type_name("field"), None); 168 assert_eq!(WhitelistedType::from_type_name("group"), None); 169 assert_eq!(WhitelistedType::from_type_name("invalid"), None); 170 } 171 172 #[test] 173 fn test_display() { 174 assert_eq!(format!("{}", WhitelistedType::Address), "address"); 175 assert_eq!(format!("{}", WhitelistedType::U64), "u64"); 176 assert_eq!(format!("{}", WhitelistedType::U32), "u32"); 177 assert_eq!(format!("{}", WhitelistedType::U8), "u8"); 178 assert_eq!(format!("{}", WhitelistedType::Boolean), "boolean"); 179 } 180 }