Skip to main content

Overview

The BLAKE2 inline provides an optimized implementation of the BLAKE2b hash function with 512-bit output. BLAKE2 is faster than MD5, SHA-1, SHA-2, and SHA-3, while being at least as secure as SHA-3.

API Reference

Constants

pub const IV: [u64; 8];  // BLAKE2b initialization vector
pub const SIGMA: [[usize; 16]; 12];  // Message permutation table

pub const STATE_VECTOR_LEN: usize = 8;
pub const STATE_SIZE_IN_BYTES: usize = 64;
pub const MSG_BLOCK_LEN: usize = 16;
pub const BLOCK_INPUT_SIZE_IN_BYTES: usize = 128;
pub const OUTPUT_SIZE_IN_BYTES: usize = 64;

Initialization Vector

pub const IV: [u64; 8] = [
    0x6a09e667f3bcc908,
    0xbb67ae8584caa73b,
    0x3c6ef372fe94f82b,
    0xa54ff53a5f1d36f1,
    0x510e527fade682d1,
    0x9b05688c2b3e6c1f,
    0x1f83d9abfb41bd6b,
    0x5be0cd19137e2179,
];

Usage Examples

Basic Hashing

use blake2_inline::blake2b;

#[jolt::provable]
fn hash_data(data: &[u8]) -> [u8; 64] {
    blake2b(data)
}

Keyed Hashing (MAC)

use blake2_inline::blake2b_keyed;

#[jolt::provable]
fn create_mac(key: &[u8], message: &[u8]) -> [u8; 64] {
    blake2b_keyed(key, message)
}

File Integrity Verification

use blake2_inline::blake2b;

#[jolt::provable]
fn verify_file_hash(contents: &[u8], expected: [u8; 64]) -> bool {
    let hash = blake2b(contents);
    hash == expected
}

Implementation Details

Compression Function

BLAKE2b uses a compression function similar to ChaCha20:
  • State: 8 × 64-bit words (512 bits)
  • Message block: 16 × 64-bit words (1024 bits)
  • Rounds: 12 rounds using the SIGMA permutation table

SIGMA Permutation Table

The message schedule uses a 12-round permutation pattern:
pub const SIGMA: [[usize; 16]; 12] = [
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
    [14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3],
    // ... (10 more rounds)
];

Custom Instruction

  • BLAKE2_COMPRESS (funct3=0x00, funct7=0x02): Performs BLAKE2b compression

Memory Layout

  • Input block: 128 bytes (16 × 64-bit words)
  • State: 64 bytes (8 × 64-bit words)
  • Output: 64 bytes

Performance Characteristics

  • Block size: 128 bytes per compression
  • Output size: 64 bytes (512 bits)
  • Speed: Faster than SHA-2 and SHA-3 on modern hardware
  • Security: 512-bit security level

BLAKE2 Variants

The inline implements BLAKE2b (64-bit optimized). Other variants:
  • BLAKE2b: Optimized for 64-bit platforms (this implementation)
  • BLAKE2s: Optimized for 8-32 bit platforms (not included)
  • BLAKE2bp: Parallel variant (not included)
  • BLAKE2sp: Parallel variant for smaller platforms (not included)

Feature Flags

  • host: Enables reference implementation for host-side execution
    • Guest code: Compile WITHOUT this feature
    • Prover code: Compile WITH this feature

Constants Reference

pub const INLINE_OPCODE: u32 = 0x0B;
pub const BLAKE2_FUNCT3: u32 = 0x00;
pub const BLAKE2_FUNCT7: u32 = 0x02;
pub const BLAKE2_NAME: &str = "BLAKE2_INLINE";

Comparison with Standard Implementation

  • Cycle count: ~10-50x reduction compared to pure Rust implementation
  • Proving time: Proportionally faster proof generation
  • Compatibility: Standard BLAKE2b output format

Security Properties

  • Collision resistance: 512 bits
  • Pre-image resistance: 512 bits
  • MAC security: Full key size (up to 64 bytes)

Source Code Location

jolt-inlines/blake2/
├── src/
│   ├── lib.rs          # Module definitions and constants
│   ├── sdk.rs          # Public API
│   ├── exec.rs         # Host-side reference implementation
│   └── sequence_builder.rs  # Instruction sequence generation
└── Cargo.toml

See Also

Build docs developers (and LLMs) love