/htdocs/wp-includes/sodium_compat/src/Core32/Curve25519/Fe.php

https://gitlab.com/VTTE/sitios-vtte · PHP · 185 lines · 106 code · 12 blank · 67 comment · 18 complexity · 93f701bb01e99676df0d732a935c810e MD5 · raw file

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