|
| 1 | +# OpenVM Memcpy Extension |
| 2 | + |
| 3 | +This extension provides a custom RISC-V instruction `memcpy_loop` that optimizes memory copy operations by handling different alignment shifts efficiently. |
| 4 | + |
| 5 | +## Custom Instruction: `memcpy_loop shift` |
| 6 | + |
| 7 | +### Format |
| 8 | +``` |
| 9 | +memcpy_loop shift |
| 10 | +``` |
| 11 | + |
| 12 | +Where `shift` is an immediate value (0, 1, 2, or 3) representing the byte alignment shift. |
| 13 | + |
| 14 | +### RISC-V Encoding |
| 15 | +- **Opcode**: `0x73` (custom opcode) |
| 16 | +- **Funct3**: `0x0` (custom funct3) |
| 17 | +- **Immediate**: 12-bit signed immediate for shift value |
| 18 | +- **Format**: I-type instruction |
| 19 | + |
| 20 | +### Usage |
| 21 | +The `memcpy_loop` instruction is designed to replace repetitive shift-handling code in memcpy implementations. Instead of having separate code blocks for each shift value, you can use a single instruction: |
| 22 | + |
| 23 | +```assembly |
| 24 | +# Instead of this repetitive code: |
| 25 | +.Lshift_1: |
| 26 | + lw a5, 0(a4) |
| 27 | + sb a5, 0(a3) |
| 28 | + srli a1, a5, 8 |
| 29 | + sb a1, 1(a3) |
| 30 | + # ... more shift handling code |
| 31 | +
|
| 32 | +# You can use: |
| 33 | +memcpy_loop 1 # Handles shift=1 case |
| 34 | +``` |
| 35 | + |
| 36 | +### Benefits |
| 37 | +1. **Code Size Reduction**: Eliminates repetitive shift-handling code |
| 38 | +2. **Performance**: Optimized implementation in the circuit layer |
| 39 | +3. **Maintainability**: Single instruction handles all shift cases |
| 40 | +4. **Verification**: Zero-knowledge proof ensures correct execution |
| 41 | + |
| 42 | +## Implementation Details |
| 43 | + |
| 44 | +### Circuit Layer |
| 45 | +The instruction is implemented in the `MemcpyIterationAir` circuit which: |
| 46 | +- Reads 4 words (16 bytes) from memory |
| 47 | +- Applies the specified shift to combine words |
| 48 | +- Writes the result to the destination |
| 49 | +- Handles all shift values (0, 1, 2, 3) efficiently |
| 50 | + |
| 51 | +### Transpiler Extension |
| 52 | +The `MemcpyTranspilerExtension` translates the RISC-V instruction into OpenVM's internal format: |
| 53 | +- Parses I-type instruction format |
| 54 | +- Validates shift value (0-3) |
| 55 | +- Converts to OpenVM instruction with shift as operand |
| 56 | + |
| 57 | +### Example Usage |
| 58 | +See `example_memcpy_optimized.s` for a complete example showing how to use the custom instruction to optimize a memcpy implementation. |
| 59 | + |
| 60 | +## Building and Testing |
| 61 | + |
| 62 | +### Compilation |
| 63 | +```bash |
| 64 | +# Build the extension |
| 65 | +cargo build --package openvm-memcpy-circuit --package openvm-memcpy-transpiler |
| 66 | + |
| 67 | +# Check for compilation errors |
| 68 | +cargo check --package openvm-memcpy-circuit --package openvm-memcpy-transpiler |
| 69 | +``` |
| 70 | + |
| 71 | +### Integration |
| 72 | +To use this extension in your OpenVM project: |
| 73 | + |
| 74 | +1. Add the transpiler extension to your OpenVM configuration |
| 75 | +2. Use the `memcpy_loop` instruction in your RISC-V assembly |
| 76 | +3. The circuit will handle the execution and verification |
| 77 | + |
| 78 | +## Architecture |
| 79 | + |
| 80 | +``` |
| 81 | +RISC-V Assembly → Transpiler Extension → OpenVM Instruction → MemcpyIterationAir → Execution |
| 82 | +``` |
| 83 | + |
| 84 | +The extension provides: |
| 85 | +- **Transpiler**: `extensions/memcpy/transpiler/` - Translates RISC-V to OpenVM |
| 86 | +- **Circuit**: `extensions/memcpy/circuit/` - Implements the instruction logic |
0 commit comments