1#[derive(Debug, Clone, Copy, PartialEq)]
15pub enum WebizenOpcode {
16 MatchSubject(u64),
18 MatchPredicate(u64),
19 MatchObject(u64),
20 EvalMetadataMask(u32),
21 BindRegister {
22 vector_id: u8,
23 register_index: usize,
24 },
25 MatchRegister {
26 vector_id: u8,
27 register_index: usize,
28 },
29 HaltIfFalse,
30 LessThan {
33 register_index: usize,
34 threshold: f64,
35 },
36 GreaterThan {
37 register_index: usize,
38 threshold: f64,
39 },
40 LoadFloat {
42 register_index: usize,
43 value: f64,
44 },
45 HaltIfTrue,
47}
48
49pub const LANE_PASSTHROUGH: u8 = 0;
51pub const LANE_PERMISSIVE: u8 = 1;
52pub const LANE_BILATERAL: u8 = 2;
53pub const LANE_SPATIOTEMPORAL: u8 = 3;
54
55pub struct WebizenRule {
57 pub name: &'static str,
58 pub routing_lane: u8,
59 pub bytecode: &'static [WebizenOpcode],
60}
61
62pub struct WebizenVM {
64 pub registers: [Option<u64>; 16],
65 pub float_regs: [f64; 16],
66 bytecode_buf: [Option<WebizenOpcode>; 64],
67}
68
69impl WebizenVM {
70 pub fn new() -> Self {
72 Self {
73 registers: [None; 16],
74 float_regs: [0.0; 16],
75 bytecode_buf: [None; 64],
76 }
77 }
78
79 pub fn load_bytecode(&mut self, ops: &[WebizenOpcode]) {
81 for slot in self.bytecode_buf.iter_mut() {
82 *slot = None;
83 }
84 for (i, &op) in ops.iter().enumerate().take(64) {
85 self.bytecode_buf[i] = Some(op);
86 }
87 }
88
89 pub fn execute(&mut self, s: u64, p: u64, o: u64, c: u64, m: u64) -> bool {
92 let mut flag = true;
93 for op in self.bytecode_buf.iter().flatten().copied() {
94 match op {
95 WebizenOpcode::MatchSubject(v) => flag = s == v,
96 WebizenOpcode::MatchPredicate(v) => flag = p == v,
97 WebizenOpcode::MatchObject(v) => flag = o == v,
98 WebizenOpcode::EvalMetadataMask(mask) => {
99 flag = ((m & 0xFFFF) as u32 & mask) == mask;
100 }
101 WebizenOpcode::BindRegister {
102 vector_id,
103 register_index,
104 } => {
105 let v = match vector_id {
106 0 => s,
107 1 => p,
108 2 => o,
109 3 => c,
110 _ => 0,
111 };
112 self.registers[register_index] = Some(v);
113 flag = true;
114 }
115 WebizenOpcode::MatchRegister {
116 vector_id,
117 register_index,
118 } => {
119 let v = match vector_id {
120 0 => s,
121 1 => p,
122 2 => o,
123 3 => c,
124 _ => 0,
125 };
126 flag = self.registers[register_index] == Some(v);
127 }
128 WebizenOpcode::LoadFloat {
129 register_index,
130 value,
131 } => {
132 self.float_regs[register_index] = value;
133 flag = true;
134 }
135 WebizenOpcode::LessThan {
136 register_index,
137 threshold,
138 } => {
139 flag = self.float_regs[register_index] < threshold;
140 }
141 WebizenOpcode::GreaterThan {
142 register_index,
143 threshold,
144 } => {
145 flag = self.float_regs[register_index] > threshold;
146 }
147 WebizenOpcode::HaltIfFalse => {
148 if !flag {
149 return false;
150 }
151 }
152 WebizenOpcode::HaltIfTrue => {
153 if flag {
154 return true;
155 }
156 }
157 }
158 }
159 flag
160 }
161
162 pub fn check_threshold(&mut self, value: f64, ops: &[WebizenOpcode]) -> bool {
165 self.float_regs[0] = value;
166 self.load_bytecode(ops);
167 self.execute(0, 0, 0, 0, 0)
168 }
169}
170
171pub mod policy_rules {
177 use super::*;
178
179 pub const COOPERATIVE_OBLIGATION_GATE: &[WebizenOpcode] = &[
182 WebizenOpcode::EvalMetadataMask(0x0008),
183 WebizenOpcode::HaltIfFalse,
184 ];
185
186 pub const GUARDIAN_IDENTITY_GATE: &[WebizenOpcode] = &[
189 WebizenOpcode::EvalMetadataMask(0x0001),
190 WebizenOpcode::HaltIfFalse,
191 ];
192
193 pub const COMMERCIAL_BLOCK_GATE: &[WebizenOpcode] = &[
196 WebizenOpcode::EvalMetadataMask(0x0004),
197 WebizenOpcode::HaltIfTrue, ];
199}
200
201pub fn evaluate_policy_constraint(
204 name: &str,
205 s: u64,
206 p: u64,
207 o: u64,
208 c: u64,
209 m: u64,
210) -> (bool, u8) {
211 let mut vm = WebizenVM::new();
212 let (ops, lane) = match name {
213 "cooperative_obligation" => (policy_rules::COOPERATIVE_OBLIGATION_GATE, LANE_PERMISSIVE),
214 "guardian_identity" => (policy_rules::GUARDIAN_IDENTITY_GATE, LANE_BILATERAL),
215 "commercial_block" => (policy_rules::COMMERCIAL_BLOCK_GATE, LANE_BILATERAL),
216 _ => return (true, LANE_PASSTHROUGH), };
218 vm.load_bytecode(ops);
219 (vm.execute(s, p, o, c, m), lane)
220}
221
222#[cfg(test)]
223mod tests {
224 use super::*;
225
226 #[test]
227 fn test_numeric_comparison() {
228 let mut vm = WebizenVM::new();
229 let ops = &[
231 WebizenOpcode::LessThan {
232 register_index: 0,
233 threshold: 6.0,
234 },
235 WebizenOpcode::HaltIfFalse,
236 ];
237 assert!(vm.check_threshold(5.5, ops), "5.5h should be < 6h");
238 assert!(!vm.check_threshold(7.0, ops), "7h should not be < 6h");
239 }
240
241 #[test]
242 fn test_policy_gate_passthrough() {
243 let (passed, lane) = evaluate_policy_constraint("cooperative_obligation", 0, 0, 0, 0, 0);
245 assert!(!passed);
246 assert_eq!(lane, LANE_PERMISSIVE);
247 }
248
249 #[test]
250 fn test_policy_gate_satisfied() {
251 let m = 0x0008u64;
253 let (passed, lane) = evaluate_policy_constraint("cooperative_obligation", 0, 0, 0, 0, m);
254 assert!(passed);
255 assert_eq!(lane, LANE_PERMISSIVE);
256 }
257}