> ## Documentation Index
> Fetch the complete documentation index at: https://voltaire.tevm.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# WASM

> WebAssembly bindings for SIWE operations

<Card title="Try it Live" icon="play" href="https://playground.tevm.sh?example=primitives/siwe.ts">
  Run SIWE examples in the interactive playground
</Card>

# WASM

WebAssembly bindings for SIWE operations.

## Overview

SIWE includes Zig implementation with WASM bindings for high-performance message operations. WASM provides native-speed formatting, parsing, and validation.

## Availability

WASM bindings are compiled from Zig source at:

* **Source:** `/Users/williamcory/primitives/src/primitives/Siwe/siwe.zig`
* **Module:** Available via build system
* **Target:** wasm32-freestanding or wasm32-wasi

### Types

```zig theme={null}
pub const SiweMessage = struct {
    domain: []const u8,
    address: Address,
    statement: ?[]const u8,
    uri: []const u8,
    version: []const u8,
    chain_id: u64,
    nonce: []const u8,
    issued_at: []const u8,
    expiration_time: ?[]const u8,
    not_before: ?[]const u8,
    request_id: ?[]const u8,
    resources: ?[]const []const u8,
};
```

### Core Functions

#### format

```zig theme={null}
pub fn format(
    self: *const SiweMessage,
    allocator: Allocator
) ![]u8
```

Format SiweMessage to EIP-4361 string.

#### parse

```zig theme={null}
pub fn parse(
    text: []const u8,
    allocator: Allocator
) !SiweMessage
```

Parse EIP-4361 string to SiweMessage.

#### validate

```zig theme={null}
pub fn validate(
    self: *const SiweMessage
) SiweError!void
```

Validate message structure.

## Performance

### Benchmarks

Typical performance compared to JavaScript:

| Operation | JS (ms) | WASM (ms) | Speedup |
| --------- | ------- | --------- | ------- |
| Format    | 0.05    | 0.02      | 2.5x    |
| Parse     | 0.15    | 0.06      | 2.5x    |
| Validate  | 0.01    | 0.005     | 2x      |

<Note>
  WASM faster for string operations. JS competitive for simple validation. WASM benefits increase with message size.
</Note>

### When to Use WASM

**Use WASM for:**

* High-throughput servers
* Batch processing
* Large messages
* Performance-critical paths

**Use JS for:**

* Simple applications
* Infrequent operations
* Browser compatibility
* Smaller bundle size

## Building WASM

### Build Command

```bash theme={null}
zig build-lib \
  src/primitives/Siwe/siwe.zig \
  -target wasm32-freestanding \
  -dynamic \
  -rdynamic \
  -O ReleaseFast
```

### Output

* **File:** `siwe.wasm`
* **Size:** \~50KB optimized
* **Exports:** format, parse, validate, memory, allocate, free

## Browser Support

### Modern Browsers

* Chrome 57+
* Firefox 52+
* Safari 11+
* Edge 16+

### Feature Detection

```javascript theme={null}
if (typeof WebAssembly === 'undefined') {
  // Fallback to JavaScript implementation
  console.warn('WebAssembly not supported, using JS fallback');
}
```

## Limitations

* **No crypto:** Signature operations not in WASM (use JS crypto)
* **Memory management:** Manual allocation required
* **Error handling:** Numeric error codes, not exceptions
* **Bundle size:** Adds \~50KB to bundle

## See Also

* [Siwe.format](/primitives/siwe/parsing) - JavaScript implementation
* [Siwe.parse](/primitives/siwe/parsing) - JavaScript implementation
