1<?php
2
3if (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 */
12class 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 if (!($array[$i] instanceof ParagonIE_Sodium_Core32_Int32)) {
52 throw new TypeError('Expected ParagonIE_Sodium_Core32_Int32');
53 }
54 $array[$i]->overflow = 0;
55 $obj->offsetSet($i, $array[$i]);
56 }
57 }
58 return $obj;
59 }
60
61 /**
62 * @internal You should not use this directly from another application
63 *
64 * @param array<int, int> $array
65 * @param bool $save_indexes
66 * @return self
67 * @throws SodiumException
68 * @throws TypeError
69 */
70 public static function fromIntArray($array, $save_indexes = null)
71 {
72 $count = count($array);
73 if ($save_indexes) {
74 $keys = array_keys($array);
75 } else {
76 $keys = range(0, $count - 1);
77 }
78 $array = array_values($array);
79 $set = array();
80 /** @var int $i */
81 /** @var int $v */
82 foreach ($array as $i => $v) {
83 $set[$i] = ParagonIE_Sodium_Core32_Int32::fromInt($v);
84 }
85
86 $obj = new ParagonIE_Sodium_Core32_Curve25519_Fe();
87 if ($save_indexes) {
88 for ($i = 0; $i < $count; ++$i) {
89 $set[$i]->overflow = 0;
90 $obj->offsetSet($keys[$i], $set[$i]);
91 }
92 } else {
93 for ($i = 0; $i < $count; ++$i) {
94 $set[$i]->overflow = 0;
95 $obj->offsetSet($i, $set[$i]);
96 }
97 }
98 return $obj;
99 }
100
101 /**
102 * @internal You should not use this directly from another application
103 *
104 * @param mixed $offset
105 * @param mixed $value
106 * @return void
107 * @throws SodiumException
108 * @throws TypeError
109 */
110 #[ReturnTypeWillChange]
111 public function offsetSet($offset, $value)
112 {
113 if (!($value instanceof ParagonIE_Sodium_Core32_Int32)) {
114 throw new InvalidArgumentException('Expected an instance of ParagonIE_Sodium_Core32_Int32');
115 }
116 if (is_null($offset)) {
117 $this->container[] = $value;
118 } else {
119 ParagonIE_Sodium_Core32_Util::declareScalarType($offset, 'int', 1);
120 $this->container[(int) $offset] = $value;
121 }
122 }
123
124 /**
125 * @internal You should not use this directly from another application
126 *
127 * @param mixed $offset
128 * @return bool
129 * @psalm-suppress MixedArrayOffset
130 */
131 #[ReturnTypeWillChange]
132 public function offsetExists($offset)
133 {
134 return isset($this->container[$offset]);
135 }
136
137 /**
138 * @internal You should not use this directly from another application
139 *
140 * @param mixed $offset
141 * @return void
142 * @psalm-suppress MixedArrayOffset
143 */
144 #[ReturnTypeWillChange]
145 public function offsetUnset($offset)
146 {
147 unset($this->container[$offset]);
148 }
149
150 /**
151 * @internal You should not use this directly from another application
152 *
153 * @param mixed $offset
154 * @return ParagonIE_Sodium_Core32_Int32
155 * @psalm-suppress MixedArrayOffset
156 */
157 #[ReturnTypeWillChange]
158 public function offsetGet($offset)
159 {
160 if (!isset($this->container[$offset])) {
161 $this->container[(int) $offset] = new ParagonIE_Sodium_Core32_Int32();
162 }
163 /** @var ParagonIE_Sodium_Core32_Int32 $get */
164 $get = $this->container[$offset];
165 return $get;
166 }
167
168 /**
169 * @internal You should not use this directly from another application
170 *
171 * @return array
172 */
173 public function __debugInfo()
174 {
175 if (empty($this->container)) {
176 return array();
177 }
178 $c = array(
179 (int) ($this->container[0]->toInt()),
180 (int) ($this->container[1]->toInt()),
181 (int) ($this->container[2]->toInt()),
182 (int) ($this->container[3]->toInt()),
183 (int) ($this->container[4]->toInt()),
184 (int) ($this->container[5]->toInt()),
185 (int) ($this->container[6]->toInt()),
186 (int) ($this->container[7]->toInt()),
187 (int) ($this->container[8]->toInt()),
188 (int) ($this->container[9]->toInt())
189 );
190 return array(implode(', ', $c));
191 }
192}
193