From 7a84c6fd290be252a305b756bbe204db1404fa4a Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Fri, 6 Sep 2024 14:19:40 +0800 Subject: [PATCH] sm3: add copyResultsBy4 test case --- sm3/sm3blocks_test.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/sm3/sm3blocks_test.go b/sm3/sm3blocks_test.go index a206655..c5fca6d 100644 --- a/sm3/sm3blocks_test.go +++ b/sm3/sm3blocks_test.go @@ -7,6 +7,7 @@ package sm3 import ( + "encoding/binary" "fmt" "testing" ) @@ -57,7 +58,7 @@ func TestBlockMultBy4(t *testing.T) { buffer := make([]byte, preallocSizeBy4) blockMultBy4(&digs[0], &p[0], &buffer[0], 1) expected := "[66c7f0f4 62eeedd9 d1f2d46b dc10e4e2 4167c487 5cf2f7a2 297da02b 8f4ba8e0]" - for i:=0; i<4; i++ { + for i := 0; i < 4; i++ { s := fmt.Sprintf("%x", digs[i][:]) if s != expected { t.Errorf("digs[%d] got %s", i, s) @@ -68,7 +69,7 @@ func TestBlockMultBy4(t *testing.T) { p = createTwoBlocksBy4() blockMultBy4(&digs[0], &p[0], &buffer[0], 2) expected = "[debe9ff9 2275b8a1 38604889 c18e5a4d 6fdb70e5 387e5765 293dcba3 9c0c5732]" - for i:=0; i<4; i++ { + for i := 0; i < 4; i++ { s := fmt.Sprintf("%x", digs[i][:]) if s != expected { t.Errorf("digs[%d] got %s", i, s) @@ -76,6 +77,28 @@ func TestBlockMultBy4(t *testing.T) { } } +func TestCopyResultsBy4(t *testing.T) { + var m [4][8]uint32 + var ret, expected [128]byte + var k uint32 = 0 + for i := 0; i < 4; i++ { + for j := 0; j < 8; j++ { + m[i][j] = k + k++ + fmt.Printf("%08x ", m[i][j]) + } + fmt.Println() + } + copyResultsBy4(&m[0][0], &ret[0]) + + for i := 0; i < 4; i++ { + for j := 0; j < 8; j++ { + binary.BigEndian.PutUint32(expected[i*32+j*4:], m[i][j]) + } + } + t.Errorf("got %x, expected %x\n", ret[:], expected[:]) +} + func BenchmarkOneBlockBy4(b *testing.B) { digs := initState4() p := createOneBlockBy4()