cipher: xts asm, add random test

This commit is contained in:
Sun Yimin 2023-08-21 11:07:42 +08:00 committed by GitHub
parent abbe36143c
commit 76635c4cf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -30,7 +30,7 @@ jobs:
uses: actions/checkout@v3
- name: Test
run: go test -v -short ./cipher/...
run: go test -v -short ./...
env:
DISABLE_SM3NI: 1
DISABLE_SM4NI: 1

View File

@ -5,7 +5,9 @@ package cipher
import (
"bytes"
"crypto/rand"
"encoding/hex"
"io"
"testing"
)
@ -45,6 +47,31 @@ func TestDoubleTweakGB(t *testing.T) {
testDoubleTweak(t, true)
}
func testDoubleTweakRandomly(t *testing.T, isGB bool) {
var tweak, t1, t2 [16]byte
io.ReadFull(rand.Reader, tweak[:])
copy(t1[:], tweak[:])
copy(t2[:], tweak[:])
mul2(&t1, isGB)
mul2Generic(&t2, isGB)
if !bytes.Equal(t1[:], t2[:]) {
t.Errorf("tweak %x, expected %x, got %x", tweak[:], t2[:], t1[:])
}
}
func TestDoubleTweakRandomly(t *testing.T) {
for i := 0; i < 10; i++ {
testDoubleTweakRandomly(t, false)
}
}
func TestDoubleTweakGBRandomly(t *testing.T) {
for i := 0; i < 10; i++ {
testDoubleTweakRandomly(t, true)
}
}
func testDoubleTweaks(t *testing.T, isGB bool) {
for _, tk := range testTweakVector {
tweak, _ := hex.DecodeString(tk)