Home/Instructions/SM4 Key Schedule
SM4KS

RISC-V SM4KS Instruction Details

Instruction ManualR-type

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

Instruction Syntax

sm4ks 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

SM4KS uses opcode 0110011 (0x33), funct3 000, funct7 0011010. 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: 0011010 (0x1a)

Instruction Behavior

sm4ks is a Zksed SM4 key-schedule acceleration instruction for RV32 and RV64. It selects one byte from rs2[31:0] using bs, applies the SM4 S-Box and key-schedule 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. It is used to build the SM4 key schedule, not to run the data encrypt/decrypt round directly.

Quick Understanding & Search Notes

SM4KS is a Zksed scalar cryptography instruction for SM4 key schedule 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 key-schedule 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 «sm4ks a0, a1, a2, 0».

Chinese National Crypto

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

Key Schedule

Understand this scenario with real code like «sm4ks 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.
The key-schedule linear layer differs from the data-round linear layer used by sm4ed.
On RV64, the 32-bit result is sign-extended to XLEN.

FAQ

Does SM4KS use floating-point or vector registers?

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

Is SM4KS 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.