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
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
433
// SKC-LISP: Cryptographic Functions for WASM (Phase 3D-3)
// Pure Rust implementations of Blake3 + Ed25519 for browser execution
// No FFI dependencies (pure WASM)

use wasm_bindgen::prelude::*;

// ============================================================================
// Blake3 WASM Implementation
// ============================================================================

#[wasm_bindgen]
#[derive(Debug)]
pub struct Blake3VerificationResult {
    pub valid: bool,
    pub error_code: u8,  // 0=match, 1=mismatch, 2=invalid_input
}

/// Compute Blake3 hash of input (returns 32-byte digest)
#[wasm_bindgen]
pub fn blake3_hash(input: &[u8]) -> Vec<u8> {
    // Use blake3 crate for pure Rust implementation
    let mut hasher = blake3::Hasher::new();
    hasher.update(input);
    hasher.finalize().as_bytes().to_vec()
}

/// Verify Blake3 digest matches expected value
#[wasm_bindgen]
pub fn blake3_verify_wasm(
    payload: &[u8],
    expected_digest: &[u8],
) -> Blake3VerificationResult {
    // Validate inputs
    if payload.is_empty() || expected_digest.is_empty() {
        return Blake3VerificationResult {
            valid: false,
            error_code: 2,  // invalid_input
        };
    }

    if expected_digest.len() != 32 {
        return Blake3VerificationResult {
            valid: false,
            error_code: 2,  // invalid_input (digest must be 32 bytes)
        };
    }

    // Compute Blake3 digest
    let mut hasher = blake3::Hasher::new();
    hasher.update(payload);
    let computed = hasher.finalize();

    // Constant-time comparison (prevent timing attacks)
    let mut match_flag = true;
    for (computed_byte, expected_byte) in computed.as_bytes().iter().zip(expected_digest) {
        if computed_byte != expected_byte {
            match_flag = false;
        }
    }

    Blake3VerificationResult {
        valid: match_flag,
        error_code: if match_flag { 0 } else { 1 },  // 0=match, 1=mismatch
    }
}

// ============================================================================
// Ed25519 WASM Implementation
// ============================================================================

#[wasm_bindgen]
#[derive(Debug)]
pub struct Ed25519VerificationResult {
    pub valid: bool,
    pub error_code: u8,  // 0=valid, 1=invalid, 2=invalid_input
}

/// Verify Ed25519 signature
#[wasm_bindgen]
pub fn ed25519_verify_wasm(
    message: &[u8],
    signature: &[u8],
    public_key: &[u8],
) -> Ed25519VerificationResult {
    // Validate inputs
    if message.is_empty() {
        return Ed25519VerificationResult {
            valid: false,
            error_code: 2,  // invalid_input
        };
    }

    if signature.len() != 64 {
        return Ed25519VerificationResult {
            valid: false,
            error_code: 2,  // invalid_input (signature must be 64 bytes)
        };
    }

    if public_key.len() != 32 {
        return Ed25519VerificationResult {
            valid: false,
            error_code: 2,  // invalid_input (public key must be 32 bytes)
        };
    }

    // Convert to ed25519-zebra types
    let sig_bytes: [u8; 64] = match signature.try_into() {
        Ok(b) => b,
        Err(_) => {
            return Ed25519VerificationResult {
                valid: false,
                error_code: 2,
            }
        }
    };

    let pk_bytes: [u8; 32] = match public_key.try_into() {
        Ok(b) => b,
        Err(_) => {
            return Ed25519VerificationResult {
                valid: false,
                error_code: 2,
            }
        }
    };

    // Parse signature + public key
    let signature = ed25519_zebra::Signature::from(sig_bytes);

    let public_key: ed25519_zebra::VerificationKey = match ed25519_zebra::VerificationKeyBytes::from(pk_bytes)
        .try_into()
    {
        Ok(pk) => pk,
        Err(_) => {
            return Ed25519VerificationResult {
                valid: false,
                error_code: 2,
            }
        }
    };

    // Verify signature
    match public_key.verify(&signature, message) {
        Ok(()) => Ed25519VerificationResult {
            valid: true,
            error_code: 0,  // valid
        },
        Err(_) => Ed25519VerificationResult {
            valid: false,
            error_code: 1,  // invalid
        },
    }
}

