Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(6)

Delta Between Two Patch Sets: src/pkg/crypto/blowfish/blowfish_test.go

Issue 217116: code review 217116: Implementation of the symmetric block cipher Blowfish. (Closed)
Left Patch Set: code review 217116: Implementation of the symmetric block cipher Blowfish. Created 15 years ago
Right Patch Set: code review 217116: Implementation of the symmetric block cipher Blowfish. Created 15 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/crypto/blowfish/block.go ('k') | src/pkg/crypto/blowfish/cipher.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2010 The Go Authors. All rights reserved. 1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package blowfish 5 package blowfish
6 6
7 import ( 7 import (
8 "testing" 8 "testing"
9 "encoding/binary"
10 ) 9 )
11 10
12 type CryptTest struct { 11 type CryptTest struct {
13 key []byte 12 key []byte
14 in []byte 13 in []byte
15 out []byte 14 out []byte
16 } 15 }
17 16
18 // Test vector values are from http://www.schneier.com/code/vectors.txt 17 // Test vector values are from http://www.schneier.com/code/vectors.txt.
19 var encryptTests = []CryptTest{ 18 var encryptTests = []CryptTest{
20 CryptTest{ 19 CryptTest{
21 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 20 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
22 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 21 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
23 []byte{0x4E, 0xF9, 0x97, 0x45, 0x61, 0x98, 0xDD, 0x78}}, 22 []byte{0x4E, 0xF9, 0x97, 0x45, 0x61, 0x98, 0xDD, 0x78}},
24 CryptTest{ 23 CryptTest{
25 []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 24 []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
26 []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 25 []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
27 []byte{0x51, 0x86, 0x6F, 0xD5, 0xB8, 0x5E, 0xCB, 0x8A}}, 26 []byte{0x51, 0x86, 0x6F, 0xD5, 0xB8, 0x5E, 0xCB, 0x8A}},
28 CryptTest{ 27 CryptTest{
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 pt := make([]byte, len(tt.in)) 183 pt := make([]byte, len(tt.in))
185 c.Decrypt(tt.out, pt) 184 c.Decrypt(tt.out, pt)
186 for j, v := range pt { 185 for j, v := range pt {
187 if v != tt.in[j] { 186 if v != tt.in[j] {
188 t.Errorf("Cipher.Decrypt, test vector #%d: plain -text[%d] = %#x, expected %#x", i, j, v, tt.in[j]) 187 t.Errorf("Cipher.Decrypt, test vector #%d: plain -text[%d] = %#x, expected %#x", i, j, v, tt.in[j])
189 break 188 break
190 } 189 }
191 } 190 }
192 } 191 }
193 } 192 }
194
195 func BenchmarkEncoding(b *testing.B) {
196 var buffer = make([]byte, 8)
197 for i := 0; i < b.N; i++ {
198 packWithBinary(b, buffer)
199 }
200 }
201
202 func packWithBinary(b *testing.B, ba []byte) {
203 l := binary.BigEndian.Uint32(ba[0:])
204 r := binary.BigEndian.Uint32(ba[4:])
205 // ...
206 binary.BigEndian.PutUint32(ba[0:], l)
207 binary.BigEndian.PutUint32(ba[4:], r)
208 b.SetBytes(int64(8))
209 }
210
211 func BenchmarkShifts(b *testing.B) {
212 var buffer = make([]byte, 8)
213 for i := 0; i < b.N; i++ {
214 packWithShifts(b, buffer)
215 }
216 }
217
218 func packWithShifts(b *testing.B, ba []byte) {
219 l := uint32(ba[0])<<24 | uint32(ba[1])<<16 | uint32(ba[2])<<8 | uint32(b a[3])
220 r := uint32(ba[4])<<24 | uint32(ba[5])<<16 | uint32(ba[6])<<8 | uint32(b a[7])
221 // ...
222 ba[0], ba[1], ba[2], ba[3] = byte(l>>24), byte(l>>16), byte(l>>8), byte( l)
223 ba[4], ba[5], ba[6], ba[7] = byte(r>>24), byte(r>>16), byte(r>>8), byte( r)
224 b.SetBytes(int64(8))
225 }
226
227 func BenchmarkCallF(b *testing.B) {
228 var x uint32 = 0x01020304
229 for i := 0; i < b.N; i++ {
230 for j := 0; j < 16; j++ {
231 f1(b, x)
232 }
233 }
234 }
235
236 func f1(b *testing.B, x uint32) {
237 i1 := uint(x & 0xFF)
238 i2 := uint(x >> 8 & 0xFF)
239 i3 := uint(x >> 16 & 0xFF)
240 i4 := uint(x >> 24 & 0xFF)
241 r := s0[i1] + s1[i2]
242 r ^= s2[i3]
243 r += s3[i4]
244 b.SetBytes(int64(4))
245 }
246
247 func BenchmarkInlineF(b *testing.B) {
248 for i := 0; i < b.N; i++ {
249 f2(b)
250 }
251 }
252
253 func f2(b *testing.B) {
254 l := ((s0[0] + s1[0]) ^ s2[0]) + s3[0]
255 r := ((s0[1] + s1[1]) ^ s2[1]) + s3[1]
256 l ^= ((s0[2] + s1[2]) ^ s2[2]) + s3[2]
257 r ^= ((s0[3] + s1[3]) ^ s2[3]) + s3[3]
258 l ^= ((s0[4] + s1[4]) ^ s2[4]) + s3[4]
259 r ^= ((s0[5] + s1[5]) ^ s2[5]) + s3[5]
260 l ^= ((s0[6] + s1[6]) ^ s2[6]) + s3[6]
261 r ^= ((s0[7] + s1[7]) ^ s2[7]) + s3[7]
262 l ^= ((s0[8] + s1[8]) ^ s2[8]) + s3[8]
263 r ^= ((s0[9] + s1[9]) ^ s2[9]) + s3[9]
264 l ^= ((s0[10] + s1[10]) ^ s2[10]) + s3[10]
265 r ^= ((s0[11] + s1[11]) ^ s2[11]) + s3[11]
266 l ^= ((s0[12] + s1[12]) ^ s2[12]) + s3[12]
267 r ^= ((s0[13] + s1[13]) ^ s2[13]) + s3[13]
268 l ^= ((s0[14] + s1[14]) ^ s2[14]) + s3[14]
269 r ^= ((s0[15] + s1[15]) ^ s2[15]) + s3[15]
270 b.SetBytes(int64(64))
271 }
272
273 func BenchmarkUint32Index(b *testing.B) {
274 for i := 0; i < b.N; i++ {
275 f3(b)
276 }
277 }
278
279 func f3(b *testing.B) {
280 xl, xr := uint32(0x01020304), uint32(0x05060708)
281 xl ^= p[0]
282 xr ^= ((s0[xl>>24] + s1[xl>>16&0xFF]) ^ s2[xl>>8&0xFF]) + s3[xl&0xFF] ^ p[1]
283 xl ^= ((s0[xr>>24] + s1[xr>>16&0xFF]) ^ s2[xr>>8&0xFF]) + s3[xr&0xFF] ^ p[2]
284 xr ^= ((s0[xl>>24] + s1[xl>>16&0xFF]) ^ s2[xl>>8&0xFF]) + s3[xl&0xFF] ^ p[3]
285 xl ^= ((s0[xr>>24] + s1[xr>>16&0xFF]) ^ s2[xr>>8&0xFF]) + s3[xr&0xFF] ^ p[4]
286 xr ^= ((s0[xl>>24] + s1[xl>>16&0xFF]) ^ s2[xl>>8&0xFF]) + s3[xl&0xFF] ^ p[5]
287 xl ^= ((s0[xr>>24] + s1[xr>>16&0xFF]) ^ s2[xr>>8&0xFF]) + s3[xr&0xFF] ^ p[6]
288 xr ^= ((s0[xl>>24] + s1[xl>>16&0xFF]) ^ s2[xl>>8&0xFF]) + s3[xl&0xFF] ^ p[7]
289 xl ^= ((s0[xr>>24] + s1[xr>>16&0xFF]) ^ s2[xr>>8&0xFF]) + s3[xr&0xFF] ^ p[8]
290 xr ^= ((s0[xl>>24] + s1[xl>>16&0xFF]) ^ s2[xl>>8&0xFF]) + s3[xl&0xFF] ^ p[9]
291 xl ^= ((s0[xr>>24] + s1[xr>>16&0xFF]) ^ s2[xr>>8&0xFF]) + s3[xr&0xFF] ^ p[10]
292 xr ^= ((s0[xl>>24] + s1[xl>>16&0xFF]) ^ s2[xl>>8&0xFF]) + s3[xl&0xFF] ^ p[11]
293 xl ^= ((s0[xr>>24] + s1[xr>>16&0xFF]) ^ s2[xr>>8&0xFF]) + s3[xr&0xFF] ^ p[12]
294 xr ^= ((s0[xl>>24] + s1[xl>>16&0xFF]) ^ s2[xl>>8&0xFF]) + s3[xl&0xFF] ^ p[13]
295 xl ^= ((s0[xr>>24] + s1[xr>>16&0xFF]) ^ s2[xr>>8&0xFF]) + s3[xr&0xFF] ^ p[14]
296 xr ^= ((s0[xl>>24] + s1[xl>>16&0xFF]) ^ s2[xl>>8&0xFF]) + s3[xl&0xFF] ^ p[15]
297 xl ^= ((s0[xr>>24] + s1[xr>>16&0xFF]) ^ s2[xr>>8&0xFF]) + s3[xr&0xFF] ^ p[16]
298 xr ^= p[17]
299 b.SetBytes(int64(64))
300 }
301
302 func BenchmarkByteIndex(b *testing.B) {
303 for i := 0; i < b.N; i++ {
304 f4(b)
305 }
306 }
307
308 func f4(b *testing.B) {
309 xl, xr := uint32(0x01020304), uint32(0x05060708)
310 xl ^= p[0]
311 xr ^= ((s0[byte(xl>>24)] + s1[byte(xl>>16&0xFF)]) ^ s2[byte(xl>>8&0xFF)] ) + s3[byte(xl&0xFF)] ^ p[1]
312 xl ^= ((s0[byte(xr>>24)] + s1[byte(xr>>16&0xFF)]) ^ s2[byte(xr>>8&0xFF)] ) + s3[byte(xr&0xFF)] ^ p[2]
313 xr ^= ((s0[byte(xl>>24)] + s1[byte(xl>>16&0xFF)]) ^ s2[byte(xl>>8&0xFF)] ) + s3[byte(xl&0xFF)] ^ p[3]
314 xl ^= ((s0[byte(xr>>24)] + s1[byte(xr>>16&0xFF)]) ^ s2[byte(xr>>8&0xFF)] ) + s3[byte(xr&0xFF)] ^ p[4]
315 xr ^= ((s0[byte(xl>>24)] + s1[byte(xl>>16&0xFF)]) ^ s2[byte(xl>>8&0xFF)] ) + s3[byte(xl&0xFF)] ^ p[5]
316 xl ^= ((s0[byte(xr>>24)] + s1[byte(xr>>16&0xFF)]) ^ s2[byte(xr>>8&0xFF)] ) + s3[byte(xr&0xFF)] ^ p[6]
317 xr ^= ((s0[byte(xl>>24)] + s1[byte(xl>>16&0xFF)]) ^ s2[byte(xl>>8&0xFF)] ) + s3[byte(xl&0xFF)] ^ p[7]
318 xl ^= ((s0[byte(xr>>24)] + s1[byte(xr>>16&0xFF)]) ^ s2[byte(xr>>8&0xFF)] ) + s3[byte(xr&0xFF)] ^ p[8]
319 xr ^= ((s0[byte(xl>>24)] + s1[byte(xl>>16&0xFF)]) ^ s2[byte(xl>>8&0xFF)] ) + s3[byte(xl&0xFF)] ^ p[9]
320 xl ^= ((s0[byte(xr>>24)] + s1[byte(xr>>16&0xFF)]) ^ s2[byte(xr>>8&0xFF)] ) + s3[byte(xr&0xFF)] ^ p[10]
321 xr ^= ((s0[byte(xl>>24)] + s1[byte(xl>>16&0xFF)]) ^ s2[byte(xl>>8&0xFF)] ) + s3[byte(xl&0xFF)] ^ p[11]
322 xl ^= ((s0[byte(xr>>24)] + s1[byte(xr>>16&0xFF)]) ^ s2[byte(xr>>8&0xFF)] ) + s3[byte(xr&0xFF)] ^ p[12]
323 xr ^= ((s0[byte(xl>>24)] + s1[byte(xl>>16&0xFF)]) ^ s2[byte(xl>>8&0xFF)] ) + s3[byte(xl&0xFF)] ^ p[13]
324 xl ^= ((s0[byte(xr>>24)] + s1[byte(xr>>16&0xFF)]) ^ s2[byte(xr>>8&0xFF)] ) + s3[byte(xr&0xFF)] ^ p[14]
325 xr ^= ((s0[byte(xl>>24)] + s1[byte(xl>>16&0xFF)]) ^ s2[byte(xl>>8&0xFF)] ) + s3[byte(xl&0xFF)] ^ p[15]
326 xl ^= ((s0[byte(xr>>24)] + s1[byte(xr>>16&0xFF)]) ^ s2[byte(xr>>8&0xFF)] ) + s3[byte(xr&0xFF)] ^ p[16]
327 xr ^= p[17]
328 b.SetBytes(int64(64))
329 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b