Skip to main content

superinstance_ffi/
holonomy.rs

1/// Check holonomy: product of all transforms must equal 1 (identity)
2/// Each transform is a packed i64 representing a group element.
3/// For simplicity, we check if the cumulative XOR-based hash equals 0 or
4/// if the product of signed values equals 1.
5#[no_mangle]
6pub extern "C" fn si_holonomy_check(transforms: *const i64, len: usize) -> bool {
7    if transforms.is_null() || len == 0 {
8        return true; // trivial loop
9    }
10    let slice = unsafe { std::slice::from_raw_parts(transforms, len) };
11    let product: i64 = slice.iter().product();
12    product == 1
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    #[test]
20    fn test_holonomy_trivial() {
21        assert!(si_holonomy_check(std::ptr::null(), 0));
22    }
23
24    #[test]
25    fn test_holonomy_identity() {
26        let transforms: [i64; 3] = [1, 1, 1];
27        assert!(si_holonomy_check(transforms.as_ptr(), 3));
28    }
29
30    #[test]
31    fn test_holonomy_product_one() {
32        // [-1, -1, 1] product = 1
33        let transforms: [i64; 3] = [-1, -1, 1];
34        assert!(si_holonomy_check(transforms.as_ptr(), 3));
35    }
36
37    #[test]
38    fn test_holonomy_fail() {
39        let transforms: [i64; 2] = [2, 3]; // 6 != 1
40        assert!(!si_holonomy_check(transforms.as_ptr(), 2));
41    }
42}