Line data Source code
1 : #ifndef HEADER_fd_src_ballet_keccak256_fd_keccak256_h 2 : #define HEADER_fd_src_ballet_keccak256_fd_keccak256_h 3 : 4 : /* fd_keccak256 provides APIs for Keccak256 hashing of messages. */ 5 : 6 : #include "../fd_ballet_base.h" 7 : 8 : /* FD_KECCAK256_{ALIGN,FOOTPRINT} describe the alignment and footprint needed 9 : for a memory region to hold a fd_keccak256_t. ALIGN is a positive 10 : integer power of 2. FOOTPRINT is a multiple of align. ALIGN is 11 : recommended to be at least double cache line to mitigate various 12 : kinds of false sharing. These are provided to facilitate compile 13 : time declarations. */ 14 : 15 : #define FD_KECCAK256_ALIGN (128UL) 16 : #define FD_KECCAK256_FOOTPRINT (256UL) 17 : 18 : /* FD_KECCAK256_HASH_SZ describe the size of a KECCAK256 hash in bytes. */ 19 : 20 : #define FD_KECCAK256_HASH_SZ (32UL) /* == 2^FD_KECCAK256_LG_HASH_SZ, explicit to workaround compiler limitations */ 21 : 22 : /* A fd_keccak256_t should be treated as an opaque handle of a keccak256 23 : calculation state. (It technically isn't here facilitate compile 24 : time declarations of fd_keccak256_t memory.) */ 25 : 26 899158 : #define FD_KECCAK256_STATE_SZ (25UL) 27 903752 : #define FD_KECCAK256_OUT_SZ (32UL) 28 899158 : #define FD_KECCAK256_RATE ((sizeof(ulong)*FD_KECCAK256_STATE_SZ) - (2*FD_KECCAK256_OUT_SZ)) 29 : 30 : struct __attribute__((aligned(FD_KECCAK256_ALIGN))) fd_keccak256_private { 31 : 32 : /* This point is 128-byte aligned */ 33 : 34 : /* This point is 64-byte aligned */ 35 : 36 : ulong state[ 25 ]; 37 : 38 : ulong padding_start; /* Number of buffered bytes, in [0,FD_KECCAK256_RATE) */ 39 : 40 : /* Padding to 128-byte here */ 41 : }; 42 : 43 : typedef struct fd_keccak256_private fd_keccak256_t; 44 : 45 : FD_PROTOTYPES_BEGIN 46 : 47 : /* fd_keccak256_init starts a keccak256 calculation. sha is assumed to be a 48 : current local join to a keccak256 calculation state with no other 49 : concurrent operation that would modify the state while this is 50 : executing. Any preexisting state for an in-progress or recently 51 : completed calculation will be discarded. Returns sha (on return, sha 52 : will have the state of a new in-progress calculation). */ 53 : 54 : fd_keccak256_t * 55 : fd_keccak256_init( fd_keccak256_t * sha ); 56 : 57 : /* fd_keccak256_append adds sz bytes locally pointed to by data an 58 : in-progress keccak256 calculation. sha, data and sz are assumed to be 59 : valid (i.e. sha is a current local join to a keccak256 calculation state 60 : with no other concurrent operations that would modify the state while 61 : this is executing, data points to the first of the sz bytes and will 62 : be unmodified while this is running with no interest retained after 63 : return ... data==NULL is fine if sz==0). Returns sha (on return, sha 64 : will have the updated state of the in-progress calculation). 65 : 66 : It does not matter how the user groups data bytes for a keccak256 67 : calculation; the final hash will be identical. */ 68 : 69 : fd_keccak256_t * 70 : fd_keccak256_append( fd_keccak256_t * sha, 71 : void const * data, 72 : ulong sz ); 73 : 74 : /* fd_keccak256_fini finishes a a keccak256 calculation. sha and hash are 75 : assumed to be valid (i.e. sha is a local join to a keccak256 calculation 76 : state that has an in-progress calculation with no other concurrent 77 : operations that would modify the state while this is executing and 78 : hash points to the first byte of a 32-byte memory region where the 79 : result of the calculation should be stored). Returns hash (on 80 : return, there will be no calculation in-progress on sha and 32-byte 81 : buffer pointed to by hash will be populated with the calculation 82 : result). */ 83 : 84 : void * 85 : fd_keccak256_fini( fd_keccak256_t * sha, 86 : void * hash ); 87 : 88 : /* fd_keccak256_hash is a convenience implementation of: 89 : 90 : fd_keccak256_t keccak[1]; 91 : return fd_keccak256_fini( fd_keccak256_append( fd_keccak256_init( keccak ), data, sz ), hash ) 92 : 93 : It may eventually be streamlined. */ 94 : 95 : void * 96 : fd_keccak256_hash( void const * data, 97 : ulong sz, 98 : void * hash ); 99 : 100 : FD_PROTOTYPES_END 101 : 102 : #endif /* HEADER_fd_src_ballet_keccak256_fd_keccak256_h */