github.com/emmansun/gmsm@v0.29.1/sm3/sm3blocks_test.go (about) 1 // Copyright 2024 Sun Yimin. All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 //go:build (amd64 || arm64 || s390x || ppc64 || ppc64le) && !purego 6 7 package sm3 8 9 import ( 10 "bytes" 11 "encoding/binary" 12 "fmt" 13 "testing" 14 ) 15 16 func initState4() [4]*[8]uint32 { 17 d := new(digest) 18 d.Reset() 19 var dig1 = d.h 20 var dig2 = d.h 21 var dig3 = d.h 22 return [4]*[8]uint32{&d.h, &dig1, &dig2, &dig3} 23 } 24 25 func createOneBlockBy4() [4]*byte { 26 var p1 [64]byte 27 p1[0] = 0x61 28 p1[1] = 0x62 29 p1[2] = 0x63 30 p1[3] = 0x80 31 p1[63] = 0x18 32 var p2 = p1 33 var p3 = p1 34 var p4 = p1 35 return [4]*byte{&p1[0], &p2[0], &p3[0], &p4[0]} 36 } 37 38 func createOneRandomBlockBy4() [4]*byte { 39 var p1 = [64]byte{ 40 0x49, 0xcf, 0x14, 0x64, 0x9f, 0x32, 0x4a, 0x07, 41 0xe0, 0xd5, 0xbb, 0x2a, 0x00, 0xf7, 0xf0, 0x5d, 42 0x5f, 0x5b, 0xdd, 0x6d, 0x14, 0xdf, 0xf0, 0x28, 43 0xe0, 0x71, 0x32, 0x7e, 0xc0, 0x31, 0x10, 0x45, 44 0x90, 0xed, 0xdb, 0x18, 0xf9, 0x8b, 0x76, 0x3e, 45 0x18, 0xbf, 0x38, 0x2f, 0xf7, 0xc3, 0x87, 0x5f, 46 0x30, 0x27, 0x7f, 0x31, 0x79, 0xba, 0xeb, 0xd7, 47 0x95, 0xe7, 0x85, 0x3f, 0xa6, 0x43, 0xfd, 0xf2, 48 } 49 50 var p2 = p1 51 var p3 = p1 52 var p4 = p1 53 return [4]*byte{&p1[0], &p2[0], &p3[0], &p4[0]} 54 } 55 56 func createTwoBlocksBy4() [4]*byte { 57 var p1 [128]byte 58 p1[0] = 0x61 59 p1[1] = 0x62 60 p1[2] = 0x63 61 p1[3] = 0x64 62 copy(p1[4:], p1[:4]) 63 copy(p1[8:], p1[:8]) 64 copy(p1[16:], p1[:16]) 65 copy(p1[32:], p1[:32]) 66 p1[64] = 0x80 67 p1[126] = 0x02 68 var p2 = p1 69 var p3 = p1 70 var p4 = p1 71 return [4]*byte{&p1[0], &p2[0], &p3[0], &p4[0]} 72 } 73 74 func TestBlockMultBy4(t *testing.T) { 75 digs := initState4() 76 p := createOneBlockBy4() 77 buffer := make([]byte, preallocSizeBy4) 78 blockMultBy4(&digs[0], &p[0], &buffer[0], 1) 79 expected := "[66c7f0f4 62eeedd9 d1f2d46b dc10e4e2 4167c487 5cf2f7a2 297da02b 8f4ba8e0]" 80 for i := 0; i < 4; i++ { 81 s := fmt.Sprintf("%x", digs[i][:]) 82 if s != expected { 83 t.Errorf("digs[%d] got %s", i, s) 84 } 85 } 86 87 digs = initState4() 88 p = createOneRandomBlockBy4() 89 blockMultBy4(&digs[0], &p[0], &buffer[0], 1) 90 expected = "[8c2b6dd5 cc894103 6ec67d69 6154d5fd 62f48fd 984112e3 9e63659e 542709af]" 91 for i := 0; i < 4; i++ { 92 s := fmt.Sprintf("%x", digs[i][:]) 93 if s != expected { 94 t.Errorf("digs[%d] got %s", i, s) 95 } 96 } 97 98 digs = initState4() 99 p = createTwoBlocksBy4() 100 blockMultBy4(&digs[0], &p[0], &buffer[0], 2) 101 expected = "[debe9ff9 2275b8a1 38604889 c18e5a4d 6fdb70e5 387e5765 293dcba3 9c0c5732]" 102 for i := 0; i < 4; i++ { 103 s := fmt.Sprintf("%x", digs[i][:]) 104 if s != expected { 105 t.Errorf("digs[%d] got %s", i, s) 106 } 107 } 108 } 109 110 func TestCopyResultsBy4(t *testing.T) { 111 var m [4][8]uint32 112 var ret, expected [128]byte 113 for i := 0; i < 4; i++ { 114 for j := 0; j < 8; j++ { 115 m[i][j] = _K[i*4+j] 116 } 117 } 118 copyResultsBy4(&m[0][0], &ret[0]) 119 120 for i := 0; i < 4; i++ { 121 for j := 0; j < 8; j++ { 122 binary.BigEndian.PutUint32(expected[i*32+j*4:], m[i][j]) 123 } 124 } 125 if !bytes.Equal(ret[:], expected[:]) { 126 t.Errorf("got %x, expected %x\n", ret[:], expected[:]) 127 } 128 } 129 130 func BenchmarkOneBlockBy4(b *testing.B) { 131 digs := initState4() 132 p := createOneBlockBy4() 133 buffer := make([]byte, preallocSizeBy4) 134 b.SetBytes(64 * 4) 135 b.ReportAllocs() 136 b.ResetTimer() 137 for i := 0; i < b.N; i++ { 138 blockMultBy4(&digs[0], &p[0], &buffer[0], 1) 139 } 140 } 141 142 func BenchmarkTwoBlocksBy4(b *testing.B) { 143 digs := initState4() 144 p := createTwoBlocksBy4() 145 buffer := make([]byte, preallocSizeBy4) 146 b.SetBytes(64 * 2 * 4) 147 b.ReportAllocs() 148 b.ResetTimer() 149 for i := 0; i < b.N; i++ { 150 blockMultBy4(&digs[0], &p[0], &buffer[0], 2) 151 } 152 }