Skip to content

Armv8.2 SM3和SM4

Sun Yimin edited this page Jan 5, 2022 · 31 revisions

SM3 arm64 plain asm on arm64-graviton2

go test -v -short -bench . -run=^$ ./...
goos: linux
goarch: arm64
pkg: github.com/emmansun/gmsm/sm3
BenchmarkHash8Bytes
BenchmarkHash8Bytes-2     	 2738724	       438.4 ns/op	  18.25 MB/s
BenchmarkHash1K
BenchmarkHash1K-2         	  192519	      6232 ns/op	 164.32 MB/s
BenchmarkHash8K
BenchmarkHash8K-2         	   24950	     48112 ns/op	 170.27 MB/s
BenchmarkHash8K_SH256
BenchmarkHash8K_SH256-2   	  223354	      5369 ns/op	1525.81 MB/s
PASS
ok  	github.com/emmansun/gmsm/sm3	5.857s

和CPU指令级别的差距基本上是10倍!

SM4 with AES

AESE指令相当于:

  1. AddRoundKey(state, RoudKey)
  2. ShiftRows(State)
  3. SubBytes(State)

所以,如果RoundKey = 0, 那么AESE相当于执行了

  1. ShiftRows(State)
  2. SubBytes(State)
    go test -v -short -bench . -run=^$ ./...
    goos: linux
    goarch: arm64
    pkg: github.com/emmansun/gmsm/sm4
    BenchmarkEncrypt
    BenchmarkEncrypt-2   	 2145859	       559.1 ns/op	  28.62 MB/s
    BenchmarkDecrypt
    BenchmarkDecrypt-2   	 2145296	       559.4 ns/op	  28.60 MB/s
    BenchmarkExpand
    BenchmarkExpand-2    	 2064466	       581.2 ns/op
    PASS
    ok  	github.com/emmansun/gmsm/sm4	5.334s

Reference

SM3和SM4 CPU指令实现,找不到相关CPU环境,mark先。

  1. Summary of A64 cryptographic instructions
  2. Arm A64 Instruction Set Architecture
  3. linux arm64 crypto / (https://github.com/torvalds/linux/tree/master/arch/arm64/crypto)
  4. A Quick Guide to Go's Assembler
  5. Golang arm instructions mapping
  6. A C/C++ header file that converts Intel SSE intrinsics to Arm/Aarch64 NEON intrinsics.
  7. asm2go