PHP Classes

File: src/Core/Curve25519/Fe.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   PHP Sodium Compat   src/Core/Curve25519/Fe.php   Download  
File: src/Core/Curve25519/Fe.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Sodium Compat
Cryptographic functions of libsodium in pure PHP
Author: By
Last change: PHP 8.1: fix deprecation warnings

As of PHP 8.1, PHP will start throwing deprecation warnings along the lines of:
```
Deprecated: Return type of [CLASS]::[METHOD]() should either be compatible with [PHP NATIVE INTERFACE]::[METHOD](): [TYPE], or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice
```

These type of deprecation notices relate to the [Return types for internal methods RFC](https://wiki.php.net/rfc/internal_method_return_types) in PHP 8.1.

Basically, as of PHP 8.1, these methods in classes which implement PHP native interfaces are expected to have a return type declared.
The return type can be the same as used in PHP itself or a more specific type. This complies with the Liskov principle of covariance, which allows the return type of a child overloaded method to be more specific than that of the parent.

As this package has a minimum PHP requirement of PHP 5.2 based on the `composer.json` file, the return type (PHP 7.0 feature) can not be added, so I've added the attribute instead.

While attributes are a PHP 8.0 feature only, due to the syntax choice for `#[]`, they will ignored in PHP < 8 and can be safely added.
Date: 1 year ago
Size: 3,089 bytes
 

Contents

Class file image Download
<?php

if (class_exists('ParagonIE_Sodium_Core_Curve25519_Fe', false)) {
    return;
}

/**
 * Class ParagonIE_Sodium_Core_Curve25519_Fe
 *
 * This represents a Field Element
 */
class ParagonIE_Sodium_Core_Curve25519_Fe implements ArrayAccess
{
   
/**
     * @var array<int, int>
     */
   
protected $container = array();

   
/**
     * @var int
     */
   
protected $size = 10;

   
/**
     * @internal You should not use this directly from another application
     *
     * @param array<int, int> $array
     * @param bool $save_indexes
     * @return self
     */
   
public static function fromArray($array, $save_indexes = null)
    {
       
$count = count($array);
        if (
$save_indexes) {
           
$keys = array_keys($array);
        } else {
           
$keys = range(0, $count - 1);
        }
       
$array = array_values($array);
       
/** @var array<int, int> $keys */

       
$obj = new ParagonIE_Sodium_Core_Curve25519_Fe();
        if (
$save_indexes) {
            for (
$i = 0; $i < $count; ++$i) {
               
$obj->offsetSet($keys[$i], $array[$i]);
            }
        } else {
            for (
$i = 0; $i < $count; ++$i) {
               
$obj->offsetSet($i, $array[$i]);
            }
        }
        return
$obj;
    }

   
/**
     * @internal You should not use this directly from another application
     *
     * @param int|null $offset
     * @param int $value
     * @return void
     * @psalm-suppress MixedArrayOffset
     */
    #[ReturnTypeWillChange]
   
public function offsetSet($offset, $value)
    {
        if (!
is_int($value)) {
            throw new
InvalidArgumentException('Expected an integer');
        }
        if (
is_null($offset)) {
           
$this->container[] = $value;
        } else {
           
$this->container[$offset] = $value;
        }
    }

   
/**
     * @internal You should not use this directly from another application
     *
     * @param int $offset
     * @return bool
     * @psalm-suppress MixedArrayOffset
     */
    #[ReturnTypeWillChange]
   
public function offsetExists($offset)
    {
        return isset(
$this->container[$offset]);
    }

   
/**
     * @internal You should not use this directly from another application
     *
     * @param int $offset
     * @return void
     * @psalm-suppress MixedArrayOffset
     */
    #[ReturnTypeWillChange]
   
public function offsetUnset($offset)
    {
        unset(
$this->container[$offset]);
    }

   
/**
     * @internal You should not use this directly from another application
     *
     * @param int $offset
     * @return int
     * @psalm-suppress MixedArrayOffset
     */
    #[ReturnTypeWillChange]
   
public function offsetGet($offset)
    {
        if (!isset(
$this->container[$offset])) {
           
$this->container[$offset] = 0;
        }
        return (int) (
$this->container[$offset]);
    }

   
/**
     * @internal You should not use this directly from another application
     *
     * @return array
     */
   
public function __debugInfo()
    {
        return array(
implode(', ', $this->container));
    }
}