// ============================================================================
// NASM Mutation Validator (ported to Rust for WASM)
// ============================================================================

#[wasm_bindgen]
pub struct MutationValidationResult {
    pub valid: bool,
    pub error_code: u8,
}

impl MutationValidationResult {
    pub fn with_message(valid: bool, error_code: u8) -> Self {
        MutationValidationResult { valid, error_code }
    }
}

/// 8-point mutation validation gate (ported from NASM)
#[wasm_bindgen]
pub fn validate_mutation_wasm(
    event_id: u32,
    generation: u32,
    source_hash: &[u8],
    bytecode_hash: &[u8],
    native_code_hash: &[u8],
    actor_signature: &[u8],
) -> MutationValidationResult {
    let mut errors = Vec::new();

    // Gate 1: Event ID is non-zero
    if event_id == 0 {
        errors.push("Event ID must be non-zero");
    }

    // Gate 2: Generation counter is valid
    if generation > 0x7FFFFFFF {
        errors.push("Generation counter overflow");
    }

    // Gate 3: Source hash is 32 bytes
    if source_hash.len() != 32 {
        errors.push("Source hash must be 32 bytes");
    }

    // Gate 4: Bytecode hash is 32 bytes
    if bytecode_hash.len() != 32 {
        errors.push("Bytecode hash must be 32 bytes");
    }

    // Gate 5: Native code hash is 32 bytes
    if native_code_hash.len() != 32 {
        errors.push("Native code hash must be 32 bytes");
    }

    // Gate 6: Signature is 64 bytes
    if actor_signature.len() != 64 {
        errors.push("Actor signature must be 64 bytes");
    }

    // Gate 7: Hashes are distinct (no aliasing)
    if source_hash == bytecode_hash || bytecode_hash == native_code_hash {
        errors.push("Hash aliasing detected (hashes must be distinct)");
    }

    // Gate 8: No hash is all-zeros
    if source_hash.iter().all(|&b| b == 0) {
        errors.push("Source hash is all-zeros (invalid)");
    }

    let valid = errors.is_empty();

    MutationValidationResult {
        valid,
        error_code: if valid { 0 } else { 1 },
    }
}

// ============================================================================
// Proof Certificate Validation (WASM)
// ============================================================================

#[wasm_bindgen]
pub struct ProofCertificateValidationResult {
    pub valid: bool,
    pub theorem_id: u32,
    pub theorems_covered: u32,
    pub error_code: u8,
}

/// Validate proof certificate structure + signature
#[wasm_bindgen]
pub fn validate_proof_certificate_wasm(
    cert_bytes: &[u8],
) -> ProofCertificateValidationResult {
    // Expected structure: 157 bytes
    // [0:4]    theorem_id (u32, LE)
    // [4:36]   proof_hash (32 bytes)
    // [36:40]  theorems_covered (u32, LE)
    // [40:44]  machine_state_invariants (u32, LE)
    // [44:60]  cranelift_backend (16 bytes, null-padded)
    // [60:61]  optimization_level (u8)
    // [61:125] signature (64 bytes)
    // [125:157] public_key (32 bytes)

    let mut errors = Vec::new();

    // Gate 1: Size check
    if cert_bytes.len() != 157 {
        return ProofCertificateValidationResult {
            valid: false,
            theorem_id: 0,
            theorems_covered: 0,
            error_code: 2,
        };
    }

    // Parse fields
    let theorem_id = u32::from_le_bytes([
        cert_bytes[0],
        cert_bytes[1],
        cert_bytes[2],
        cert_bytes[3],
    ]);

    let theorems_covered = u32::from_le_bytes([
        cert_bytes[36],
        cert_bytes[37],
        cert_bytes[38],
        cert_bytes[39],
    ]);

    let machine_state_inv = u32::from_le_bytes([
        cert_bytes[40],
        cert_bytes[41],
        cert_bytes[42],
        cert_bytes[43],
    ]);

    let opt_level = cert_bytes[60];

    // Gate 2: Theorem ID in range
    if theorem_id < 0x0001 || theorem_id > 0x000B {
        errors.push(format!("Theorem ID out of range: 0x{:04X}", theorem_id));
    }

    // Gate 3: At least one theorem covered
    if theorems_covered == 0 {
        errors.push("No theorems covered (bitmask is zero)".to_string());
    }

    // Gate 4: Machine state invariants non-zero
    if machine_state_inv == 0 {
        errors.push("Machine state invariants must be non-zero".to_string());
    }

    // Gate 5: Optimization level in range
    if opt_level > 2 {
        errors.push(format!("Optimization level out of range: {}", opt_level));
    }

    // Gate 6: Backend string is valid
    let backend_bytes = &cert_bytes[44..60];
    let backend_str = std::str::from_utf8(backend_bytes)
        .unwrap_or("")
        .trim_end_matches('\0');

    if !["x86_64", "aarch64", "wasm32"].contains(&backend_str) {
        errors.push(format!("Unknown backend: {}", backend_str));
    }

    let valid = errors.is_empty();

    ProofCertificateValidationResult {
        valid,
        theorem_id,
        theorems_covered,
        error_code: if valid { 0 } else { 1 },
    }
}

