primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
Documentation
// SPDX-License-Identifier: AGPL-3.0-or-later
// FHE AND Gate - GPU Implementation
//
// Implements Boolean AND operation on FHE-encrypted data
// For TFHE binary gates: AND(a,b) = (a * b) mod q
//
// This is a simplified FHE AND gate using polynomial representation.
// Production FHE would use gate bootstrapping, but this demonstrates
// the computational pattern for encrypted Boolean logic.

@group(0) @binding(0) var<storage, read> input_a: array<u32>;
@group(0) @binding(1) var<storage, read> input_b: array<u32>;
@group(0) @binding(2) var<storage, read_write> output: array<u32>;

struct Params {
    degree: u32,        // Polynomial degree (N)
    modulus_lo: u32,    // q lower 32 bits
    modulus_hi: u32,    // q upper 32 bits
    _pad0: u32,         // Alignment padding
}

@group(0) @binding(3) var<uniform> params: Params;

// Helper: Compare two 64-bit values (a >= b)
fn u64_gte(a: vec2<u32>, b: vec2<u32>) -> bool {
    if (a.y > b.y) { return true; }
    if (a.y < b.y) { return false; }
    return a.x >= b.x;
}

// Helper: Subtract two 64-bit values
fn u64_sub(a: vec2<u32>, b: vec2<u32>) -> vec2<u32> {
    let borrow = select(0u, 1u, a.x < b.x);
    return vec2<u32>(a.x - b.x, a.y - b.y - borrow);
}

// Simple modular reduction for values likely to be < 2*q
fn simple_mod_reduce(a: vec2<u32>, q: vec2<u32>) -> vec2<u32> {
    var r = a;
    for (var i = 0u; i < 100u; i = i + 1u) {
        if (!u64_gte(r, q)) {
            break;
        }
        r = u64_sub(r, q);
    }
    return r;
}

// Modular multiplication for small values (32-bit inputs)
// For binary FHE, coefficients are typically 0 or 1
fn modular_mul_small(a: u32, b: u32, q: vec2<u32>) -> u32 {
    // For small values (< 256), the product fits in 32 bits
    // Simple 32-bit multiplication
    let product = a * b;
    
    // Reduce modulo q if needed
    let product_vec = vec2<u32>(product, 0u);
    let reduced = simple_mod_reduce(product_vec, q);
    return reduced.x;
}

@compute @workgroup_size(256)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let idx = global_id.x;
    
    // Bounds check
    if (idx >= params.degree) {
        return;
    }
    
    // Get inputs (coefficients from LWE ciphertexts)
    let a = input_a[idx];
    let b = input_b[idx];
    
    // Modulus as 64-bit value
    let q = vec2<u32>(params.modulus_lo, params.modulus_hi);
    
    // Boolean AND: result = (a * b) mod q
    // For binary FHE, a and b are typically 0 or 1 (or noisy versions)
    // Multiplication implements AND: 0*0=0, 0*1=0, 1*0=0, 1*1=1
    output[idx] = modular_mul_small(a, b, q);
}