1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Frozen algebraic-law declarations that conformance engines verify per operation.
use crate::monotonic_direction::MonotonicDirection;
/// Function pointer used by custom algebraic law checks.
///
/// The first argument is the operation under test. The second argument is the
/// witness tuple encoded as `u32` values. Returning `true` means the law holds
/// for that witness.
pub type LawCheckFn = fn(fn(&[u8]) -> Vec<u8>, &[u32]) -> bool;
/// An algebraic law that an operation must satisfy in the frozen data contract.
///
/// Laws are declared per-operation in the registry. The algebra checker
/// verifies each law exhaustively on small domains and with witnesses on full
/// domains. Example: `AlgebraicLaw::Commutative` records that `add(a, b)` and
/// `add(b, a)` must produce the same bytes.
#[derive(Debug, Clone)]
pub enum AlgebraicLaw {
/// Standard notation: `forall a b . f(a,b) = f(b,a)`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::Commutative;
/// ```
Commutative,
/// Standard notation: `forall a b c . f(f(a,b),c) = f(a,f(b,c))`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::Associative;
/// ```
Associative,
/// Standard notation: `forall a . f(a,e) = a`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::Identity { element: 0 };
/// ```
Identity {
/// The identity element as a `u32` value.
element: u32,
},
/// Standard notation: `forall a . f(e,a) = a`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::LeftIdentity { element: 0 };
/// ```
LeftIdentity {
/// The left identity element as a `u32` value.
element: u32,
},
/// Standard notation: `forall a . f(a,e) = a`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::RightIdentity { element: 0 };
/// ```
RightIdentity {
/// The right identity element as a `u32` value.
element: u32,
},
/// Standard notation: `forall a . f(a,a) = e`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::SelfInverse { result: 0 };
/// ```
SelfInverse {
/// The result of `f(a, a)` as a `u32` value.
result: u32,
},
/// Standard notation: `forall a . f(a,a) = a`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::Idempotent;
/// ```
Idempotent,
/// Standard notation: `forall a . f(a,z) = z and f(z,a) = z`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::Absorbing { element: 0 };
/// ```
Absorbing {
/// The absorbing element.
element: u32,
},
/// Standard notation: `forall a . f(z,a) = z`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::LeftAbsorbing { element: 0 };
/// ```
LeftAbsorbing {
/// The left absorbing argument.
element: u32,
},
/// Standard notation: `forall a . f(a,z) = z`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::RightAbsorbing { element: 0 };
/// ```
RightAbsorbing {
/// The right absorbing argument.
element: u32,
},
/// Standard notation: `forall a . f(f(a)) = a`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::Involution;
/// ```
Involution,
/// Standard notation: `forall a b . f(g(a,b)) = h(f(a),f(b))`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::DeMorgan { inner_op: "and", dual_op: "or" };
/// ```
DeMorgan {
/// The operation on the left side.
inner_op: &'static str,
/// The dual operation on the right side.
dual_op: &'static str,
},
/// Standard notation: `forall a b . a <= b -> f(a) <= f(b)`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::Monotone;
/// ```
Monotone,
/// Standard notation: `forall a b . a <= b -> f(a) <= f(b)` or
/// `forall a b . a <= b -> f(a) >= f(b)`.
///
/// ```
/// use vyre_spec::{AlgebraicLaw, MonotonicDirection};
/// let _law = AlgebraicLaw::Monotonic {
/// direction: MonotonicDirection::NonDecreasing,
/// };
/// ```
Monotonic {
/// Direction of monotonicity.
direction: MonotonicDirection,
},
/// Standard notation: `forall a b . lo <= f(a,b) <= hi`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::Bounded { lo: 0, hi: 32 };
/// ```
Bounded {
/// Inclusive lower bound.
lo: u32,
/// Inclusive upper bound.
hi: u32,
},
/// Standard notation: `forall a . f(a,g(a)) = universe`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::Complement {
/// complement_op: "not",
/// universe: u32::MAX,
/// };
/// ```
Complement {
/// The complementary operation.
complement_op: &'static str,
/// The constant they sum or combine to.
universe: u32,
},
/// Standard notation: `forall a b c . f(a,g(b,c)) = g(f(a,b),f(a,c))`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::DistributiveOver { over_op: "add" };
/// ```
DistributiveOver {
/// The operation that this law distributes over.
over_op: &'static str,
},
/// Standard notation: `forall a b . f(a,g(a,b)) = a`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::LatticeAbsorption { dual_op: "min" };
/// ```
LatticeAbsorption {
/// The dual lattice operation.
dual_op: &'static str,
},
/// Standard notation: `forall a b . f(g(a,b),b) = a`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::InverseOf { op: "add" };
/// ```
InverseOf {
/// The operation this operation inverts.
op: &'static str,
},
/// Standard notation: `forall a b . exactly_one(lt(a,b), eq(a,b), gt(a,b))`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::Trichotomy {
/// less_op: "lt",
/// equal_op: "eq",
/// greater_op: "gt",
/// };
/// ```
Trichotomy {
/// Strict less-than operation id.
less_op: &'static str,
/// Equality operation id.
equal_op: &'static str,
/// Strict greater-than operation id.
greater_op: &'static str,
},
/// Standard notation: `forall a b . f(a,b) = 0 -> a = 0 or b = 0`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// let _law = AlgebraicLaw::ZeroProduct { holds: true };
/// ```
ZeroProduct {
/// Whether this law actually holds.
holds: bool,
},
/// Standard notation: `forall x0 ... xn . predicate(x0, ..., xn)`.
///
/// ```
/// use vyre_spec::AlgebraicLaw;
/// fn check(_op: fn(&[u8]) -> Vec<u8>, _args: &[u32]) -> bool { true }
/// let _law = AlgebraicLaw::Custom {
/// name: "custom",
/// description: "custom predicate",
/// arity: 1,
/// check,
/// };
/// ```
Custom {
/// Human-readable name for this law.
name: &'static str,
/// Description of what the law asserts.
description: &'static str,
/// Number of `u32` witness values passed to the predicate.
arity: usize,
/// Predicate function that returns true when the law holds.
check: LawCheckFn,
},
}
impl AlgebraicLaw {
/// Human-readable name for reporting.
#[must_use]
pub fn name(&self) -> &str {
match self {
Self::Commutative => "commutative",
Self::Associative => "associative",
Self::Identity { .. } => "identity",
Self::LeftIdentity { .. } => "left-identity",
Self::RightIdentity { .. } => "right-identity",
Self::SelfInverse { .. } => "self-inverse",
Self::Idempotent => "idempotent",
Self::Absorbing { .. } => "absorbing",
Self::LeftAbsorbing { .. } => "left-absorbing",
Self::RightAbsorbing { .. } => "right-absorbing",
Self::Involution => "involution",
Self::DeMorgan { .. } => "de-morgan",
Self::Monotone => "monotone",
Self::Monotonic { .. } => "monotonic",
Self::Bounded { .. } => "bounded",
Self::Complement { .. } => "complement",
Self::DistributiveOver { .. } => "distributive",
Self::LatticeAbsorption { .. } => "lattice-absorption",
Self::InverseOf { .. } => "inverse-of",
Self::Trichotomy { .. } => "trichotomy",
Self::ZeroProduct { .. } => "zero-product",
Self::Custom { name, .. } => name,
}
}
/// Whether this law applies to binary operations.
#[must_use]
pub fn is_binary(&self) -> bool {
matches!(
self,
Self::Commutative
| Self::Associative
| Self::Identity { .. }
| Self::LeftIdentity { .. }
| Self::RightIdentity { .. }
| Self::SelfInverse { .. }
| Self::Idempotent
| Self::Absorbing { .. }
| Self::LeftAbsorbing { .. }
| Self::RightAbsorbing { .. }
| Self::Bounded { .. }
| Self::Complement { .. }
| Self::DistributiveOver { .. }
| Self::LatticeAbsorption { .. }
| Self::InverseOf { .. }
| Self::Trichotomy { .. }
| Self::ZeroProduct { .. }
| Self::Custom { .. }
)
}
/// Whether this law applies to unary operations.
#[must_use]
pub fn is_unary(&self) -> bool {
matches!(
self,
Self::Involution
| Self::Monotone
| Self::Monotonic { .. }
| Self::Bounded { .. }
| Self::Complement { .. }
| Self::DeMorgan { .. }
| Self::Custom { .. }
)
}
}
impl PartialEq for AlgebraicLaw {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Commutative, Self::Commutative)
| (Self::Associative, Self::Associative)
| (Self::Idempotent, Self::Idempotent)
| (Self::Involution, Self::Involution)
| (Self::Monotone, Self::Monotone) => true,
(Self::Identity { element: left }, Self::Identity { element: right })
| (Self::LeftIdentity { element: left }, Self::LeftIdentity { element: right })
| (Self::RightIdentity { element: left }, Self::RightIdentity { element: right })
| (Self::Absorbing { element: left }, Self::Absorbing { element: right })
| (Self::LeftAbsorbing { element: left }, Self::LeftAbsorbing { element: right })
| (Self::RightAbsorbing { element: left }, Self::RightAbsorbing { element: right }) => {
left == right
}
(Self::SelfInverse { result: left }, Self::SelfInverse { result: right }) => {
left == right
}
(
Self::DeMorgan {
inner_op: left_inner,
dual_op: left_dual,
},
Self::DeMorgan {
inner_op: right_inner,
dual_op: right_dual,
},
) => left_inner == right_inner && left_dual == right_dual,
(Self::Monotonic { direction: left }, Self::Monotonic { direction: right }) => {
left == right
}
(
Self::Bounded {
lo: left_lo,
hi: left_hi,
},
Self::Bounded {
lo: right_lo,
hi: right_hi,
},
) => left_lo == right_lo && left_hi == right_hi,
(
Self::Complement {
complement_op: left_op,
universe: left_universe,
},
Self::Complement {
complement_op: right_op,
universe: right_universe,
},
) => left_op == right_op && left_universe == right_universe,
(
Self::DistributiveOver { over_op: left },
Self::DistributiveOver { over_op: right },
)
| (
Self::LatticeAbsorption { dual_op: left },
Self::LatticeAbsorption { dual_op: right },
)
| (Self::InverseOf { op: left }, Self::InverseOf { op: right }) => left == right,
(
Self::Trichotomy {
less_op: left_less,
equal_op: left_equal,
greater_op: left_greater,
},
Self::Trichotomy {
less_op: right_less,
equal_op: right_equal,
greater_op: right_greater,
},
) => {
left_less == right_less
&& left_equal == right_equal
&& left_greater == right_greater
}
(Self::ZeroProduct { holds: left }, Self::ZeroProduct { holds: right }) => {
left == right
}
(
Self::Custom {
name: left_name,
arity: left_arity,
check: left_check,
..
},
Self::Custom {
name: right_name,
arity: right_arity,
check: right_check,
..
},
) => {
left_name == right_name
&& left_arity == right_arity
&& core::ptr::fn_addr_eq(*left_check, *right_check)
}
_ => false,
}
}
}