3v4l.org

run code in 300+ 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 = 43
Branch analysis from position: 32
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 73
Branch analysis from position: 62
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 73
2 jumps found. (Code = 43) Position 1 = 87, Position 2 = 95
Branch analysis from position: 87
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 95
2 jumps found. (Code = 43) Position 1 = 102, Position 2 = 105
Branch analysis from position: 102
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 105
2 jumps found. (Code = 43) Position 1 = 115, Position 2 = 128
Branch analysis from position: 115
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 128
2 jumps found. (Code = 43) Position 1 = 150, Position 2 = 153
Branch analysis from position: 150
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 153
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/e9q13
function name:  (null)
number of ops:  166
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, ->43
   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      > EXIT                                                     255
   46    43    >   INIT_FCALL                                               'mb_substr'
         44        SEND_VAR                                                 !4
         45        SEND_VAL                                                 64
         46        SEND_VAL                                                 32
         47        SEND_VAL                                                 '8bit'
         48        DO_ICALL                                         $30     
         49        ASSIGN                                                   !6, $30
   47    50        INIT_FCALL_BY_NAME                                       'sodium_crypto_sign_open'
         51        SEND_VAR_EX                                              !4
         52        SEND_VAR_EX                                              !2
         53        DO_FCALL                                      0  $32     
         54        ASSIGN                                                   !7, $32
   48    55        INIT_FCALL                                               'hash_equals'
         56        CONCAT                                           ~34     !6, !3
         57        SEND_VAL                                                 ~34
         58        SEND_VAR                                                 !7
         59        DO_ICALL                                         $35     
         60        BOOL_NOT                                         ~36     $35
         61      > JMPZ                                                     ~36, ->73
   49    62    >   INIT_FCALL                                               'bin2hex'
         63        SEND_VAR                                                 !4
         64        DO_ICALL                                         $37     
         65        ECHO                                                     $37
         66        ECHO                                                     '%0A'
   50    67        INIT_FCALL                                               'bin2hex'
         68        SEND_VAR                                                 !7
         69        DO_ICALL                                         $38     
         70        ECHO                                                     $38
         71        ECHO                                                     '%0A'
   51    72      > EXIT                                                     255
   55    73    >   ASSIGN                                                   !8, 'Cryptography+nerds+in+the+kitchen+have+too+much+thyme+on+their+hands.'
   56    74        INIT_FCALL                                               'sodium_crypto_rsign_detached'
         75        SEND_VAR                                                 !8
         76        SEND_VAR                                                 !1
         77        DO_FCALL                                      0  $40     
         78        ASSIGN                                                   !9, $40
   57    79        INIT_FCALL                                               'mb_strlen'
         80        SEND_VAR                                                 !9
         81        SEND_VAL                                                 '8bit'
         82        DO_ICALL                                         $42     
         83        ASSIGN                                                   !10, $42
   59    84        FETCH_CONSTANT                                   ~44     'SODIUM_CRYPTO_RSIGN_SIGBYTES'
         85        IS_NOT_IDENTICAL                                         !10, ~44
         86      > JMPZ                                                     ~45, ->95
   60    87    >   ECHO                                                     'Expected+'
         88        FETCH_CONSTANT                                   ~46     'SODIUM_CRYPTO_RSIGN_SIGBYTES'
         89        ECHO                                                     ~46
         90        ECHO                                                     '%3B+got+'
         91        ECHO                                                     !10
         92        ECHO                                                     '.'
         93        ECHO                                                     '%0A'
   61    94      > EXIT                                                     255
   63    95    >   INIT_FCALL                                               'sodium_crypto_rsign_verify_detached'
         96        SEND_VAR                                                 !9
         97        SEND_VAR                                                 !8
         98        SEND_VAR                                                 !2
         99        DO_FCALL                                      0  $47     
        100        BOOL_NOT                                         ~48     $47
        101      > JMPZ                                                     ~48, ->105
   64   102    >   ECHO                                                     'Invalid+signature.'
        103        ECHO                                                     '%0A'
   65   104      > EXIT                                                     255
   68   105    >   INIT_FCALL                                               'sodium_crypto_rsign_detached'
        106        SEND_VAR                                                 !8
        107        SEND_VAR                                                 !1
        108        DO_FCALL                                      0  $49     
        109        ASSIGN                                                   !11, $49
   69   110        INIT_FCALL                                               'hash_equals'
        111        SEND_VAR                                                 !9
        112        SEND_VAR                                                 !11
        113        DO_ICALL                                         $51     
        114      > JMPZ                                                     $51, ->128
   70   115    >   ECHO                                                     'RNG+Failure.'
        116        ECHO                                                     '%0A'
   71   117        INIT_FCALL                                               'bin2hex'
        118        SEND_VAR                                                 !9
        119        DO_ICALL                                         $52     
        120        ECHO                                                     $52
        121        ECHO                                                     '%0A'
   72   122        INIT_FCALL                                               'bin2hex'
        123        SEND_VAR                                                 !11
        124        DO_ICALL                                         $53     
        125        ECHO                                                     $53
        126        ECHO                                                     '%0A'
   73   127      > EXIT                                                     255
   76   128    >   INIT_FCALL                                               'mb_substr'
        129        SEND_VAR                                                 !9
        130        SEND_VAL                                                 0
        131        SEND_VAL                                                 64
        132        SEND_VAL                                                 '8bit'
        133        DO_ICALL                                         $54     
        134        ASSIGN                                                   !12, $54
   77   135        INIT_FCALL                                               'mb_substr'
        136        SEND_VAR                                                 !9
        137        SEND_VAL                                                 64
        138        SEND_VAL                                                 32
        139        SEND_VAL                                                 '8bit'
        140        DO_ICALL                                         $56     
        141        ASSIGN                                                   !13, $56
   79   142        INIT_FCALL                                               'sodium_crypto_rsign_verify_detached'
        143        SEND_VAR                                                 !12
        144        CONCAT                                           ~58     !13, !8
        145        SEND_VAL                                                 ~58
        146        SEND_VAR                                                 !2
        147        DO_FCALL                                      0  $59     
        148        BOOL_NOT                                         ~60     $59
        149      > JMPZ                                                     ~60, ->153
   80   150    >   ECHO                                                     'Invalid+signature.'
        151        ECHO                                                     '%0A'
   81   152      > EXIT                                                     255
   84   153    >   ECHO                                                     'All+tests+pass%21'
        154        ECHO                                                     '%0A'
   87   155        INIT_FCALL                                               'var_dump'
        156        INIT_FCALL                                               'bin2hex'
        157        SEND_VAR                                                 !9
        158        DO_ICALL                                         $61     
        159        SEND_VAR                                                 $61
        160        INIT_FCALL                                               'bin2hex'
        161        SEND_VAR                                                 !11
        162        DO_ICALL                                         $62     
        163        SEND_VAR                                                 $62
        164        DO_ICALL                                                 
        165      > 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.0.0


preferences:
176.32 ms | 1419 KiB | 33 Q