github.com/cloudflare/circl@v1.5.0/simd/keccakf1600/f1600x_test.go (about)

     1  package keccakf1600
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  // From the Keccak code package.
     9  var permutationOfZeroes = [25]uint64{
    10  	0xF1258F7940E1DDE7, 0x84D5CCF933C0478A, 0xD598261EA65AA9EE,
    11  	0xBD1547306F80494D, 0x8B284E056253D057, 0xFF97A42D7F8E6FD4,
    12  	0x90FEE5A0A44647C4, 0x8C5BDA0CD6192E76, 0xAD30A6F71B19059C,
    13  	0x30935AB7D08FFC64, 0xEB5AA93F2317D635, 0xA9A6E6260D712103,
    14  	0x81A57C16DBCF555F, 0x43B831CD0347C826, 0x01F22F1A11A5569F,
    15  	0x05E5635A21D9AE61, 0x64BEFEF28CC970F2, 0x613670957BC46611,
    16  	0xB87C5A554FD00ECB, 0x8C3EE88A1CCF32C8, 0x940C7922AE3A2614,
    17  	0x1841F924A2C509E4, 0x16F53526E70465C2, 0x75F644E97F30A13B,
    18  	0xEAF1FF7B5CECA249,
    19  }
    20  
    21  func TestKeccakF1600x2(t *testing.T) {
    22  	test := func(t *testing.T, turbo bool, f func(s *StateX2, a []uint64)) {
    23  		t.Helper()
    24  		var state StateX2
    25  		a := state.Initialize(turbo)
    26  		f(&state, a)
    27  		for i := 0; i < 25; i++ {
    28  			for j := 0; j < 2; j++ {
    29  				if a[2*i+j] != permutationOfZeroes[i] {
    30  					t.Fatalf("%X", a)
    31  				}
    32  			}
    33  		}
    34  	}
    35  
    36  	t.Run("Generic", func(t *testing.T) {
    37  		test(t, false, func(s *StateX2, a []uint64) { permuteScalarX2(a, false) })
    38  	})
    39  	t.Run("SIMD", func(t *testing.T) {
    40  		test(t, false, func(s *StateX2, a []uint64) { s.Permute() })
    41  	})
    42  }
    43  
    44  func TestKeccakF1600x4(t *testing.T) {
    45  	test := func(t *testing.T, turbo bool, f func(s *StateX4, a []uint64)) {
    46  		t.Helper()
    47  		var state StateX4
    48  		a := state.Initialize(turbo)
    49  		f(&state, a)
    50  		for i := 0; i < 25; i++ {
    51  			for j := 0; j < 4; j++ {
    52  				if a[4*i+j] != permutationOfZeroes[i] {
    53  					t.Fatal()
    54  				}
    55  			}
    56  		}
    57  	}
    58  
    59  	t.Run("Generic", func(t *testing.T) {
    60  		test(t, false, func(s *StateX4, a []uint64) { permuteScalarX4(a, false) })
    61  	})
    62  	t.Run("SIMD", func(t *testing.T) {
    63  		test(t, false, func(s *StateX4, a []uint64) { s.Permute() })
    64  	})
    65  }
    66  
    67  func TestTurboX2(t *testing.T) {
    68  	var state1, state2 StateX2
    69  	a1 := state1.Initialize(true)
    70  	a2 := state2.Initialize(true)
    71  	permuteScalarX2(a1, true)
    72  	state2.Permute()
    73  	if !reflect.DeepEqual(a1, a2) {
    74  		t.Fatal()
    75  	}
    76  }
    77  
    78  func TestTurboX4(t *testing.T) {
    79  	var state1, state2 StateX4
    80  	a1 := state1.Initialize(true)
    81  	a2 := state2.Initialize(true)
    82  	permuteScalarX4(a1, true)
    83  	state2.Permute()
    84  	if !reflect.DeepEqual(a1, a2) {
    85  		t.Fatal()
    86  	}
    87  }
    88  
    89  func BenchmarkF1600x2(b *testing.B) {
    90  	benchmark := func(b *testing.B, turbo bool, f func(s *StateX2, a []uint64)) {
    91  		var state StateX2
    92  		a := state.Initialize(turbo)
    93  
    94  		for i := 0; i < b.N; i++ {
    95  			f(&state, a)
    96  		}
    97  	}
    98  
    99  	bench2 := func(b *testing.B, turbo bool) {
   100  		b.Run("Generic", func(b *testing.B) {
   101  			benchmark(b, turbo, func(s *StateX2, a []uint64) { permuteScalarX2(a, turbo) })
   102  		})
   103  		b.Run("SIMD", func(b *testing.B) {
   104  			benchmark(b, turbo, func(s *StateX2, a []uint64) { s.Permute() })
   105  		})
   106  	}
   107  
   108  	b.Run("Regular", func(b *testing.B) { bench2(b, false) })
   109  	b.Run("Turbo", func(b *testing.B) { bench2(b, true) })
   110  }
   111  
   112  func BenchmarkF1600x4(b *testing.B) {
   113  	benchmark := func(b *testing.B, turbo bool, f func(s *StateX4, a []uint64)) {
   114  		var state StateX4
   115  		a := state.Initialize(turbo)
   116  
   117  		for i := 0; i < b.N; i++ {
   118  			f(&state, a)
   119  		}
   120  	}
   121  
   122  	bench2 := func(b *testing.B, turbo bool) {
   123  		b.Run("Generic", func(b *testing.B) {
   124  			benchmark(b, turbo, func(s *StateX4, a []uint64) { permuteScalarX4(a, turbo) })
   125  		})
   126  		b.Run("SIMD", func(b *testing.B) {
   127  			benchmark(b, turbo, func(s *StateX4, a []uint64) { s.Permute() })
   128  		})
   129  	}
   130  
   131  	b.Run("Regular", func(b *testing.B) { bench2(b, false) })
   132  	b.Run("Turbo", func(b *testing.B) { bench2(b, true) })
   133  }