pub fn swap_bytes_32(builder: &CircuitBuilder, input: Wire) -> WireExpand 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:
-
Pass 1: Swap adjacent bytes within each 32-bit half
- Mask:
0x00FF00FF00FF00FF - Operation:
((x & mask) << 8) | ((x >> 8) & mask)
- Mask:
-
Pass 2: Swap adjacent 16-bit units within each 32-bit half
- Mask:
0x0000FFFF0000FFFF - Operation:
((x & mask) << 16) | ((x >> 16) & mask)
- Mask:
§Arguments
builder- The circuit builder to add constraints toinput- 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)