gmsm icon indicating copy to clipboard operation
gmsm copied to clipboard

[go1.17] internal/subtle xor 简化代码,去除重复

Open emmansun opened this issue 1 year ago • 0 comments

amd64/arm64使用指针:

//go:noescape
func xorBytes(dst, a, b *byte, n int)

而generic实现使用切片:

// fastXORBytes xors in bulk. It only works on architectures that
// support unaligned read/writes.
// n needs to be smaller or equal than the length of a and b.
func fastXORBytes(dst, a, b []byte, n int) {

golang 1.17提供了unsafe.Slice方法,可用用来简化代码:

func xorBytes(dstb, xb, yb *byte, n int) {
	// xorBytes assembly is written using pointers and n. Back to slices.
	dst := unsafe.Slice(dstb, n)
	x := unsafe.Slice(xb, n)
	y := unsafe.Slice(yb, n)
...
}

emmansun avatar Aug 19 '22 02:08 emmansun