3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); define('SODIUM_CRYPTO_RSIGN_SIGBYTES', 96); function sodium_crypto_rsign(string $message, string $sk): string { return sodium_crypto_sign(random_bytes(32) . $message, $sk); } function sodium_crypto_rsign_open(string $sm, string $pk): string { $opened = sodium_crypto_sign_open($sm, $pk); return mb_substr($opened, 32, null, '8bit'); } function sodium_crypto_rsign_detached(string $message, string $sk): string { $random = random_bytes(32); return sodium_crypto_sign_detached($random . $message, $sk) . $random; } function sodium_crypto_rsign_verify_detached(string $signature, string $message, string $pk): bool { $sig = mb_substr($signature, 0, 64, '8bit'); $random = mb_substr($signature, 64, 32, '8bit'); return sodium_crypto_sign_verify_detached($sig, $random . $message, $pk); } /*** TESTS ***/ $kp = sodium_crypto_sign_keypair(); $sk = sodium_crypto_sign_secretkey($kp); $pk = sodium_crypto_sign_publickey($kp); # First test: Does it work? $message = 'This is a test message.'; $signed = sodium_crypto_rsign($message, $sk); $ropen = sodium_crypto_rsign_open($signed, $pk); if (!hash_equals($message, $ropen)) { echo bin2hex($signed), PHP_EOL; echo bin2hex($ropen), PHP_EOL; exit(255); } # Second test: Is it backwards compatible? $random = mb_substr($signed, 64, 32, '8bit'); $open = sodium_crypto_sign_open($signed, $pk); if (!hash_equals($random . $message, $open)) { echo bin2hex($signed), PHP_EOL; echo bin2hex($open), PHP_EOL; exit(255); } # Third test: Detached API $msg2 = 'Cryptography nerds in the kitchen have too much thyme on their hands.'; // Do you feel timing attacked? $sig = sodium_crypto_rsign_detached($msg2, $sk); $len = mb_strlen($sig, '8bit'); if ($len !== SODIUM_CRYPTO_RSIGN_SIGBYTES) { echo 'Expected ', SODIUM_CRYPTO_RSIGN_SIGBYTES, '; got ', $len, '.', PHP_EOL; exit(255); } if (!sodium_crypto_rsign_verify_detached($sig, $msg2, $pk)) { echo 'Invalid signature.', PHP_EOL; exit(255); } $sig2 = sodium_crypto_rsign_detached($msg2, $sk); if (hash_equals($sig, $sig2)) { echo 'RNG Failure.', PHP_EOL; echo bin2hex($sig), PHP_EOL; echo bin2hex($sig2), PHP_EOL; exit(255); } $sigLeft = mb_substr($sig, 0, 64, '8bit'); $sigRight = mb_substr($sig, 64, 32, '8bit'); if (!sodium_crypto_rsign_verify_detached($sigLeft, $sigRight . $msg2, $pk)) { echo 'Invalid signature.', PHP_EOL; exit(255); } echo 'All tests pass!', PHP_EOL; // Two signatures of the same message: var_dump(bin2hex($sig), bin2hex($sig2));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 45
Branch analysis from position: 32
1 jumps found. (Code = 61) Position 1 = -2
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 64, Position 2 = 77
Branch analysis from position: 64
1 jumps found. (Code = 61) Position 1 = -2
Branch analysis from position: 77
2 jumps found. (Code = 43) Position 1 = 91, Position 2 = 101
Branch analysis from position: 91
1 jumps found. (Code = 61) Position 1 = -2
Branch analysis from position: 101
2 jumps found. (Code = 43) Position 1 = 108, Position 2 = 113
Branch analysis from position: 108
1 jumps found. (Code = 61) Position 1 = -2
Branch analysis from position: 113
2 jumps found. (Code = 43) Position 1 = 123, Position 2 = 138
Branch analysis from position: 123
1 jumps found. (Code = 61) Position 1 = -2
Branch analysis from position: 138
2 jumps found. (Code = 43) Position 1 = 160, Position 2 = 165
Branch analysis from position: 160
1 jumps found. (Code = 61) Position 1 = -2
Branch analysis from position: 165
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/e9q13
function name:  (null)
number of ops:  178
compiled vars:  !0 = $kp, !1 = $sk, !2 = $pk, !3 = $message, !4 = $signed, !5 = $ropen, !6 = $random, !7 = $open, !8 = $msg2, !9 = $sig, !10 = $len, !11 = $sig2, !12 = $sigLeft, !13 = $sigRight
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   INIT_FCALL                                                   'define'
          1        SEND_VAL                                                     'SODIUM_CRYPTO_RSIGN_SIGBYTES'
          2        SEND_VAL                                                     96
          3        DO_ICALL                                                     
   31     4        INIT_FCALL_BY_NAME                                           'sodium_crypto_sign_keypair'
          5        DO_FCALL                                          0  $15     
          6        ASSIGN                                                       !0, $15
   32     7        INIT_FCALL_BY_NAME                                           'sodium_crypto_sign_secretkey'
          8        SEND_VAR_EX                                                  !0
          9        DO_FCALL                                          0  $17     
         10        ASSIGN                                                       !1, $17
   33    11        INIT_FCALL_BY_NAME                                           'sodium_crypto_sign_publickey'
         12        SEND_VAR_EX                                                  !0
         13        DO_FCALL                                          0  $19     
         14        ASSIGN                                                       !2, $19
   36    15        ASSIGN                                                       !3, 'This+is+a+test+message.'
   37    16        INIT_FCALL                                                   'sodium_crypto_rsign'
         17        SEND_VAR                                                     !3
         18        SEND_VAR                                                     !1
         19        DO_FCALL                                          0  $22     
         20        ASSIGN                                                       !4, $22
   38    21        INIT_FCALL                                                   'sodium_crypto_rsign_open'
         22        SEND_VAR                                                     !4
         23        SEND_VAR                                                     !2
         24        DO_FCALL                                          0  $24     
         25        ASSIGN                                                       !5, $24
   39    26        INIT_FCALL                                                   'hash_equals'
         27        SEND_VAR                                                     !3
         28        SEND_VAR                                                     !5
         29        DO_ICALL                                             $26     
         30        BOOL_NOT                                             ~27     $26
         31      > JMPZ                                                         ~27, ->45
   40    32    >   INIT_FCALL                                                   'bin2hex'
         33        SEND_VAR                                                     !4
         34        DO_ICALL                                             $28     
         35        ECHO                                                         $28
         36        ECHO                                                         '%0A'
   41    37        INIT_FCALL                                                   'bin2hex'
         38        SEND_VAR                                                     !5
         39        DO_ICALL                                             $29     
         40        ECHO                                                         $29
         41        ECHO                                                         '%0A'
   42    42      > INIT_FCALL                                                   'exit'
         43*       SEND_VAL                                                     255
         44*       DO_ICALL                                                     
   46    45    >   INIT_FCALL                                                   'mb_substr'
         46        SEND_VAR                                                     !4
         47        SEND_VAL                                                     64
         48        SEND_VAL                                                     32
         49        SEND_VAL                                                     '8bit'
         50        DO_ICALL                                             $31     
         51        ASSIGN                                                       !6, $31
   47    52        INIT_FCALL_BY_NAME                                           'sodium_crypto_sign_open'
         53        SEND_VAR_EX                                                  !4
         54        SEND_VAR_EX                                                  !2
         55        DO_FCALL                                          0  $33     
         56        ASSIGN                                                       !7, $33
   48    57        INIT_FCALL                                                   'hash_equals'
         58        CONCAT                                               ~35     !6, !3
         59        SEND_VAL                                                     ~35
         60        SEND_VAR                                                     !7
         61        DO_ICALL                                             $36     
         62        BOOL_NOT                                             ~37     $36
         63      > JMPZ                                                         ~37, ->77
   49    64    >   INIT_FCALL                                                   'bin2hex'
         65        SEND_VAR                                                     !4
         66        DO_ICALL                                             $38     
         67        ECHO                                                         $38
         68        ECHO                                                         '%0A'
   50    69        INIT_FCALL                                                   'bin2hex'
         70        SEND_VAR                                                     !7
         71        DO_ICALL                                             $39     
         72        ECHO                                                         $39
         73        ECHO                                                         '%0A'
   51    74      > INIT_FCALL                                                   'exit'
         75*       SEND_VAL                                                     255
         76*       DO_ICALL                                                     
   55    77    >   ASSIGN                                                       !8, 'Cryptography+nerds+in+the+kitchen+have+too+much+thyme+on+their+hands.'
   56    78        INIT_FCALL                                                   'sodium_crypto_rsign_detached'
         79        SEND_VAR                                                     !8
         80        SEND_VAR                                                     !1
         81        DO_FCALL                                          0  $42     
         82        ASSIGN                                                       !9, $42
   57    83        INIT_FCALL                                                   'mb_strlen'
         84        SEND_VAR                                                     !9
         85        SEND_VAL                                                     '8bit'
         86        DO_ICALL                                             $44     
         87        ASSIGN                                                       !10, $44
   59    88        FETCH_CONSTANT                                       ~46     'SODIUM_CRYPTO_RSIGN_SIGBYTES'
         89        IS_NOT_IDENTICAL                                             !10, ~46
         90      > JMPZ                                                         ~47, ->101
   60    91    >   ECHO                                                         'Expected+'
         92        FETCH_CONSTANT                                       ~48     'SODIUM_CRYPTO_RSIGN_SIGBYTES'
         93        ECHO                                                         ~48
         94        ECHO                                                         '%3B+got+'
         95        ECHO                                                         !10
         96        ECHO                                                         '.'
         97        ECHO                                                         '%0A'
   61    98      > INIT_FCALL                                                   'exit'
         99*       SEND_VAL                                                     255
        100*       DO_ICALL                                                     
   63   101    >   INIT_FCALL                                                   'sodium_crypto_rsign_verify_detached'
        102        SEND_VAR                                                     !9
        103        SEND_VAR                                                     !8
        104        SEND_VAR                                                     !2
        105        DO_FCALL                                          0  $50     
        106        BOOL_NOT                                             ~51     $50
        107      > JMPZ                                                         ~51, ->113
   64   108    >   ECHO                                                         'Invalid+signature.'
        109        ECHO                                                         '%0A'
   65   110      > INIT_FCALL                                                   'exit'
        111*       SEND_VAL                                                     255
        112*       DO_ICALL                                                     
   68   113    >   INIT_FCALL                                                   'sodium_crypto_rsign_detached'
        114        SEND_VAR                                                     !8
        115        SEND_VAR                                                     !1
        116        DO_FCALL                                          0  $53     
        117        ASSIGN                                                       !11, $53
   69   118        INIT_FCALL                                                   'hash_equals'
        119        SEND_VAR                                                     !9
        120        SEND_VAR                                                     !11
        121        DO_ICALL                                             $55     
        122      > JMPZ                                                         $55, ->138
   70   123    >   ECHO                                                         'RNG+Failure.'
        124        ECHO                                                         '%0A'
   71   125        INIT_FCALL                                                   'bin2hex'
        126        SEND_VAR                                                     !9
        127        DO_ICALL                                             $56     
        128        ECHO                                                         $56
        129        ECHO                                                         '%0A'
   72   130        INIT_FCALL                                                   'bin2hex'
        131        SEND_VAR                                                     !11
        132        DO_ICALL                                             $57     
        133        ECHO                                                         $57
        134        ECHO                                                         '%0A'
   73   135      > INIT_FCALL                                                   'exit'
        136*       SEND_VAL                                                     255
        137*       DO_ICALL                                                     
   76   138    >   INIT_FCALL                                                   'mb_substr'
        139        SEND_VAR                                                     !9
        140        SEND_VAL                                                     0
        141        SEND_VAL                                                     64
        142        SEND_VAL                                                     '8bit'
        143        DO_ICALL                                             $59     
        144        ASSIGN                                                       !12, $59
   77   145        INIT_FCALL                                                   'mb_substr'
        146        SEND_VAR                                                     !9
        147        SEND_VAL                                                     64
        148        SEND_VAL                                                     32
        149        SEND_VAL                                                     '8bit'
        150        DO_ICALL                                             $61     
        151        ASSIGN                                                       !13, $61
   79   152        INIT_FCALL                                                   'sodium_crypto_rsign_verify_detached'
        153        SEND_VAR                                                     !12
        154        CONCAT                                               ~63     !13, !8
        155        SEND_VAL                                                     ~63
        156        SEND_VAR                                                     !2
        157        DO_FCALL                                          0  $64     
        158        BOOL_NOT                                             ~65     $64
        159      > JMPZ                                                         ~65, ->165
   80   160    >   ECHO                                                         'Invalid+signature.'
        161        ECHO                                                         '%0A'
   81   162      > INIT_FCALL                                                   'exit'
        163*       SEND_VAL                                                     255
        164*       DO_ICALL                                                     
   84   165    >   ECHO                                                         'All+tests+pass%21'
        166        ECHO                                                         '%0A'
   87   167        INIT_FCALL                                                   'var_dump'
        168        INIT_FCALL                                                   'bin2hex'
        169        SEND_VAR                                                     !9
        170        DO_ICALL                                             $67     
        171        SEND_VAR                                                     $67
        172        INIT_FCALL                                                   'bin2hex'
        173        SEND_VAR                                                     !11
        174        DO_ICALL                                             $68     
        175        SEND_VAR                                                     $68
        176        DO_ICALL                                                     
        177      > RETURN                                                       1

