Skip to main content

swap_bytes_32

Function swap_bytes_32 

Source
pub fn swap_bytes_32(builder: &CircuitBuilder, input: Wire) -> Wire
Expand description

Swaps bytes within each 32-bit half of a 64-bit word independently.

This function performs byte swapping on the high and low 32-bit halves of a 64-bit word in parallel. Each half has its byte order reversed independently, without affecting the other half.

§Algorithm

The implementation uses the Hacker’s Delight algorithm applied to both 32-bit halves simultaneously:

  1. Pass 1: Swap adjacent bytes within each 32-bit half

    • Mask: 0x00FF00FF00FF00FF
    • Operation: ((x & mask) << 8) | ((x >> 8) & mask)
  2. Pass 2: Swap adjacent 16-bit units within each 32-bit half

    • Mask: 0x0000FFFF0000FFFF
    • Operation: ((x & mask) << 16) | ((x >> 16) & mask)

§Arguments

  • builder - The circuit builder to add constraints to
  • input - Wire containing the 64-bit value to process

§Returns

  • Wire containing the result with bytes swapped within each 32-bit half

§Example

Input: 0x0123456789ABCDEF (bytes: 01 23 45 67 | 89 AB CD EF) Output: 0x6745230189ABCDEF (bytes: 67 45 23 01 | EF CD AB 89)