balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

Fe.php (5207B)


      1 <?php
      2 
      3 if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Fe', false)) {
      4     return;
      5 }
      6 
      7 /**
      8  * Class ParagonIE_Sodium_Core32_Curve25519_Fe
      9  *
     10  * This represents a Field Element
     11  */
     12 class ParagonIE_Sodium_Core32_Curve25519_Fe implements ArrayAccess
     13 {
     14     /**
     15      * @var array<int, ParagonIE_Sodium_Core32_Int32>
     16      */
     17     protected $container = array();
     18 
     19     /**
     20      * @var int
     21      */
     22     protected $size = 10;
     23 
     24     /**
     25      * @internal You should not use this directly from another application
     26      *
     27      * @param array<int, ParagonIE_Sodium_Core32_Int32> $array
     28      * @param bool $save_indexes
     29      * @return self
     30      * @throws SodiumException
     31      * @throws TypeError
     32      */
     33     public static function fromArray($array, $save_indexes = null)
     34     {
     35         $count = count($array);
     36         if ($save_indexes) {
     37             $keys = array_keys($array);
     38         } else {
     39             $keys = range(0, $count - 1);
     40         }
     41         $array = array_values($array);
     42 
     43         $obj = new ParagonIE_Sodium_Core32_Curve25519_Fe();
     44         if ($save_indexes) {
     45             for ($i = 0; $i < $count; ++$i) {
     46                 $array[$i]->overflow = 0;
     47                 $obj->offsetSet($keys[$i], $array[$i]);
     48             }
     49         } else {
     50             for ($i = 0; $i < $count; ++$i) {
     51                 $array[$i]->overflow = 0;
     52                 $obj->offsetSet($i, $array[$i]);
     53             }
     54         }
     55         return $obj;
     56     }
     57 
     58     /**
     59      * @internal You should not use this directly from another application
     60      *
     61      * @param array<int, int> $array
     62      * @param bool $save_indexes
     63      * @return self
     64      * @throws SodiumException
     65      * @throws TypeError
     66      */
     67     public static function fromIntArray($array, $save_indexes = null)
     68     {
     69         $count = count($array);
     70         if ($save_indexes) {
     71             $keys = array_keys($array);
     72         } else {
     73             $keys = range(0, $count - 1);
     74         }
     75         $array = array_values($array);
     76         $set = array();
     77         /** @var int $i */
     78         /** @var int $v */
     79         foreach ($array as $i => $v) {
     80             $set[$i] = ParagonIE_Sodium_Core32_Int32::fromInt($v);
     81         }
     82 
     83         $obj = new ParagonIE_Sodium_Core32_Curve25519_Fe();
     84         if ($save_indexes) {
     85             for ($i = 0; $i < $count; ++$i) {
     86                 $set[$i]->overflow = 0;
     87                 $obj->offsetSet($keys[$i], $set[$i]);
     88             }
     89         } else {
     90             for ($i = 0; $i < $count; ++$i) {
     91                 $set[$i]->overflow = 0;
     92                 $obj->offsetSet($i, $set[$i]);
     93             }
     94         }
     95         return $obj;
     96     }
     97 
     98     /**
     99      * @internal You should not use this directly from another application
    100      *
    101      * @param mixed $offset
    102      * @param mixed $value
    103      * @return void
    104      * @throws SodiumException
    105      * @throws TypeError
    106      */
    107     public function offsetSet($offset, $value)
    108     {
    109         if (!($value instanceof ParagonIE_Sodium_Core32_Int32)) {
    110             throw new InvalidArgumentException('Expected an instance of ParagonIE_Sodium_Core32_Int32');
    111         }
    112         if (is_null($offset)) {
    113             $this->container[] = $value;
    114         } else {
    115             ParagonIE_Sodium_Core32_Util::declareScalarType($offset, 'int', 1);
    116             $this->container[(int) $offset] = $value;
    117         }
    118     }
    119 
    120     /**
    121      * @internal You should not use this directly from another application
    122      *
    123      * @param mixed $offset
    124      * @return bool
    125      * @psalm-suppress MixedArrayOffset
    126      */
    127     public function offsetExists($offset)
    128     {
    129         return isset($this->container[$offset]);
    130     }
    131 
    132     /**
    133      * @internal You should not use this directly from another application
    134      *
    135      * @param mixed $offset
    136      * @return void
    137      * @psalm-suppress MixedArrayOffset
    138      */
    139     public function offsetUnset($offset)
    140     {
    141         unset($this->container[$offset]);
    142     }
    143 
    144     /**
    145      * @internal You should not use this directly from another application
    146      *
    147      * @param mixed $offset
    148      * @return ParagonIE_Sodium_Core32_Int32
    149      * @psalm-suppress MixedArrayOffset
    150      */
    151     public function offsetGet($offset)
    152     {
    153         if (!isset($this->container[$offset])) {
    154             $this->container[(int) $offset] = new ParagonIE_Sodium_Core32_Int32();
    155         }
    156         /** @var ParagonIE_Sodium_Core32_Int32 $get */
    157         $get = $this->container[$offset];
    158         return $get;
    159     }
    160 
    161     /**
    162      * @internal You should not use this directly from another application
    163      *
    164      * @return array
    165      */
    166     public function __debugInfo()
    167     {
    168         if (empty($this->container)) {
    169             return array();
    170         }
    171         $c = array(
    172             (int) ($this->container[0]->toInt()),
    173             (int) ($this->container[1]->toInt()),
    174             (int) ($this->container[2]->toInt()),
    175             (int) ($this->container[3]->toInt()),
    176             (int) ($this->container[4]->toInt()),
    177             (int) ($this->container[5]->toInt()),
    178             (int) ($this->container[6]->toInt()),
    179             (int) ($this->container[7]->toInt()),
    180             (int) ($this->container[8]->toInt()),
    181             (int) ($this->container[9]->toInt())
    182         );
    183         return array(implode(', ', $c));
    184     }
    185 }