Function sodium_crypto_rsign:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/e9q13
function name:  sodium_crypto_rsign
number of ops:  14
compiled vars:  !0 = $message, !1 = $sk
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    6     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
    8     2        INIT_FCALL_BY_NAME                                           'sodium_crypto_sign'
          3        INIT_FCALL                                                   'random_bytes'
          4        SEND_VAL                                                     32
          5        DO_ICALL                                             $2      
          6        CONCAT                                               ~3      $2, !0
          7        SEND_VAL_EX                                                  ~3
          8        SEND_VAR_EX                                                  !1
          9        DO_FCALL                                          0  $4      
         10        VERIFY_RETURN_TYPE                                           $4
         11      > RETURN                                                       $4
    9    12*       VERIFY_RETURN_TYPE                                           
         13*     > RETURN                                                       null

End of function sodium_crypto_rsign

Function sodium_crypto_rsign_open:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/e9q13
function name:  sodium_crypto_rsign_open
number of ops:  17
compiled vars:  !0 = $sm, !1 = $pk, !2 = $opened
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   11     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   13     2        INIT_FCALL_BY_NAME                                           'sodium_crypto_sign_open'
          3        SEND_VAR_EX                                                  !0
          4        SEND_VAR_EX                                                  !1
          5        DO_FCALL                                          0  $3      
          6        ASSIGN                                                       !2, $3
   14     7        INIT_FCALL                                                   'mb_substr'
          8        SEND_VAR                                                     !2
          9        SEND_VAL                                                     32
         10        SEND_VAL                                                     null
         11        SEND_VAL                                                     '8bit'
         12        DO_ICALL                                             $5      
         13        VERIFY_RETURN_TYPE                                           $5
         14      > RETURN                                                       $5
   15    15*       VERIFY_RETURN_TYPE                                           
         16*     > RETURN                                                       null

