Home/Instructions/SM4 Encrypt/Decrypt
SM4ED

RISC-V SM4ED Instruction Details

Instruction ManualR-type

SM4 encrypt/decrypt byte step: select a byte from rs2 by bs, apply S-Box/linear layer, XOR with rs1

Instruction Syntax

sm4ed rd, rs1, rs2, bs
Operand Breakdown
Destination rd: register receiving the operation result.
Source rs1: register holding the first operand.
Source rs2: register holding the second operand.
ZksedCrypto & Security

Instruction Encoding

31..25
funct7
24..20
rs2
19..15
rs1
14..12
funct3
11..7
rd
6..0
opcode

SM4ED uses opcode 0110011 (0x33), funct3 000, funct7 0011000. The rs1 and rs2 fields select the two source registers, and rd selects the destination register.

Format: R-type
opcode: 0110011 (0x33)
funct3: 000 (0x0)
funct7: 0011000 (0x18)

Instruction Behavior

sm4ed is a Zksed SM4 encrypt/decrypt round-function acceleration instruction for RV32 and RV64. It selects one byte from rs2[31:0] using bs, applies the SM4 S-Box and data-round linear layer, rotates the result to the byte position selected by bs, XORs it with rs1[31:0], and writes rd; on RV64 the 32-bit result is sign-extended to XLEN. SM4 encryption and decryption use the same core operation, differing in round-key order.

Quick Understanding & Search Notes

SM4ED is a Zksed scalar cryptography instruction for SM4 encrypt/decrypt round byte step. This page is checked against the official scalar crypto extension, avoiding confusion among round functions, key schedule steps, and operand sources.

Selects a byte from rs2 using bs, applies the SM4 S-box and data-round linear layer, then XORs with rs1; on RV64 the 32-bit result is sign-extended.
Scalar crypto instructions use integer X registers, and the official spec requires the relevant crypto instructions to be implemented with data-independent execution latency.

Common Usage Scenarios

Crypto & Security

Understand this scenario with real code like «sm4ed a0, a1, a2, 0».

Chinese National Crypto

Understand this scenario with real code like «sm4ed a0, a1, a2, 0».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is R-type.
  • Confirm the operand order matches the example.
Semantic Check
  • Ensure the destination register usage is compatible with the calling convention.
  • Confirm this is not the lower-level form of a pseudo-instruction expansion.

Pitfalls / Common Confusions

bs selects one byte from rs2[31:0]; rs1 is the final XOR source.
This is a byte-level T-table style step, not a complete SM4 round by itself.
On RV64, the 32-bit result is sign-extended to XLEN.

FAQ

Does SM4ED use floating-point or vector registers?

No. These scalar crypto extension instructions use integer X registers.

Is SM4ED a complete algorithm implementation?

No. It is a low-level step from AES, SHA, SM3, or SM4; software still combines instructions with the algorithm schedule, round constants, or round keys.