LCOV - code coverage report
Current view: top level - ballet/lthash - fd_lthash.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 17 32 53.1 %
Date: 2026-10-23 17:13:25 Functions: 8 1086 0.7 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_ballet_lthash_fd_lthash_h
       2             : #define HEADER_fd_src_ballet_lthash_fd_lthash_h
       3             : 
       4             : /* LtHash provides APIs for lattice-based incremental hash based on blake3.
       5             :    https://eprint.iacr.org/2019/227
       6             : */
       7             : 
       8             : #include "../blake3/fd_blake3.h"
       9             : 
      10             : #define FD_LTHASH_ALIGN       (64UL) /* sufficient for AVX512 */
      11         282 : #define FD_LTHASH_LEN_BYTES (2048UL)
      12     9125584 : #define FD_LTHASH_LEN_ELEMS (1024UL)
      13             : 
      14             : union __attribute__((aligned(FD_LTHASH_ALIGN))) fd_lthash_value {
      15             :   uchar  bytes[FD_LTHASH_LEN_BYTES];
      16             :   ushort words[FD_LTHASH_LEN_ELEMS];
      17             : };
      18             : typedef union fd_lthash_value fd_lthash_value_t;
      19             : 
      20             : FD_PROTOTYPES_BEGIN
      21             : 
      22             : static inline fd_lthash_value_t *
      23             : fd_lthash_fini( fd_blake3_t * sha,
      24           0 :                 fd_lthash_value_t * hash ) {
      25           0 :   return fd_blake3_fini_2048( sha, hash->bytes );
      26           0 : }
      27             : 
      28             : static inline fd_lthash_value_t *
      29         282 : fd_lthash_zero( fd_lthash_value_t * r ) {
      30         282 :   return fd_memset( r->bytes, 0, FD_LTHASH_LEN_BYTES );
      31         282 : }
      32             : 
      33             : static inline int
      34           0 : fd_lthash_is_zero( fd_lthash_value_t const * r ) {
      35           0 :   for ( ulong i=0; i<FD_LTHASH_LEN_ELEMS; i++ ) {
      36           0 :     if( r->words[i] != 0 ) {
      37           0 :       return 0; /* not zero */
      38           0 :     }
      39           0 :   }
      40           0 : 
      41           0 :   return 1;
      42           0 : }
      43             : 
      44             : static inline int
      45             : fd_lthash_eq( fd_lthash_value_t const * a,
      46           0 :               fd_lthash_value_t const * b ) {
      47           0 :   return fd_memeq( a->bytes, b->bytes, FD_LTHASH_LEN_BYTES );
      48           0 : }
      49             : 
      50             : static inline fd_lthash_value_t *
      51             : fd_lthash_add( fd_lthash_value_t * restrict       r,
      52        4592 :                fd_lthash_value_t const * restrict a ) {
      53     4701680 :   for ( ulong i=0; i<FD_LTHASH_LEN_ELEMS; i++ ) {
      54     4697088 :     r->words[i] = (ushort)( r->words[i] + a->words[i] );
      55     4697088 :   }
      56        4592 :   return r;
      57        4592 : }
      58             : 
      59             : static inline fd_lthash_value_t *
      60             : fd_lthash_sub( fd_lthash_value_t * restrict       r,
      61        4320 :                fd_lthash_value_t const * restrict a ) {
      62     4423904 :   for ( ulong i=0; i<FD_LTHASH_LEN_ELEMS; i++ ) {
      63     4419584 :     r->words[i] = (ushort)( r->words[i] - a->words[i] );
      64     4419584 :   }
      65        4320 :   return r;
      66        4320 : }
      67             : 
      68             : FD_PROTOTYPES_END
      69             : 
      70             : #endif /* HEADER_fd_src_ballet_lthash_fd_lthash_h */

Generated by: LCOV version 1.14