End of function sodium_crypto_rsign_open

Function sodium_crypto_rsign_detached:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/e9q13
function name:  sodium_crypto_rsign_detached
number of ops:  16
compiled vars:  !0 = $message, !1 = $sk, !2 = $random
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   17     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   19     2        INIT_FCALL                                                   'random_bytes'
          3        SEND_VAL                                                     32
          4        DO_ICALL                                             $3      
          5        ASSIGN                                                       !2, $3
   20     6        INIT_FCALL_BY_NAME                                           'sodium_crypto_sign_detached'
          7        CONCAT                                               ~5      !2, !0
          8        SEND_VAL_EX                                                  ~5
          9        SEND_VAR_EX                                                  !1
         10        DO_FCALL                                          0  $6      
         11        CONCAT                                               ~7      $6, !2
         12        VERIFY_RETURN_TYPE                                           ~7
         13      > RETURN                                                       ~7
   21    14*       VERIFY_RETURN_TYPE                                           
         15*     > RETURN                                                       null

End of function sodium_crypto_rsign_detached

Function sodium_crypto_rsign_verify_detached:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/e9q13
function name:  sodium_crypto_rsign_verify_detached
number of ops:  27
compiled vars:  !0 = $signature, !1 = $message, !2 = $pk, !3 = $sig, !4 = $random
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
   25     3        INIT_FCALL                                                   'mb_substr'
          4        SEND_VAR                                                     !0
          5        SEND_VAL                                                     0
          6        SEND_VAL                                                     64
          7        SEND_VAL                                                     '8bit'
          8        DO_ICALL                                             $5      
          9        ASSIGN                                                       !3, $5
   26    10        INIT_FCALL                                                   'mb_substr'
         11        SEND_VAR                                                     !0
         12        SEND_VAL                                                     64
         13        SEND_VAL                                                     32
         14        SEND_VAL                                                     '8bit'
         15        DO_ICALL                                             $7      
         16        ASSIGN                                                       !4, $7
   27    17        INIT_FCALL_BY_NAME                                           'sodium_crypto_sign_verify_detached'
         18        SEND_VAR_EX                                                  !3
         19        CONCAT                                               ~9      !4, !1
         20        SEND_VAL_EX                                                  ~9
         21        SEND_VAR_EX                                                  !2
         22        DO_FCALL                                          0  $10     
         23        VERIFY_RETURN_TYPE                                           $10
         24      > RETURN                                                       $10
   28    25*       VERIFY_RETURN_TYPE                                           
         26*     > RETURN                                                       null

End of function sodium_crypto_rsign_verify_detached

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
183.49 ms | 2477 KiB | 27 Q