LCOV - code coverage report
Current view: top level - ballet/keccak256 - fd_keccak256.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 40 40 100.0 %
Date: 2026-05-23 07:06:57 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include "fd_keccak256.h"
       2             : #include "fd_keccak256_private.h"
       3             : 
       4             : fd_keccak256_t *
       5     1200252 : fd_keccak256_init( fd_keccak256_t * sha ) {
       6     1200252 :   fd_memset( sha->state, 0, sizeof( sha->state ) );
       7             : 
       8     1200252 :   sha->padding_start = 0;
       9             : 
      10     1200252 :   return sha;
      11     1200252 : }
      12             : 
      13             : fd_keccak256_t *
      14             : fd_keccak256_append( fd_keccak256_t * sha,
      15             :                      void const *     _data,
      16     1200423 :                      ulong            sz ) {
      17             : 
      18             :   /* If no data to append, we are done */
      19             : 
      20     1200423 :   if( FD_UNLIKELY( !sz ) ) return sha; /* optimize for non-trivial append */
      21             : 
      22             :   /* Unpack inputs */
      23             : 
      24     1200312 :   ulong * state         = sha->state;
      25     1200312 :   uchar * state_bytes   = (uchar*) sha->state;
      26     1200312 :   ulong   padding_start = sha->padding_start;
      27             : 
      28     1200312 :   uchar const * data = (uchar const *)_data;
      29             : 
      30     1200312 :   ulong state_idx = padding_start;
      31   892936203 :   for( ulong i = 0; i < sz; i++ ) {
      32   891735891 :     state_bytes[state_idx] ^= data[i];
      33   891735891 :     state_idx++;
      34   891735891 :     if( state_idx >= FD_KECCAK256_RATE ) {
      35     6000906 :       fd_keccak256_core(state);
      36     6000906 :       state_idx = 0;
      37     6000906 :     }
      38   891735891 :   }
      39             : 
      40     1200312 :   sha->padding_start = state_idx;
      41             : 
      42     1200312 :   return sha;
      43     1200423 : }
      44             : 
      45             : void *
      46             : fd_keccak256_fini( fd_keccak256_t * sha,
      47     1200252 :                    void *           hash ) {
      48             : 
      49             :   /* Unpack inputs */
      50             : 
      51     1200252 :   ulong * state         = sha->state;
      52     1200252 :   uchar * state_bytes   = (uchar*) sha->state;
      53     1200252 :   ulong   padding_start = sha->padding_start;
      54             : 
      55             : 
      56             :   /* Append the terminating message byte */
      57             : 
      58     1200252 :   state_bytes[padding_start] ^= (uchar)0x01;
      59     1200252 :   state_bytes[FD_KECCAK256_RATE-1] ^= (uchar)0x80;
      60     1200252 :   fd_keccak256_core(state);
      61             : 
      62             :   /* Copy the result into hash */
      63             : 
      64     1200252 :   fd_memcpy(hash, state, FD_KECCAK256_OUT_SZ);
      65     1200252 :   return hash;
      66     1200252 : }
      67             : 
      68             : void *
      69             : fd_keccak256_hash( void const * _data,
      70             :                 ulong        sz,
      71      600060 :                 void *       _hash ) {
      72      600060 :   fd_keccak256_t sha;
      73      600060 :   fd_keccak256_init( &sha );
      74      600060 :   fd_keccak256_append( &sha, _data, sz );
      75      600060 :   fd_keccak256_fini( &sha, _hash );
      76             : 
      77             : 
      78      600060 :   return _hash;
      79      600060 : }

Generated by: LCOV version 1.14