Ignore:
Timestamp:
Nov 24, 2020, 10:58:56 AM (4 years ago)
Author:
chronos
Message:
  • Modified: More static types added.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/BigInt.php

    r870 r888  
    99// First byte is MSB and last byte is LSB
    1010
    11 function BigIntNew($BitWidth)
     11function BigIntNew($BitWidth): string
    1212{
    1313  return str_repeat(chr(0), $BitWidth >> 3);
    1414}
    1515
    16 function BigIntNewAs($A)
     16function BigIntNewAs($A): string
    1717{
    1818  return str_repeat(chr(0), strlen($A));
    1919}
    2020
    21 function BigIntCheck($A, $B)
     21function BigIntCheck(string $A, string $B): void
    2222{
    2323    if (strlen($A) != strlen($B))
     
    2525}
    2626
    27 function BigIntAdd($A, $B)
     27function BigIntAdd(string $A, string $B): string
    2828{
    2929  BigIntCheck($A, $B);
     
    4040}
    4141
    42 function BigIntSub($A, $B)
     42function BigIntSub(string $A, string $B): string
    4343{
    4444  BigIntCheck($A, $B);
     
    5555}
    5656
    57 function BigIntInc($A)
     57function BigIntInc(string $A): string
    5858{
    5959  $Result = BigIntNewAs($A);
     
    6969}
    7070
    71 function BigIntDec($A)
     71function BigIntDec(string $A): string
    7272{
    7373  $Result = BigIntNewAs($A);
     
    8383}
    8484
    85 function BigIntNeg($A)
     85function BigIntNeg(string $A): string
    8686{
    8787  return BigIntInc(BigIntNot($A));
    8888}
    8989
    90 function BigIntIsNeg($A)
     90function BigIntIsNeg(string $A): string
    9191{
    9292  return ord($A[0]) & 0x80;
    9393}
    9494
    95 function BigIntEqual($A, $B)
     95function BigIntEqual(string $A, string $B): bool
    9696{
    9797  return $A == $B; // Simple string comparison
    9898}
    9999
    100 function BigIntNotEqual($A, $B)
     100function BigIntNotEqual(string $A, string $B): bool
    101101{
    102102  return $A != $B; // Simple string comparison
    103103}
    104104
    105 function BigIntGreater($A, $B)
     105function BigIntGreater(string $A, string $B): bool
    106106{
    107107  return BigIntIsNeg(BigIntSub($B, $A));
    108108}
    109109
    110 function BigIntGreaterOrEqual($A, $B)
     110function BigIntGreaterOrEqual(string $A, string $B): bool
    111111{
    112112  return BigIntIsNeg(BigIntSub($B, $A)) or BigIntEqual($A, $B);
    113113}
    114114
    115 function BigIntLesser($A, $B)
     115function BigIntLesser(string $A, string $B): bool
    116116{
    117117  return BigIntIsNeg(BigIntSub($A, $B));
    118118}
    119119
    120 function BigIntLesserOrEqual($A, $B)
     120function BigIntLesserOrEqual(string $A, string $B): bool
    121121{
    122122  return BigIntIsNeg(BigIntSub($A, $B)) or BigIntEqual($A, $B);
    123123}
    124124
    125 function BigIntMul($A, $B)
    126 {
    127   BigIntCheck($A, $B);
    128   $Result = BigIntNewAs($A);
    129   while (BigIntGreater($B, BigIntNew()))
     125function BigIntMul(string $A, string $B): string
     126{
     127  BigIntCheck($A, $B);
     128  $Result = BigIntNewAs($A);
     129  while (BigIntGreater($B, BigIntNewAs($A)))
    130130  {
    131131    $Result = BigIntAdd($Result, $A);
     
    135135}
    136136
    137 function BigIntDiv($A, $B)
     137function BigIntDiv(string $A, string $B): string
    138138{
    139139  BigIntCheck($A, $B);
     
    147147}
    148148
    149 function BigIntMod($A, $B)
     149function BigIntMod(string $A, string $B): string
    150150{
    151151  BigIntCheck($A, $B);
     
    159159}
    160160
    161 function BigIntAnd($A, $B)
     161function BigIntAnd(string $A, string $B): string
    162162{
    163163  BigIntCheck($A, $B);
     
    170170}
    171171
    172 function BigIntOr($A, $B)
     172function BigIntOr(string $A, string $B): string
    173173{
    174174  BigIntCheck($A, $B);
     
    181181}
    182182
    183 function BigIntXor($A, $B)
     183function BigIntXor(string $A, string $B): string
    184184{
    185185  BigIntCheck($A, $B);
     
    192192}
    193193
    194 function BigIntNot($A)
     194function BigIntNot(string $A): string
    195195{
    196196  $Result = BigIntNewAs($A);
     
    202202}
    203203
    204 function BigIntShl1($A)
     204function BigIntShl1(string $A): string
    205205{
    206206  $Result = BigIntNewAs($A);
     
    213213}
    214214
    215 function BigIntShl($A, $B)
     215function BigIntShl(string $A, string $B): string
    216216{
    217217  BigIntCheck($A, $B);
     
    224224}
    225225
    226 function BigIntShr1($A)
     226function BigIntShr1(string $A): string
    227227{
    228228  $Result = BigIntNewAs($A);
     
    235235}
    236236
    237 function BigIntShr($A, $B)
     237function BigIntShr(string $A, string $B): string
    238238{
    239239  BigIntCheck($A, $B);
     
    246246}
    247247
    248 function BigIntToHex($A)
     248function BigIntToHex(string $A): string
    249249{
    250250  $Result = '';
     
    256256}
    257257
    258 function HexToBigInt($A, $BitWidth)
     258function HexToBigInt(string $A, int $BitWidth): string
    259259{
    260260  $Result = BigIntNew($BitWidth);
     
    275275}
    276276
    277 function BigIntToBin($A)
     277function BigIntToBin(string $A): string
    278278{
    279279  $Result = '';
     
    285285}
    286286
    287 function BinToBigInt($A, $BitWidth)
     287function BinToBigInt(string $A, int $BitWidth): string
    288288{
    289289  $Result = BigIntNew($BitWidth);
     
    305305}
    306306
    307 function BigIntToInt($A)
     307function BigIntToInt(string $A): int
    308308{
    309309  // 32-bit int support
     
    311311}
    312312
    313 function IntToBigInt($A, $BitWidth)
     313function IntToBigInt(int $A, int $BitWidth): string
    314314{
    315315  $Result = BigIntNew($BitWidth);
     
    322322}
    323323
    324 function BigIntToDec($A)
     324function BigIntToDec(string $A): string
    325325{
    326326  $BitWidth = strlen($A) << 3;
     
    335335}
    336336
    337 function DecToBigInt($A, $BitWidth)
     337function DecToBigInt(string $A, int $BitWidth): string
    338338{
    339339  $Result = BigIntNew($BitWidth);
     
    342342  {
    343343    $Result = BigIntMul($Result, IntToBigInt(10, $BitWidth));
    344     $Result = BigIntAdd($Result, IntToBigInt(intval($A[$I], $BitWidth)));
     344    $Result = BigIntAdd($Result, IntToBigInt(intval($A[$I]), $BitWidth));
    345345    $I++;
    346346  }
Note: See TracChangeset for help on using the changeset viewer.