LCOV - code coverage report
Current view: top level - ballet/merlin - fd_merlin.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 87 92 94.6 %
Date: 2026-02-13 06:06:24 Functions: 11 12 91.7 %

          Line data    Source code
       1             : #include "fd_merlin.h"
       2             : #include "../keccak256/fd_keccak256_private.h"
       3             : 
       4             : /* Derived from https://github.com/hdevalence/libmerlin */
       5             : 
       6             : /* Strobe-128 Internals */
       7             : 
       8        1818 : #define STROBE_R 166
       9           9 : #define FLAG_I (1)
      10          87 : #define FLAG_A (1 << 1)
      11          96 : #define FLAG_C (1 << 2)
      12             : #define FLAG_T (1 << 3)
      13          48 : #define FLAG_M (1 << 4)
      14          87 : #define FLAG_K (1 << 5)
      15             : 
      16             : static inline void
      17          15 : strobe128_run_f( fd_merlin_strobe128_t * ctx ) {
      18          15 :   ctx->state_bytes[ctx->pos] ^= ctx->pos_begin;
      19          15 :   ctx->state_bytes[ctx->pos + 1] ^= 0x04;
      20          15 :   ctx->state_bytes[STROBE_R + 1] ^= 0x80;
      21          15 :   fd_keccak256_core( ctx->state );
      22          15 :   ctx->pos = 0;
      23          15 :   ctx->pos_begin = 0;
      24          15 : }
      25             : 
      26             : static void
      27             : strobe128_absorb( fd_merlin_strobe128_t * ctx,
      28             :                   uchar const *           data,
      29         204 :                   ulong const             data_len) {
      30        1527 :   for ( ulong i=0; i<data_len; i++ ) {
      31        1323 :     ctx->state_bytes[ctx->pos] ^= data[i];
      32        1323 :     ctx->pos++;
      33        1323 :     if (ctx->pos == STROBE_R) {
      34           6 :       strobe128_run_f(ctx);
      35           6 :     }
      36        1323 :   }
      37         204 : }
      38             : 
      39             : static void
      40             : strobe128_squeeze( fd_merlin_strobe128_t * ctx,
      41             :                    uchar *                 data,
      42           9 :                    ulong const             data_len) {
      43         489 :   for ( ulong i=0; i<data_len; i++ ) {
      44         480 :     data[i] = ctx->state_bytes[ctx->pos];
      45         480 :     ctx->state_bytes[ctx->pos] = 0;
      46         480 :     ctx->pos++;
      47         480 :     if (ctx->pos == STROBE_R) {
      48           0 :       strobe128_run_f(ctx);
      49           0 :     }
      50         480 :   }
      51           9 : }
      52             : 
      53             : static void
      54             : strobe128_begin_op( fd_merlin_strobe128_t * ctx,
      55          87 :                     uchar                   flags ) {
      56             :   /* Note: this implementation cuts some corners, see code below.
      57             :      Our implementation of Merlin doesn't use these features. */
      58             : 
      59             :   /*
      60             :   if (more) {
      61             :     // Changing flags while continuing is illegal
      62             :     assert(ctx->cur_flags == flags);
      63             :     return;
      64             :   }
      65             : 
      66             :   // T flag is not supported
      67             :   assert(!(flags & FLAG_T));
      68             :   */
      69             : 
      70          87 :   uchar old_begin = ctx->pos_begin;
      71          87 :   ctx->pos_begin = (uchar)(ctx->pos + 1);
      72          87 :   ctx->cur_flags = flags;
      73             : 
      74          87 :   uchar data[2] = { old_begin, flags };
      75          87 :   strobe128_absorb( ctx, data, 2 );
      76             : 
      77             :   /* Force running the permutation if C or K is set. */
      78          87 :   uchar force_f = 0 != (flags & (FLAG_C | FLAG_K));
      79             : 
      80          87 :   if (force_f && ctx->pos != 0) {
      81           9 :     strobe128_run_f(ctx);
      82           9 :   }
      83          87 : }
      84             : 
      85             : /* Strobe-128 */
      86             : 
      87             : static inline void
      88             : strobe128_meta_ad( fd_merlin_strobe128_t * ctx,
      89             :                    uchar const *           data,
      90             :                    ulong                   data_len,
      91          87 :                    uchar                   more ) {
      92          87 :   if ( more==0 ) {
      93          48 :     strobe128_begin_op( ctx, FLAG_M | FLAG_A );
      94          48 :   }
      95          87 :   strobe128_absorb(   ctx, data, data_len );
      96          87 : }
      97             : 
      98             : static inline void
      99             : strobe128_ad(fd_merlin_strobe128_t * ctx,
     100             :              uchar const *           data,
     101             :              ulong const             data_len,
     102          30 :              uchar                   more) {
     103          30 :   if ( more==0 ) {
     104          30 :     strobe128_begin_op( ctx, FLAG_A );
     105          30 :   }
     106          30 :   strobe128_absorb(   ctx, data, data_len );
     107          30 : }
     108             : 
     109             : static inline void
     110             : strobe128_prf( fd_merlin_strobe128_t * ctx,
     111             :                uchar *                 data,
     112             :                ulong const             data_len,
     113           9 :                uchar                   more ) {
     114           9 :   if ( more==0 ) {
     115           9 :     strobe128_begin_op(ctx, FLAG_I | FLAG_A | FLAG_C );
     116           9 :   }
     117           9 :   strobe128_squeeze(ctx, data, data_len);
     118           9 : }
     119             : 
     120             : static inline void
     121             : strobe128_init( fd_merlin_strobe128_t * ctx,
     122             :                 uchar const *           label,
     123           9 :                 ulong const             label_len ) {
     124           9 :   uchar init[18] = {
     125           9 :     1,  168, 1,  0,   1,  96, 83, 84, 82,
     126           9 :     79, 66,  69, 118, 49, 46, 48, 46, 50,
     127           9 :   };
     128           9 :   fd_memset( ctx->state_bytes, 0, 200 );
     129           9 :   fd_memcpy( ctx->state_bytes, init, 18 );
     130           9 :   fd_keccak256_core( ctx->state );
     131           9 :   ctx->pos = 0;
     132           9 :   ctx->pos_begin = 0;
     133           9 :   ctx->cur_flags = 0;
     134             : 
     135           9 :   strobe128_meta_ad( ctx, label, label_len, 0 );
     136           9 : }
     137             : 
     138             : /* Merlin */
     139             : 
     140             : void
     141             : fd_merlin_transcript_init( fd_merlin_transcript_t * mctx,
     142             :                            char const * const       label,
     143           9 :                            uint const               label_len ) {
     144           9 :   strobe128_init(&mctx->sctx, (uchar *)FD_MERLIN_LITERAL("Merlin v1.0"));
     145           9 :   fd_merlin_transcript_append_message(mctx, FD_MERLIN_LITERAL("dom-sep"), (uchar *)label, label_len);
     146           9 : }
     147             : 
     148             : void
     149             : fd_merlin_transcript_append_message( fd_merlin_transcript_t * mctx,
     150             :                                      char const * const       label,
     151             :                                      uint const               label_len,
     152             :                                      uchar const *            message,
     153          30 :                                      uint const               message_len ) {
     154          30 :   strobe128_meta_ad(&mctx->sctx, (uchar *)label, label_len, 0);
     155          30 :   strobe128_meta_ad(&mctx->sctx, (uchar *)&message_len, 4, 1);
     156          30 :   strobe128_ad(&mctx->sctx, message, message_len, 0);
     157          30 : }
     158             : 
     159             : inline void
     160             : fd_merlin_transcript_append_u64( fd_merlin_transcript_t * mctx,
     161             :                                  char const * const       label,
     162             :                                  uint const               label_len,
     163           0 :                                  ulong const              message_u64 ) {
     164           0 :   fd_merlin_transcript_append_message( mctx, label, label_len, (uchar *)&message_u64, 8 );
     165           0 : }
     166             : 
     167             : void
     168             : fd_merlin_transcript_challenge_bytes( fd_merlin_transcript_t * mctx,
     169             :                                       char const * const       label,
     170             :                                       uint const               label_len,
     171             :                                       uchar *                  buffer,
     172           9 :                                       uint const               buffer_len ) {
     173           9 :   strobe128_meta_ad(&mctx->sctx, (uchar *)label, label_len, 0);
     174           9 :   strobe128_meta_ad(&mctx->sctx, (uchar *)&buffer_len, 4, 1);
     175           9 :   strobe128_prf(&mctx->sctx, buffer, buffer_len, 0);
     176           9 : }

Generated by: LCOV version 1.14