LCOV - code coverage report
Current view: top level - flamenco/runtime/program/zksdk/merlin - fd_merlin.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 92 0.0 %
Date: 2025-03-20 12:08:36 Functions: 0 12 0.0 %

          Line data    Source code
       1             : #include "fd_merlin.h"
       2             : #include "../../../../../ballet/keccak256/fd_keccak256_private.h"
       3             : 
       4             : /* Derived from https://github.com/hdevalence/libmerlin */
       5             : 
       6             : /* Strobe-128 Internals */
       7             : 
       8           0 : #define STROBE_R 166
       9           0 : #define FLAG_I (1)
      10           0 : #define FLAG_A (1 << 1)
      11           0 : #define FLAG_C (1 << 2)
      12             : #define FLAG_T (1 << 3)
      13           0 : #define FLAG_M (1 << 4)
      14           0 : #define FLAG_K (1 << 5)
      15             : 
      16             : static inline void
      17           0 : strobe128_run_f( fd_merlin_strobe128_t * ctx ) {
      18           0 :   ctx->state_bytes[ctx->pos] ^= ctx->pos_begin;
      19           0 :   ctx->state_bytes[ctx->pos + 1] ^= 0x04;
      20           0 :   ctx->state_bytes[STROBE_R + 1] ^= 0x80;
      21           0 :   fd_keccak256_core( ctx->state );
      22           0 :   ctx->pos = 0;
      23           0 :   ctx->pos_begin = 0;
      24           0 : }
      25             : 
      26             : static void
      27             : strobe128_absorb( fd_merlin_strobe128_t * ctx,
      28             :                   uchar const *           data,
      29           0 :                   ulong const             data_len) {
      30           0 :   for ( ulong i=0; i<data_len; i++ ) {
      31           0 :     ctx->state_bytes[ctx->pos] ^= data[i];
      32           0 :     ctx->pos++;
      33           0 :     if (ctx->pos == STROBE_R) {
      34           0 :       strobe128_run_f(ctx);
      35           0 :     }
      36           0 :   }
      37           0 : }
      38             : 
      39             : static void
      40             : strobe128_squeeze( fd_merlin_strobe128_t * ctx,
      41             :                    uchar *                 data,
      42           0 :                    ulong const             data_len) {
      43           0 :   for ( ulong i=0; i<data_len; i++ ) {
      44           0 :     data[i] = ctx->state_bytes[ctx->pos];
      45           0 :     ctx->state_bytes[ctx->pos] = 0;
      46           0 :     ctx->pos++;
      47           0 :     if (ctx->pos == STROBE_R) {
      48           0 :       strobe128_run_f(ctx);
      49           0 :     }
      50           0 :   }
      51           0 : }
      52             : 
      53             : static void
      54             : strobe128_begin_op( fd_merlin_strobe128_t * ctx,
      55           0 :                     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           0 :   uchar old_begin = ctx->pos_begin;
      71           0 :   ctx->pos_begin = (uchar)(ctx->pos + 1);
      72           0 :   ctx->cur_flags = flags;
      73             : 
      74           0 :   uchar data[2] = { old_begin, flags };
      75           0 :   strobe128_absorb( ctx, data, 2 );
      76             : 
      77             :   /* Force running the permutation if C or K is set. */
      78           0 :   uchar force_f = 0 != (flags & (FLAG_C | FLAG_K));
      79             : 
      80           0 :   if (force_f && ctx->pos != 0) {
      81           0 :     strobe128_run_f(ctx);
      82           0 :   }
      83           0 : }
      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           0 :                    uchar                   more ) {
      92           0 :   if ( more==0 ) {
      93           0 :     strobe128_begin_op( ctx, FLAG_M | FLAG_A );
      94           0 :   }
      95           0 :   strobe128_absorb(   ctx, data, data_len );
      96           0 : }
      97             : 
      98             : static inline void
      99             : strobe128_ad(fd_merlin_strobe128_t * ctx,
     100             :              uchar const *           data,
     101             :              ulong const             data_len,
     102           0 :              uchar                   more) {
     103           0 :   if ( more==0 ) {
     104           0 :     strobe128_begin_op( ctx, FLAG_A );
     105           0 :   }
     106           0 :   strobe128_absorb(   ctx, data, data_len );
     107           0 : }
     108             : 
     109             : static inline void
     110             : strobe128_prf( fd_merlin_strobe128_t * ctx,
     111             :                uchar *                 data,
     112             :                ulong const             data_len,
     113           0 :                uchar                   more ) {
     114           0 :   if ( more==0 ) {
     115           0 :     strobe128_begin_op(ctx, FLAG_I | FLAG_A | FLAG_C );
     116           0 :   }
     117           0 :   strobe128_squeeze(ctx, data, data_len);
     118           0 : }
     119             : 
     120             : static inline void
     121             : strobe128_init( fd_merlin_strobe128_t * ctx,
     122             :                 uchar const *           label,
     123           0 :                 ulong const             label_len ) {
     124           0 :   uchar init[18] = {
     125           0 :     1,  168, 1,  0,   1,  96, 83, 84, 82,
     126           0 :     79, 66,  69, 118, 49, 46, 48, 46, 50,
     127           0 :   };
     128           0 :   fd_memset( ctx->state_bytes, 0, 200 );
     129           0 :   fd_memcpy( ctx->state_bytes, init, 18 );
     130           0 :   fd_keccak256_core( ctx->state );
     131           0 :   ctx->pos = 0;
     132           0 :   ctx->pos_begin = 0;
     133           0 :   ctx->cur_flags = 0;
     134             : 
     135           0 :   strobe128_meta_ad( ctx, label, label_len, 0 );
     136           0 : }
     137             : 
     138             : /* Merlin */
     139             : 
     140             : void
     141             : fd_merlin_transcript_init( fd_merlin_transcript_t * mctx,
     142             :                            char const * const       label,
     143           0 :                            uint const               label_len ) {
     144           0 :   strobe128_init(&mctx->sctx, (uchar *)FD_MERLIN_LITERAL("Merlin v1.0"));
     145           0 :   fd_merlin_transcript_append_message(mctx, FD_MERLIN_LITERAL("dom-sep"), (uchar *)label, label_len);
     146           0 : }
     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           0 :                                      uint const               message_len ) {
     154           0 :   strobe128_meta_ad(&mctx->sctx, (uchar *)label, label_len, 0);
     155           0 :   strobe128_meta_ad(&mctx->sctx, (uchar *)&message_len, 4, 1);
     156           0 :   strobe128_ad(&mctx->sctx, message, message_len, 0);
     157           0 : }
     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           0 :                                       uint const               buffer_len ) {
     173           0 :   strobe128_meta_ad(&mctx->sctx, (uchar *)label, label_len, 0);
     174           0 :   strobe128_meta_ad(&mctx->sctx, (uchar *)&buffer_len, 4, 1);
     175           0 :   strobe128_prf(&mctx->sctx, buffer, buffer_len, 0);
     176           0 : }

Generated by: LCOV version 1.14