From ec053573f170c46567eb7087639ec529c2dfe25c Mon Sep 17 00:00:00 2001 From: fumiama Date: Tue, 23 Nov 2021 13:18:58 +0800 Subject: [PATCH] =?UTF-8?q?perf(tea):=20use=20runtime.fastrand=20name=20?= =?UTF-8?q?=20=20=20=20=20=20=20=20=20old=20time/op=20=20=20=20new=20time/?= =?UTF-8?q?op=20=20=20=20delta=20TEAen/16-8=20=20=20=20=20=20=20242ns=20?= =?UTF-8?q?=C2=B1=201%=20=20=20=20=20225ns=20=C2=B1=202%=20=20-6.98%=20=20?= =?UTF-8?q?(p=3D0.008=20n=3D5+5)=20TEAen/256-8=20=20=20=20=201.71=C2=B5s?= =?UTF-8?q?=20=C2=B1=200%=20=20=20=201.68=C2=B5s=20=C2=B1=200%=20=20-1.58%?= =?UTF-8?q?=20=20(p=3D0.008=20n=3D5+5)=20TEAen/4K-8=20=20=20=20=20=2025.0?= =?UTF-8?q?=C2=B5s=20=C2=B1=201%=20=20=20=2025.0=C2=B5s=20=C2=B1=200%=20?= =?UTF-8?q?=20=20=20~=20=20=20=20=20(p=3D1.000=20n=3D5+5)=20TEAen/32K-8=20?= =?UTF-8?q?=20=20=20=20=20202=C2=B5s=20=C2=B1=201%=20=20=20=20=20202=C2=B5?= =?UTF-8?q?s=20=C2=B1=200%=20=20=20=20~=20=20=20=20=20(p=3D0.548=20n=3D5+5?= =?UTF-8?q?)=20TEAde/16-8=20=20=20=20=20=20=20208ns=20=C2=B1=200%=20=20=20?= =?UTF-8?q?=20=20207ns=20=C2=B1=200%=20=20=20=20~=20=20=20=20=20(p=3D0.198?= =?UTF-8?q?=20n=3D5+5)=20TEAde/256-8=20=20=20=20=201.65=C2=B5s=20=C2=B1=20?= =?UTF-8?q?0%=20=20=20=201.64=C2=B5s=20=C2=B1=200%=20=20-0.39%=20=20(p=3D0?= =?UTF-8?q?.048=20n=3D5+5)=20TEAde/4K-8=20=20=20=20=20=2024.6=C2=B5s=20?= =?UTF-8?q?=C2=B1=200%=20=20=20=2024.6=C2=B5s=20=C2=B1=201%=20=20=20=20~?= =?UTF-8?q?=20=20=20=20=20(p=3D1.000=20n=3D5+5)=20TEAde/32K-8=20=20=20=20?= =?UTF-8?q?=20=20199=C2=B5s=20=C2=B1=200%=20=20=20=20=20199=C2=B5s=20?= =?UTF-8?q?=C2=B1=200%=20=20=20=20~=20=20=20=20=20(p=3D0.905=20n=3D4+5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta TEAen/16-8 66.2MB/s ± 1% 71.2MB/s ± 2% +7.51% (p=0.008 n=5+5) TEAen/256-8 150MB/s ± 0% 152MB/s ± 0% +1.61% (p=0.008 n=5+5) TEAen/4K-8 164MB/s ± 1% 164MB/s ± 0% ~ (p=1.000 n=5+5) TEAen/32K-8 162MB/s ± 1% 162MB/s ± 0% ~ (p=0.548 n=5+5) TEAde/16-8 154MB/s ± 0% 154MB/s ± 0% ~ (p=0.222 n=5+5) TEAde/256-8 165MB/s ± 0% 165MB/s ± 0% ~ (p=0.056 n=5+5) TEAde/4K-8 167MB/s ± 0% 167MB/s ± 1% ~ (p=1.000 n=5+5) TEAde/32K-8 165MB/s ± 0% 165MB/s ± 0% ~ (p=0.825 n=4+5) --- binary/tea.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/binary/tea.go b/binary/tea.go index 371dac04..a0bdff4c 100644 --- a/binary/tea.go +++ b/binary/tea.go @@ -2,11 +2,15 @@ package binary import ( "encoding/binary" - "math/rand" + _ "unsafe" // required by go:linkname ) type TEA [4]uint32 +// Uint32 returns a lock free uint32 value. +//go:linkname Uint32 runtime.fastrand +func Uint32() uint32 + // Encrypt tea 加密 // http://bbs.chinaunix.net/thread-583468-1-1.html // 感谢xichen大佬对TEA的解释 @@ -14,7 +18,9 @@ func (t TEA) Encrypt(src []byte) (dst []byte) { lens := len(src) fill := 10 - (lens+1)%8 dst = make([]byte, fill+lens+7) - _, _ = rand.Read(dst[0:fill]) + binary.LittleEndian.PutUint32(dst, Uint32()) + binary.LittleEndian.PutUint32(dst[4:], Uint32()) + binary.LittleEndian.PutUint32(dst[8:], Uint32()) dst[0] = byte(fill-3) | 0xF8 // 存储pad长度 copy(dst[fill:], src) @@ -36,14 +42,13 @@ func (t TEA) Decrypt(data []byte) []byte { return nil } dst := make([]byte, len(data)) - var iv1, iv2, holder, tmp uint64 + var iv1, iv2, holder uint64 for i := 0; i < len(dst); i += 8 { - block := binary.BigEndian.Uint64(data[i:]) - tmp = t.decode(block ^ iv2) - iv2 = tmp - holder = tmp ^ iv1 - iv1 = block - binary.BigEndian.PutUint64(dst[i:], holder) + iv1 = binary.BigEndian.Uint64(data[i:]) + iv2 ^= iv1 + iv2 = t.decode(iv2) + binary.BigEndian.PutUint64(dst[i:], iv2^holder) + holder = iv1 } return dst[dst[0]&7+3 : len(data)-7] }