]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/des: faster block expansion.
authorRémy Oudompheng <oudomphe@phare.normalesup.org>
Fri, 26 Jul 2013 07:10:29 +0000 (09:10 +0200)
committerRémy Oudompheng <oudomphe@phare.normalesup.org>
Fri, 26 Jul 2013 07:10:29 +0000 (09:10 +0200)
On amd64:

benchmark           old ns/op    new ns/op    delta
BenchmarkEncrypt         6170         3593  -41.77%
BenchmarkDecrypt         6209         3564  -42.60%

benchmark            old MB/s     new MB/s  speedup
BenchmarkEncrypt         1.30         2.23    1.72x
BenchmarkDecrypt         1.29         2.24    1.74x

Update #4299.

R=golang-dev, agl, bradfitz, rsc
CC=golang-dev
https://golang.org/cl/11874043

src/pkg/crypto/des/block.go
src/pkg/crypto/des/const.go
src/pkg/crypto/des/des_test.go

index c11c62cd7239e30e7139fba3e32eb63fe98d512b..805bb9f8cc0c75c84d593b7cc2212e474619c5db 100644 (file)
@@ -40,7 +40,7 @@ func decryptBlock(subkeys []uint64, dst, src []byte) {
 
 // DES Feistel function
 func feistel(right uint32, key uint64) (result uint32) {
-       sBoxLocations := key ^ permuteBlock(uint64(right), expansionFunction[:])
+       sBoxLocations := key ^ expandBlock(right)
        var sBoxResult uint32
        for i := uint8(0); i < 8; i++ {
                sBoxLocation := uint8(sBoxLocations>>42) & 0x3f
@@ -63,6 +63,18 @@ func permuteBlock(src uint64, permutation []uint8) (block uint64) {
        return
 }
 
+// expandBlock expands an input block of 32 bits,
+// producing an output block of 48 bits.
+func expandBlock(src uint32) (block uint64) {
+       src = (src << 5) | (src >> 27)
+       for i := 0; i < 8; i++ {
+               block <<= 6
+               block |= uint64(src) & (1<<6 - 1)
+               src = (src << 4) | (src >> 28)
+       }
+       return
+}
+
 // creates 16 28-bit blocks rotated according
 // to the rotation schedule
 func ksRotate(in uint32) (out []uint32) {
index 2bd485ee80e00022abd145944fc1eaa959d6469d..1b898dc2758be01d12ef54ad884a9cfcf9ea85d6 100644 (file)
@@ -32,17 +32,6 @@ var finalPermutation = [64]byte{
        31, 63, 23, 55, 15, 47, 7, 39,
 }
 
-// Used to expand an input block of 32 bits, producing an output block of 48
-// bits.
-var expansionFunction = [48]byte{
-       0, 31, 30, 29, 28, 27, 28, 27,
-       26, 25, 24, 23, 24, 23, 22, 21,
-       20, 19, 20, 19, 18, 17, 16, 15,
-       16, 15, 14, 13, 12, 11, 12, 11,
-       10, 9, 8, 7, 8, 7, 6, 5,
-       4, 3, 4, 3, 2, 1, 0, 31,
-}
-
 // Yields a 32-bit output from a 32-bit input
 var permutationFunction = [32]byte{
        16, 25, 12, 11, 3, 20, 4, 15,
index 2e87e99b67f126ac30f20bd6b7aff9885bb1db9f..5a9308a8d2b8b002bdd1ec99518bbb633d0e7c03 100644 (file)
@@ -1521,3 +1521,31 @@ func ExampleNewTripleDESCipher() {
        // See crypto/cipher for how to use a cipher.Block for encryption and
        // decryption.
 }
+
+func BenchmarkEncrypt(b *testing.B) {
+       tt := encryptDESTests[0]
+       c, err := NewCipher(tt.key)
+       if err != nil {
+               b.Fatal("NewCipher:", err)
+       }
+       out := make([]byte, len(tt.in))
+       b.SetBytes(int64(len(out)))
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               c.Encrypt(out, tt.in)
+       }
+}
+
+func BenchmarkDecrypt(b *testing.B) {
+       tt := encryptDESTests[0]
+       c, err := NewCipher(tt.key)
+       if err != nil {
+               b.Fatal("NewCipher:", err)
+       }
+       out := make([]byte, len(tt.out))
+       b.SetBytes(int64(len(out)))
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               c.Decrypt(out, tt.out)
+       }
+}