LCOV - code coverage report
Current view: top level - ballet/siphash13 - fd_siphash13.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 35 35 100.0 %
Date: 2026-09-17 04:28:31 Functions: 1 29 3.4 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_ballet_siphash13_fd_siphash13_h
       2             : #define HEADER_fd_src_ballet_siphash13_fd_siphash13_h
       3             : 
       4             : /* fd_siphash13 provides APIs for SipHash1-3.
       5             :    (1 compression round, 3 finalization rounds)
       6             : 
       7             :    This code is a modified version of https://github.com/antirez/siphash
       8             :    For further license info see NOTICE in the root of this repo.
       9             : 
      10             :    Copyright (c) 2012-2016 Jean-Philippe Aumasson
      11             :    <jeanphilippe.aumasson@gmail.com>
      12             :    Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to>
      13             :    Copyright (c) 2017 Salvatore Sanfilippo <antirez@gmail.com>
      14             :    Modified 2023 by Firedancer Contributors */
      15             : 
      16             : #include "../fd_ballet_base.h"
      17             : 
      18             : #define FD_SIPHASH13_ALIGN     (128UL)
      19             : #define FD_SIPHASH13_FOOTPRINT (128UL)
      20             : 
      21             : struct __attribute__((aligned(FD_SIPHASH13_ALIGN))) fd_siphash13_private {
      22             :   ulong v[ 4 ];
      23             :   ulong n;
      24             :   uchar buf[ 8 ];
      25             : };
      26             : 
      27             : typedef struct fd_siphash13_private fd_siphash13_t;
      28             : 
      29             : /* FD_SIPHASH_ROUND is the SipHash1-3 round function */
      30             : 
      31             : #define FD_SIPHASH_ROUND(v)                    \
      32   483875364 :   do {                                         \
      33   483875364 :     (v)[0] += (v)[1];                          \
      34   483875364 :     (v)[1] = fd_ulong_rotate_left((v)[1], 13); \
      35   483875364 :     (v)[1] ^= (v)[0];                          \
      36   483875364 :     (v)[0] = fd_ulong_rotate_left((v)[0], 32); \
      37   483875364 :     (v)[2] += (v)[3];                          \
      38   483875364 :     (v)[3] = fd_ulong_rotate_left((v)[3], 16); \
      39   483875364 :     (v)[3] ^= (v)[2];                          \
      40   483875364 :     (v)[0] += (v)[3];                          \
      41   483875364 :     (v)[3] = fd_ulong_rotate_left((v)[3], 21); \
      42   483875364 :     (v)[3] ^= (v)[0];                          \
      43   483875364 :     (v)[2] += (v)[1];                          \
      44   483875364 :     (v)[1] = fd_ulong_rotate_left((v)[1], 17); \
      45   483875364 :     (v)[1] ^= (v)[2];                          \
      46   483875364 :     (v)[2] = fd_ulong_rotate_left((v)[2], 32); \
      47   483875364 :   } while (0)
      48             : 
      49             : FD_PROTOTYPES_BEGIN
      50             : 
      51             : /* fd_siphash13_init starts a new SipHash1-3 calculation */
      52             : 
      53             : fd_siphash13_t *
      54             : fd_siphash13_init( fd_siphash13_t * sip,
      55             :                    ulong            k0,
      56             :                    ulong            k1 );
      57             : 
      58             : fd_siphash13_t *
      59             : fd_siphash13_append( fd_siphash13_t * sip,
      60             :                      uchar const *    data,
      61             :                      ulong            sz );
      62             : 
      63             : /* fd_siphash13_append_fast is an aligned-only version of
      64             :    fd_siphash13_append.  sip->n and sz must be multiplies of 8 bytes. */
      65             : 
      66             : fd_siphash13_t *
      67             : fd_siphash13_append_fast( fd_siphash13_t * sip,
      68             :                           uchar const *    data,
      69             :                           ulong            sz );
      70             : 
      71             : /* fd_siphash13_fini finishes a SipHash1-3 calculation.  Returns the
      72             :    hash value. */
      73             : 
      74             : ulong
      75             : fd_siphash13_fini( fd_siphash13_t * sip );
      76             : 
      77             : /* fd_siphash13_hash is a streamlined implementation of:
      78             : 
      79             :      fd_siphash13_t sip[1];
      80             :      return fd_siphash13_fini( fd_siphash13_append( fd_siphash13_init( sip ), data, sz ) );
      81             : 
      82             :    This can be faster for small message sizes. */
      83             : 
      84             : FD_FN_PURE ulong
      85             : fd_siphash13_hash( void const * data,
      86             :                    ulong        sz,
      87             :                    ulong        k0,
      88             :                    ulong        k1 );
      89             : 
      90             : /* fd_siphash13_fini_x32 is an inline specialization of
      91             : 
      92             :      fd_siphash13_t sip[1];
      93             :      *sip = *sip_primed;
      94             :      fd_siphash13_append( sip, data, 32UL );
      95             :      return fd_siphash13_fini( sip );
      96             : 
      97             :    where sip_primed is a primed hasher whose absorbed byte count is a
      98             :    multiple of 8 (so there are no residual bytes) and then exactly 32
      99             :    bytes are appended to it. */
     100             : 
     101             : FD_FN_PURE static inline ulong
     102             : fd_siphash13_fini_x32( fd_siphash13_t const * primed,
     103       88956 :                        void const *           data ) {
     104       88956 :   ulong         v[ 4 ] = { primed->v[0], primed->v[1], primed->v[2], primed->v[3] };
     105       88956 :   uchar const * d      = (uchar const *)data;
     106             : 
     107             :   /* append */
     108      444780 :   for( ulong i=0UL; i<4UL; i++ ) {
     109      355824 :     ulong m = FD_LOAD( ulong, d+8UL*i );
     110      355824 :     v[ 3 ] ^= m;
     111      355824 :     FD_SIPHASH_ROUND( v );
     112      355824 :     v[ 0 ] ^= m;
     113      355824 :   }
     114             : 
     115             :   /* fini */
     116       88956 :   ulong b = (primed->n+32UL)<<56;
     117       88956 :   v[ 3 ] ^= b;
     118       88956 :   FD_SIPHASH_ROUND( v );
     119       88956 :   v[ 0 ] ^= b;
     120             : 
     121       88956 :   v[ 2 ] ^= 0xffUL;
     122       88956 :   FD_SIPHASH_ROUND( v );
     123       88956 :   FD_SIPHASH_ROUND( v );
     124       88956 :   FD_SIPHASH_ROUND( v );
     125       88956 :   return v[ 0 ]^v[ 1 ]^v[ 2 ]^v[ 3 ];
     126       88956 : }
     127             : 
     128             : FD_PROTOTYPES_END
     129             : 
     130             : #endif /* HEADER_fd_src_ballet_siphash13_fd_siphash13_h */

Generated by: LCOV version 1.14