// ============================================================================
// WASM Module Initialization
// ============================================================================

#[wasm_bindgen(start)]
pub fn init_wasm() {
    // Initialize panic hook for better error messages in browser console
    #[cfg(feature = "console_error_panic_hook")]
    console_error_panic_hook::set_once();
}

// ============================================================================
// Tests (compiled with `cargo test --target wasm32-unknown-unknown`)
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_blake3_hash() {
        let input = b"test payload";
        let hash = blake3_hash(input);
        assert_eq!(hash.len(), 32);
    }

    #[test]
    fn test_blake3_verify_valid() {
        let input = b"test payload";
        let digest = blake3_hash(input);
        let result = blake3_verify_wasm(input, &digest);
        assert!(result.valid);
        assert_eq!(result.error_code, 0);
    }

    #[test]
    fn test_blake3_verify_invalid() {
        let input = b"test payload";
        let digest = blake3_hash(input);
        let mut bad_digest = digest.clone();
        bad_digest[0] ^= 0xFF;  // Flip bits
        let result = blake3_verify_wasm(input, &bad_digest);
        assert!(!result.valid);
        assert_eq!(result.error_code, 1);
    }

    #[test]
    fn test_mutation_validation_valid() {
        let result = validate_mutation_wasm(
            1,  // event_id
            5,  // generation
            &[0x01; 32],  // source_hash
            &[0x02; 32],  // bytecode_hash
            &[0x03; 32],  // native_code_hash
            &[0x04; 64],  // actor_signature
        );
        assert!(result.valid);
        assert_eq!(result.error_code, 0);
    }

    #[test]
    fn test_mutation_validation_invalid_event_id() {
        let result = validate_mutation_wasm(
            0,  // event_id (invalid: must be non-zero)
            5,
            &[0x01; 32],
            &[0x02; 32],
            &[0x03; 32],
            &[0x04; 64],
        );
        assert!(!result.valid);
    }

    #[test]
    fn test_proof_certificate_validation() {
        let mut cert = vec![0u8; 157];
        // Set theorem_id to 0x0001
        cert[0] = 0x01;
        cert[1] = 0x00;
        // Set theorems_covered to 0x000F
        cert[36] = 0x0F;
        cert[37] = 0x00;
        // Set machine_state_invariants to 0x07
        cert[40] = 0x07;
        cert[41] = 0x00;
        // Set backend to "x86_64"
        for (i, byte) in b"x86_64".iter().enumerate() {
            cert[44 + i] = *byte;
        }
        // Set optimization_level to 2
        cert[60] = 2;

        let result = validate_proof_certificate_wasm(&cert);
        assert!(result.valid);
        assert_eq!(result.theorem_id, 0x0001);
        assert_eq!(result.theorems_covered, 0x000F);
    }
}