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.4 compat nits
Merge pull request #175 from paragonie/box-v2

Remove vendor/paragonie from box.json
Stricter typing
Clean up psalm-suppress statements
Set minimum PHP version to 8.1 for v2.x
Date: 27 days ago
Size: 2,976 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);

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

/**
 * Class ParagonIE_Sodium_Core_Curve25519_Fe
 *
 * This represents a Field Element
 *
 * @psalm-suppress MissingTemplateParam
 */
class ParagonIE_Sodium_Core_Curve25519_Fe implements ArrayAccess
{
   
/**
     * @var array<int, int>
     */
   
protected array $container = [];

   
/**
     * @var int
     */
   
protected int $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 $array, bool $save_indexes = false): self
   
{
       
$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
     */
    #[ReturnTypeWillChange]
   
public function offsetSet($offset, $value): void
   
{
        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
     */
    #[ReturnTypeWillChange]
   
public function offsetExists($offset): bool
   
{
        return isset(
$this->container[$offset]);
    }

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

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

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