sha3_auto_test Subroutine

public subroutine sha3_auto_test()

Arguments

None

Source Code

subroutine sha3_auto_test()

  !call sha3_test11()
  call sha3_test21()
  call sha3_test31()
  call sha3_test41()
  call sha3_test51()
  call sha3_test61()
contains
!================================================================================
subroutine sha3_test11()
!================================================================================
  integer(kind=int8), dimension(512/8) :: digest
  integer(kind=int8), dimension(:), allocatable :: buffer
  type(sha3_state) :: S

  print *
  print *, 'TEST11  : hash empty string'
  print '(a,a128)', '         ', sha3_hexdigest( sha3(buffer,512))
  allocate( buffer(0) )
  call sha3_update( S, buffer, 512 )
  call sha3_digest( S, digest )
  print '(a,a128)', '         ',sha3_hexdigest( digest )
  print '(a,2a128)', '         A69F73CCA23A9AC5C8B567DC185A756E97C982164FE25859E0D1DCC1475C80', &
       'A615B2123AF1F5F94C11E3E9402C3AC558F500199D95B6D3E301758586281DCD26'
end subroutine sha3_test11
!================================================================================
subroutine sha3_test21()
!================================================================================
  character(len=1024) :: m
  integer(kind=int8), dimension(224/8) :: digest
  integer(kind=int8), dimension(:), allocatable :: buffer
  type(sha3_state) :: S

  print *
  print *, 'TEST21  : hash "abc"'
  m = 'abc'
  allocate( buffer(len_trim(m)) )
  buffer = transfer( trim(m), buffer )
  print *, '        ', sha3_hexdigest( sha3( buffer, 224 ) )
  call sha3_update( S, buffer, 224 )
  call sha3_digest( S, digest )
  print *, '        ', sha3_hexdigest( digest )
  print *, '        E642824C3F8CF24AD09234EE7D3C766FC9A3A5168D0C94AD73B46FDF'

  deallocate( buffer )

end subroutine sha3_test21
!================================================================================
subroutine sha3_test31()
!================================================================================
  character(len=1024) :: m
  integer(kind=int8), dimension(224/8) :: digest
  integer(kind=int8), dimension(:), allocatable :: buffer
  type(sha3_state) :: S

  print *
  print *, 'TEST31  : hash "abc...stu"'

  m = 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'
  allocate( buffer(len_trim(m)) )
  buffer = transfer( trim(m), buffer )
  print *, '        ', sha3_hexdigest( sha3( buffer, 224 ) )
  call sha3_update( S, buffer, 224 )
  call sha3_digest( S, digest )
  print *, '        ', sha3_hexdigest( digest )
  print *, '        543E6868E1666C1A643630DF77367AE5A62A85070A51C14CBF665CBC'

  deallocate( buffer )

end subroutine sha3_test31
!================================================================================
subroutine sha3_test41()
!================================================================================
  integer, parameter :: N = 1000*1000
  integer, parameter :: M = 100
  integer(kind=int8), dimension(224/8) :: digest
  integer(kind=int8), dimension(:), allocatable :: buffer
  type(sha3_state) :: S
  integer :: i, j
  real :: t1, t2, d1, d2, d3

  print *
  print *, 'TEST41  : hash "a"*',N

  allocate( buffer(N) )
  do i = 1, N
     buffer(i) = 97_int8
  enddo

  call cpu_time( t1 )
  call sha3_update( S, buffer, 224 )
  call sha3_digest( S, digest )
  call cpu_time( t2 )
  d1 = t2 - t1
  print *, '        ', sha3_hexdigest( digest )
  call cpu_time( t1 )
  digest = sha3( buffer, 224 )
  call cpu_time( t2 )
  d2 = t2 - t1
  ! now provide it in small packets
  call cpu_time( t1 )
  j = 0
  do i = 1, N/M
     call sha3_update( S, buffer(j+1:j+M) )
     j = j + M
  enddo
  call sha3_digest( S, digest )
  call cpu_time( t2 )
  d3 = t2 - t1
  print *, '        ', sha3_hexdigest( digest )
  print *, '        D69335B93325192E516A912E6D19A15CB51C6ED5C15243E7A7FD653C'
  deallocate( buffer )

  !print *, 'timings: ', d1, d2, d3

  !call sha3_file( 'sha3.f90', 224, digest )

end subroutine sha3_test41
!================================================================================
subroutine sha3_test51()
!================================================================================
  integer               :: i, j
  character(len=128)    :: digest, fname, fname2
  character(len=256)    :: line
  integer, dimension(4) :: dv, mds

  dv = (/ 224, 256, 384, 512 /)
  mds = (/ 56, 64, 96, 128 /)

  print *
  print *, 'TEST 51 : hash files and compare digests with reference'

  ! loop on test vectors
  do i = 1, 5
     write( fname2, '(a,i3.3,a)' ) 'test_vectors/test_', i, '.digests'
     open( unit=12, file=trim(fname2) )
     print *, '   file #', i
     ! loop on SHA3 variant
     do j = 1, 4
        write( fname, '(a,i3.3,a)' ) 'test_vectors/test_', i, '.msg'
        call sha3_file( dv(j), fname, digest )
        write( *, '(10x,i3,1x,a)' ) dv(j), trim(digest)
        read( 12, '(a)' ) line
        write( *, '(10x,a)' ) trim(line)
        print *
     enddo
     close( 12 )
     print *
  enddo

end subroutine sha3_test51
!================================================================================
subroutine sha3_test61()
!================================================================================
  integer, parameter :: N = 100*1024*1024
  integer(kind=int8), dimension(224/8) :: digest
  integer(kind=int8), dimension(:), allocatable :: buffer
  type(sha3_state) :: S
  integer :: i
  real :: t1, t2, d1

  print *
  print *, 'TEST61  : speed test (hash 100 MiB)'

  allocate( buffer(N) )
  do i = 1, N
     buffer(i) = 97_int8
  enddo

  call cpu_time( t1 )
  call sha3_update( S, buffer, 224 )
  call sha3_digest( S, digest )
  call cpu_time( t2 )
  d1 = t2 - t1
  print *, '        ', sha3_hexdigest( digest )

  print *, 'timings: ', d1, 's'
  deallocate( buffer )

end subroutine sha3_test61

end subroutine sha3_auto_test