@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Curve25519', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -15,3771 +15,3771 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class ParagonIE_Sodium_Core_Curve25519 extends ParagonIE_Sodium_Core_Curve25519_H |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Get a field element of size 10 with a value of 0 |
|
| 20 | - * |
|
| 21 | - * @internal You should not use this directly from another application |
|
| 22 | - * |
|
| 23 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 24 | - */ |
|
| 25 | - public static function fe_0() |
|
| 26 | - { |
|
| 27 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 28 | - array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) |
|
| 29 | - ); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Get a field element of size 10 with a value of 1 |
|
| 34 | - * |
|
| 35 | - * @internal You should not use this directly from another application |
|
| 36 | - * |
|
| 37 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 38 | - */ |
|
| 39 | - public static function fe_1() |
|
| 40 | - { |
|
| 41 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 42 | - array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0) |
|
| 43 | - ); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Add two field elements. |
|
| 48 | - * |
|
| 49 | - * @internal You should not use this directly from another application |
|
| 50 | - * |
|
| 51 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 52 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 53 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 54 | - * @psalm-suppress MixedAssignment |
|
| 55 | - * @psalm-suppress MixedOperand |
|
| 56 | - */ |
|
| 57 | - public static function fe_add( |
|
| 58 | - ParagonIE_Sodium_Core_Curve25519_Fe $f, |
|
| 59 | - ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 60 | - ) { |
|
| 61 | - /** @var array<int, int> $arr */ |
|
| 62 | - $arr = array(); |
|
| 63 | - for ($i = 0; $i < 10; ++$i) { |
|
| 64 | - $arr[$i] = (int) ($f[$i] + $g[$i]); |
|
| 65 | - } |
|
| 66 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($arr); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Constant-time conditional move. |
|
| 71 | - * |
|
| 72 | - * @internal You should not use this directly from another application |
|
| 73 | - * |
|
| 74 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 75 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 76 | - * @param int $b |
|
| 77 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 78 | - * @psalm-suppress MixedAssignment |
|
| 79 | - */ |
|
| 80 | - public static function fe_cmov( |
|
| 81 | - ParagonIE_Sodium_Core_Curve25519_Fe $f, |
|
| 82 | - ParagonIE_Sodium_Core_Curve25519_Fe $g, |
|
| 83 | - $b = 0 |
|
| 84 | - ) { |
|
| 85 | - /** @var array<int, int> $h */ |
|
| 86 | - $h = array(); |
|
| 87 | - $b *= -1; |
|
| 88 | - for ($i = 0; $i < 10; ++$i) { |
|
| 89 | - $x = (($f[$i] ^ $g[$i]) & $b); |
|
| 90 | - $h[$i] = ($f[$i]) ^ $x; |
|
| 91 | - } |
|
| 92 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($h); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Create a copy of a field element. |
|
| 97 | - * |
|
| 98 | - * @internal You should not use this directly from another application |
|
| 99 | - * |
|
| 100 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 101 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 102 | - */ |
|
| 103 | - public static function fe_copy(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 104 | - { |
|
| 105 | - $h = clone $f; |
|
| 106 | - return $h; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Give: 32-byte string. |
|
| 111 | - * Receive: A field element object to use for internal calculations. |
|
| 112 | - * |
|
| 113 | - * @internal You should not use this directly from another application |
|
| 114 | - * |
|
| 115 | - * @param string $s |
|
| 116 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 117 | - * @throws RangeException |
|
| 118 | - * @throws TypeError |
|
| 119 | - */ |
|
| 120 | - public static function fe_frombytes($s) |
|
| 121 | - { |
|
| 122 | - if (self::strlen($s) !== 32) { |
|
| 123 | - throw new RangeException('Expected a 32-byte string.'); |
|
| 124 | - } |
|
| 125 | - $h0 = self::load_4($s); |
|
| 126 | - $h1 = self::load_3(self::substr($s, 4, 3)) << 6; |
|
| 127 | - $h2 = self::load_3(self::substr($s, 7, 3)) << 5; |
|
| 128 | - $h3 = self::load_3(self::substr($s, 10, 3)) << 3; |
|
| 129 | - $h4 = self::load_3(self::substr($s, 13, 3)) << 2; |
|
| 130 | - $h5 = self::load_4(self::substr($s, 16, 4)); |
|
| 131 | - $h6 = self::load_3(self::substr($s, 20, 3)) << 7; |
|
| 132 | - $h7 = self::load_3(self::substr($s, 23, 3)) << 5; |
|
| 133 | - $h8 = self::load_3(self::substr($s, 26, 3)) << 4; |
|
| 134 | - $h9 = (self::load_3(self::substr($s, 29, 3)) & 8388607) << 2; |
|
| 135 | - |
|
| 136 | - $carry9 = ($h9 + (1 << 24)) >> 25; |
|
| 137 | - $h0 += self::mul($carry9, 19, 5); |
|
| 138 | - $h9 -= $carry9 << 25; |
|
| 139 | - $carry1 = ($h1 + (1 << 24)) >> 25; |
|
| 140 | - $h2 += $carry1; |
|
| 141 | - $h1 -= $carry1 << 25; |
|
| 142 | - $carry3 = ($h3 + (1 << 24)) >> 25; |
|
| 143 | - $h4 += $carry3; |
|
| 144 | - $h3 -= $carry3 << 25; |
|
| 145 | - $carry5 = ($h5 + (1 << 24)) >> 25; |
|
| 146 | - $h6 += $carry5; |
|
| 147 | - $h5 -= $carry5 << 25; |
|
| 148 | - $carry7 = ($h7 + (1 << 24)) >> 25; |
|
| 149 | - $h8 += $carry7; |
|
| 150 | - $h7 -= $carry7 << 25; |
|
| 151 | - |
|
| 152 | - $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 153 | - $h1 += $carry0; |
|
| 154 | - $h0 -= $carry0 << 26; |
|
| 155 | - $carry2 = ($h2 + (1 << 25)) >> 26; |
|
| 156 | - $h3 += $carry2; |
|
| 157 | - $h2 -= $carry2 << 26; |
|
| 158 | - $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 159 | - $h5 += $carry4; |
|
| 160 | - $h4 -= $carry4 << 26; |
|
| 161 | - $carry6 = ($h6 + (1 << 25)) >> 26; |
|
| 162 | - $h7 += $carry6; |
|
| 163 | - $h6 -= $carry6 << 26; |
|
| 164 | - $carry8 = ($h8 + (1 << 25)) >> 26; |
|
| 165 | - $h9 += $carry8; |
|
| 166 | - $h8 -= $carry8 << 26; |
|
| 167 | - |
|
| 168 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 169 | - array( |
|
| 170 | - (int) $h0, |
|
| 171 | - (int) $h1, |
|
| 172 | - (int) $h2, |
|
| 173 | - (int) $h3, |
|
| 174 | - (int) $h4, |
|
| 175 | - (int) $h5, |
|
| 176 | - (int) $h6, |
|
| 177 | - (int) $h7, |
|
| 178 | - (int) $h8, |
|
| 179 | - (int) $h9 |
|
| 180 | - ) |
|
| 181 | - ); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Convert a field element to a byte string. |
|
| 186 | - * |
|
| 187 | - * @internal You should not use this directly from another application |
|
| 188 | - * |
|
| 189 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $h |
|
| 190 | - * @return string |
|
| 191 | - */ |
|
| 192 | - public static function fe_tobytes(ParagonIE_Sodium_Core_Curve25519_Fe $h) |
|
| 193 | - { |
|
| 194 | - $h0 = (int) $h[0]; |
|
| 195 | - $h1 = (int) $h[1]; |
|
| 196 | - $h2 = (int) $h[2]; |
|
| 197 | - $h3 = (int) $h[3]; |
|
| 198 | - $h4 = (int) $h[4]; |
|
| 199 | - $h5 = (int) $h[5]; |
|
| 200 | - $h6 = (int) $h[6]; |
|
| 201 | - $h7 = (int) $h[7]; |
|
| 202 | - $h8 = (int) $h[8]; |
|
| 203 | - $h9 = (int) $h[9]; |
|
| 204 | - |
|
| 205 | - $q = (self::mul($h9, 19, 5) + (1 << 24)) >> 25; |
|
| 206 | - $q = ($h0 + $q) >> 26; |
|
| 207 | - $q = ($h1 + $q) >> 25; |
|
| 208 | - $q = ($h2 + $q) >> 26; |
|
| 209 | - $q = ($h3 + $q) >> 25; |
|
| 210 | - $q = ($h4 + $q) >> 26; |
|
| 211 | - $q = ($h5 + $q) >> 25; |
|
| 212 | - $q = ($h6 + $q) >> 26; |
|
| 213 | - $q = ($h7 + $q) >> 25; |
|
| 214 | - $q = ($h8 + $q) >> 26; |
|
| 215 | - $q = ($h9 + $q) >> 25; |
|
| 216 | - |
|
| 217 | - $h0 += self::mul($q, 19, 5); |
|
| 218 | - |
|
| 219 | - $carry0 = $h0 >> 26; |
|
| 220 | - $h1 += $carry0; |
|
| 221 | - $h0 -= $carry0 << 26; |
|
| 222 | - $carry1 = $h1 >> 25; |
|
| 223 | - $h2 += $carry1; |
|
| 224 | - $h1 -= $carry1 << 25; |
|
| 225 | - $carry2 = $h2 >> 26; |
|
| 226 | - $h3 += $carry2; |
|
| 227 | - $h2 -= $carry2 << 26; |
|
| 228 | - $carry3 = $h3 >> 25; |
|
| 229 | - $h4 += $carry3; |
|
| 230 | - $h3 -= $carry3 << 25; |
|
| 231 | - $carry4 = $h4 >> 26; |
|
| 232 | - $h5 += $carry4; |
|
| 233 | - $h4 -= $carry4 << 26; |
|
| 234 | - $carry5 = $h5 >> 25; |
|
| 235 | - $h6 += $carry5; |
|
| 236 | - $h5 -= $carry5 << 25; |
|
| 237 | - $carry6 = $h6 >> 26; |
|
| 238 | - $h7 += $carry6; |
|
| 239 | - $h6 -= $carry6 << 26; |
|
| 240 | - $carry7 = $h7 >> 25; |
|
| 241 | - $h8 += $carry7; |
|
| 242 | - $h7 -= $carry7 << 25; |
|
| 243 | - $carry8 = $h8 >> 26; |
|
| 244 | - $h9 += $carry8; |
|
| 245 | - $h8 -= $carry8 << 26; |
|
| 246 | - $carry9 = $h9 >> 25; |
|
| 247 | - $h9 -= $carry9 << 25; |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * @var array<int, int> |
|
| 251 | - */ |
|
| 252 | - $s = array( |
|
| 253 | - (int) (($h0 >> 0) & 0xff), |
|
| 254 | - (int) (($h0 >> 8) & 0xff), |
|
| 255 | - (int) (($h0 >> 16) & 0xff), |
|
| 256 | - (int) ((($h0 >> 24) | ($h1 << 2)) & 0xff), |
|
| 257 | - (int) (($h1 >> 6) & 0xff), |
|
| 258 | - (int) (($h1 >> 14) & 0xff), |
|
| 259 | - (int) ((($h1 >> 22) | ($h2 << 3)) & 0xff), |
|
| 260 | - (int) (($h2 >> 5) & 0xff), |
|
| 261 | - (int) (($h2 >> 13) & 0xff), |
|
| 262 | - (int) ((($h2 >> 21) | ($h3 << 5)) & 0xff), |
|
| 263 | - (int) (($h3 >> 3) & 0xff), |
|
| 264 | - (int) (($h3 >> 11) & 0xff), |
|
| 265 | - (int) ((($h3 >> 19) | ($h4 << 6)) & 0xff), |
|
| 266 | - (int) (($h4 >> 2) & 0xff), |
|
| 267 | - (int) (($h4 >> 10) & 0xff), |
|
| 268 | - (int) (($h4 >> 18) & 0xff), |
|
| 269 | - (int) (($h5 >> 0) & 0xff), |
|
| 270 | - (int) (($h5 >> 8) & 0xff), |
|
| 271 | - (int) (($h5 >> 16) & 0xff), |
|
| 272 | - (int) ((($h5 >> 24) | ($h6 << 1)) & 0xff), |
|
| 273 | - (int) (($h6 >> 7) & 0xff), |
|
| 274 | - (int) (($h6 >> 15) & 0xff), |
|
| 275 | - (int) ((($h6 >> 23) | ($h7 << 3)) & 0xff), |
|
| 276 | - (int) (($h7 >> 5) & 0xff), |
|
| 277 | - (int) (($h7 >> 13) & 0xff), |
|
| 278 | - (int) ((($h7 >> 21) | ($h8 << 4)) & 0xff), |
|
| 279 | - (int) (($h8 >> 4) & 0xff), |
|
| 280 | - (int) (($h8 >> 12) & 0xff), |
|
| 281 | - (int) ((($h8 >> 20) | ($h9 << 6)) & 0xff), |
|
| 282 | - (int) (($h9 >> 2) & 0xff), |
|
| 283 | - (int) (($h9 >> 10) & 0xff), |
|
| 284 | - (int) (($h9 >> 18) & 0xff) |
|
| 285 | - ); |
|
| 286 | - return self::intArrayToString($s); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Is a field element negative? (1 = yes, 0 = no. Used in calculations.) |
|
| 291 | - * |
|
| 292 | - * @internal You should not use this directly from another application |
|
| 293 | - * |
|
| 294 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 295 | - * @return int |
|
| 296 | - * @throws SodiumException |
|
| 297 | - * @throws TypeError |
|
| 298 | - */ |
|
| 299 | - public static function fe_isnegative(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 300 | - { |
|
| 301 | - $str = self::fe_tobytes($f); |
|
| 302 | - return (int) (self::chrToInt($str[0]) & 1); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Returns 0 if this field element results in all NUL bytes. |
|
| 307 | - * |
|
| 308 | - * @internal You should not use this directly from another application |
|
| 309 | - * |
|
| 310 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 311 | - * @return bool |
|
| 312 | - * @throws SodiumException |
|
| 313 | - * @throws TypeError |
|
| 314 | - */ |
|
| 315 | - public static function fe_isnonzero(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 316 | - { |
|
| 317 | - static $zero; |
|
| 318 | - if ($zero === null) { |
|
| 319 | - $zero = str_repeat("\x00", 32); |
|
| 320 | - } |
|
| 321 | - /** @var string $zero */ |
|
| 322 | - /** @var string $str */ |
|
| 323 | - $str = self::fe_tobytes($f); |
|
| 324 | - return !self::verify_32($str, (string) $zero); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Multiply two field elements |
|
| 329 | - * |
|
| 330 | - * h = f * g |
|
| 331 | - * |
|
| 332 | - * @internal You should not use this directly from another application |
|
| 333 | - * |
|
| 334 | - * @security Is multiplication a source of timing leaks? If so, can we do |
|
| 335 | - * anything to prevent that from happening? |
|
| 336 | - * |
|
| 337 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 338 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 339 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 340 | - */ |
|
| 341 | - public static function fe_mul( |
|
| 342 | - ParagonIE_Sodium_Core_Curve25519_Fe $f, |
|
| 343 | - ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 344 | - ) { |
|
| 345 | - $f0 = $f[0]; |
|
| 346 | - $f1 = $f[1]; |
|
| 347 | - $f2 = $f[2]; |
|
| 348 | - $f3 = $f[3]; |
|
| 349 | - $f4 = $f[4]; |
|
| 350 | - $f5 = $f[5]; |
|
| 351 | - $f6 = $f[6]; |
|
| 352 | - $f7 = $f[7]; |
|
| 353 | - $f8 = $f[8]; |
|
| 354 | - $f9 = $f[9]; |
|
| 355 | - $g0 = $g[0]; |
|
| 356 | - $g1 = $g[1]; |
|
| 357 | - $g2 = $g[2]; |
|
| 358 | - $g3 = $g[3]; |
|
| 359 | - $g4 = $g[4]; |
|
| 360 | - $g5 = $g[5]; |
|
| 361 | - $g6 = $g[6]; |
|
| 362 | - $g7 = $g[7]; |
|
| 363 | - $g8 = $g[8]; |
|
| 364 | - $g9 = $g[9]; |
|
| 365 | - $g1_19 = self::mul($g1, 19, 5); |
|
| 366 | - $g2_19 = self::mul($g2, 19, 5); |
|
| 367 | - $g3_19 = self::mul($g3, 19, 5); |
|
| 368 | - $g4_19 = self::mul($g4, 19, 5); |
|
| 369 | - $g5_19 = self::mul($g5, 19, 5); |
|
| 370 | - $g6_19 = self::mul($g6, 19, 5); |
|
| 371 | - $g7_19 = self::mul($g7, 19, 5); |
|
| 372 | - $g8_19 = self::mul($g8, 19, 5); |
|
| 373 | - $g9_19 = self::mul($g9, 19, 5); |
|
| 374 | - $f1_2 = $f1 << 1; |
|
| 375 | - $f3_2 = $f3 << 1; |
|
| 376 | - $f5_2 = $f5 << 1; |
|
| 377 | - $f7_2 = $f7 << 1; |
|
| 378 | - $f9_2 = $f9 << 1; |
|
| 379 | - $f0g0 = self::mul($f0, $g0, 26); |
|
| 380 | - $f0g1 = self::mul($f0, $g1, 25); |
|
| 381 | - $f0g2 = self::mul($f0, $g2, 26); |
|
| 382 | - $f0g3 = self::mul($f0, $g3, 25); |
|
| 383 | - $f0g4 = self::mul($f0, $g4, 26); |
|
| 384 | - $f0g5 = self::mul($f0, $g5, 25); |
|
| 385 | - $f0g6 = self::mul($f0, $g6, 26); |
|
| 386 | - $f0g7 = self::mul($f0, $g7, 25); |
|
| 387 | - $f0g8 = self::mul($f0, $g8, 26); |
|
| 388 | - $f0g9 = self::mul($f0, $g9, 26); |
|
| 389 | - $f1g0 = self::mul($f1, $g0, 26); |
|
| 390 | - $f1g1_2 = self::mul($f1_2, $g1, 25); |
|
| 391 | - $f1g2 = self::mul($f1, $g2, 26); |
|
| 392 | - $f1g3_2 = self::mul($f1_2, $g3, 25); |
|
| 393 | - $f1g4 = self::mul($f1, $g4, 26); |
|
| 394 | - $f1g5_2 = self::mul($f1_2, $g5, 25); |
|
| 395 | - $f1g6 = self::mul($f1, $g6, 26); |
|
| 396 | - $f1g7_2 = self::mul($f1_2, $g7, 25); |
|
| 397 | - $f1g8 = self::mul($f1, $g8, 26); |
|
| 398 | - $f1g9_38 = self::mul($g9_19, $f1_2, 26); |
|
| 399 | - $f2g0 = self::mul($f2, $g0, 26); |
|
| 400 | - $f2g1 = self::mul($f2, $g1, 25); |
|
| 401 | - $f2g2 = self::mul($f2, $g2, 26); |
|
| 402 | - $f2g3 = self::mul($f2, $g3, 25); |
|
| 403 | - $f2g4 = self::mul($f2, $g4, 26); |
|
| 404 | - $f2g5 = self::mul($f2, $g5, 25); |
|
| 405 | - $f2g6 = self::mul($f2, $g6, 26); |
|
| 406 | - $f2g7 = self::mul($f2, $g7, 25); |
|
| 407 | - $f2g8_19 = self::mul($g8_19, $f2, 26); |
|
| 408 | - $f2g9_19 = self::mul($g9_19, $f2, 26); |
|
| 409 | - $f3g0 = self::mul($f3, $g0, 26); |
|
| 410 | - $f3g1_2 = self::mul($f3_2, $g1, 25); |
|
| 411 | - $f3g2 = self::mul($f3, $g2, 26); |
|
| 412 | - $f3g3_2 = self::mul($f3_2, $g3, 25); |
|
| 413 | - $f3g4 = self::mul($f3, $g4, 26); |
|
| 414 | - $f3g5_2 = self::mul($f3_2, $g5, 25); |
|
| 415 | - $f3g6 = self::mul($f3, $g6, 26); |
|
| 416 | - $f3g7_38 = self::mul($g7_19, $f3_2, 26); |
|
| 417 | - $f3g8_19 = self::mul($g8_19, $f3, 25); |
|
| 418 | - $f3g9_38 = self::mul($g9_19, $f3_2, 26); |
|
| 419 | - $f4g0 = self::mul($f4, $g0, 26); |
|
| 420 | - $f4g1 = self::mul($f4, $g1, 25); |
|
| 421 | - $f4g2 = self::mul($f4, $g2, 26); |
|
| 422 | - $f4g3 = self::mul($f4, $g3, 25); |
|
| 423 | - $f4g4 = self::mul($f4, $g4, 26); |
|
| 424 | - $f4g5 = self::mul($f4, $g5, 25); |
|
| 425 | - $f4g6_19 = self::mul($g6_19, $f4, 26); |
|
| 426 | - $f4g7_19 = self::mul($g7_19, $f4, 26); |
|
| 427 | - $f4g8_19 = self::mul($g8_19, $f4, 26); |
|
| 428 | - $f4g9_19 = self::mul($g9_19, $f4, 26); |
|
| 429 | - $f5g0 = self::mul($f5, $g0, 26); |
|
| 430 | - $f5g1_2 = self::mul($f5_2, $g1, 25); |
|
| 431 | - $f5g2 = self::mul($f5, $g2, 26); |
|
| 432 | - $f5g3_2 = self::mul($f5_2, $g3, 25); |
|
| 433 | - $f5g4 = self::mul($f5, $g4, 26); |
|
| 434 | - $f5g5_38 = self::mul($g5_19, $f5_2, 26); |
|
| 435 | - $f5g6_19 = self::mul($g6_19, $f5, 25); |
|
| 436 | - $f5g7_38 = self::mul($g7_19, $f5_2, 26); |
|
| 437 | - $f5g8_19 = self::mul($g8_19, $f5, 25); |
|
| 438 | - $f5g9_38 = self::mul($g9_19, $f5_2, 26); |
|
| 439 | - $f6g0 = self::mul($f6, $g0, 26); |
|
| 440 | - $f6g1 = self::mul($f6, $g1, 25); |
|
| 441 | - $f6g2 = self::mul($f6, $g2, 26); |
|
| 442 | - $f6g3 = self::mul($f6, $g3, 25); |
|
| 443 | - $f6g4_19 = self::mul($g4_19, $f6, 26); |
|
| 444 | - $f6g5_19 = self::mul($g5_19, $f6, 26); |
|
| 445 | - $f6g6_19 = self::mul($g6_19, $f6, 26); |
|
| 446 | - $f6g7_19 = self::mul($g7_19, $f6, 26); |
|
| 447 | - $f6g8_19 = self::mul($g8_19, $f6, 26); |
|
| 448 | - $f6g9_19 = self::mul($g9_19, $f6, 26); |
|
| 449 | - $f7g0 = self::mul($f7, $g0, 26); |
|
| 450 | - $f7g1_2 = self::mul($f7_2, $g1, 25); |
|
| 451 | - $f7g2 = self::mul($f7, $g2, 26); |
|
| 452 | - $f7g3_38 = self::mul($g3_19, $f7_2, 26); |
|
| 453 | - $f7g4_19 = self::mul($g4_19, $f7, 26); |
|
| 454 | - $f7g5_38 = self::mul($g5_19, $f7_2, 26); |
|
| 455 | - $f7g6_19 = self::mul($g6_19, $f7, 25); |
|
| 456 | - $f7g7_38 = self::mul($g7_19, $f7_2, 26); |
|
| 457 | - $f7g8_19 = self::mul($g8_19, $f7, 25); |
|
| 458 | - $f7g9_38 = self::mul($g9_19,$f7_2, 26); |
|
| 459 | - $f8g0 = self::mul($f8, $g0, 26); |
|
| 460 | - $f8g1 = self::mul($f8, $g1, 25); |
|
| 461 | - $f8g2_19 = self::mul($g2_19, $f8, 26); |
|
| 462 | - $f8g3_19 = self::mul($g3_19, $f8, 26); |
|
| 463 | - $f8g4_19 = self::mul($g4_19, $f8, 26); |
|
| 464 | - $f8g5_19 = self::mul($g5_19, $f8, 26); |
|
| 465 | - $f8g6_19 = self::mul($g6_19, $f8, 26); |
|
| 466 | - $f8g7_19 = self::mul($g7_19, $f8, 26); |
|
| 467 | - $f8g8_19 = self::mul($g8_19, $f8, 26); |
|
| 468 | - $f8g9_19 = self::mul($g9_19, $f8, 26); |
|
| 469 | - $f9g0 = self::mul($f9, $g0, 26); |
|
| 470 | - $f9g1_38 = self::mul($g1_19, $f9_2, 26); |
|
| 471 | - $f9g2_19 = self::mul($g2_19, $f9, 25); |
|
| 472 | - $f9g3_38 = self::mul($g3_19, $f9_2, 26); |
|
| 473 | - $f9g4_19 = self::mul($g4_19, $f9, 25); |
|
| 474 | - $f9g5_38 = self::mul($g5_19, $f9_2, 26); |
|
| 475 | - $f9g6_19 = self::mul($g6_19, $f9, 25); |
|
| 476 | - $f9g7_38 = self::mul($g7_19, $f9_2, 26); |
|
| 477 | - $f9g8_19 = self::mul($g8_19, $f9, 25); |
|
| 478 | - $f9g9_38 = self::mul($g9_19, $f9_2, 26); |
|
| 479 | - $h0 = $f0g0 + $f1g9_38 + $f2g8_19 + $f3g7_38 + $f4g6_19 + $f5g5_38 + $f6g4_19 + $f7g3_38 + $f8g2_19 + $f9g1_38; |
|
| 480 | - $h1 = $f0g1 + $f1g0 + $f2g9_19 + $f3g8_19 + $f4g7_19 + $f5g6_19 + $f6g5_19 + $f7g4_19 + $f8g3_19 + $f9g2_19; |
|
| 481 | - $h2 = $f0g2 + $f1g1_2 + $f2g0 + $f3g9_38 + $f4g8_19 + $f5g7_38 + $f6g6_19 + $f7g5_38 + $f8g4_19 + $f9g3_38; |
|
| 482 | - $h3 = $f0g3 + $f1g2 + $f2g1 + $f3g0 + $f4g9_19 + $f5g8_19 + $f6g7_19 + $f7g6_19 + $f8g5_19 + $f9g4_19; |
|
| 483 | - $h4 = $f0g4 + $f1g3_2 + $f2g2 + $f3g1_2 + $f4g0 + $f5g9_38 + $f6g8_19 + $f7g7_38 + $f8g6_19 + $f9g5_38; |
|
| 484 | - $h5 = $f0g5 + $f1g4 + $f2g3 + $f3g2 + $f4g1 + $f5g0 + $f6g9_19 + $f7g8_19 + $f8g7_19 + $f9g6_19; |
|
| 485 | - $h6 = $f0g6 + $f1g5_2 + $f2g4 + $f3g3_2 + $f4g2 + $f5g1_2 + $f6g0 + $f7g9_38 + $f8g8_19 + $f9g7_38; |
|
| 486 | - $h7 = $f0g7 + $f1g6 + $f2g5 + $f3g4 + $f4g3 + $f5g2 + $f6g1 + $f7g0 + $f8g9_19 + $f9g8_19; |
|
| 487 | - $h8 = $f0g8 + $f1g7_2 + $f2g6 + $f3g5_2 + $f4g4 + $f5g3_2 + $f6g2 + $f7g1_2 + $f8g0 + $f9g9_38; |
|
| 488 | - $h9 = $f0g9 + $f1g8 + $f2g7 + $f3g6 + $f4g5 + $f5g4 + $f6g3 + $f7g2 + $f8g1 + $f9g0 ; |
|
| 489 | - |
|
| 490 | - $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 491 | - $h1 += $carry0; |
|
| 492 | - $h0 -= $carry0 << 26; |
|
| 493 | - $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 494 | - $h5 += $carry4; |
|
| 495 | - $h4 -= $carry4 << 26; |
|
| 496 | - |
|
| 497 | - $carry1 = ($h1 + (1 << 24)) >> 25; |
|
| 498 | - $h2 += $carry1; |
|
| 499 | - $h1 -= $carry1 << 25; |
|
| 500 | - $carry5 = ($h5 + (1 << 24)) >> 25; |
|
| 501 | - $h6 += $carry5; |
|
| 502 | - $h5 -= $carry5 << 25; |
|
| 503 | - |
|
| 504 | - $carry2 = ($h2 + (1 << 25)) >> 26; |
|
| 505 | - $h3 += $carry2; |
|
| 506 | - $h2 -= $carry2 << 26; |
|
| 507 | - $carry6 = ($h6 + (1 << 25)) >> 26; |
|
| 508 | - $h7 += $carry6; |
|
| 509 | - $h6 -= $carry6 << 26; |
|
| 510 | - |
|
| 511 | - $carry3 = ($h3 + (1 << 24)) >> 25; |
|
| 512 | - $h4 += $carry3; |
|
| 513 | - $h3 -= $carry3 << 25; |
|
| 514 | - $carry7 = ($h7 + (1 << 24)) >> 25; |
|
| 515 | - $h8 += $carry7; |
|
| 516 | - $h7 -= $carry7 << 25; |
|
| 517 | - |
|
| 518 | - $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 519 | - $h5 += $carry4; |
|
| 520 | - $h4 -= $carry4 << 26; |
|
| 521 | - $carry8 = ($h8 + (1 << 25)) >> 26; |
|
| 522 | - $h9 += $carry8; |
|
| 523 | - $h8 -= $carry8 << 26; |
|
| 524 | - |
|
| 525 | - $carry9 = ($h9 + (1 << 24)) >> 25; |
|
| 526 | - $h0 += self::mul($carry9, 19, 5); |
|
| 527 | - $h9 -= $carry9 << 25; |
|
| 528 | - |
|
| 529 | - $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 530 | - $h1 += $carry0; |
|
| 531 | - $h0 -= $carry0 << 26; |
|
| 532 | - |
|
| 533 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 534 | - array( |
|
| 535 | - (int) $h0, |
|
| 536 | - (int) $h1, |
|
| 537 | - (int) $h2, |
|
| 538 | - (int) $h3, |
|
| 539 | - (int) $h4, |
|
| 540 | - (int) $h5, |
|
| 541 | - (int) $h6, |
|
| 542 | - (int) $h7, |
|
| 543 | - (int) $h8, |
|
| 544 | - (int) $h9 |
|
| 545 | - ) |
|
| 546 | - ); |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - /** |
|
| 550 | - * Get the negative values for each piece of the field element. |
|
| 551 | - * |
|
| 552 | - * h = -f |
|
| 553 | - * |
|
| 554 | - * @internal You should not use this directly from another application |
|
| 555 | - * |
|
| 556 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 557 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 558 | - * @psalm-suppress MixedAssignment |
|
| 559 | - */ |
|
| 560 | - public static function fe_neg(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 561 | - { |
|
| 562 | - $h = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 563 | - for ($i = 0; $i < 10; ++$i) { |
|
| 564 | - $h[$i] = -$f[$i]; |
|
| 565 | - } |
|
| 566 | - return $h; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Square a field element |
|
| 571 | - * |
|
| 572 | - * h = f * f |
|
| 573 | - * |
|
| 574 | - * @internal You should not use this directly from another application |
|
| 575 | - * |
|
| 576 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 577 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 578 | - */ |
|
| 579 | - public static function fe_sq(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 580 | - { |
|
| 581 | - $f0 = (int) $f[0]; |
|
| 582 | - $f1 = (int) $f[1]; |
|
| 583 | - $f2 = (int) $f[2]; |
|
| 584 | - $f3 = (int) $f[3]; |
|
| 585 | - $f4 = (int) $f[4]; |
|
| 586 | - $f5 = (int) $f[5]; |
|
| 587 | - $f6 = (int) $f[6]; |
|
| 588 | - $f7 = (int) $f[7]; |
|
| 589 | - $f8 = (int) $f[8]; |
|
| 590 | - $f9 = (int) $f[9]; |
|
| 591 | - |
|
| 592 | - $f0_2 = $f0 << 1; |
|
| 593 | - $f1_2 = $f1 << 1; |
|
| 594 | - $f2_2 = $f2 << 1; |
|
| 595 | - $f3_2 = $f3 << 1; |
|
| 596 | - $f4_2 = $f4 << 1; |
|
| 597 | - $f5_2 = $f5 << 1; |
|
| 598 | - $f6_2 = $f6 << 1; |
|
| 599 | - $f7_2 = $f7 << 1; |
|
| 600 | - $f5_38 = self::mul($f5, 38, 6); |
|
| 601 | - $f6_19 = self::mul($f6, 19, 5); |
|
| 602 | - $f7_38 = self::mul($f7, 38, 6); |
|
| 603 | - $f8_19 = self::mul($f8, 19, 5); |
|
| 604 | - $f9_38 = self::mul($f9, 38, 6); |
|
| 605 | - $f0f0 = self::mul($f0, $f0, 26); |
|
| 606 | - $f0f1_2 = self::mul($f0_2, $f1, 26); |
|
| 607 | - $f0f2_2 = self::mul($f0_2, $f2, 26); |
|
| 608 | - $f0f3_2 = self::mul($f0_2, $f3, 26); |
|
| 609 | - $f0f4_2 = self::mul($f0_2, $f4, 26); |
|
| 610 | - $f0f5_2 = self::mul($f0_2, $f5, 26); |
|
| 611 | - $f0f6_2 = self::mul($f0_2, $f6, 26); |
|
| 612 | - $f0f7_2 = self::mul($f0_2, $f7, 26); |
|
| 613 | - $f0f8_2 = self::mul($f0_2, $f8, 26); |
|
| 614 | - $f0f9_2 = self::mul($f0_2, $f9, 26); |
|
| 615 | - $f1f1_2 = self::mul($f1_2, $f1, 26); |
|
| 616 | - $f1f2_2 = self::mul($f1_2, $f2, 26); |
|
| 617 | - $f1f3_4 = self::mul($f1_2, $f3_2, 26); |
|
| 618 | - $f1f4_2 = self::mul($f1_2, $f4, 26); |
|
| 619 | - $f1f5_4 = self::mul($f1_2, $f5_2, 26); |
|
| 620 | - $f1f6_2 = self::mul($f1_2, $f6, 26); |
|
| 621 | - $f1f7_4 = self::mul($f1_2, $f7_2, 26); |
|
| 622 | - $f1f8_2 = self::mul($f1_2, $f8, 26); |
|
| 623 | - $f1f9_76 = self::mul($f9_38, $f1_2, 27); |
|
| 624 | - $f2f2 = self::mul($f2, $f2, 27); |
|
| 625 | - $f2f3_2 = self::mul($f2_2, $f3, 27); |
|
| 626 | - $f2f4_2 = self::mul($f2_2, $f4, 27); |
|
| 627 | - $f2f5_2 = self::mul($f2_2, $f5, 27); |
|
| 628 | - $f2f6_2 = self::mul($f2_2, $f6, 27); |
|
| 629 | - $f2f7_2 = self::mul($f2_2, $f7, 27); |
|
| 630 | - $f2f8_38 = self::mul($f8_19, $f2_2, 27); |
|
| 631 | - $f2f9_38 = self::mul($f9_38, $f2, 26); |
|
| 632 | - $f3f3_2 = self::mul($f3_2, $f3, 26); |
|
| 633 | - $f3f4_2 = self::mul($f3_2, $f4, 26); |
|
| 634 | - $f3f5_4 = self::mul($f3_2, $f5_2, 26); |
|
| 635 | - $f3f6_2 = self::mul($f3_2, $f6, 26); |
|
| 636 | - $f3f7_76 = self::mul($f7_38, $f3_2, 26); |
|
| 637 | - $f3f8_38 = self::mul($f8_19, $f3_2, 26); |
|
| 638 | - $f3f9_76 = self::mul($f9_38, $f3_2, 26); |
|
| 639 | - $f4f4 = self::mul($f4, $f4, 26); |
|
| 640 | - $f4f5_2 = self::mul($f4_2, $f5, 26); |
|
| 641 | - $f4f6_38 = self::mul($f6_19, $f4_2, 27); |
|
| 642 | - $f4f7_38 = self::mul($f7_38, $f4, 26); |
|
| 643 | - $f4f8_38 = self::mul($f8_19, $f4_2, 27); |
|
| 644 | - $f4f9_38 = self::mul($f9_38, $f4, 26); |
|
| 645 | - $f5f5_38 = self::mul($f5_38, $f5, 26); |
|
| 646 | - $f5f6_38 = self::mul($f6_19, $f5_2, 26); |
|
| 647 | - $f5f7_76 = self::mul($f7_38, $f5_2, 26); |
|
| 648 | - $f5f8_38 = self::mul($f8_19, $f5_2, 26); |
|
| 649 | - $f5f9_76 = self::mul($f9_38, $f5_2, 26); |
|
| 650 | - $f6f6_19 = self::mul($f6_19, $f6, 26); |
|
| 651 | - $f6f7_38 = self::mul($f7_38, $f6, 26); |
|
| 652 | - $f6f8_38 = self::mul($f8_19, $f6_2, 27); |
|
| 653 | - $f6f9_38 = self::mul($f9_38, $f6, 26); |
|
| 654 | - $f7f7_38 = self::mul($f7_38, $f7, 26); |
|
| 655 | - $f7f8_38 = self::mul($f8_19, $f7_2, 26); |
|
| 656 | - $f7f9_76 = self::mul($f9_38, $f7_2, 26); |
|
| 657 | - $f8f8_19 = self::mul($f8_19, $f8, 26); |
|
| 658 | - $f8f9_38 = self::mul($f9_38, $f8, 26); |
|
| 659 | - $f9f9_38 = self::mul($f9_38, $f9, 26); |
|
| 660 | - $h0 = $f0f0 + $f1f9_76 + $f2f8_38 + $f3f7_76 + $f4f6_38 + $f5f5_38; |
|
| 661 | - $h1 = $f0f1_2 + $f2f9_38 + $f3f8_38 + $f4f7_38 + $f5f6_38; |
|
| 662 | - $h2 = $f0f2_2 + $f1f1_2 + $f3f9_76 + $f4f8_38 + $f5f7_76 + $f6f6_19; |
|
| 663 | - $h3 = $f0f3_2 + $f1f2_2 + $f4f9_38 + $f5f8_38 + $f6f7_38; |
|
| 664 | - $h4 = $f0f4_2 + $f1f3_4 + $f2f2 + $f5f9_76 + $f6f8_38 + $f7f7_38; |
|
| 665 | - $h5 = $f0f5_2 + $f1f4_2 + $f2f3_2 + $f6f9_38 + $f7f8_38; |
|
| 666 | - $h6 = $f0f6_2 + $f1f5_4 + $f2f4_2 + $f3f3_2 + $f7f9_76 + $f8f8_19; |
|
| 667 | - $h7 = $f0f7_2 + $f1f6_2 + $f2f5_2 + $f3f4_2 + $f8f9_38; |
|
| 668 | - $h8 = $f0f8_2 + $f1f7_4 + $f2f6_2 + $f3f5_4 + $f4f4 + $f9f9_38; |
|
| 669 | - $h9 = $f0f9_2 + $f1f8_2 + $f2f7_2 + $f3f6_2 + $f4f5_2; |
|
| 670 | - |
|
| 671 | - $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 672 | - $h1 += $carry0; |
|
| 673 | - $h0 -= $carry0 << 26; |
|
| 674 | - $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 675 | - $h5 += $carry4; |
|
| 676 | - $h4 -= $carry4 << 26; |
|
| 677 | - |
|
| 678 | - $carry1 = ($h1 + (1 << 24)) >> 25; |
|
| 679 | - $h2 += $carry1; |
|
| 680 | - $h1 -= $carry1 << 25; |
|
| 681 | - $carry5 = ($h5 + (1 << 24)) >> 25; |
|
| 682 | - $h6 += $carry5; |
|
| 683 | - $h5 -= $carry5 << 25; |
|
| 684 | - |
|
| 685 | - $carry2 = ($h2 + (1 << 25)) >> 26; |
|
| 686 | - $h3 += $carry2; |
|
| 687 | - $h2 -= $carry2 << 26; |
|
| 688 | - $carry6 = ($h6 + (1 << 25)) >> 26; |
|
| 689 | - $h7 += $carry6; |
|
| 690 | - $h6 -= $carry6 << 26; |
|
| 691 | - |
|
| 692 | - $carry3 = ($h3 + (1 << 24)) >> 25; |
|
| 693 | - $h4 += $carry3; |
|
| 694 | - $h3 -= $carry3 << 25; |
|
| 695 | - $carry7 = ($h7 + (1 << 24)) >> 25; |
|
| 696 | - $h8 += $carry7; |
|
| 697 | - $h7 -= $carry7 << 25; |
|
| 698 | - |
|
| 699 | - $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 700 | - $h5 += $carry4; |
|
| 701 | - $h4 -= $carry4 << 26; |
|
| 702 | - $carry8 = ($h8 + (1 << 25)) >> 26; |
|
| 703 | - $h9 += $carry8; |
|
| 704 | - $h8 -= $carry8 << 26; |
|
| 705 | - |
|
| 706 | - $carry9 = ($h9 + (1 << 24)) >> 25; |
|
| 707 | - $h0 += self::mul($carry9, 19, 5); |
|
| 708 | - $h9 -= $carry9 << 25; |
|
| 709 | - |
|
| 710 | - $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 711 | - $h1 += $carry0; |
|
| 712 | - $h0 -= $carry0 << 26; |
|
| 713 | - |
|
| 714 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 715 | - array( |
|
| 716 | - (int) $h0, |
|
| 717 | - (int) $h1, |
|
| 718 | - (int) $h2, |
|
| 719 | - (int) $h3, |
|
| 720 | - (int) $h4, |
|
| 721 | - (int) $h5, |
|
| 722 | - (int) $h6, |
|
| 723 | - (int) $h7, |
|
| 724 | - (int) $h8, |
|
| 725 | - (int) $h9 |
|
| 726 | - ) |
|
| 727 | - ); |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - |
|
| 731 | - /** |
|
| 732 | - * Square and double a field element |
|
| 733 | - * |
|
| 734 | - * h = 2 * f * f |
|
| 735 | - * |
|
| 736 | - * @internal You should not use this directly from another application |
|
| 737 | - * |
|
| 738 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 739 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 740 | - */ |
|
| 741 | - public static function fe_sq2(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 742 | - { |
|
| 743 | - $f0 = (int) $f[0]; |
|
| 744 | - $f1 = (int) $f[1]; |
|
| 745 | - $f2 = (int) $f[2]; |
|
| 746 | - $f3 = (int) $f[3]; |
|
| 747 | - $f4 = (int) $f[4]; |
|
| 748 | - $f5 = (int) $f[5]; |
|
| 749 | - $f6 = (int) $f[6]; |
|
| 750 | - $f7 = (int) $f[7]; |
|
| 751 | - $f8 = (int) $f[8]; |
|
| 752 | - $f9 = (int) $f[9]; |
|
| 753 | - |
|
| 754 | - $f0_2 = $f0 << 1; |
|
| 755 | - $f1_2 = $f1 << 1; |
|
| 756 | - $f2_2 = $f2 << 1; |
|
| 757 | - $f3_2 = $f3 << 1; |
|
| 758 | - $f4_2 = $f4 << 1; |
|
| 759 | - $f5_2 = $f5 << 1; |
|
| 760 | - $f6_2 = $f6 << 1; |
|
| 761 | - $f7_2 = $f7 << 1; |
|
| 762 | - $f5_38 = self::mul($f5, 38, 6); /* 1.959375*2^30 */ |
|
| 763 | - $f6_19 = self::mul($f6, 19, 5); /* 1.959375*2^30 */ |
|
| 764 | - $f7_38 = self::mul($f7, 38, 6); /* 1.959375*2^30 */ |
|
| 765 | - $f8_19 = self::mul($f8, 19, 5); /* 1.959375*2^30 */ |
|
| 766 | - $f9_38 = self::mul($f9, 38, 6); /* 1.959375*2^30 */ |
|
| 767 | - $f0f0 = self::mul($f0, $f0, 24); |
|
| 768 | - $f0f1_2 = self::mul($f0_2, $f1, 24); |
|
| 769 | - $f0f2_2 = self::mul($f0_2, $f2, 24); |
|
| 770 | - $f0f3_2 = self::mul($f0_2, $f3, 24); |
|
| 771 | - $f0f4_2 = self::mul($f0_2, $f4, 24); |
|
| 772 | - $f0f5_2 = self::mul($f0_2, $f5, 24); |
|
| 773 | - $f0f6_2 = self::mul($f0_2, $f6, 24); |
|
| 774 | - $f0f7_2 = self::mul($f0_2, $f7, 24); |
|
| 775 | - $f0f8_2 = self::mul($f0_2, $f8, 24); |
|
| 776 | - $f0f9_2 = self::mul($f0_2, $f9, 24); |
|
| 777 | - $f1f1_2 = self::mul($f1_2, $f1, 24); |
|
| 778 | - $f1f2_2 = self::mul($f1_2, $f2, 24); |
|
| 779 | - $f1f3_4 = self::mul($f1_2, $f3_2, 24); |
|
| 780 | - $f1f4_2 = self::mul($f1_2, $f4, 24); |
|
| 781 | - $f1f5_4 = self::mul($f1_2, $f5_2, 24); |
|
| 782 | - $f1f6_2 = self::mul($f1_2, $f6, 24); |
|
| 783 | - $f1f7_4 = self::mul($f1_2, $f7_2, 24); |
|
| 784 | - $f1f8_2 = self::mul($f1_2, $f8, 24); |
|
| 785 | - $f1f9_76 = self::mul($f9_38, $f1_2, 24); |
|
| 786 | - $f2f2 = self::mul($f2, $f2, 24); |
|
| 787 | - $f2f3_2 = self::mul($f2_2, $f3, 24); |
|
| 788 | - $f2f4_2 = self::mul($f2_2, $f4, 24); |
|
| 789 | - $f2f5_2 = self::mul($f2_2, $f5, 24); |
|
| 790 | - $f2f6_2 = self::mul($f2_2, $f6, 24); |
|
| 791 | - $f2f7_2 = self::mul($f2_2, $f7, 24); |
|
| 792 | - $f2f8_38 = self::mul($f8_19, $f2_2, 25); |
|
| 793 | - $f2f9_38 = self::mul($f9_38, $f2, 24); |
|
| 794 | - $f3f3_2 = self::mul($f3_2, $f3, 24); |
|
| 795 | - $f3f4_2 = self::mul($f3_2, $f4, 24); |
|
| 796 | - $f3f5_4 = self::mul($f3_2, $f5_2, 24); |
|
| 797 | - $f3f6_2 = self::mul($f3_2, $f6, 24); |
|
| 798 | - $f3f7_76 = self::mul($f7_38, $f3_2, 24); |
|
| 799 | - $f3f8_38 = self::mul($f8_19, $f3_2, 24); |
|
| 800 | - $f3f9_76 = self::mul($f9_38, $f3_2, 24); |
|
| 801 | - $f4f4 = self::mul($f4, $f4, 24); |
|
| 802 | - $f4f5_2 = self::mul($f4_2, $f5, 24); |
|
| 803 | - $f4f6_38 = self::mul($f6_19, $f4_2, 25); |
|
| 804 | - $f4f7_38 = self::mul($f7_38, $f4, 24); |
|
| 805 | - $f4f8_38 = self::mul($f8_19, $f4_2, 25); |
|
| 806 | - $f4f9_38 = self::mul($f9_38, $f4, 24); |
|
| 807 | - $f5f5_38 = self::mul($f5_38, $f5, 24); |
|
| 808 | - $f5f6_38 = self::mul($f6_19, $f5_2, 24); |
|
| 809 | - $f5f7_76 = self::mul($f7_38, $f5_2, 24); |
|
| 810 | - $f5f8_38 = self::mul($f8_19, $f5_2, 24); |
|
| 811 | - $f5f9_76 = self::mul($f9_38, $f5_2, 24); |
|
| 812 | - $f6f6_19 = self::mul($f6_19, $f6, 24); |
|
| 813 | - $f6f7_38 = self::mul($f7_38, $f6, 24); |
|
| 814 | - $f6f8_38 = self::mul($f8_19, $f6_2, 25); |
|
| 815 | - $f6f9_38 = self::mul($f9_38, $f6, 24); |
|
| 816 | - $f7f7_38 = self::mul($f7_38, $f7, 24); |
|
| 817 | - $f7f8_38 = self::mul($f8_19, $f7_2, 24); |
|
| 818 | - $f7f9_76 = self::mul($f9_38, $f7_2, 24); |
|
| 819 | - $f8f8_19 = self::mul($f8_19, $f8, 24); |
|
| 820 | - $f8f9_38 = self::mul($f9_38, $f8, 24); |
|
| 821 | - $f9f9_38 = self::mul($f9_38, $f9, 24); |
|
| 822 | - |
|
| 823 | - $h0 = (int) ($f0f0 + $f1f9_76 + $f2f8_38 + $f3f7_76 + $f4f6_38 + $f5f5_38) << 1; |
|
| 824 | - $h1 = (int) ($f0f1_2 + $f2f9_38 + $f3f8_38 + $f4f7_38 + $f5f6_38) << 1; |
|
| 825 | - $h2 = (int) ($f0f2_2 + $f1f1_2 + $f3f9_76 + $f4f8_38 + $f5f7_76 + $f6f6_19) << 1; |
|
| 826 | - $h3 = (int) ($f0f3_2 + $f1f2_2 + $f4f9_38 + $f5f8_38 + $f6f7_38) << 1; |
|
| 827 | - $h4 = (int) ($f0f4_2 + $f1f3_4 + $f2f2 + $f5f9_76 + $f6f8_38 + $f7f7_38) << 1; |
|
| 828 | - $h5 = (int) ($f0f5_2 + $f1f4_2 + $f2f3_2 + $f6f9_38 + $f7f8_38) << 1; |
|
| 829 | - $h6 = (int) ($f0f6_2 + $f1f5_4 + $f2f4_2 + $f3f3_2 + $f7f9_76 + $f8f8_19) << 1; |
|
| 830 | - $h7 = (int) ($f0f7_2 + $f1f6_2 + $f2f5_2 + $f3f4_2 + $f8f9_38) << 1; |
|
| 831 | - $h8 = (int) ($f0f8_2 + $f1f7_4 + $f2f6_2 + $f3f5_4 + $f4f4 + $f9f9_38) << 1; |
|
| 832 | - $h9 = (int) ($f0f9_2 + $f1f8_2 + $f2f7_2 + $f3f6_2 + $f4f5_2) << 1; |
|
| 833 | - |
|
| 834 | - $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 835 | - $h1 += $carry0; |
|
| 836 | - $h0 -= $carry0 << 26; |
|
| 837 | - $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 838 | - $h5 += $carry4; |
|
| 839 | - $h4 -= $carry4 << 26; |
|
| 840 | - |
|
| 841 | - $carry1 = ($h1 + (1 << 24)) >> 25; |
|
| 842 | - $h2 += $carry1; |
|
| 843 | - $h1 -= $carry1 << 25; |
|
| 844 | - $carry5 = ($h5 + (1 << 24)) >> 25; |
|
| 845 | - $h6 += $carry5; |
|
| 846 | - $h5 -= $carry5 << 25; |
|
| 847 | - |
|
| 848 | - $carry2 = ($h2 + (1 << 25)) >> 26; |
|
| 849 | - $h3 += $carry2; |
|
| 850 | - $h2 -= $carry2 << 26; |
|
| 851 | - $carry6 = ($h6 + (1 << 25)) >> 26; |
|
| 852 | - $h7 += $carry6; |
|
| 853 | - $h6 -= $carry6 << 26; |
|
| 854 | - |
|
| 855 | - $carry3 = ($h3 + (1 << 24)) >> 25; |
|
| 856 | - $h4 += $carry3; |
|
| 857 | - $h3 -= $carry3 << 25; |
|
| 858 | - $carry7 = ($h7 + (1 << 24)) >> 25; |
|
| 859 | - $h8 += $carry7; |
|
| 860 | - $h7 -= $carry7 << 25; |
|
| 861 | - |
|
| 862 | - $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 863 | - $h5 += $carry4; |
|
| 864 | - $h4 -= $carry4 << 26; |
|
| 865 | - $carry8 = ($h8 + (1 << 25)) >> 26; |
|
| 866 | - $h9 += $carry8; |
|
| 867 | - $h8 -= $carry8 << 26; |
|
| 868 | - |
|
| 869 | - $carry9 = ($h9 + (1 << 24)) >> 25; |
|
| 870 | - $h0 += self::mul($carry9, 19, 5); |
|
| 871 | - $h9 -= $carry9 << 25; |
|
| 872 | - |
|
| 873 | - $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 874 | - $h1 += $carry0; |
|
| 875 | - $h0 -= $carry0 << 26; |
|
| 876 | - |
|
| 877 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 878 | - array( |
|
| 879 | - (int) $h0, |
|
| 880 | - (int) $h1, |
|
| 881 | - (int) $h2, |
|
| 882 | - (int) $h3, |
|
| 883 | - (int) $h4, |
|
| 884 | - (int) $h5, |
|
| 885 | - (int) $h6, |
|
| 886 | - (int) $h7, |
|
| 887 | - (int) $h8, |
|
| 888 | - (int) $h9 |
|
| 889 | - ) |
|
| 890 | - ); |
|
| 891 | - } |
|
| 892 | - |
|
| 893 | - /** |
|
| 894 | - * @internal You should not use this directly from another application |
|
| 895 | - * |
|
| 896 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $Z |
|
| 897 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 898 | - */ |
|
| 899 | - public static function fe_invert(ParagonIE_Sodium_Core_Curve25519_Fe $Z) |
|
| 900 | - { |
|
| 901 | - $z = clone $Z; |
|
| 902 | - $t0 = self::fe_sq($z); |
|
| 903 | - $t1 = self::fe_sq($t0); |
|
| 904 | - $t1 = self::fe_sq($t1); |
|
| 905 | - $t1 = self::fe_mul($z, $t1); |
|
| 906 | - $t0 = self::fe_mul($t0, $t1); |
|
| 907 | - $t2 = self::fe_sq($t0); |
|
| 908 | - $t1 = self::fe_mul($t1, $t2); |
|
| 909 | - $t2 = self::fe_sq($t1); |
|
| 910 | - for ($i = 1; $i < 5; ++$i) { |
|
| 911 | - $t2 = self::fe_sq($t2); |
|
| 912 | - } |
|
| 913 | - $t1 = self::fe_mul($t2, $t1); |
|
| 914 | - $t2 = self::fe_sq($t1); |
|
| 915 | - for ($i = 1; $i < 10; ++$i) { |
|
| 916 | - $t2 = self::fe_sq($t2); |
|
| 917 | - } |
|
| 918 | - $t2 = self::fe_mul($t2, $t1); |
|
| 919 | - $t3 = self::fe_sq($t2); |
|
| 920 | - for ($i = 1; $i < 20; ++$i) { |
|
| 921 | - $t3 = self::fe_sq($t3); |
|
| 922 | - } |
|
| 923 | - $t2 = self::fe_mul($t3, $t2); |
|
| 924 | - $t2 = self::fe_sq($t2); |
|
| 925 | - for ($i = 1; $i < 10; ++$i) { |
|
| 926 | - $t2 = self::fe_sq($t2); |
|
| 927 | - } |
|
| 928 | - $t1 = self::fe_mul($t2, $t1); |
|
| 929 | - $t2 = self::fe_sq($t1); |
|
| 930 | - for ($i = 1; $i < 50; ++$i) { |
|
| 931 | - $t2 = self::fe_sq($t2); |
|
| 932 | - } |
|
| 933 | - $t2 = self::fe_mul($t2, $t1); |
|
| 934 | - $t3 = self::fe_sq($t2); |
|
| 935 | - for ($i = 1; $i < 100; ++$i) { |
|
| 936 | - $t3 = self::fe_sq($t3); |
|
| 937 | - } |
|
| 938 | - $t2 = self::fe_mul($t3, $t2); |
|
| 939 | - $t2 = self::fe_sq($t2); |
|
| 940 | - for ($i = 1; $i < 50; ++$i) { |
|
| 941 | - $t2 = self::fe_sq($t2); |
|
| 942 | - } |
|
| 943 | - $t1 = self::fe_mul($t2, $t1); |
|
| 944 | - $t1 = self::fe_sq($t1); |
|
| 945 | - for ($i = 1; $i < 5; ++$i) { |
|
| 946 | - $t1 = self::fe_sq($t1); |
|
| 947 | - } |
|
| 948 | - return self::fe_mul($t1, $t0); |
|
| 949 | - } |
|
| 950 | - |
|
| 951 | - /** |
|
| 952 | - * @internal You should not use this directly from another application |
|
| 953 | - * |
|
| 954 | - * @ref https://github.com/jedisct1/libsodium/blob/68564326e1e9dc57ef03746f85734232d20ca6fb/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c#L1054-L1106 |
|
| 955 | - * |
|
| 956 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $z |
|
| 957 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 958 | - */ |
|
| 959 | - public static function fe_pow22523(ParagonIE_Sodium_Core_Curve25519_Fe $z) |
|
| 960 | - { |
|
| 961 | - # fe_sq(t0, z); |
|
| 962 | - # fe_sq(t1, t0); |
|
| 963 | - # fe_sq(t1, t1); |
|
| 964 | - # fe_mul(t1, z, t1); |
|
| 965 | - # fe_mul(t0, t0, t1); |
|
| 966 | - # fe_sq(t0, t0); |
|
| 967 | - # fe_mul(t0, t1, t0); |
|
| 968 | - # fe_sq(t1, t0); |
|
| 969 | - $t0 = self::fe_sq($z); |
|
| 970 | - $t1 = self::fe_sq($t0); |
|
| 971 | - $t1 = self::fe_sq($t1); |
|
| 972 | - $t1 = self::fe_mul($z, $t1); |
|
| 973 | - $t0 = self::fe_mul($t0, $t1); |
|
| 974 | - $t0 = self::fe_sq($t0); |
|
| 975 | - $t0 = self::fe_mul($t1, $t0); |
|
| 976 | - $t1 = self::fe_sq($t0); |
|
| 977 | - |
|
| 978 | - # for (i = 1; i < 5; ++i) { |
|
| 979 | - # fe_sq(t1, t1); |
|
| 980 | - # } |
|
| 981 | - for ($i = 1; $i < 5; ++$i) { |
|
| 982 | - $t1 = self::fe_sq($t1); |
|
| 983 | - } |
|
| 984 | - |
|
| 985 | - # fe_mul(t0, t1, t0); |
|
| 986 | - # fe_sq(t1, t0); |
|
| 987 | - $t0 = self::fe_mul($t1, $t0); |
|
| 988 | - $t1 = self::fe_sq($t0); |
|
| 989 | - |
|
| 990 | - # for (i = 1; i < 10; ++i) { |
|
| 991 | - # fe_sq(t1, t1); |
|
| 992 | - # } |
|
| 993 | - for ($i = 1; $i < 10; ++$i) { |
|
| 994 | - $t1 = self::fe_sq($t1); |
|
| 995 | - } |
|
| 996 | - |
|
| 997 | - # fe_mul(t1, t1, t0); |
|
| 998 | - # fe_sq(t2, t1); |
|
| 999 | - $t1 = self::fe_mul($t1, $t0); |
|
| 1000 | - $t2 = self::fe_sq($t1); |
|
| 1001 | - |
|
| 1002 | - # for (i = 1; i < 20; ++i) { |
|
| 1003 | - # fe_sq(t2, t2); |
|
| 1004 | - # } |
|
| 1005 | - for ($i = 1; $i < 20; ++$i) { |
|
| 1006 | - $t2 = self::fe_sq($t2); |
|
| 1007 | - } |
|
| 1008 | - |
|
| 1009 | - # fe_mul(t1, t2, t1); |
|
| 1010 | - # fe_sq(t1, t1); |
|
| 1011 | - $t1 = self::fe_mul($t2, $t1); |
|
| 1012 | - $t1 = self::fe_sq($t1); |
|
| 1013 | - |
|
| 1014 | - # for (i = 1; i < 10; ++i) { |
|
| 1015 | - # fe_sq(t1, t1); |
|
| 1016 | - # } |
|
| 1017 | - for ($i = 1; $i < 10; ++$i) { |
|
| 1018 | - $t1 = self::fe_sq($t1); |
|
| 1019 | - } |
|
| 1020 | - |
|
| 1021 | - # fe_mul(t0, t1, t0); |
|
| 1022 | - # fe_sq(t1, t0); |
|
| 1023 | - $t0 = self::fe_mul($t1, $t0); |
|
| 1024 | - $t1 = self::fe_sq($t0); |
|
| 1025 | - |
|
| 1026 | - # for (i = 1; i < 50; ++i) { |
|
| 1027 | - # fe_sq(t1, t1); |
|
| 1028 | - # } |
|
| 1029 | - for ($i = 1; $i < 50; ++$i) { |
|
| 1030 | - $t1 = self::fe_sq($t1); |
|
| 1031 | - } |
|
| 1032 | - |
|
| 1033 | - # fe_mul(t1, t1, t0); |
|
| 1034 | - # fe_sq(t2, t1); |
|
| 1035 | - $t1 = self::fe_mul($t1, $t0); |
|
| 1036 | - $t2 = self::fe_sq($t1); |
|
| 1037 | - |
|
| 1038 | - # for (i = 1; i < 100; ++i) { |
|
| 1039 | - # fe_sq(t2, t2); |
|
| 1040 | - # } |
|
| 1041 | - for ($i = 1; $i < 100; ++$i) { |
|
| 1042 | - $t2 = self::fe_sq($t2); |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - # fe_mul(t1, t2, t1); |
|
| 1046 | - # fe_sq(t1, t1); |
|
| 1047 | - $t1 = self::fe_mul($t2, $t1); |
|
| 1048 | - $t1 = self::fe_sq($t1); |
|
| 1049 | - |
|
| 1050 | - # for (i = 1; i < 50; ++i) { |
|
| 1051 | - # fe_sq(t1, t1); |
|
| 1052 | - # } |
|
| 1053 | - for ($i = 1; $i < 50; ++$i) { |
|
| 1054 | - $t1 = self::fe_sq($t1); |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - # fe_mul(t0, t1, t0); |
|
| 1058 | - # fe_sq(t0, t0); |
|
| 1059 | - # fe_sq(t0, t0); |
|
| 1060 | - # fe_mul(out, t0, z); |
|
| 1061 | - $t0 = self::fe_mul($t1, $t0); |
|
| 1062 | - $t0 = self::fe_sq($t0); |
|
| 1063 | - $t0 = self::fe_sq($t0); |
|
| 1064 | - return self::fe_mul($t0, $z); |
|
| 1065 | - } |
|
| 1066 | - |
|
| 1067 | - /** |
|
| 1068 | - * Subtract two field elements. |
|
| 1069 | - * |
|
| 1070 | - * h = f - g |
|
| 1071 | - * |
|
| 1072 | - * Preconditions: |
|
| 1073 | - * |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. |
|
| 1074 | - * |g| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. |
|
| 1075 | - * |
|
| 1076 | - * Postconditions: |
|
| 1077 | - * |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. |
|
| 1078 | - * |
|
| 1079 | - * @internal You should not use this directly from another application |
|
| 1080 | - * |
|
| 1081 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 1082 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 1083 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 1084 | - * @psalm-suppress MixedOperand |
|
| 1085 | - */ |
|
| 1086 | - public static function fe_sub(ParagonIE_Sodium_Core_Curve25519_Fe $f, ParagonIE_Sodium_Core_Curve25519_Fe $g) |
|
| 1087 | - { |
|
| 1088 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 1089 | - array( |
|
| 1090 | - (int) ($f[0] - $g[0]), |
|
| 1091 | - (int) ($f[1] - $g[1]), |
|
| 1092 | - (int) ($f[2] - $g[2]), |
|
| 1093 | - (int) ($f[3] - $g[3]), |
|
| 1094 | - (int) ($f[4] - $g[4]), |
|
| 1095 | - (int) ($f[5] - $g[5]), |
|
| 1096 | - (int) ($f[6] - $g[6]), |
|
| 1097 | - (int) ($f[7] - $g[7]), |
|
| 1098 | - (int) ($f[8] - $g[8]), |
|
| 1099 | - (int) ($f[9] - $g[9]) |
|
| 1100 | - ) |
|
| 1101 | - ); |
|
| 1102 | - } |
|
| 1103 | - |
|
| 1104 | - /** |
|
| 1105 | - * Add two group elements. |
|
| 1106 | - * |
|
| 1107 | - * r = p + q |
|
| 1108 | - * |
|
| 1109 | - * @internal You should not use this directly from another application |
|
| 1110 | - * |
|
| 1111 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1112 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q |
|
| 1113 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1114 | - */ |
|
| 1115 | - public static function ge_add( |
|
| 1116 | - ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, |
|
| 1117 | - ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q |
|
| 1118 | - ) { |
|
| 1119 | - $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); |
|
| 1120 | - $r->X = self::fe_add($p->Y, $p->X); |
|
| 1121 | - $r->Y = self::fe_sub($p->Y, $p->X); |
|
| 1122 | - $r->Z = self::fe_mul($r->X, $q->YplusX); |
|
| 1123 | - $r->Y = self::fe_mul($r->Y, $q->YminusX); |
|
| 1124 | - $r->T = self::fe_mul($q->T2d, $p->T); |
|
| 1125 | - $r->X = self::fe_mul($p->Z, $q->Z); |
|
| 1126 | - $t0 = self::fe_add($r->X, $r->X); |
|
| 1127 | - $r->X = self::fe_sub($r->Z, $r->Y); |
|
| 1128 | - $r->Y = self::fe_add($r->Z, $r->Y); |
|
| 1129 | - $r->Z = self::fe_add($t0, $r->T); |
|
| 1130 | - $r->T = self::fe_sub($t0, $r->T); |
|
| 1131 | - return $r; |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - /** |
|
| 1135 | - * @internal You should not use this directly from another application |
|
| 1136 | - * |
|
| 1137 | - * @ref https://github.com/jedisct1/libsodium/blob/157c4a80c13b117608aeae12178b2d38825f9f8f/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c#L1185-L1215 |
|
| 1138 | - * @param string $a |
|
| 1139 | - * @return array<int, mixed> |
|
| 1140 | - * @throws SodiumException |
|
| 1141 | - * @throws TypeError |
|
| 1142 | - */ |
|
| 1143 | - public static function slide($a) |
|
| 1144 | - { |
|
| 1145 | - if (self::strlen($a) < 256) { |
|
| 1146 | - if (self::strlen($a) < 16) { |
|
| 1147 | - $a = str_pad($a, 256, '0', STR_PAD_RIGHT); |
|
| 1148 | - } |
|
| 1149 | - } |
|
| 1150 | - /** @var array<int, int> $r */ |
|
| 1151 | - $r = array(); |
|
| 1152 | - |
|
| 1153 | - /** @var int $i */ |
|
| 1154 | - for ($i = 0; $i < 256; ++$i) { |
|
| 1155 | - $r[$i] = (int) ( |
|
| 1156 | - 1 & ( |
|
| 1157 | - self::chrToInt($a[(int) ($i >> 3)]) |
|
| 1158 | - >> |
|
| 1159 | - ($i & 7) |
|
| 1160 | - ) |
|
| 1161 | - ); |
|
| 1162 | - } |
|
| 1163 | - |
|
| 1164 | - for ($i = 0;$i < 256;++$i) { |
|
| 1165 | - if ($r[$i]) { |
|
| 1166 | - for ($b = 1;$b <= 6 && $i + $b < 256;++$b) { |
|
| 1167 | - if ($r[$i + $b]) { |
|
| 1168 | - if ($r[$i] + ($r[$i + $b] << $b) <= 15) { |
|
| 1169 | - $r[$i] += $r[$i + $b] << $b; |
|
| 1170 | - $r[$i + $b] = 0; |
|
| 1171 | - } elseif ($r[$i] - ($r[$i + $b] << $b) >= -15) { |
|
| 1172 | - $r[$i] -= $r[$i + $b] << $b; |
|
| 1173 | - for ($k = $i + $b; $k < 256; ++$k) { |
|
| 1174 | - if (!$r[$k]) { |
|
| 1175 | - $r[$k] = 1; |
|
| 1176 | - break; |
|
| 1177 | - } |
|
| 1178 | - $r[$k] = 0; |
|
| 1179 | - } |
|
| 1180 | - } else { |
|
| 1181 | - break; |
|
| 1182 | - } |
|
| 1183 | - } |
|
| 1184 | - } |
|
| 1185 | - } |
|
| 1186 | - } |
|
| 1187 | - return $r; |
|
| 1188 | - } |
|
| 1189 | - |
|
| 1190 | - /** |
|
| 1191 | - * @internal You should not use this directly from another application |
|
| 1192 | - * |
|
| 1193 | - * @param string $s |
|
| 1194 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 1195 | - * @throws SodiumException |
|
| 1196 | - * @throws TypeError |
|
| 1197 | - */ |
|
| 1198 | - public static function ge_frombytes_negate_vartime($s) |
|
| 1199 | - { |
|
| 1200 | - static $d = null; |
|
| 1201 | - if (!$d) { |
|
| 1202 | - $d = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d); |
|
| 1203 | - } |
|
| 1204 | - |
|
| 1205 | - # fe_frombytes(h->Y,s); |
|
| 1206 | - # fe_1(h->Z); |
|
| 1207 | - $h = new ParagonIE_Sodium_Core_Curve25519_Ge_P3( |
|
| 1208 | - self::fe_0(), |
|
| 1209 | - self::fe_frombytes($s), |
|
| 1210 | - self::fe_1() |
|
| 1211 | - ); |
|
| 1212 | - |
|
| 1213 | - # fe_sq(u,h->Y); |
|
| 1214 | - # fe_mul(v,u,d); |
|
| 1215 | - # fe_sub(u,u,h->Z); /* u = y^2-1 */ |
|
| 1216 | - # fe_add(v,v,h->Z); /* v = dy^2+1 */ |
|
| 1217 | - $u = self::fe_sq($h->Y); |
|
| 1218 | - /** @var ParagonIE_Sodium_Core_Curve25519_Fe $d */ |
|
| 1219 | - $v = self::fe_mul($u, $d); |
|
| 1220 | - $u = self::fe_sub($u, $h->Z); /* u = y^2 - 1 */ |
|
| 1221 | - $v = self::fe_add($v, $h->Z); /* v = dy^2 + 1 */ |
|
| 1222 | - |
|
| 1223 | - # fe_sq(v3,v); |
|
| 1224 | - # fe_mul(v3,v3,v); /* v3 = v^3 */ |
|
| 1225 | - # fe_sq(h->X,v3); |
|
| 1226 | - # fe_mul(h->X,h->X,v); |
|
| 1227 | - # fe_mul(h->X,h->X,u); /* x = uv^7 */ |
|
| 1228 | - $v3 = self::fe_sq($v); |
|
| 1229 | - $v3 = self::fe_mul($v3, $v); /* v3 = v^3 */ |
|
| 1230 | - $h->X = self::fe_sq($v3); |
|
| 1231 | - $h->X = self::fe_mul($h->X, $v); |
|
| 1232 | - $h->X = self::fe_mul($h->X, $u); /* x = uv^7 */ |
|
| 1233 | - |
|
| 1234 | - # fe_pow22523(h->X,h->X); /* x = (uv^7)^((q-5)/8) */ |
|
| 1235 | - # fe_mul(h->X,h->X,v3); |
|
| 1236 | - # fe_mul(h->X,h->X,u); /* x = uv^3(uv^7)^((q-5)/8) */ |
|
| 1237 | - $h->X = self::fe_pow22523($h->X); /* x = (uv^7)^((q-5)/8) */ |
|
| 1238 | - $h->X = self::fe_mul($h->X, $v3); |
|
| 1239 | - $h->X = self::fe_mul($h->X, $u); /* x = uv^3(uv^7)^((q-5)/8) */ |
|
| 1240 | - |
|
| 1241 | - # fe_sq(vxx,h->X); |
|
| 1242 | - # fe_mul(vxx,vxx,v); |
|
| 1243 | - # fe_sub(check,vxx,u); /* vx^2-u */ |
|
| 1244 | - $vxx = self::fe_sq($h->X); |
|
| 1245 | - $vxx = self::fe_mul($vxx, $v); |
|
| 1246 | - $check = self::fe_sub($vxx, $u); /* vx^2 - u */ |
|
| 1247 | - |
|
| 1248 | - # if (fe_isnonzero(check)) { |
|
| 1249 | - # fe_add(check,vxx,u); /* vx^2+u */ |
|
| 1250 | - # if (fe_isnonzero(check)) { |
|
| 1251 | - # return -1; |
|
| 1252 | - # } |
|
| 1253 | - # fe_mul(h->X,h->X,sqrtm1); |
|
| 1254 | - # } |
|
| 1255 | - if (self::fe_isnonzero($check)) { |
|
| 1256 | - $check = self::fe_add($vxx, $u); /* vx^2 + u */ |
|
| 1257 | - if (self::fe_isnonzero($check)) { |
|
| 1258 | - throw new RangeException('Internal check failed.'); |
|
| 1259 | - } |
|
| 1260 | - $h->X = self::fe_mul( |
|
| 1261 | - $h->X, |
|
| 1262 | - ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1) |
|
| 1263 | - ); |
|
| 1264 | - } |
|
| 1265 | - |
|
| 1266 | - # if (fe_isnegative(h->X) == (s[31] >> 7)) { |
|
| 1267 | - # fe_neg(h->X,h->X); |
|
| 1268 | - # } |
|
| 1269 | - $i = self::chrToInt($s[31]); |
|
| 1270 | - if (self::fe_isnegative($h->X) === ($i >> 7)) { |
|
| 1271 | - $h->X = self::fe_neg($h->X); |
|
| 1272 | - } |
|
| 1273 | - |
|
| 1274 | - # fe_mul(h->T,h->X,h->Y); |
|
| 1275 | - $h->T = self::fe_mul($h->X, $h->Y); |
|
| 1276 | - return $h; |
|
| 1277 | - } |
|
| 1278 | - |
|
| 1279 | - /** |
|
| 1280 | - * @internal You should not use this directly from another application |
|
| 1281 | - * |
|
| 1282 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R |
|
| 1283 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1284 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q |
|
| 1285 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1286 | - */ |
|
| 1287 | - public static function ge_madd( |
|
| 1288 | - ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R, |
|
| 1289 | - ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, |
|
| 1290 | - ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q |
|
| 1291 | - ) { |
|
| 1292 | - $r = clone $R; |
|
| 1293 | - $r->X = self::fe_add($p->Y, $p->X); |
|
| 1294 | - $r->Y = self::fe_sub($p->Y, $p->X); |
|
| 1295 | - $r->Z = self::fe_mul($r->X, $q->yplusx); |
|
| 1296 | - $r->Y = self::fe_mul($r->Y, $q->yminusx); |
|
| 1297 | - $r->T = self::fe_mul($q->xy2d, $p->T); |
|
| 1298 | - $t0 = self::fe_add(clone $p->Z, clone $p->Z); |
|
| 1299 | - $r->X = self::fe_sub($r->Z, $r->Y); |
|
| 1300 | - $r->Y = self::fe_add($r->Z, $r->Y); |
|
| 1301 | - $r->Z = self::fe_add($t0, $r->T); |
|
| 1302 | - $r->T = self::fe_sub($t0, $r->T); |
|
| 1303 | - |
|
| 1304 | - return $r; |
|
| 1305 | - } |
|
| 1306 | - |
|
| 1307 | - /** |
|
| 1308 | - * @internal You should not use this directly from another application |
|
| 1309 | - * |
|
| 1310 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R |
|
| 1311 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1312 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q |
|
| 1313 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1314 | - */ |
|
| 1315 | - public static function ge_msub( |
|
| 1316 | - ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R, |
|
| 1317 | - ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, |
|
| 1318 | - ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q |
|
| 1319 | - ) { |
|
| 1320 | - $r = clone $R; |
|
| 1321 | - |
|
| 1322 | - $r->X = self::fe_add($p->Y, $p->X); |
|
| 1323 | - $r->Y = self::fe_sub($p->Y, $p->X); |
|
| 1324 | - $r->Z = self::fe_mul($r->X, $q->yminusx); |
|
| 1325 | - $r->Y = self::fe_mul($r->Y, $q->yplusx); |
|
| 1326 | - $r->T = self::fe_mul($q->xy2d, $p->T); |
|
| 1327 | - $t0 = self::fe_add($p->Z, $p->Z); |
|
| 1328 | - $r->X = self::fe_sub($r->Z, $r->Y); |
|
| 1329 | - $r->Y = self::fe_add($r->Z, $r->Y); |
|
| 1330 | - $r->Z = self::fe_sub($t0, $r->T); |
|
| 1331 | - $r->T = self::fe_add($t0, $r->T); |
|
| 1332 | - |
|
| 1333 | - return $r; |
|
| 1334 | - } |
|
| 1335 | - |
|
| 1336 | - /** |
|
| 1337 | - * @internal You should not use this directly from another application |
|
| 1338 | - * |
|
| 1339 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p |
|
| 1340 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 |
|
| 1341 | - */ |
|
| 1342 | - public static function ge_p1p1_to_p2(ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p) |
|
| 1343 | - { |
|
| 1344 | - $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P2(); |
|
| 1345 | - $r->X = self::fe_mul($p->X, $p->T); |
|
| 1346 | - $r->Y = self::fe_mul($p->Y, $p->Z); |
|
| 1347 | - $r->Z = self::fe_mul($p->Z, $p->T); |
|
| 1348 | - return $r; |
|
| 1349 | - } |
|
| 1350 | - |
|
| 1351 | - /** |
|
| 1352 | - * @internal You should not use this directly from another application |
|
| 1353 | - * |
|
| 1354 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p |
|
| 1355 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 1356 | - */ |
|
| 1357 | - public static function ge_p1p1_to_p3(ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p) |
|
| 1358 | - { |
|
| 1359 | - $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P3(); |
|
| 1360 | - $r->X = self::fe_mul($p->X, $p->T); |
|
| 1361 | - $r->Y = self::fe_mul($p->Y, $p->Z); |
|
| 1362 | - $r->Z = self::fe_mul($p->Z, $p->T); |
|
| 1363 | - $r->T = self::fe_mul($p->X, $p->Y); |
|
| 1364 | - return $r; |
|
| 1365 | - } |
|
| 1366 | - |
|
| 1367 | - /** |
|
| 1368 | - * @internal You should not use this directly from another application |
|
| 1369 | - * |
|
| 1370 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 |
|
| 1371 | - */ |
|
| 1372 | - public static function ge_p2_0() |
|
| 1373 | - { |
|
| 1374 | - return new ParagonIE_Sodium_Core_Curve25519_Ge_P2( |
|
| 1375 | - self::fe_0(), |
|
| 1376 | - self::fe_1(), |
|
| 1377 | - self::fe_1() |
|
| 1378 | - ); |
|
| 1379 | - } |
|
| 1380 | - |
|
| 1381 | - /** |
|
| 1382 | - * @internal You should not use this directly from another application |
|
| 1383 | - * |
|
| 1384 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P2 $p |
|
| 1385 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1386 | - */ |
|
| 1387 | - public static function ge_p2_dbl(ParagonIE_Sodium_Core_Curve25519_Ge_P2 $p) |
|
| 1388 | - { |
|
| 1389 | - $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); |
|
| 1390 | - |
|
| 1391 | - $r->X = self::fe_sq($p->X); |
|
| 1392 | - $r->Z = self::fe_sq($p->Y); |
|
| 1393 | - $r->T = self::fe_sq2($p->Z); |
|
| 1394 | - $r->Y = self::fe_add($p->X, $p->Y); |
|
| 1395 | - $t0 = self::fe_sq($r->Y); |
|
| 1396 | - $r->Y = self::fe_add($r->Z, $r->X); |
|
| 1397 | - $r->Z = self::fe_sub($r->Z, $r->X); |
|
| 1398 | - $r->X = self::fe_sub($t0, $r->Y); |
|
| 1399 | - $r->T = self::fe_sub($r->T, $r->Z); |
|
| 1400 | - |
|
| 1401 | - return $r; |
|
| 1402 | - } |
|
| 1403 | - |
|
| 1404 | - /** |
|
| 1405 | - * @internal You should not use this directly from another application |
|
| 1406 | - * |
|
| 1407 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 1408 | - */ |
|
| 1409 | - public static function ge_p3_0() |
|
| 1410 | - { |
|
| 1411 | - return new ParagonIE_Sodium_Core_Curve25519_Ge_P3( |
|
| 1412 | - self::fe_0(), |
|
| 1413 | - self::fe_1(), |
|
| 1414 | - self::fe_1(), |
|
| 1415 | - self::fe_0() |
|
| 1416 | - ); |
|
| 1417 | - } |
|
| 1418 | - |
|
| 1419 | - /** |
|
| 1420 | - * @internal You should not use this directly from another application |
|
| 1421 | - * |
|
| 1422 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1423 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_Cached |
|
| 1424 | - */ |
|
| 1425 | - public static function ge_p3_to_cached(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p) |
|
| 1426 | - { |
|
| 1427 | - static $d2 = null; |
|
| 1428 | - if ($d2 === null) { |
|
| 1429 | - $d2 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d2); |
|
| 1430 | - } |
|
| 1431 | - /** @var ParagonIE_Sodium_Core_Curve25519_Fe $d2 */ |
|
| 1432 | - $r = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached(); |
|
| 1433 | - $r->YplusX = self::fe_add($p->Y, $p->X); |
|
| 1434 | - $r->YminusX = self::fe_sub($p->Y, $p->X); |
|
| 1435 | - $r->Z = self::fe_copy($p->Z); |
|
| 1436 | - $r->T2d = self::fe_mul($p->T, $d2); |
|
| 1437 | - return $r; |
|
| 1438 | - } |
|
| 1439 | - |
|
| 1440 | - /** |
|
| 1441 | - * @internal You should not use this directly from another application |
|
| 1442 | - * |
|
| 1443 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1444 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 |
|
| 1445 | - */ |
|
| 1446 | - public static function ge_p3_to_p2(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p) |
|
| 1447 | - { |
|
| 1448 | - return new ParagonIE_Sodium_Core_Curve25519_Ge_P2( |
|
| 1449 | - self::fe_copy($p->X), |
|
| 1450 | - self::fe_copy($p->Y), |
|
| 1451 | - self::fe_copy($p->Z) |
|
| 1452 | - ); |
|
| 1453 | - } |
|
| 1454 | - |
|
| 1455 | - /** |
|
| 1456 | - * @internal You should not use this directly from another application |
|
| 1457 | - * |
|
| 1458 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h |
|
| 1459 | - * @return string |
|
| 1460 | - * @throws SodiumException |
|
| 1461 | - * @throws TypeError |
|
| 1462 | - */ |
|
| 1463 | - public static function ge_p3_tobytes(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h) |
|
| 1464 | - { |
|
| 1465 | - $recip = self::fe_invert($h->Z); |
|
| 1466 | - $x = self::fe_mul($h->X, $recip); |
|
| 1467 | - $y = self::fe_mul($h->Y, $recip); |
|
| 1468 | - $s = self::fe_tobytes($y); |
|
| 1469 | - $s[31] = self::intToChr( |
|
| 1470 | - self::chrToInt($s[31]) ^ (self::fe_isnegative($x) << 7) |
|
| 1471 | - ); |
|
| 1472 | - return $s; |
|
| 1473 | - } |
|
| 1474 | - |
|
| 1475 | - /** |
|
| 1476 | - * @internal You should not use this directly from another application |
|
| 1477 | - * |
|
| 1478 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1479 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1480 | - */ |
|
| 1481 | - public static function ge_p3_dbl(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p) |
|
| 1482 | - { |
|
| 1483 | - $q = self::ge_p3_to_p2($p); |
|
| 1484 | - return self::ge_p2_dbl($q); |
|
| 1485 | - } |
|
| 1486 | - |
|
| 1487 | - /** |
|
| 1488 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_Precomp |
|
| 1489 | - */ |
|
| 1490 | - public static function ge_precomp_0() |
|
| 1491 | - { |
|
| 1492 | - return new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( |
|
| 1493 | - self::fe_1(), |
|
| 1494 | - self::fe_1(), |
|
| 1495 | - self::fe_0() |
|
| 1496 | - ); |
|
| 1497 | - } |
|
| 1498 | - |
|
| 1499 | - /** |
|
| 1500 | - * @internal You should not use this directly from another application |
|
| 1501 | - * |
|
| 1502 | - * @param int $b |
|
| 1503 | - * @param int $c |
|
| 1504 | - * @return int |
|
| 1505 | - */ |
|
| 1506 | - public static function equal($b, $c) |
|
| 1507 | - { |
|
| 1508 | - return (int) ((($b ^ $c) - 1) >> 31) & 1; |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - /** |
|
| 1512 | - * @internal You should not use this directly from another application |
|
| 1513 | - * |
|
| 1514 | - * @param int|string $char |
|
| 1515 | - * @return int (1 = yes, 0 = no) |
|
| 1516 | - * @throws SodiumException |
|
| 1517 | - * @throws TypeError |
|
| 1518 | - */ |
|
| 1519 | - public static function negative($char) |
|
| 1520 | - { |
|
| 1521 | - if (is_int($char)) { |
|
| 1522 | - return ($char >> 63) & 1; |
|
| 1523 | - } |
|
| 1524 | - $x = self::chrToInt(self::substr($char, 0, 1)); |
|
| 1525 | - return (int) ($x >> 63); |
|
| 1526 | - } |
|
| 1527 | - |
|
| 1528 | - /** |
|
| 1529 | - * Conditional move |
|
| 1530 | - * |
|
| 1531 | - * @internal You should not use this directly from another application |
|
| 1532 | - * |
|
| 1533 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $t |
|
| 1534 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $u |
|
| 1535 | - * @param int $b |
|
| 1536 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_Precomp |
|
| 1537 | - */ |
|
| 1538 | - public static function cmov( |
|
| 1539 | - ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $t, |
|
| 1540 | - ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $u, |
|
| 1541 | - $b |
|
| 1542 | - ) { |
|
| 1543 | - if (!is_int($b)) { |
|
| 1544 | - throw new InvalidArgumentException('Expected an integer.'); |
|
| 1545 | - } |
|
| 1546 | - return new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( |
|
| 1547 | - self::fe_cmov($t->yplusx, $u->yplusx, $b), |
|
| 1548 | - self::fe_cmov($t->yminusx, $u->yminusx, $b), |
|
| 1549 | - self::fe_cmov($t->xy2d, $u->xy2d, $b) |
|
| 1550 | - ); |
|
| 1551 | - } |
|
| 1552 | - |
|
| 1553 | - /** |
|
| 1554 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $t |
|
| 1555 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $u |
|
| 1556 | - * @param int $b |
|
| 1557 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_Cached |
|
| 1558 | - */ |
|
| 1559 | - public static function ge_cmov_cached( |
|
| 1560 | - ParagonIE_Sodium_Core_Curve25519_Ge_Cached $t, |
|
| 1561 | - ParagonIE_Sodium_Core_Curve25519_Ge_Cached $u, |
|
| 1562 | - $b |
|
| 1563 | - ) { |
|
| 1564 | - $b &= 1; |
|
| 1565 | - $ret = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached(); |
|
| 1566 | - $ret->YplusX = self::fe_cmov($t->YplusX, $u->YplusX, $b); |
|
| 1567 | - $ret->YminusX = self::fe_cmov($t->YminusX, $u->YminusX, $b); |
|
| 1568 | - $ret->Z = self::fe_cmov($t->Z, $u->Z, $b); |
|
| 1569 | - $ret->T2d = self::fe_cmov($t->T2d, $u->T2d, $b); |
|
| 1570 | - return $ret; |
|
| 1571 | - } |
|
| 1572 | - |
|
| 1573 | - /** |
|
| 1574 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached[] $cached |
|
| 1575 | - * @param int $b |
|
| 1576 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_Cached |
|
| 1577 | - * @throws SodiumException |
|
| 1578 | - */ |
|
| 1579 | - public static function ge_cmov8_cached(array $cached, $b) |
|
| 1580 | - { |
|
| 1581 | - // const unsigned char bnegative = negative(b); |
|
| 1582 | - // const unsigned char babs = b - (((-bnegative) & b) * ((signed char) 1 << 1)); |
|
| 1583 | - $bnegative = self::negative($b); |
|
| 1584 | - $babs = $b - (((-$bnegative) & $b) << 1); |
|
| 1585 | - |
|
| 1586 | - // ge25519_cached_0(t); |
|
| 1587 | - $t = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( |
|
| 1588 | - self::fe_1(), |
|
| 1589 | - self::fe_1(), |
|
| 1590 | - self::fe_1(), |
|
| 1591 | - self::fe_0() |
|
| 1592 | - ); |
|
| 1593 | - |
|
| 1594 | - // ge25519_cmov_cached(t, &cached[0], equal(babs, 1)); |
|
| 1595 | - // ge25519_cmov_cached(t, &cached[1], equal(babs, 2)); |
|
| 1596 | - // ge25519_cmov_cached(t, &cached[2], equal(babs, 3)); |
|
| 1597 | - // ge25519_cmov_cached(t, &cached[3], equal(babs, 4)); |
|
| 1598 | - // ge25519_cmov_cached(t, &cached[4], equal(babs, 5)); |
|
| 1599 | - // ge25519_cmov_cached(t, &cached[5], equal(babs, 6)); |
|
| 1600 | - // ge25519_cmov_cached(t, &cached[6], equal(babs, 7)); |
|
| 1601 | - // ge25519_cmov_cached(t, &cached[7], equal(babs, 8)); |
|
| 1602 | - for ($x = 0; $x < 8; ++$x) { |
|
| 1603 | - $t = self::ge_cmov_cached($t, $cached[$x], self::equal($babs, $x + 1)); |
|
| 1604 | - } |
|
| 1605 | - |
|
| 1606 | - // fe25519_copy(minust.YplusX, t->YminusX); |
|
| 1607 | - // fe25519_copy(minust.YminusX, t->YplusX); |
|
| 1608 | - // fe25519_copy(minust.Z, t->Z); |
|
| 1609 | - // fe25519_neg(minust.T2d, t->T2d); |
|
| 1610 | - $minust = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( |
|
| 1611 | - self::fe_copy($t->YminusX), |
|
| 1612 | - self::fe_copy($t->YplusX), |
|
| 1613 | - self::fe_copy($t->Z), |
|
| 1614 | - self::fe_neg($t->T2d) |
|
| 1615 | - ); |
|
| 1616 | - return self::ge_cmov_cached($t, $minust, $bnegative); |
|
| 1617 | - } |
|
| 1618 | - |
|
| 1619 | - /** |
|
| 1620 | - * @internal You should not use this directly from another application |
|
| 1621 | - * |
|
| 1622 | - * @param int $pos |
|
| 1623 | - * @param int $b |
|
| 1624 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_Precomp |
|
| 1625 | - * @throws SodiumException |
|
| 1626 | - * @throws TypeError |
|
| 1627 | - * @psalm-suppress MixedArgument |
|
| 1628 | - * @psalm-suppress MixedArrayAccess |
|
| 1629 | - * @psalm-suppress MixedArrayOffset |
|
| 1630 | - */ |
|
| 1631 | - public static function ge_select($pos = 0, $b = 0) |
|
| 1632 | - { |
|
| 1633 | - static $base = null; |
|
| 1634 | - if ($base === null) { |
|
| 1635 | - $base = array(); |
|
| 1636 | - /** @var int $i */ |
|
| 1637 | - foreach (self::$base as $i => $bas) { |
|
| 1638 | - for ($j = 0; $j < 8; ++$j) { |
|
| 1639 | - $base[$i][$j] = new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( |
|
| 1640 | - ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($bas[$j][0]), |
|
| 1641 | - ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($bas[$j][1]), |
|
| 1642 | - ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($bas[$j][2]) |
|
| 1643 | - ); |
|
| 1644 | - } |
|
| 1645 | - } |
|
| 1646 | - } |
|
| 1647 | - /** @var array<int, array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Precomp>> $base */ |
|
| 1648 | - if (!is_int($pos)) { |
|
| 1649 | - throw new InvalidArgumentException('Position must be an integer'); |
|
| 1650 | - } |
|
| 1651 | - if ($pos < 0 || $pos > 31) { |
|
| 1652 | - throw new RangeException('Position is out of range [0, 31]'); |
|
| 1653 | - } |
|
| 1654 | - |
|
| 1655 | - $bnegative = self::negative($b); |
|
| 1656 | - $babs = $b - (((-$bnegative) & $b) << 1); |
|
| 1657 | - |
|
| 1658 | - $t = self::ge_precomp_0(); |
|
| 1659 | - for ($i = 0; $i < 8; ++$i) { |
|
| 1660 | - $t = self::cmov( |
|
| 1661 | - $t, |
|
| 1662 | - $base[$pos][$i], |
|
| 1663 | - self::equal($babs, $i + 1) |
|
| 1664 | - ); |
|
| 1665 | - } |
|
| 1666 | - $minusT = new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( |
|
| 1667 | - self::fe_copy($t->yminusx), |
|
| 1668 | - self::fe_copy($t->yplusx), |
|
| 1669 | - self::fe_neg($t->xy2d) |
|
| 1670 | - ); |
|
| 1671 | - return self::cmov($t, $minusT, $bnegative); |
|
| 1672 | - } |
|
| 1673 | - |
|
| 1674 | - /** |
|
| 1675 | - * Subtract two group elements. |
|
| 1676 | - * |
|
| 1677 | - * r = p - q |
|
| 1678 | - * |
|
| 1679 | - * @internal You should not use this directly from another application |
|
| 1680 | - * |
|
| 1681 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1682 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q |
|
| 1683 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1684 | - */ |
|
| 1685 | - public static function ge_sub( |
|
| 1686 | - ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, |
|
| 1687 | - ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q |
|
| 1688 | - ) { |
|
| 1689 | - $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); |
|
| 1690 | - |
|
| 1691 | - $r->X = self::fe_add($p->Y, $p->X); |
|
| 1692 | - $r->Y = self::fe_sub($p->Y, $p->X); |
|
| 1693 | - $r->Z = self::fe_mul($r->X, $q->YminusX); |
|
| 1694 | - $r->Y = self::fe_mul($r->Y, $q->YplusX); |
|
| 1695 | - $r->T = self::fe_mul($q->T2d, $p->T); |
|
| 1696 | - $r->X = self::fe_mul($p->Z, $q->Z); |
|
| 1697 | - $t0 = self::fe_add($r->X, $r->X); |
|
| 1698 | - $r->X = self::fe_sub($r->Z, $r->Y); |
|
| 1699 | - $r->Y = self::fe_add($r->Z, $r->Y); |
|
| 1700 | - $r->Z = self::fe_sub($t0, $r->T); |
|
| 1701 | - $r->T = self::fe_add($t0, $r->T); |
|
| 1702 | - |
|
| 1703 | - return $r; |
|
| 1704 | - } |
|
| 1705 | - |
|
| 1706 | - /** |
|
| 1707 | - * Convert a group element to a byte string. |
|
| 1708 | - * |
|
| 1709 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P2 $h |
|
| 1710 | - * @return string |
|
| 1711 | - * @throws SodiumException |
|
| 1712 | - * @throws TypeError |
|
| 1713 | - */ |
|
| 1714 | - public static function ge_tobytes(ParagonIE_Sodium_Core_Curve25519_Ge_P2 $h) |
|
| 1715 | - { |
|
| 1716 | - $recip = self::fe_invert($h->Z); |
|
| 1717 | - $x = self::fe_mul($h->X, $recip); |
|
| 1718 | - $y = self::fe_mul($h->Y, $recip); |
|
| 1719 | - $s = self::fe_tobytes($y); |
|
| 1720 | - $s[31] = self::intToChr( |
|
| 1721 | - self::chrToInt($s[31]) ^ (self::fe_isnegative($x) << 7) |
|
| 1722 | - ); |
|
| 1723 | - return $s; |
|
| 1724 | - } |
|
| 1725 | - |
|
| 1726 | - /** |
|
| 1727 | - * @internal You should not use this directly from another application |
|
| 1728 | - * |
|
| 1729 | - * @param string $a |
|
| 1730 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A |
|
| 1731 | - * @param string $b |
|
| 1732 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 |
|
| 1733 | - * @throws SodiumException |
|
| 1734 | - * @throws TypeError |
|
| 1735 | - * @psalm-suppress MixedArgument |
|
| 1736 | - * @psalm-suppress MixedArrayAccess |
|
| 1737 | - */ |
|
| 1738 | - public static function ge_double_scalarmult_vartime( |
|
| 1739 | - $a, |
|
| 1740 | - ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A, |
|
| 1741 | - $b |
|
| 1742 | - ) { |
|
| 1743 | - /** @var array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Cached> $Ai */ |
|
| 1744 | - $Ai = array(); |
|
| 1745 | - |
|
| 1746 | - /** @var array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Precomp> $Bi */ |
|
| 1747 | - static $Bi = array(); |
|
| 1748 | - if (!$Bi) { |
|
| 1749 | - for ($i = 0; $i < 8; ++$i) { |
|
| 1750 | - $Bi[$i] = new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( |
|
| 1751 | - ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$base2[$i][0]), |
|
| 1752 | - ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$base2[$i][1]), |
|
| 1753 | - ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$base2[$i][2]) |
|
| 1754 | - ); |
|
| 1755 | - } |
|
| 1756 | - } |
|
| 1757 | - for ($i = 0; $i < 8; ++$i) { |
|
| 1758 | - $Ai[$i] = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( |
|
| 1759 | - self::fe_0(), |
|
| 1760 | - self::fe_0(), |
|
| 1761 | - self::fe_0(), |
|
| 1762 | - self::fe_0() |
|
| 1763 | - ); |
|
| 1764 | - } |
|
| 1765 | - |
|
| 1766 | - # slide(aslide,a); |
|
| 1767 | - # slide(bslide,b); |
|
| 1768 | - /** @var array<int, int> $aslide */ |
|
| 1769 | - $aslide = self::slide($a); |
|
| 1770 | - /** @var array<int, int> $bslide */ |
|
| 1771 | - $bslide = self::slide($b); |
|
| 1772 | - |
|
| 1773 | - # ge_p3_to_cached(&Ai[0],A); |
|
| 1774 | - # ge_p3_dbl(&t,A); ge_p1p1_to_p3(&A2,&t); |
|
| 1775 | - $Ai[0] = self::ge_p3_to_cached($A); |
|
| 1776 | - $t = self::ge_p3_dbl($A); |
|
| 1777 | - $A2 = self::ge_p1p1_to_p3($t); |
|
| 1778 | - |
|
| 1779 | - # ge_add(&t,&A2,&Ai[0]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[1],&u); |
|
| 1780 | - # ge_add(&t,&A2,&Ai[1]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[2],&u); |
|
| 1781 | - # ge_add(&t,&A2,&Ai[2]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[3],&u); |
|
| 1782 | - # ge_add(&t,&A2,&Ai[3]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[4],&u); |
|
| 1783 | - # ge_add(&t,&A2,&Ai[4]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[5],&u); |
|
| 1784 | - # ge_add(&t,&A2,&Ai[5]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[6],&u); |
|
| 1785 | - # ge_add(&t,&A2,&Ai[6]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[7],&u); |
|
| 1786 | - for ($i = 0; $i < 7; ++$i) { |
|
| 1787 | - $t = self::ge_add($A2, $Ai[$i]); |
|
| 1788 | - $u = self::ge_p1p1_to_p3($t); |
|
| 1789 | - $Ai[$i + 1] = self::ge_p3_to_cached($u); |
|
| 1790 | - } |
|
| 1791 | - |
|
| 1792 | - # ge_p2_0(r); |
|
| 1793 | - $r = self::ge_p2_0(); |
|
| 1794 | - |
|
| 1795 | - # for (i = 255;i >= 0;--i) { |
|
| 1796 | - # if (aslide[i] || bslide[i]) break; |
|
| 1797 | - # } |
|
| 1798 | - $i = 255; |
|
| 1799 | - for (; $i >= 0; --$i) { |
|
| 1800 | - if ($aslide[$i] || $bslide[$i]) { |
|
| 1801 | - break; |
|
| 1802 | - } |
|
| 1803 | - } |
|
| 1804 | - |
|
| 1805 | - # for (;i >= 0;--i) { |
|
| 1806 | - for (; $i >= 0; --$i) { |
|
| 1807 | - # ge_p2_dbl(&t,r); |
|
| 1808 | - $t = self::ge_p2_dbl($r); |
|
| 1809 | - |
|
| 1810 | - # if (aslide[i] > 0) { |
|
| 1811 | - if ($aslide[$i] > 0) { |
|
| 1812 | - # ge_p1p1_to_p3(&u,&t); |
|
| 1813 | - # ge_add(&t,&u,&Ai[aslide[i]/2]); |
|
| 1814 | - $u = self::ge_p1p1_to_p3($t); |
|
| 1815 | - $t = self::ge_add( |
|
| 1816 | - $u, |
|
| 1817 | - $Ai[(int) floor($aslide[$i] / 2)] |
|
| 1818 | - ); |
|
| 1819 | - # } else if (aslide[i] < 0) { |
|
| 1820 | - } elseif ($aslide[$i] < 0) { |
|
| 1821 | - # ge_p1p1_to_p3(&u,&t); |
|
| 1822 | - # ge_sub(&t,&u,&Ai[(-aslide[i])/2]); |
|
| 1823 | - $u = self::ge_p1p1_to_p3($t); |
|
| 1824 | - $t = self::ge_sub( |
|
| 1825 | - $u, |
|
| 1826 | - $Ai[(int) floor(-$aslide[$i] / 2)] |
|
| 1827 | - ); |
|
| 1828 | - } |
|
| 1829 | - |
|
| 1830 | - # if (bslide[i] > 0) { |
|
| 1831 | - if ($bslide[$i] > 0) { |
|
| 1832 | - /** @var int $index */ |
|
| 1833 | - $index = (int) floor($bslide[$i] / 2); |
|
| 1834 | - # ge_p1p1_to_p3(&u,&t); |
|
| 1835 | - # ge_madd(&t,&u,&Bi[bslide[i]/2]); |
|
| 1836 | - $u = self::ge_p1p1_to_p3($t); |
|
| 1837 | - $t = self::ge_madd($t, $u, $Bi[$index]); |
|
| 1838 | - # } else if (bslide[i] < 0) { |
|
| 1839 | - } elseif ($bslide[$i] < 0) { |
|
| 1840 | - /** @var int $index */ |
|
| 1841 | - $index = (int) floor(-$bslide[$i] / 2); |
|
| 1842 | - # ge_p1p1_to_p3(&u,&t); |
|
| 1843 | - # ge_msub(&t,&u,&Bi[(-bslide[i])/2]); |
|
| 1844 | - $u = self::ge_p1p1_to_p3($t); |
|
| 1845 | - $t = self::ge_msub($t, $u, $Bi[$index]); |
|
| 1846 | - } |
|
| 1847 | - # ge_p1p1_to_p2(r,&t); |
|
| 1848 | - $r = self::ge_p1p1_to_p2($t); |
|
| 1849 | - } |
|
| 1850 | - return $r; |
|
| 1851 | - } |
|
| 1852 | - |
|
| 1853 | - /** |
|
| 1854 | - * @internal You should not use this directly from another application |
|
| 1855 | - * |
|
| 1856 | - * @param string $a |
|
| 1857 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1858 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 1859 | - * @throws SodiumException |
|
| 1860 | - * @throws TypeError |
|
| 1861 | - * @psalm-suppress MixedAssignment |
|
| 1862 | - * @psalm-suppress MixedOperand |
|
| 1863 | - */ |
|
| 1864 | - public static function ge_scalarmult($a, $p) |
|
| 1865 | - { |
|
| 1866 | - $e = array_fill(0, 64, 0); |
|
| 1867 | - |
|
| 1868 | - /** @var ParagonIE_Sodium_Core_Curve25519_Ge_Cached[] $pi */ |
|
| 1869 | - $pi = array(); |
|
| 1870 | - |
|
| 1871 | - // ge25519_p3_to_cached(&pi[1 - 1], p); /* p */ |
|
| 1872 | - $pi[0] = self::ge_p3_to_cached($p); |
|
| 1873 | - |
|
| 1874 | - // ge25519_p3_dbl(&t2, p); |
|
| 1875 | - // ge25519_p1p1_to_p3(&p2, &t2); |
|
| 1876 | - // ge25519_p3_to_cached(&pi[2 - 1], &p2); /* 2p = 2*p */ |
|
| 1877 | - $t2 = self::ge_p3_dbl($p); |
|
| 1878 | - $p2 = self::ge_p1p1_to_p3($t2); |
|
| 1879 | - $pi[1] = self::ge_p3_to_cached($p2); |
|
| 1880 | - |
|
| 1881 | - // ge25519_add_cached(&t3, p, &pi[2 - 1]); |
|
| 1882 | - // ge25519_p1p1_to_p3(&p3, &t3); |
|
| 1883 | - // ge25519_p3_to_cached(&pi[3 - 1], &p3); /* 3p = 2p+p */ |
|
| 1884 | - $t3 = self::ge_add($p, $pi[1]); |
|
| 1885 | - $p3 = self::ge_p1p1_to_p3($t3); |
|
| 1886 | - $pi[2] = self::ge_p3_to_cached($p3); |
|
| 1887 | - |
|
| 1888 | - // ge25519_p3_dbl(&t4, &p2); |
|
| 1889 | - // ge25519_p1p1_to_p3(&p4, &t4); |
|
| 1890 | - // ge25519_p3_to_cached(&pi[4 - 1], &p4); /* 4p = 2*2p */ |
|
| 1891 | - $t4 = self::ge_p3_dbl($p2); |
|
| 1892 | - $p4 = self::ge_p1p1_to_p3($t4); |
|
| 1893 | - $pi[3] = self::ge_p3_to_cached($p4); |
|
| 1894 | - |
|
| 1895 | - // ge25519_add_cached(&t5, p, &pi[4 - 1]); |
|
| 1896 | - // ge25519_p1p1_to_p3(&p5, &t5); |
|
| 1897 | - // ge25519_p3_to_cached(&pi[5 - 1], &p5); /* 5p = 4p+p */ |
|
| 1898 | - $t5 = self::ge_add($p, $pi[3]); |
|
| 1899 | - $p5 = self::ge_p1p1_to_p3($t5); |
|
| 1900 | - $pi[4] = self::ge_p3_to_cached($p5); |
|
| 1901 | - |
|
| 1902 | - // ge25519_p3_dbl(&t6, &p3); |
|
| 1903 | - // ge25519_p1p1_to_p3(&p6, &t6); |
|
| 1904 | - // ge25519_p3_to_cached(&pi[6 - 1], &p6); /* 6p = 2*3p */ |
|
| 1905 | - $t6 = self::ge_p3_dbl($p3); |
|
| 1906 | - $p6 = self::ge_p1p1_to_p3($t6); |
|
| 1907 | - $pi[5] = self::ge_p3_to_cached($p6); |
|
| 1908 | - |
|
| 1909 | - // ge25519_add_cached(&t7, p, &pi[6 - 1]); |
|
| 1910 | - // ge25519_p1p1_to_p3(&p7, &t7); |
|
| 1911 | - // ge25519_p3_to_cached(&pi[7 - 1], &p7); /* 7p = 6p+p */ |
|
| 1912 | - $t7 = self::ge_add($p, $pi[5]); |
|
| 1913 | - $p7 = self::ge_p1p1_to_p3($t7); |
|
| 1914 | - $pi[6] = self::ge_p3_to_cached($p7); |
|
| 1915 | - |
|
| 1916 | - // ge25519_p3_dbl(&t8, &p4); |
|
| 1917 | - // ge25519_p1p1_to_p3(&p8, &t8); |
|
| 1918 | - // ge25519_p3_to_cached(&pi[8 - 1], &p8); /* 8p = 2*4p */ |
|
| 1919 | - $t8 = self::ge_p3_dbl($p4); |
|
| 1920 | - $p8 = self::ge_p1p1_to_p3($t8); |
|
| 1921 | - $pi[7] = self::ge_p3_to_cached($p8); |
|
| 1922 | - |
|
| 1923 | - |
|
| 1924 | - // for (i = 0; i < 32; ++i) { |
|
| 1925 | - // e[2 * i + 0] = (a[i] >> 0) & 15; |
|
| 1926 | - // e[2 * i + 1] = (a[i] >> 4) & 15; |
|
| 1927 | - // } |
|
| 1928 | - for ($i = 0; $i < 32; ++$i) { |
|
| 1929 | - $e[($i << 1) ] = self::chrToInt($a[$i]) & 15; |
|
| 1930 | - $e[($i << 1) + 1] = (self::chrToInt($a[$i]) >> 4) & 15; |
|
| 1931 | - } |
|
| 1932 | - // /* each e[i] is between 0 and 15 */ |
|
| 1933 | - // /* e[63] is between 0 and 7 */ |
|
| 1934 | - |
|
| 1935 | - // carry = 0; |
|
| 1936 | - // for (i = 0; i < 63; ++i) { |
|
| 1937 | - // e[i] += carry; |
|
| 1938 | - // carry = e[i] + 8; |
|
| 1939 | - // carry >>= 4; |
|
| 1940 | - // e[i] -= carry * ((signed char) 1 << 4); |
|
| 1941 | - // } |
|
| 1942 | - $carry = 0; |
|
| 1943 | - for ($i = 0; $i < 63; ++$i) { |
|
| 1944 | - $e[$i] += $carry; |
|
| 1945 | - $carry = $e[$i] + 8; |
|
| 1946 | - $carry >>= 4; |
|
| 1947 | - $e[$i] -= $carry << 4; |
|
| 1948 | - } |
|
| 1949 | - // e[63] += carry; |
|
| 1950 | - // /* each e[i] is between -8 and 8 */ |
|
| 1951 | - $e[63] += $carry; |
|
| 1952 | - |
|
| 1953 | - // ge25519_p3_0(h); |
|
| 1954 | - $h = self::ge_p3_0(); |
|
| 1955 | - |
|
| 1956 | - // for (i = 63; i != 0; i--) { |
|
| 1957 | - for ($i = 63; $i != 0; --$i) { |
|
| 1958 | - // ge25519_cmov8_cached(&t, pi, e[i]); |
|
| 1959 | - $t = self::ge_cmov8_cached($pi, $e[$i]); |
|
| 1960 | - // ge25519_add_cached(&r, h, &t); |
|
| 1961 | - $r = self::ge_add($h, $t); |
|
| 1962 | - |
|
| 1963 | - // ge25519_p1p1_to_p2(&s, &r); |
|
| 1964 | - // ge25519_p2_dbl(&r, &s); |
|
| 1965 | - // ge25519_p1p1_to_p2(&s, &r); |
|
| 1966 | - // ge25519_p2_dbl(&r, &s); |
|
| 1967 | - // ge25519_p1p1_to_p2(&s, &r); |
|
| 1968 | - // ge25519_p2_dbl(&r, &s); |
|
| 1969 | - // ge25519_p1p1_to_p2(&s, &r); |
|
| 1970 | - // ge25519_p2_dbl(&r, &s); |
|
| 1971 | - $s = self::ge_p1p1_to_p2($r); |
|
| 1972 | - $r = self::ge_p2_dbl($s); |
|
| 1973 | - $s = self::ge_p1p1_to_p2($r); |
|
| 1974 | - $r = self::ge_p2_dbl($s); |
|
| 1975 | - $s = self::ge_p1p1_to_p2($r); |
|
| 1976 | - $r = self::ge_p2_dbl($s); |
|
| 1977 | - $s = self::ge_p1p1_to_p2($r); |
|
| 1978 | - $r = self::ge_p2_dbl($s); |
|
| 1979 | - |
|
| 1980 | - // ge25519_p1p1_to_p3(h, &r); /* *16 */ |
|
| 1981 | - $h = self::ge_p1p1_to_p3($r); /* *16 */ |
|
| 1982 | - } |
|
| 1983 | - |
|
| 1984 | - // ge25519_cmov8_cached(&t, pi, e[i]); |
|
| 1985 | - // ge25519_add_cached(&r, h, &t); |
|
| 1986 | - // ge25519_p1p1_to_p3(h, &r); |
|
| 1987 | - $t = self::ge_cmov8_cached($pi, $e[0]); |
|
| 1988 | - $r = self::ge_add($h, $t); |
|
| 1989 | - return self::ge_p1p1_to_p3($r); |
|
| 1990 | - } |
|
| 1991 | - |
|
| 1992 | - /** |
|
| 1993 | - * @internal You should not use this directly from another application |
|
| 1994 | - * |
|
| 1995 | - * @param string $a |
|
| 1996 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 1997 | - * @throws SodiumException |
|
| 1998 | - * @throws TypeError |
|
| 1999 | - * @psalm-suppress MixedAssignment |
|
| 2000 | - * @psalm-suppress MixedOperand |
|
| 2001 | - */ |
|
| 2002 | - public static function ge_scalarmult_base($a) |
|
| 2003 | - { |
|
| 2004 | - /** @var array<int, int> $e */ |
|
| 2005 | - $e = array(); |
|
| 2006 | - $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); |
|
| 2007 | - |
|
| 2008 | - for ($i = 0; $i < 32; ++$i) { |
|
| 2009 | - $dbl = (int) $i << 1; |
|
| 2010 | - $e[$dbl] = (int) self::chrToInt($a[$i]) & 15; |
|
| 2011 | - $e[$dbl + 1] = (int) (self::chrToInt($a[$i]) >> 4) & 15; |
|
| 2012 | - } |
|
| 2013 | - |
|
| 2014 | - $carry = 0; |
|
| 2015 | - for ($i = 0; $i < 63; ++$i) { |
|
| 2016 | - $e[$i] += $carry; |
|
| 2017 | - $carry = $e[$i] + 8; |
|
| 2018 | - $carry >>= 4; |
|
| 2019 | - $e[$i] -= $carry << 4; |
|
| 2020 | - } |
|
| 2021 | - $e[63] += (int) $carry; |
|
| 2022 | - |
|
| 2023 | - $h = self::ge_p3_0(); |
|
| 2024 | - |
|
| 2025 | - for ($i = 1; $i < 64; $i += 2) { |
|
| 2026 | - $t = self::ge_select((int) floor($i / 2), (int) $e[$i]); |
|
| 2027 | - $r = self::ge_madd($r, $h, $t); |
|
| 2028 | - $h = self::ge_p1p1_to_p3($r); |
|
| 2029 | - } |
|
| 2030 | - |
|
| 2031 | - $r = self::ge_p3_dbl($h); |
|
| 2032 | - |
|
| 2033 | - $s = self::ge_p1p1_to_p2($r); |
|
| 2034 | - $r = self::ge_p2_dbl($s); |
|
| 2035 | - $s = self::ge_p1p1_to_p2($r); |
|
| 2036 | - $r = self::ge_p2_dbl($s); |
|
| 2037 | - $s = self::ge_p1p1_to_p2($r); |
|
| 2038 | - $r = self::ge_p2_dbl($s); |
|
| 2039 | - |
|
| 2040 | - $h = self::ge_p1p1_to_p3($r); |
|
| 2041 | - |
|
| 2042 | - for ($i = 0; $i < 64; $i += 2) { |
|
| 2043 | - $t = self::ge_select($i >> 1, (int) $e[$i]); |
|
| 2044 | - $r = self::ge_madd($r, $h, $t); |
|
| 2045 | - $h = self::ge_p1p1_to_p3($r); |
|
| 2046 | - } |
|
| 2047 | - return $h; |
|
| 2048 | - } |
|
| 2049 | - |
|
| 2050 | - /** |
|
| 2051 | - * Calculates (ab + c) mod l |
|
| 2052 | - * where l = 2^252 + 27742317777372353535851937790883648493 |
|
| 2053 | - * |
|
| 2054 | - * @internal You should not use this directly from another application |
|
| 2055 | - * |
|
| 2056 | - * @param string $a |
|
| 2057 | - * @param string $b |
|
| 2058 | - * @param string $c |
|
| 2059 | - * @return string |
|
| 2060 | - * @throws TypeError |
|
| 2061 | - */ |
|
| 2062 | - public static function sc_muladd($a, $b, $c) |
|
| 2063 | - { |
|
| 2064 | - $a0 = 2097151 & self::load_3(self::substr($a, 0, 3)); |
|
| 2065 | - $a1 = 2097151 & (self::load_4(self::substr($a, 2, 4)) >> 5); |
|
| 2066 | - $a2 = 2097151 & (self::load_3(self::substr($a, 5, 3)) >> 2); |
|
| 2067 | - $a3 = 2097151 & (self::load_4(self::substr($a, 7, 4)) >> 7); |
|
| 2068 | - $a4 = 2097151 & (self::load_4(self::substr($a, 10, 4)) >> 4); |
|
| 2069 | - $a5 = 2097151 & (self::load_3(self::substr($a, 13, 3)) >> 1); |
|
| 2070 | - $a6 = 2097151 & (self::load_4(self::substr($a, 15, 4)) >> 6); |
|
| 2071 | - $a7 = 2097151 & (self::load_3(self::substr($a, 18, 3)) >> 3); |
|
| 2072 | - $a8 = 2097151 & self::load_3(self::substr($a, 21, 3)); |
|
| 2073 | - $a9 = 2097151 & (self::load_4(self::substr($a, 23, 4)) >> 5); |
|
| 2074 | - $a10 = 2097151 & (self::load_3(self::substr($a, 26, 3)) >> 2); |
|
| 2075 | - $a11 = (self::load_4(self::substr($a, 28, 4)) >> 7); |
|
| 2076 | - |
|
| 2077 | - $b0 = 2097151 & self::load_3(self::substr($b, 0, 3)); |
|
| 2078 | - $b1 = 2097151 & (self::load_4(self::substr($b, 2, 4)) >> 5); |
|
| 2079 | - $b2 = 2097151 & (self::load_3(self::substr($b, 5, 3)) >> 2); |
|
| 2080 | - $b3 = 2097151 & (self::load_4(self::substr($b, 7, 4)) >> 7); |
|
| 2081 | - $b4 = 2097151 & (self::load_4(self::substr($b, 10, 4)) >> 4); |
|
| 2082 | - $b5 = 2097151 & (self::load_3(self::substr($b, 13, 3)) >> 1); |
|
| 2083 | - $b6 = 2097151 & (self::load_4(self::substr($b, 15, 4)) >> 6); |
|
| 2084 | - $b7 = 2097151 & (self::load_3(self::substr($b, 18, 3)) >> 3); |
|
| 2085 | - $b8 = 2097151 & self::load_3(self::substr($b, 21, 3)); |
|
| 2086 | - $b9 = 2097151 & (self::load_4(self::substr($b, 23, 4)) >> 5); |
|
| 2087 | - $b10 = 2097151 & (self::load_3(self::substr($b, 26, 3)) >> 2); |
|
| 2088 | - $b11 = (self::load_4(self::substr($b, 28, 4)) >> 7); |
|
| 2089 | - |
|
| 2090 | - $c0 = 2097151 & self::load_3(self::substr($c, 0, 3)); |
|
| 2091 | - $c1 = 2097151 & (self::load_4(self::substr($c, 2, 4)) >> 5); |
|
| 2092 | - $c2 = 2097151 & (self::load_3(self::substr($c, 5, 3)) >> 2); |
|
| 2093 | - $c3 = 2097151 & (self::load_4(self::substr($c, 7, 4)) >> 7); |
|
| 2094 | - $c4 = 2097151 & (self::load_4(self::substr($c, 10, 4)) >> 4); |
|
| 2095 | - $c5 = 2097151 & (self::load_3(self::substr($c, 13, 3)) >> 1); |
|
| 2096 | - $c6 = 2097151 & (self::load_4(self::substr($c, 15, 4)) >> 6); |
|
| 2097 | - $c7 = 2097151 & (self::load_3(self::substr($c, 18, 3)) >> 3); |
|
| 2098 | - $c8 = 2097151 & self::load_3(self::substr($c, 21, 3)); |
|
| 2099 | - $c9 = 2097151 & (self::load_4(self::substr($c, 23, 4)) >> 5); |
|
| 2100 | - $c10 = 2097151 & (self::load_3(self::substr($c, 26, 3)) >> 2); |
|
| 2101 | - $c11 = (self::load_4(self::substr($c, 28, 4)) >> 7); |
|
| 2102 | - |
|
| 2103 | - /* Can't really avoid the pyramid here: */ |
|
| 2104 | - $s0 = $c0 + self::mul($a0, $b0, 24); |
|
| 2105 | - $s1 = $c1 + self::mul($a0, $b1, 24) + self::mul($a1, $b0, 24); |
|
| 2106 | - $s2 = $c2 + self::mul($a0, $b2, 24) + self::mul($a1, $b1, 24) + self::mul($a2, $b0, 24); |
|
| 2107 | - $s3 = $c3 + self::mul($a0, $b3, 24) + self::mul($a1, $b2, 24) + self::mul($a2, $b1, 24) + self::mul($a3, $b0, 24); |
|
| 2108 | - $s4 = $c4 + self::mul($a0, $b4, 24) + self::mul($a1, $b3, 24) + self::mul($a2, $b2, 24) + self::mul($a3, $b1, 24) + |
|
| 2109 | - self::mul($a4, $b0, 24); |
|
| 2110 | - $s5 = $c5 + self::mul($a0, $b5, 24) + self::mul($a1, $b4, 24) + self::mul($a2, $b3, 24) + self::mul($a3, $b2, 24) + |
|
| 2111 | - self::mul($a4, $b1, 24) + self::mul($a5, $b0, 24); |
|
| 2112 | - $s6 = $c6 + self::mul($a0, $b6, 24) + self::mul($a1, $b5, 24) + self::mul($a2, $b4, 24) + self::mul($a3, $b3, 24) + |
|
| 2113 | - self::mul($a4, $b2, 24) + self::mul($a5, $b1, 24) + self::mul($a6, $b0, 24); |
|
| 2114 | - $s7 = $c7 + self::mul($a0, $b7, 24) + self::mul($a1, $b6, 24) + self::mul($a2, $b5, 24) + self::mul($a3, $b4, 24) + |
|
| 2115 | - self::mul($a4, $b3, 24) + self::mul($a5, $b2, 24) + self::mul($a6, $b1, 24) + self::mul($a7, $b0, 24); |
|
| 2116 | - $s8 = $c8 + self::mul($a0, $b8, 24) + self::mul($a1, $b7, 24) + self::mul($a2, $b6, 24) + self::mul($a3, $b5, 24) + |
|
| 2117 | - self::mul($a4, $b4, 24) + self::mul($a5, $b3, 24) + self::mul($a6, $b2, 24) + self::mul($a7, $b1, 24) + |
|
| 2118 | - self::mul($a8, $b0, 24); |
|
| 2119 | - $s9 = $c9 + self::mul($a0, $b9, 24) + self::mul($a1, $b8, 24) + self::mul($a2, $b7, 24) + self::mul($a3, $b6, 24) + |
|
| 2120 | - self::mul($a4, $b5, 24) + self::mul($a5, $b4, 24) + self::mul($a6, $b3, 24) + self::mul($a7, $b2, 24) + |
|
| 2121 | - self::mul($a8, $b1, 24) + self::mul($a9, $b0, 24); |
|
| 2122 | - $s10 = $c10 + self::mul($a0, $b10, 24) + self::mul($a1, $b9, 24) + self::mul($a2, $b8, 24) + self::mul($a3, $b7, 24) + |
|
| 2123 | - self::mul($a4, $b6, 24) + self::mul($a5, $b5, 24) + self::mul($a6, $b4, 24) + self::mul($a7, $b3, 24) + |
|
| 2124 | - self::mul($a8, $b2, 24) + self::mul($a9, $b1, 24) + self::mul($a10, $b0, 24); |
|
| 2125 | - $s11 = $c11 + self::mul($a0, $b11, 24) + self::mul($a1, $b10, 24) + self::mul($a2, $b9, 24) + self::mul($a3, $b8, 24) + |
|
| 2126 | - self::mul($a4, $b7, 24) + self::mul($a5, $b6, 24) + self::mul($a6, $b5, 24) + self::mul($a7, $b4, 24) + |
|
| 2127 | - self::mul($a8, $b3, 24) + self::mul($a9, $b2, 24) + self::mul($a10, $b1, 24) + self::mul($a11, $b0, 24); |
|
| 2128 | - $s12 = self::mul($a1, $b11, 24) + self::mul($a2, $b10, 24) + self::mul($a3, $b9, 24) + self::mul($a4, $b8, 24) + |
|
| 2129 | - self::mul($a5, $b7, 24) + self::mul($a6, $b6, 24) + self::mul($a7, $b5, 24) + self::mul($a8, $b4, 24) + |
|
| 2130 | - self::mul($a9, $b3, 24) + self::mul($a10, $b2, 24) + self::mul($a11, $b1, 24); |
|
| 2131 | - $s13 = self::mul($a2, $b11, 24) + self::mul($a3, $b10, 24) + self::mul($a4, $b9, 24) + self::mul($a5, $b8, 24) + |
|
| 2132 | - self::mul($a6, $b7, 24) + self::mul($a7, $b6, 24) + self::mul($a8, $b5, 24) + self::mul($a9, $b4, 24) + |
|
| 2133 | - self::mul($a10, $b3, 24) + self::mul($a11, $b2, 24); |
|
| 2134 | - $s14 = self::mul($a3, $b11, 24) + self::mul($a4, $b10, 24) + self::mul($a5, $b9, 24) + self::mul($a6, $b8, 24) + |
|
| 2135 | - self::mul($a7, $b7, 24) + self::mul($a8, $b6, 24) + self::mul($a9, $b5, 24) + self::mul($a10, $b4, 24) + |
|
| 2136 | - self::mul($a11, $b3, 24); |
|
| 2137 | - $s15 = self::mul($a4, $b11, 24) + self::mul($a5, $b10, 24) + self::mul($a6, $b9, 24) + self::mul($a7, $b8, 24) + |
|
| 2138 | - self::mul($a8, $b7, 24) + self::mul($a9, $b6, 24) + self::mul($a10, $b5, 24) + self::mul($a11, $b4, 24); |
|
| 2139 | - $s16 = self::mul($a5, $b11, 24) + self::mul($a6, $b10, 24) + self::mul($a7, $b9, 24) + self::mul($a8, $b8, 24) + |
|
| 2140 | - self::mul($a9, $b7, 24) + self::mul($a10, $b6, 24) + self::mul($a11, $b5, 24); |
|
| 2141 | - $s17 = self::mul($a6, $b11, 24) + self::mul($a7, $b10, 24) + self::mul($a8, $b9, 24) + self::mul($a9, $b8, 24) + |
|
| 2142 | - self::mul($a10, $b7, 24) + self::mul($a11, $b6, 24); |
|
| 2143 | - $s18 = self::mul($a7, $b11, 24) + self::mul($a8, $b10, 24) + self::mul($a9, $b9, 24) + self::mul($a10, $b8, 24) + |
|
| 2144 | - self::mul($a11, $b7, 24); |
|
| 2145 | - $s19 = self::mul($a8, $b11, 24) + self::mul($a9, $b10, 24) + self::mul($a10, $b9, 24) + self::mul($a11, $b8, 24); |
|
| 2146 | - $s20 = self::mul($a9, $b11, 24) + self::mul($a10, $b10, 24) + self::mul($a11, $b9, 24); |
|
| 2147 | - $s21 = self::mul($a10, $b11, 24) + self::mul($a11, $b10, 24); |
|
| 2148 | - $s22 = self::mul($a11, $b11, 24); |
|
| 2149 | - $s23 = 0; |
|
| 2150 | - |
|
| 2151 | - $carry0 = ($s0 + (1 << 20)) >> 21; |
|
| 2152 | - $s1 += $carry0; |
|
| 2153 | - $s0 -= $carry0 << 21; |
|
| 2154 | - $carry2 = ($s2 + (1 << 20)) >> 21; |
|
| 2155 | - $s3 += $carry2; |
|
| 2156 | - $s2 -= $carry2 << 21; |
|
| 2157 | - $carry4 = ($s4 + (1 << 20)) >> 21; |
|
| 2158 | - $s5 += $carry4; |
|
| 2159 | - $s4 -= $carry4 << 21; |
|
| 2160 | - $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 2161 | - $s7 += $carry6; |
|
| 2162 | - $s6 -= $carry6 << 21; |
|
| 2163 | - $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 2164 | - $s9 += $carry8; |
|
| 2165 | - $s8 -= $carry8 << 21; |
|
| 2166 | - $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 2167 | - $s11 += $carry10; |
|
| 2168 | - $s10 -= $carry10 << 21; |
|
| 2169 | - $carry12 = ($s12 + (1 << 20)) >> 21; |
|
| 2170 | - $s13 += $carry12; |
|
| 2171 | - $s12 -= $carry12 << 21; |
|
| 2172 | - $carry14 = ($s14 + (1 << 20)) >> 21; |
|
| 2173 | - $s15 += $carry14; |
|
| 2174 | - $s14 -= $carry14 << 21; |
|
| 2175 | - $carry16 = ($s16 + (1 << 20)) >> 21; |
|
| 2176 | - $s17 += $carry16; |
|
| 2177 | - $s16 -= $carry16 << 21; |
|
| 2178 | - $carry18 = ($s18 + (1 << 20)) >> 21; |
|
| 2179 | - $s19 += $carry18; |
|
| 2180 | - $s18 -= $carry18 << 21; |
|
| 2181 | - $carry20 = ($s20 + (1 << 20)) >> 21; |
|
| 2182 | - $s21 += $carry20; |
|
| 2183 | - $s20 -= $carry20 << 21; |
|
| 2184 | - $carry22 = ($s22 + (1 << 20)) >> 21; |
|
| 2185 | - $s23 += $carry22; |
|
| 2186 | - $s22 -= $carry22 << 21; |
|
| 2187 | - |
|
| 2188 | - $carry1 = ($s1 + (1 << 20)) >> 21; |
|
| 2189 | - $s2 += $carry1; |
|
| 2190 | - $s1 -= $carry1 << 21; |
|
| 2191 | - $carry3 = ($s3 + (1 << 20)) >> 21; |
|
| 2192 | - $s4 += $carry3; |
|
| 2193 | - $s3 -= $carry3 << 21; |
|
| 2194 | - $carry5 = ($s5 + (1 << 20)) >> 21; |
|
| 2195 | - $s6 += $carry5; |
|
| 2196 | - $s5 -= $carry5 << 21; |
|
| 2197 | - $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 2198 | - $s8 += $carry7; |
|
| 2199 | - $s7 -= $carry7 << 21; |
|
| 2200 | - $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 2201 | - $s10 += $carry9; |
|
| 2202 | - $s9 -= $carry9 << 21; |
|
| 2203 | - $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 2204 | - $s12 += $carry11; |
|
| 2205 | - $s11 -= $carry11 << 21; |
|
| 2206 | - $carry13 = ($s13 + (1 << 20)) >> 21; |
|
| 2207 | - $s14 += $carry13; |
|
| 2208 | - $s13 -= $carry13 << 21; |
|
| 2209 | - $carry15 = ($s15 + (1 << 20)) >> 21; |
|
| 2210 | - $s16 += $carry15; |
|
| 2211 | - $s15 -= $carry15 << 21; |
|
| 2212 | - $carry17 = ($s17 + (1 << 20)) >> 21; |
|
| 2213 | - $s18 += $carry17; |
|
| 2214 | - $s17 -= $carry17 << 21; |
|
| 2215 | - $carry19 = ($s19 + (1 << 20)) >> 21; |
|
| 2216 | - $s20 += $carry19; |
|
| 2217 | - $s19 -= $carry19 << 21; |
|
| 2218 | - $carry21 = ($s21 + (1 << 20)) >> 21; |
|
| 2219 | - $s22 += $carry21; |
|
| 2220 | - $s21 -= $carry21 << 21; |
|
| 2221 | - |
|
| 2222 | - $s11 += self::mul($s23, 666643, 20); |
|
| 2223 | - $s12 += self::mul($s23, 470296, 19); |
|
| 2224 | - $s13 += self::mul($s23, 654183, 20); |
|
| 2225 | - $s14 -= self::mul($s23, 997805, 20); |
|
| 2226 | - $s15 += self::mul($s23, 136657, 18); |
|
| 2227 | - $s16 -= self::mul($s23, 683901, 20); |
|
| 2228 | - |
|
| 2229 | - $s10 += self::mul($s22, 666643, 20); |
|
| 2230 | - $s11 += self::mul($s22, 470296, 19); |
|
| 2231 | - $s12 += self::mul($s22, 654183, 20); |
|
| 2232 | - $s13 -= self::mul($s22, 997805, 20); |
|
| 2233 | - $s14 += self::mul($s22, 136657, 18); |
|
| 2234 | - $s15 -= self::mul($s22, 683901, 20); |
|
| 2235 | - |
|
| 2236 | - $s9 += self::mul($s21, 666643, 20); |
|
| 2237 | - $s10 += self::mul($s21, 470296, 19); |
|
| 2238 | - $s11 += self::mul($s21, 654183, 20); |
|
| 2239 | - $s12 -= self::mul($s21, 997805, 20); |
|
| 2240 | - $s13 += self::mul($s21, 136657, 18); |
|
| 2241 | - $s14 -= self::mul($s21, 683901, 20); |
|
| 2242 | - |
|
| 2243 | - $s8 += self::mul($s20, 666643, 20); |
|
| 2244 | - $s9 += self::mul($s20, 470296, 19); |
|
| 2245 | - $s10 += self::mul($s20, 654183, 20); |
|
| 2246 | - $s11 -= self::mul($s20, 997805, 20); |
|
| 2247 | - $s12 += self::mul($s20, 136657, 18); |
|
| 2248 | - $s13 -= self::mul($s20, 683901, 20); |
|
| 2249 | - |
|
| 2250 | - $s7 += self::mul($s19, 666643, 20); |
|
| 2251 | - $s8 += self::mul($s19, 470296, 19); |
|
| 2252 | - $s9 += self::mul($s19, 654183, 20); |
|
| 2253 | - $s10 -= self::mul($s19, 997805, 20); |
|
| 2254 | - $s11 += self::mul($s19, 136657, 18); |
|
| 2255 | - $s12 -= self::mul($s19, 683901, 20); |
|
| 2256 | - |
|
| 2257 | - $s6 += self::mul($s18, 666643, 20); |
|
| 2258 | - $s7 += self::mul($s18, 470296, 19); |
|
| 2259 | - $s8 += self::mul($s18, 654183, 20); |
|
| 2260 | - $s9 -= self::mul($s18, 997805, 20); |
|
| 2261 | - $s10 += self::mul($s18, 136657, 18); |
|
| 2262 | - $s11 -= self::mul($s18, 683901, 20); |
|
| 2263 | - |
|
| 2264 | - $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 2265 | - $s7 += $carry6; |
|
| 2266 | - $s6 -= $carry6 << 21; |
|
| 2267 | - $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 2268 | - $s9 += $carry8; |
|
| 2269 | - $s8 -= $carry8 << 21; |
|
| 2270 | - $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 2271 | - $s11 += $carry10; |
|
| 2272 | - $s10 -= $carry10 << 21; |
|
| 2273 | - $carry12 = ($s12 + (1 << 20)) >> 21; |
|
| 2274 | - $s13 += $carry12; |
|
| 2275 | - $s12 -= $carry12 << 21; |
|
| 2276 | - $carry14 = ($s14 + (1 << 20)) >> 21; |
|
| 2277 | - $s15 += $carry14; |
|
| 2278 | - $s14 -= $carry14 << 21; |
|
| 2279 | - $carry16 = ($s16 + (1 << 20)) >> 21; |
|
| 2280 | - $s17 += $carry16; |
|
| 2281 | - $s16 -= $carry16 << 21; |
|
| 2282 | - |
|
| 2283 | - $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 2284 | - $s8 += $carry7; |
|
| 2285 | - $s7 -= $carry7 << 21; |
|
| 2286 | - $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 2287 | - $s10 += $carry9; |
|
| 2288 | - $s9 -= $carry9 << 21; |
|
| 2289 | - $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 2290 | - $s12 += $carry11; |
|
| 2291 | - $s11 -= $carry11 << 21; |
|
| 2292 | - $carry13 = ($s13 + (1 << 20)) >> 21; |
|
| 2293 | - $s14 += $carry13; |
|
| 2294 | - $s13 -= $carry13 << 21; |
|
| 2295 | - $carry15 = ($s15 + (1 << 20)) >> 21; |
|
| 2296 | - $s16 += $carry15; |
|
| 2297 | - $s15 -= $carry15 << 21; |
|
| 2298 | - |
|
| 2299 | - $s5 += self::mul($s17, 666643, 20); |
|
| 2300 | - $s6 += self::mul($s17, 470296, 19); |
|
| 2301 | - $s7 += self::mul($s17, 654183, 20); |
|
| 2302 | - $s8 -= self::mul($s17, 997805, 20); |
|
| 2303 | - $s9 += self::mul($s17, 136657, 18); |
|
| 2304 | - $s10 -= self::mul($s17, 683901, 20); |
|
| 2305 | - |
|
| 2306 | - $s4 += self::mul($s16, 666643, 20); |
|
| 2307 | - $s5 += self::mul($s16, 470296, 19); |
|
| 2308 | - $s6 += self::mul($s16, 654183, 20); |
|
| 2309 | - $s7 -= self::mul($s16, 997805, 20); |
|
| 2310 | - $s8 += self::mul($s16, 136657, 18); |
|
| 2311 | - $s9 -= self::mul($s16, 683901, 20); |
|
| 2312 | - |
|
| 2313 | - $s3 += self::mul($s15, 666643, 20); |
|
| 2314 | - $s4 += self::mul($s15, 470296, 19); |
|
| 2315 | - $s5 += self::mul($s15, 654183, 20); |
|
| 2316 | - $s6 -= self::mul($s15, 997805, 20); |
|
| 2317 | - $s7 += self::mul($s15, 136657, 18); |
|
| 2318 | - $s8 -= self::mul($s15, 683901, 20); |
|
| 2319 | - |
|
| 2320 | - $s2 += self::mul($s14, 666643, 20); |
|
| 2321 | - $s3 += self::mul($s14, 470296, 19); |
|
| 2322 | - $s4 += self::mul($s14, 654183, 20); |
|
| 2323 | - $s5 -= self::mul($s14, 997805, 20); |
|
| 2324 | - $s6 += self::mul($s14, 136657, 18); |
|
| 2325 | - $s7 -= self::mul($s14, 683901, 20); |
|
| 2326 | - |
|
| 2327 | - $s1 += self::mul($s13, 666643, 20); |
|
| 2328 | - $s2 += self::mul($s13, 470296, 19); |
|
| 2329 | - $s3 += self::mul($s13, 654183, 20); |
|
| 2330 | - $s4 -= self::mul($s13, 997805, 20); |
|
| 2331 | - $s5 += self::mul($s13, 136657, 18); |
|
| 2332 | - $s6 -= self::mul($s13, 683901, 20); |
|
| 2333 | - |
|
| 2334 | - $s0 += self::mul($s12, 666643, 20); |
|
| 2335 | - $s1 += self::mul($s12, 470296, 19); |
|
| 2336 | - $s2 += self::mul($s12, 654183, 20); |
|
| 2337 | - $s3 -= self::mul($s12, 997805, 20); |
|
| 2338 | - $s4 += self::mul($s12, 136657, 18); |
|
| 2339 | - $s5 -= self::mul($s12, 683901, 20); |
|
| 2340 | - $s12 = 0; |
|
| 2341 | - |
|
| 2342 | - $carry0 = ($s0 + (1 << 20)) >> 21; |
|
| 2343 | - $s1 += $carry0; |
|
| 2344 | - $s0 -= $carry0 << 21; |
|
| 2345 | - $carry2 = ($s2 + (1 << 20)) >> 21; |
|
| 2346 | - $s3 += $carry2; |
|
| 2347 | - $s2 -= $carry2 << 21; |
|
| 2348 | - $carry4 = ($s4 + (1 << 20)) >> 21; |
|
| 2349 | - $s5 += $carry4; |
|
| 2350 | - $s4 -= $carry4 << 21; |
|
| 2351 | - $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 2352 | - $s7 += $carry6; |
|
| 2353 | - $s6 -= $carry6 << 21; |
|
| 2354 | - $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 2355 | - $s9 += $carry8; |
|
| 2356 | - $s8 -= $carry8 << 21; |
|
| 2357 | - $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 2358 | - $s11 += $carry10; |
|
| 2359 | - $s10 -= $carry10 << 21; |
|
| 2360 | - |
|
| 2361 | - $carry1 = ($s1 + (1 << 20)) >> 21; |
|
| 2362 | - $s2 += $carry1; |
|
| 2363 | - $s1 -= $carry1 << 21; |
|
| 2364 | - $carry3 = ($s3 + (1 << 20)) >> 21; |
|
| 2365 | - $s4 += $carry3; |
|
| 2366 | - $s3 -= $carry3 << 21; |
|
| 2367 | - $carry5 = ($s5 + (1 << 20)) >> 21; |
|
| 2368 | - $s6 += $carry5; |
|
| 2369 | - $s5 -= $carry5 << 21; |
|
| 2370 | - $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 2371 | - $s8 += $carry7; |
|
| 2372 | - $s7 -= $carry7 << 21; |
|
| 2373 | - $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 2374 | - $s10 += $carry9; |
|
| 2375 | - $s9 -= $carry9 << 21; |
|
| 2376 | - $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 2377 | - $s12 += $carry11; |
|
| 2378 | - $s11 -= $carry11 << 21; |
|
| 2379 | - |
|
| 2380 | - $s0 += self::mul($s12, 666643, 20); |
|
| 2381 | - $s1 += self::mul($s12, 470296, 19); |
|
| 2382 | - $s2 += self::mul($s12, 654183, 20); |
|
| 2383 | - $s3 -= self::mul($s12, 997805, 20); |
|
| 2384 | - $s4 += self::mul($s12, 136657, 18); |
|
| 2385 | - $s5 -= self::mul($s12, 683901, 20); |
|
| 2386 | - $s12 = 0; |
|
| 2387 | - |
|
| 2388 | - $carry0 = $s0 >> 21; |
|
| 2389 | - $s1 += $carry0; |
|
| 2390 | - $s0 -= $carry0 << 21; |
|
| 2391 | - $carry1 = $s1 >> 21; |
|
| 2392 | - $s2 += $carry1; |
|
| 2393 | - $s1 -= $carry1 << 21; |
|
| 2394 | - $carry2 = $s2 >> 21; |
|
| 2395 | - $s3 += $carry2; |
|
| 2396 | - $s2 -= $carry2 << 21; |
|
| 2397 | - $carry3 = $s3 >> 21; |
|
| 2398 | - $s4 += $carry3; |
|
| 2399 | - $s3 -= $carry3 << 21; |
|
| 2400 | - $carry4 = $s4 >> 21; |
|
| 2401 | - $s5 += $carry4; |
|
| 2402 | - $s4 -= $carry4 << 21; |
|
| 2403 | - $carry5 = $s5 >> 21; |
|
| 2404 | - $s6 += $carry5; |
|
| 2405 | - $s5 -= $carry5 << 21; |
|
| 2406 | - $carry6 = $s6 >> 21; |
|
| 2407 | - $s7 += $carry6; |
|
| 2408 | - $s6 -= $carry6 << 21; |
|
| 2409 | - $carry7 = $s7 >> 21; |
|
| 2410 | - $s8 += $carry7; |
|
| 2411 | - $s7 -= $carry7 << 21; |
|
| 2412 | - $carry8 = $s8 >> 21; |
|
| 2413 | - $s9 += $carry8; |
|
| 2414 | - $s8 -= $carry8 << 21; |
|
| 2415 | - $carry9 = $s9 >> 21; |
|
| 2416 | - $s10 += $carry9; |
|
| 2417 | - $s9 -= $carry9 << 21; |
|
| 2418 | - $carry10 = $s10 >> 21; |
|
| 2419 | - $s11 += $carry10; |
|
| 2420 | - $s10 -= $carry10 << 21; |
|
| 2421 | - $carry11 = $s11 >> 21; |
|
| 2422 | - $s12 += $carry11; |
|
| 2423 | - $s11 -= $carry11 << 21; |
|
| 2424 | - |
|
| 2425 | - $s0 += self::mul($s12, 666643, 20); |
|
| 2426 | - $s1 += self::mul($s12, 470296, 19); |
|
| 2427 | - $s2 += self::mul($s12, 654183, 20); |
|
| 2428 | - $s3 -= self::mul($s12, 997805, 20); |
|
| 2429 | - $s4 += self::mul($s12, 136657, 18); |
|
| 2430 | - $s5 -= self::mul($s12, 683901, 20); |
|
| 2431 | - |
|
| 2432 | - $carry0 = $s0 >> 21; |
|
| 2433 | - $s1 += $carry0; |
|
| 2434 | - $s0 -= $carry0 << 21; |
|
| 2435 | - $carry1 = $s1 >> 21; |
|
| 2436 | - $s2 += $carry1; |
|
| 2437 | - $s1 -= $carry1 << 21; |
|
| 2438 | - $carry2 = $s2 >> 21; |
|
| 2439 | - $s3 += $carry2; |
|
| 2440 | - $s2 -= $carry2 << 21; |
|
| 2441 | - $carry3 = $s3 >> 21; |
|
| 2442 | - $s4 += $carry3; |
|
| 2443 | - $s3 -= $carry3 << 21; |
|
| 2444 | - $carry4 = $s4 >> 21; |
|
| 2445 | - $s5 += $carry4; |
|
| 2446 | - $s4 -= $carry4 << 21; |
|
| 2447 | - $carry5 = $s5 >> 21; |
|
| 2448 | - $s6 += $carry5; |
|
| 2449 | - $s5 -= $carry5 << 21; |
|
| 2450 | - $carry6 = $s6 >> 21; |
|
| 2451 | - $s7 += $carry6; |
|
| 2452 | - $s6 -= $carry6 << 21; |
|
| 2453 | - $carry7 = $s7 >> 21; |
|
| 2454 | - $s8 += $carry7; |
|
| 2455 | - $s7 -= $carry7 << 21; |
|
| 2456 | - $carry8 = $s8 >> 21; |
|
| 2457 | - $s9 += $carry8; |
|
| 2458 | - $s8 -= $carry8 << 21; |
|
| 2459 | - $carry9 = $s9 >> 21; |
|
| 2460 | - $s10 += $carry9; |
|
| 2461 | - $s9 -= $carry9 << 21; |
|
| 2462 | - $carry10 = $s10 >> 21; |
|
| 2463 | - $s11 += $carry10; |
|
| 2464 | - $s10 -= $carry10 << 21; |
|
| 2465 | - |
|
| 2466 | - /** |
|
| 2467 | - * @var array<int, int> |
|
| 2468 | - */ |
|
| 2469 | - $arr = array( |
|
| 2470 | - (int) (0xff & ($s0 >> 0)), |
|
| 2471 | - (int) (0xff & ($s0 >> 8)), |
|
| 2472 | - (int) (0xff & (($s0 >> 16) | $s1 << 5)), |
|
| 2473 | - (int) (0xff & ($s1 >> 3)), |
|
| 2474 | - (int) (0xff & ($s1 >> 11)), |
|
| 2475 | - (int) (0xff & (($s1 >> 19) | $s2 << 2)), |
|
| 2476 | - (int) (0xff & ($s2 >> 6)), |
|
| 2477 | - (int) (0xff & (($s2 >> 14) | $s3 << 7)), |
|
| 2478 | - (int) (0xff & ($s3 >> 1)), |
|
| 2479 | - (int) (0xff & ($s3 >> 9)), |
|
| 2480 | - (int) (0xff & (($s3 >> 17) | $s4 << 4)), |
|
| 2481 | - (int) (0xff & ($s4 >> 4)), |
|
| 2482 | - (int) (0xff & ($s4 >> 12)), |
|
| 2483 | - (int) (0xff & (($s4 >> 20) | $s5 << 1)), |
|
| 2484 | - (int) (0xff & ($s5 >> 7)), |
|
| 2485 | - (int) (0xff & (($s5 >> 15) | $s6 << 6)), |
|
| 2486 | - (int) (0xff & ($s6 >> 2)), |
|
| 2487 | - (int) (0xff & ($s6 >> 10)), |
|
| 2488 | - (int) (0xff & (($s6 >> 18) | $s7 << 3)), |
|
| 2489 | - (int) (0xff & ($s7 >> 5)), |
|
| 2490 | - (int) (0xff & ($s7 >> 13)), |
|
| 2491 | - (int) (0xff & ($s8 >> 0)), |
|
| 2492 | - (int) (0xff & ($s8 >> 8)), |
|
| 2493 | - (int) (0xff & (($s8 >> 16) | $s9 << 5)), |
|
| 2494 | - (int) (0xff & ($s9 >> 3)), |
|
| 2495 | - (int) (0xff & ($s9 >> 11)), |
|
| 2496 | - (int) (0xff & (($s9 >> 19) | $s10 << 2)), |
|
| 2497 | - (int) (0xff & ($s10 >> 6)), |
|
| 2498 | - (int) (0xff & (($s10 >> 14) | $s11 << 7)), |
|
| 2499 | - (int) (0xff & ($s11 >> 1)), |
|
| 2500 | - (int) (0xff & ($s11 >> 9)), |
|
| 2501 | - 0xff & ($s11 >> 17) |
|
| 2502 | - ); |
|
| 2503 | - return self::intArrayToString($arr); |
|
| 2504 | - } |
|
| 2505 | - |
|
| 2506 | - /** |
|
| 2507 | - * @internal You should not use this directly from another application |
|
| 2508 | - * |
|
| 2509 | - * @param string $s |
|
| 2510 | - * @return string |
|
| 2511 | - * @throws TypeError |
|
| 2512 | - */ |
|
| 2513 | - public static function sc_reduce($s) |
|
| 2514 | - { |
|
| 2515 | - $s0 = 2097151 & self::load_3(self::substr($s, 0, 3)); |
|
| 2516 | - $s1 = 2097151 & (self::load_4(self::substr($s, 2, 4)) >> 5); |
|
| 2517 | - $s2 = 2097151 & (self::load_3(self::substr($s, 5, 3)) >> 2); |
|
| 2518 | - $s3 = 2097151 & (self::load_4(self::substr($s, 7, 4)) >> 7); |
|
| 2519 | - $s4 = 2097151 & (self::load_4(self::substr($s, 10, 4)) >> 4); |
|
| 2520 | - $s5 = 2097151 & (self::load_3(self::substr($s, 13, 3)) >> 1); |
|
| 2521 | - $s6 = 2097151 & (self::load_4(self::substr($s, 15, 4)) >> 6); |
|
| 2522 | - $s7 = 2097151 & (self::load_3(self::substr($s, 18, 4)) >> 3); |
|
| 2523 | - $s8 = 2097151 & self::load_3(self::substr($s, 21, 3)); |
|
| 2524 | - $s9 = 2097151 & (self::load_4(self::substr($s, 23, 4)) >> 5); |
|
| 2525 | - $s10 = 2097151 & (self::load_3(self::substr($s, 26, 3)) >> 2); |
|
| 2526 | - $s11 = 2097151 & (self::load_4(self::substr($s, 28, 4)) >> 7); |
|
| 2527 | - $s12 = 2097151 & (self::load_4(self::substr($s, 31, 4)) >> 4); |
|
| 2528 | - $s13 = 2097151 & (self::load_3(self::substr($s, 34, 3)) >> 1); |
|
| 2529 | - $s14 = 2097151 & (self::load_4(self::substr($s, 36, 4)) >> 6); |
|
| 2530 | - $s15 = 2097151 & (self::load_3(self::substr($s, 39, 4)) >> 3); |
|
| 2531 | - $s16 = 2097151 & self::load_3(self::substr($s, 42, 3)); |
|
| 2532 | - $s17 = 2097151 & (self::load_4(self::substr($s, 44, 4)) >> 5); |
|
| 2533 | - $s18 = 2097151 & (self::load_3(self::substr($s, 47, 3)) >> 2); |
|
| 2534 | - $s19 = 2097151 & (self::load_4(self::substr($s, 49, 4)) >> 7); |
|
| 2535 | - $s20 = 2097151 & (self::load_4(self::substr($s, 52, 4)) >> 4); |
|
| 2536 | - $s21 = 2097151 & (self::load_3(self::substr($s, 55, 3)) >> 1); |
|
| 2537 | - $s22 = 2097151 & (self::load_4(self::substr($s, 57, 4)) >> 6); |
|
| 2538 | - $s23 = (self::load_4(self::substr($s, 60, 4)) >> 3); |
|
| 2539 | - |
|
| 2540 | - $s11 += self::mul($s23, 666643, 20); |
|
| 2541 | - $s12 += self::mul($s23, 470296, 19); |
|
| 2542 | - $s13 += self::mul($s23, 654183, 20); |
|
| 2543 | - $s14 -= self::mul($s23, 997805, 20); |
|
| 2544 | - $s15 += self::mul($s23, 136657, 18); |
|
| 2545 | - $s16 -= self::mul($s23, 683901, 20); |
|
| 2546 | - |
|
| 2547 | - $s10 += self::mul($s22, 666643, 20); |
|
| 2548 | - $s11 += self::mul($s22, 470296, 19); |
|
| 2549 | - $s12 += self::mul($s22, 654183, 20); |
|
| 2550 | - $s13 -= self::mul($s22, 997805, 20); |
|
| 2551 | - $s14 += self::mul($s22, 136657, 18); |
|
| 2552 | - $s15 -= self::mul($s22, 683901, 20); |
|
| 2553 | - |
|
| 2554 | - $s9 += self::mul($s21, 666643, 20); |
|
| 2555 | - $s10 += self::mul($s21, 470296, 19); |
|
| 2556 | - $s11 += self::mul($s21, 654183, 20); |
|
| 2557 | - $s12 -= self::mul($s21, 997805, 20); |
|
| 2558 | - $s13 += self::mul($s21, 136657, 18); |
|
| 2559 | - $s14 -= self::mul($s21, 683901, 20); |
|
| 2560 | - |
|
| 2561 | - $s8 += self::mul($s20, 666643, 20); |
|
| 2562 | - $s9 += self::mul($s20, 470296, 19); |
|
| 2563 | - $s10 += self::mul($s20, 654183, 20); |
|
| 2564 | - $s11 -= self::mul($s20, 997805, 20); |
|
| 2565 | - $s12 += self::mul($s20, 136657, 18); |
|
| 2566 | - $s13 -= self::mul($s20, 683901, 20); |
|
| 2567 | - |
|
| 2568 | - $s7 += self::mul($s19, 666643, 20); |
|
| 2569 | - $s8 += self::mul($s19, 470296, 19); |
|
| 2570 | - $s9 += self::mul($s19, 654183, 20); |
|
| 2571 | - $s10 -= self::mul($s19, 997805, 20); |
|
| 2572 | - $s11 += self::mul($s19, 136657, 18); |
|
| 2573 | - $s12 -= self::mul($s19, 683901, 20); |
|
| 2574 | - |
|
| 2575 | - $s6 += self::mul($s18, 666643, 20); |
|
| 2576 | - $s7 += self::mul($s18, 470296, 19); |
|
| 2577 | - $s8 += self::mul($s18, 654183, 20); |
|
| 2578 | - $s9 -= self::mul($s18, 997805, 20); |
|
| 2579 | - $s10 += self::mul($s18, 136657, 18); |
|
| 2580 | - $s11 -= self::mul($s18, 683901, 20); |
|
| 2581 | - |
|
| 2582 | - $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 2583 | - $s7 += $carry6; |
|
| 2584 | - $s6 -= $carry6 << 21; |
|
| 2585 | - $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 2586 | - $s9 += $carry8; |
|
| 2587 | - $s8 -= $carry8 << 21; |
|
| 2588 | - $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 2589 | - $s11 += $carry10; |
|
| 2590 | - $s10 -= $carry10 << 21; |
|
| 2591 | - $carry12 = ($s12 + (1 << 20)) >> 21; |
|
| 2592 | - $s13 += $carry12; |
|
| 2593 | - $s12 -= $carry12 << 21; |
|
| 2594 | - $carry14 = ($s14 + (1 << 20)) >> 21; |
|
| 2595 | - $s15 += $carry14; |
|
| 2596 | - $s14 -= $carry14 << 21; |
|
| 2597 | - $carry16 = ($s16 + (1 << 20)) >> 21; |
|
| 2598 | - $s17 += $carry16; |
|
| 2599 | - $s16 -= $carry16 << 21; |
|
| 2600 | - |
|
| 2601 | - $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 2602 | - $s8 += $carry7; |
|
| 2603 | - $s7 -= $carry7 << 21; |
|
| 2604 | - $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 2605 | - $s10 += $carry9; |
|
| 2606 | - $s9 -= $carry9 << 21; |
|
| 2607 | - $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 2608 | - $s12 += $carry11; |
|
| 2609 | - $s11 -= $carry11 << 21; |
|
| 2610 | - $carry13 = ($s13 + (1 << 20)) >> 21; |
|
| 2611 | - $s14 += $carry13; |
|
| 2612 | - $s13 -= $carry13 << 21; |
|
| 2613 | - $carry15 = ($s15 + (1 << 20)) >> 21; |
|
| 2614 | - $s16 += $carry15; |
|
| 2615 | - $s15 -= $carry15 << 21; |
|
| 2616 | - |
|
| 2617 | - $s5 += self::mul($s17, 666643, 20); |
|
| 2618 | - $s6 += self::mul($s17, 470296, 19); |
|
| 2619 | - $s7 += self::mul($s17, 654183, 20); |
|
| 2620 | - $s8 -= self::mul($s17, 997805, 20); |
|
| 2621 | - $s9 += self::mul($s17, 136657, 18); |
|
| 2622 | - $s10 -= self::mul($s17, 683901, 20); |
|
| 2623 | - |
|
| 2624 | - $s4 += self::mul($s16, 666643, 20); |
|
| 2625 | - $s5 += self::mul($s16, 470296, 19); |
|
| 2626 | - $s6 += self::mul($s16, 654183, 20); |
|
| 2627 | - $s7 -= self::mul($s16, 997805, 20); |
|
| 2628 | - $s8 += self::mul($s16, 136657, 18); |
|
| 2629 | - $s9 -= self::mul($s16, 683901, 20); |
|
| 2630 | - |
|
| 2631 | - $s3 += self::mul($s15, 666643, 20); |
|
| 2632 | - $s4 += self::mul($s15, 470296, 19); |
|
| 2633 | - $s5 += self::mul($s15, 654183, 20); |
|
| 2634 | - $s6 -= self::mul($s15, 997805, 20); |
|
| 2635 | - $s7 += self::mul($s15, 136657, 18); |
|
| 2636 | - $s8 -= self::mul($s15, 683901, 20); |
|
| 2637 | - |
|
| 2638 | - $s2 += self::mul($s14, 666643, 20); |
|
| 2639 | - $s3 += self::mul($s14, 470296, 19); |
|
| 2640 | - $s4 += self::mul($s14, 654183, 20); |
|
| 2641 | - $s5 -= self::mul($s14, 997805, 20); |
|
| 2642 | - $s6 += self::mul($s14, 136657, 18); |
|
| 2643 | - $s7 -= self::mul($s14, 683901, 20); |
|
| 2644 | - |
|
| 2645 | - $s1 += self::mul($s13, 666643, 20); |
|
| 2646 | - $s2 += self::mul($s13, 470296, 19); |
|
| 2647 | - $s3 += self::mul($s13, 654183, 20); |
|
| 2648 | - $s4 -= self::mul($s13, 997805, 20); |
|
| 2649 | - $s5 += self::mul($s13, 136657, 18); |
|
| 2650 | - $s6 -= self::mul($s13, 683901, 20); |
|
| 2651 | - |
|
| 2652 | - $s0 += self::mul($s12, 666643, 20); |
|
| 2653 | - $s1 += self::mul($s12, 470296, 19); |
|
| 2654 | - $s2 += self::mul($s12, 654183, 20); |
|
| 2655 | - $s3 -= self::mul($s12, 997805, 20); |
|
| 2656 | - $s4 += self::mul($s12, 136657, 18); |
|
| 2657 | - $s5 -= self::mul($s12, 683901, 20); |
|
| 2658 | - $s12 = 0; |
|
| 2659 | - |
|
| 2660 | - $carry0 = ($s0 + (1 << 20)) >> 21; |
|
| 2661 | - $s1 += $carry0; |
|
| 2662 | - $s0 -= $carry0 << 21; |
|
| 2663 | - $carry2 = ($s2 + (1 << 20)) >> 21; |
|
| 2664 | - $s3 += $carry2; |
|
| 2665 | - $s2 -= $carry2 << 21; |
|
| 2666 | - $carry4 = ($s4 + (1 << 20)) >> 21; |
|
| 2667 | - $s5 += $carry4; |
|
| 2668 | - $s4 -= $carry4 << 21; |
|
| 2669 | - $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 2670 | - $s7 += $carry6; |
|
| 2671 | - $s6 -= $carry6 << 21; |
|
| 2672 | - $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 2673 | - $s9 += $carry8; |
|
| 2674 | - $s8 -= $carry8 << 21; |
|
| 2675 | - $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 2676 | - $s11 += $carry10; |
|
| 2677 | - $s10 -= $carry10 << 21; |
|
| 2678 | - |
|
| 2679 | - $carry1 = ($s1 + (1 << 20)) >> 21; |
|
| 2680 | - $s2 += $carry1; |
|
| 2681 | - $s1 -= $carry1 << 21; |
|
| 2682 | - $carry3 = ($s3 + (1 << 20)) >> 21; |
|
| 2683 | - $s4 += $carry3; |
|
| 2684 | - $s3 -= $carry3 << 21; |
|
| 2685 | - $carry5 = ($s5 + (1 << 20)) >> 21; |
|
| 2686 | - $s6 += $carry5; |
|
| 2687 | - $s5 -= $carry5 << 21; |
|
| 2688 | - $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 2689 | - $s8 += $carry7; |
|
| 2690 | - $s7 -= $carry7 << 21; |
|
| 2691 | - $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 2692 | - $s10 += $carry9; |
|
| 2693 | - $s9 -= $carry9 << 21; |
|
| 2694 | - $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 2695 | - $s12 += $carry11; |
|
| 2696 | - $s11 -= $carry11 << 21; |
|
| 2697 | - |
|
| 2698 | - $s0 += self::mul($s12, 666643, 20); |
|
| 2699 | - $s1 += self::mul($s12, 470296, 19); |
|
| 2700 | - $s2 += self::mul($s12, 654183, 20); |
|
| 2701 | - $s3 -= self::mul($s12, 997805, 20); |
|
| 2702 | - $s4 += self::mul($s12, 136657, 18); |
|
| 2703 | - $s5 -= self::mul($s12, 683901, 20); |
|
| 2704 | - $s12 = 0; |
|
| 2705 | - |
|
| 2706 | - $carry0 = $s0 >> 21; |
|
| 2707 | - $s1 += $carry0; |
|
| 2708 | - $s0 -= $carry0 << 21; |
|
| 2709 | - $carry1 = $s1 >> 21; |
|
| 2710 | - $s2 += $carry1; |
|
| 2711 | - $s1 -= $carry1 << 21; |
|
| 2712 | - $carry2 = $s2 >> 21; |
|
| 2713 | - $s3 += $carry2; |
|
| 2714 | - $s2 -= $carry2 << 21; |
|
| 2715 | - $carry3 = $s3 >> 21; |
|
| 2716 | - $s4 += $carry3; |
|
| 2717 | - $s3 -= $carry3 << 21; |
|
| 2718 | - $carry4 = $s4 >> 21; |
|
| 2719 | - $s5 += $carry4; |
|
| 2720 | - $s4 -= $carry4 << 21; |
|
| 2721 | - $carry5 = $s5 >> 21; |
|
| 2722 | - $s6 += $carry5; |
|
| 2723 | - $s5 -= $carry5 << 21; |
|
| 2724 | - $carry6 = $s6 >> 21; |
|
| 2725 | - $s7 += $carry6; |
|
| 2726 | - $s6 -= $carry6 << 21; |
|
| 2727 | - $carry7 = $s7 >> 21; |
|
| 2728 | - $s8 += $carry7; |
|
| 2729 | - $s7 -= $carry7 << 21; |
|
| 2730 | - $carry8 = $s8 >> 21; |
|
| 2731 | - $s9 += $carry8; |
|
| 2732 | - $s8 -= $carry8 << 21; |
|
| 2733 | - $carry9 = $s9 >> 21; |
|
| 2734 | - $s10 += $carry9; |
|
| 2735 | - $s9 -= $carry9 << 21; |
|
| 2736 | - $carry10 = $s10 >> 21; |
|
| 2737 | - $s11 += $carry10; |
|
| 2738 | - $s10 -= $carry10 << 21; |
|
| 2739 | - $carry11 = $s11 >> 21; |
|
| 2740 | - $s12 += $carry11; |
|
| 2741 | - $s11 -= $carry11 << 21; |
|
| 2742 | - |
|
| 2743 | - $s0 += self::mul($s12, 666643, 20); |
|
| 2744 | - $s1 += self::mul($s12, 470296, 19); |
|
| 2745 | - $s2 += self::mul($s12, 654183, 20); |
|
| 2746 | - $s3 -= self::mul($s12, 997805, 20); |
|
| 2747 | - $s4 += self::mul($s12, 136657, 18); |
|
| 2748 | - $s5 -= self::mul($s12, 683901, 20); |
|
| 2749 | - |
|
| 2750 | - $carry0 = $s0 >> 21; |
|
| 2751 | - $s1 += $carry0; |
|
| 2752 | - $s0 -= $carry0 << 21; |
|
| 2753 | - $carry1 = $s1 >> 21; |
|
| 2754 | - $s2 += $carry1; |
|
| 2755 | - $s1 -= $carry1 << 21; |
|
| 2756 | - $carry2 = $s2 >> 21; |
|
| 2757 | - $s3 += $carry2; |
|
| 2758 | - $s2 -= $carry2 << 21; |
|
| 2759 | - $carry3 = $s3 >> 21; |
|
| 2760 | - $s4 += $carry3; |
|
| 2761 | - $s3 -= $carry3 << 21; |
|
| 2762 | - $carry4 = $s4 >> 21; |
|
| 2763 | - $s5 += $carry4; |
|
| 2764 | - $s4 -= $carry4 << 21; |
|
| 2765 | - $carry5 = $s5 >> 21; |
|
| 2766 | - $s6 += $carry5; |
|
| 2767 | - $s5 -= $carry5 << 21; |
|
| 2768 | - $carry6 = $s6 >> 21; |
|
| 2769 | - $s7 += $carry6; |
|
| 2770 | - $s6 -= $carry6 << 21; |
|
| 2771 | - $carry7 = $s7 >> 21; |
|
| 2772 | - $s8 += $carry7; |
|
| 2773 | - $s7 -= $carry7 << 21; |
|
| 2774 | - $carry8 = $s8 >> 21; |
|
| 2775 | - $s9 += $carry8; |
|
| 2776 | - $s8 -= $carry8 << 21; |
|
| 2777 | - $carry9 = $s9 >> 21; |
|
| 2778 | - $s10 += $carry9; |
|
| 2779 | - $s9 -= $carry9 << 21; |
|
| 2780 | - $carry10 = $s10 >> 21; |
|
| 2781 | - $s11 += $carry10; |
|
| 2782 | - $s10 -= $carry10 << 21; |
|
| 2783 | - |
|
| 2784 | - /** |
|
| 2785 | - * @var array<int, int> |
|
| 2786 | - */ |
|
| 2787 | - $arr = array( |
|
| 2788 | - (int) ($s0 >> 0), |
|
| 2789 | - (int) ($s0 >> 8), |
|
| 2790 | - (int) (($s0 >> 16) | $s1 << 5), |
|
| 2791 | - (int) ($s1 >> 3), |
|
| 2792 | - (int) ($s1 >> 11), |
|
| 2793 | - (int) (($s1 >> 19) | $s2 << 2), |
|
| 2794 | - (int) ($s2 >> 6), |
|
| 2795 | - (int) (($s2 >> 14) | $s3 << 7), |
|
| 2796 | - (int) ($s3 >> 1), |
|
| 2797 | - (int) ($s3 >> 9), |
|
| 2798 | - (int) (($s3 >> 17) | $s4 << 4), |
|
| 2799 | - (int) ($s4 >> 4), |
|
| 2800 | - (int) ($s4 >> 12), |
|
| 2801 | - (int) (($s4 >> 20) | $s5 << 1), |
|
| 2802 | - (int) ($s5 >> 7), |
|
| 2803 | - (int) (($s5 >> 15) | $s6 << 6), |
|
| 2804 | - (int) ($s6 >> 2), |
|
| 2805 | - (int) ($s6 >> 10), |
|
| 2806 | - (int) (($s6 >> 18) | $s7 << 3), |
|
| 2807 | - (int) ($s7 >> 5), |
|
| 2808 | - (int) ($s7 >> 13), |
|
| 2809 | - (int) ($s8 >> 0), |
|
| 2810 | - (int) ($s8 >> 8), |
|
| 2811 | - (int) (($s8 >> 16) | $s9 << 5), |
|
| 2812 | - (int) ($s9 >> 3), |
|
| 2813 | - (int) ($s9 >> 11), |
|
| 2814 | - (int) (($s9 >> 19) | $s10 << 2), |
|
| 2815 | - (int) ($s10 >> 6), |
|
| 2816 | - (int) (($s10 >> 14) | $s11 << 7), |
|
| 2817 | - (int) ($s11 >> 1), |
|
| 2818 | - (int) ($s11 >> 9), |
|
| 2819 | - (int) $s11 >> 17 |
|
| 2820 | - ); |
|
| 2821 | - return self::intArrayToString($arr); |
|
| 2822 | - } |
|
| 2823 | - |
|
| 2824 | - /** |
|
| 2825 | - * multiply by the order of the main subgroup l = 2^252+27742317777372353535851937790883648493 |
|
| 2826 | - * |
|
| 2827 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A |
|
| 2828 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 2829 | - */ |
|
| 2830 | - public static function ge_mul_l(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A) |
|
| 2831 | - { |
|
| 2832 | - $aslide = array( |
|
| 2833 | - 13, 0, 0, 0, 0, -1, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, |
|
| 2834 | - 0, 0, 0, -3, 0, 0, 0, 0, -13, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 3, 0, |
|
| 2835 | - 0, 0, 0, -13, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, |
|
| 2836 | - 0, 0, 11, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, -1, |
|
| 2837 | - 0, 0, 0, 0, 3, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, |
|
| 2838 | - 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 5, 0, 0, 0, 0, |
|
| 2839 | - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 2840 | - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 2841 | - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 2842 | - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 2843 | - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 2844 | - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 |
|
| 2845 | - ); |
|
| 2846 | - |
|
| 2847 | - /** @var array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Cached> $Ai size 8 */ |
|
| 2848 | - $Ai = array(); |
|
| 2849 | - |
|
| 2850 | - # ge_p3_to_cached(&Ai[0], A); |
|
| 2851 | - $Ai[0] = self::ge_p3_to_cached($A); |
|
| 2852 | - # ge_p3_dbl(&t, A); |
|
| 2853 | - $t = self::ge_p3_dbl($A); |
|
| 2854 | - # ge_p1p1_to_p3(&A2, &t); |
|
| 2855 | - $A2 = self::ge_p1p1_to_p3($t); |
|
| 2856 | - |
|
| 2857 | - for ($i = 1; $i < 8; ++$i) { |
|
| 2858 | - # ge_add(&t, &A2, &Ai[0]); |
|
| 2859 | - $t = self::ge_add($A2, $Ai[$i - 1]); |
|
| 2860 | - # ge_p1p1_to_p3(&u, &t); |
|
| 2861 | - $u = self::ge_p1p1_to_p3($t); |
|
| 2862 | - # ge_p3_to_cached(&Ai[i], &u); |
|
| 2863 | - $Ai[$i] = self::ge_p3_to_cached($u); |
|
| 2864 | - } |
|
| 2865 | - |
|
| 2866 | - $r = self::ge_p3_0(); |
|
| 2867 | - for ($i = 252; $i >= 0; --$i) { |
|
| 2868 | - $t = self::ge_p3_dbl($r); |
|
| 2869 | - if ($aslide[$i] > 0) { |
|
| 2870 | - # ge_p1p1_to_p3(&u, &t); |
|
| 2871 | - $u = self::ge_p1p1_to_p3($t); |
|
| 2872 | - # ge_add(&t, &u, &Ai[aslide[i] / 2]); |
|
| 2873 | - $t = self::ge_add($u, $Ai[(int)($aslide[$i] / 2)]); |
|
| 2874 | - } elseif ($aslide[$i] < 0) { |
|
| 2875 | - # ge_p1p1_to_p3(&u, &t); |
|
| 2876 | - $u = self::ge_p1p1_to_p3($t); |
|
| 2877 | - # ge_sub(&t, &u, &Ai[(-aslide[i]) / 2]); |
|
| 2878 | - $t = self::ge_sub($u, $Ai[(int)(-$aslide[$i] / 2)]); |
|
| 2879 | - } |
|
| 2880 | - } |
|
| 2881 | - |
|
| 2882 | - # ge_p1p1_to_p3(r, &t); |
|
| 2883 | - return self::ge_p1p1_to_p3($t); |
|
| 2884 | - } |
|
| 2885 | - |
|
| 2886 | - /** |
|
| 2887 | - * @param string $a |
|
| 2888 | - * @param string $b |
|
| 2889 | - * @return string |
|
| 2890 | - */ |
|
| 2891 | - public static function sc25519_mul($a, $b) |
|
| 2892 | - { |
|
| 2893 | - // int64_t a0 = 2097151 & load_3(a); |
|
| 2894 | - // int64_t a1 = 2097151 & (load_4(a + 2) >> 5); |
|
| 2895 | - // int64_t a2 = 2097151 & (load_3(a + 5) >> 2); |
|
| 2896 | - // int64_t a3 = 2097151 & (load_4(a + 7) >> 7); |
|
| 2897 | - // int64_t a4 = 2097151 & (load_4(a + 10) >> 4); |
|
| 2898 | - // int64_t a5 = 2097151 & (load_3(a + 13) >> 1); |
|
| 2899 | - // int64_t a6 = 2097151 & (load_4(a + 15) >> 6); |
|
| 2900 | - // int64_t a7 = 2097151 & (load_3(a + 18) >> 3); |
|
| 2901 | - // int64_t a8 = 2097151 & load_3(a + 21); |
|
| 2902 | - // int64_t a9 = 2097151 & (load_4(a + 23) >> 5); |
|
| 2903 | - // int64_t a10 = 2097151 & (load_3(a + 26) >> 2); |
|
| 2904 | - // int64_t a11 = (load_4(a + 28) >> 7); |
|
| 2905 | - $a0 = 2097151 & self::load_3(self::substr($a, 0, 3)); |
|
| 2906 | - $a1 = 2097151 & (self::load_4(self::substr($a, 2, 4)) >> 5); |
|
| 2907 | - $a2 = 2097151 & (self::load_3(self::substr($a, 5, 3)) >> 2); |
|
| 2908 | - $a3 = 2097151 & (self::load_4(self::substr($a, 7, 4)) >> 7); |
|
| 2909 | - $a4 = 2097151 & (self::load_4(self::substr($a, 10, 4)) >> 4); |
|
| 2910 | - $a5 = 2097151 & (self::load_3(self::substr($a, 13, 3)) >> 1); |
|
| 2911 | - $a6 = 2097151 & (self::load_4(self::substr($a, 15, 4)) >> 6); |
|
| 2912 | - $a7 = 2097151 & (self::load_3(self::substr($a, 18, 3)) >> 3); |
|
| 2913 | - $a8 = 2097151 & self::load_3(self::substr($a, 21, 3)); |
|
| 2914 | - $a9 = 2097151 & (self::load_4(self::substr($a, 23, 4)) >> 5); |
|
| 2915 | - $a10 = 2097151 & (self::load_3(self::substr($a, 26, 3)) >> 2); |
|
| 2916 | - $a11 = (self::load_4(self::substr($a, 28, 4)) >> 7); |
|
| 2917 | - |
|
| 2918 | - // int64_t b0 = 2097151 & load_3(b); |
|
| 2919 | - // int64_t b1 = 2097151 & (load_4(b + 2) >> 5); |
|
| 2920 | - // int64_t b2 = 2097151 & (load_3(b + 5) >> 2); |
|
| 2921 | - // int64_t b3 = 2097151 & (load_4(b + 7) >> 7); |
|
| 2922 | - // int64_t b4 = 2097151 & (load_4(b + 10) >> 4); |
|
| 2923 | - // int64_t b5 = 2097151 & (load_3(b + 13) >> 1); |
|
| 2924 | - // int64_t b6 = 2097151 & (load_4(b + 15) >> 6); |
|
| 2925 | - // int64_t b7 = 2097151 & (load_3(b + 18) >> 3); |
|
| 2926 | - // int64_t b8 = 2097151 & load_3(b + 21); |
|
| 2927 | - // int64_t b9 = 2097151 & (load_4(b + 23) >> 5); |
|
| 2928 | - // int64_t b10 = 2097151 & (load_3(b + 26) >> 2); |
|
| 2929 | - // int64_t b11 = (load_4(b + 28) >> 7); |
|
| 2930 | - $b0 = 2097151 & self::load_3(self::substr($b, 0, 3)); |
|
| 2931 | - $b1 = 2097151 & (self::load_4(self::substr($b, 2, 4)) >> 5); |
|
| 2932 | - $b2 = 2097151 & (self::load_3(self::substr($b, 5, 3)) >> 2); |
|
| 2933 | - $b3 = 2097151 & (self::load_4(self::substr($b, 7, 4)) >> 7); |
|
| 2934 | - $b4 = 2097151 & (self::load_4(self::substr($b, 10, 4)) >> 4); |
|
| 2935 | - $b5 = 2097151 & (self::load_3(self::substr($b, 13, 3)) >> 1); |
|
| 2936 | - $b6 = 2097151 & (self::load_4(self::substr($b, 15, 4)) >> 6); |
|
| 2937 | - $b7 = 2097151 & (self::load_3(self::substr($b, 18, 3)) >> 3); |
|
| 2938 | - $b8 = 2097151 & self::load_3(self::substr($b, 21, 3)); |
|
| 2939 | - $b9 = 2097151 & (self::load_4(self::substr($b, 23, 4)) >> 5); |
|
| 2940 | - $b10 = 2097151 & (self::load_3(self::substr($b, 26, 3)) >> 2); |
|
| 2941 | - $b11 = (self::load_4(self::substr($b, 28, 4)) >> 7); |
|
| 2942 | - |
|
| 2943 | - // s0 = a0 * b0; |
|
| 2944 | - // s1 = a0 * b1 + a1 * b0; |
|
| 2945 | - // s2 = a0 * b2 + a1 * b1 + a2 * b0; |
|
| 2946 | - // s3 = a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0; |
|
| 2947 | - // s4 = a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0; |
|
| 2948 | - // s5 = a0 * b5 + a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0; |
|
| 2949 | - // s6 = a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0; |
|
| 2950 | - // s7 = a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + |
|
| 2951 | - // a6 * b1 + a7 * b0; |
|
| 2952 | - // s8 = a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + |
|
| 2953 | - // a6 * b2 + a7 * b1 + a8 * b0; |
|
| 2954 | - // s9 = a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + |
|
| 2955 | - // a6 * b3 + a7 * b2 + a8 * b1 + a9 * b0; |
|
| 2956 | - // s10 = a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 + |
|
| 2957 | - // a6 * b4 + a7 * b3 + a8 * b2 + a9 * b1 + a10 * b0; |
|
| 2958 | - // s11 = a0 * b11 + a1 * b10 + a2 * b9 + a3 * b8 + a4 * b7 + a5 * b6 + |
|
| 2959 | - // a6 * b5 + a7 * b4 + a8 * b3 + a9 * b2 + a10 * b1 + a11 * b0; |
|
| 2960 | - // s12 = a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + |
|
| 2961 | - // a7 * b5 + a8 * b4 + a9 * b3 + a10 * b2 + a11 * b1; |
|
| 2962 | - // s13 = a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + |
|
| 2963 | - // a8 * b5 + a9 * b4 + a10 * b3 + a11 * b2; |
|
| 2964 | - // s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + |
|
| 2965 | - // a9 * b5 + a10 * b4 + a11 * b3; |
|
| 2966 | - // s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + |
|
| 2967 | - // a10 * b5 + a11 * b4; |
|
| 2968 | - // s16 = |
|
| 2969 | - // a5 * b11 + a6 * b10 + a7 * b9 + a8 * b8 + a9 * b7 + a10 * b6 + a11 * b5; |
|
| 2970 | - // s17 = a6 * b11 + a7 * b10 + a8 * b9 + a9 * b8 + a10 * b7 + a11 * b6; |
|
| 2971 | - // s18 = a7 * b11 + a8 * b10 + a9 * b9 + a10 * b8 + a11 * b7; |
|
| 2972 | - // s19 = a8 * b11 + a9 * b10 + a10 * b9 + a11 * b8; |
|
| 2973 | - // s20 = a9 * b11 + a10 * b10 + a11 * b9; |
|
| 2974 | - // s21 = a10 * b11 + a11 * b10; |
|
| 2975 | - // s22 = a11 * b11; |
|
| 2976 | - // s23 = 0; |
|
| 2977 | - $s0 = self::mul($a0, $b0, 22); |
|
| 2978 | - $s1 = self::mul($a0, $b1, 22) + self::mul($a1, $b0, 22); |
|
| 2979 | - $s2 = self::mul($a0, $b2, 22) + self::mul($a1, $b1, 22) + self::mul($a2, $b0, 22); |
|
| 2980 | - $s3 = self::mul($a0, $b3, 22) + self::mul($a1, $b2, 22) + self::mul($a2, $b1, 22) + self::mul($a3, $b0, 22); |
|
| 2981 | - $s4 = self::mul($a0, $b4, 22) + self::mul($a1, $b3, 22) + self::mul($a2, $b2, 22) + self::mul($a3, $b1, 22) + |
|
| 2982 | - self::mul($a4, $b0, 22); |
|
| 2983 | - $s5 = self::mul($a0, $b5, 22) + self::mul($a1, $b4, 22) + self::mul($a2, $b3, 22) + self::mul($a3, $b2, 22) + |
|
| 2984 | - self::mul($a4, $b1, 22) + self::mul($a5, $b0, 22); |
|
| 2985 | - $s6 = self::mul($a0, $b6, 22) + self::mul($a1, $b5, 22) + self::mul($a2, $b4, 22) + self::mul($a3, $b3, 22) + |
|
| 2986 | - self::mul($a4, $b2, 22) + self::mul($a5, $b1, 22) + self::mul($a6, $b0, 22); |
|
| 2987 | - $s7 = self::mul($a0, $b7, 22) + self::mul($a1, $b6, 22) + self::mul($a2, $b5, 22) + self::mul($a3, $b4, 22) + |
|
| 2988 | - self::mul($a4, $b3, 22) + self::mul($a5, $b2, 22) + self::mul($a6, $b1, 22) + self::mul($a7, $b0, 22); |
|
| 2989 | - $s8 = self::mul($a0, $b8, 22) + self::mul($a1, $b7, 22) + self::mul($a2, $b6, 22) + self::mul($a3, $b5, 22) + |
|
| 2990 | - self::mul($a4, $b4, 22) + self::mul($a5, $b3, 22) + self::mul($a6, $b2, 22) + self::mul($a7, $b1, 22) + |
|
| 2991 | - self::mul($a8, $b0, 22); |
|
| 2992 | - $s9 = self::mul($a0, $b9, 22) + self::mul($a1, $b8, 22) + self::mul($a2, $b7, 22) + self::mul($a3, $b6, 22) + |
|
| 2993 | - self::mul($a4, $b5, 22) + self::mul($a5, $b4, 22) + self::mul($a6, $b3, 22) + self::mul($a7, $b2, 22) + |
|
| 2994 | - self::mul($a8, $b1, 22) + self::mul($a9, $b0, 22); |
|
| 2995 | - $s10 = self::mul($a0, $b10, 22) + self::mul($a1, $b9, 22) + self::mul($a2, $b8, 22) + self::mul($a3, $b7, 22) + |
|
| 2996 | - self::mul($a4, $b6, 22) + self::mul($a5, $b5, 22) + self::mul($a6, $b4, 22) + self::mul($a7, $b3, 22) + |
|
| 2997 | - self::mul($a8, $b2, 22) + self::mul($a9, $b1, 22) + self::mul($a10, $b0, 22); |
|
| 2998 | - $s11 = self::mul($a0, $b11, 22) + self::mul($a1, $b10, 22) + self::mul($a2, $b9, 22) + self::mul($a3, $b8, 22) + |
|
| 2999 | - self::mul($a4, $b7, 22) + self::mul($a5, $b6, 22) + self::mul($a6, $b5, 22) + self::mul($a7, $b4, 22) + |
|
| 3000 | - self::mul($a8, $b3, 22) + self::mul($a9, $b2, 22) + self::mul($a10, $b1, 22) + self::mul($a11, $b0, 22); |
|
| 3001 | - $s12 = self::mul($a1, $b11, 22) + self::mul($a2, $b10, 22) + self::mul($a3, $b9, 22) + self::mul($a4, $b8, 22) + |
|
| 3002 | - self::mul($a5, $b7, 22) + self::mul($a6, $b6, 22) + self::mul($a7, $b5, 22) + self::mul($a8, $b4, 22) + |
|
| 3003 | - self::mul($a9, $b3, 22) + self::mul($a10, $b2, 22) + self::mul($a11, $b1, 22); |
|
| 3004 | - $s13 = self::mul($a2, $b11, 22) + self::mul($a3, $b10, 22) + self::mul($a4, $b9, 22) + self::mul($a5, $b8, 22) + |
|
| 3005 | - self::mul($a6, $b7, 22) + self::mul($a7, $b6, 22) + self::mul($a8, $b5, 22) + self::mul($a9, $b4, 22) + |
|
| 3006 | - self::mul($a10, $b3, 22) + self::mul($a11, $b2, 22); |
|
| 3007 | - $s14 = self::mul($a3, $b11, 22) + self::mul($a4, $b10, 22) + self::mul($a5, $b9, 22) + self::mul($a6, $b8, 22) + |
|
| 3008 | - self::mul($a7, $b7, 22) + self::mul($a8, $b6, 22) + self::mul($a9, $b5, 22) + self::mul($a10, $b4, 22) + |
|
| 3009 | - self::mul($a11, $b3, 22); |
|
| 3010 | - $s15 = self::mul($a4, $b11, 22) + self::mul($a5, $b10, 22) + self::mul($a6, $b9, 22) + self::mul($a7, $b8, 22) + |
|
| 3011 | - self::mul($a8, $b7, 22) + self::mul($a9, $b6, 22) + self::mul($a10, $b5, 22) + self::mul($a11, $b4, 22); |
|
| 3012 | - $s16 = |
|
| 3013 | - self::mul($a5, $b11, 22) + self::mul($a6, $b10, 22) + self::mul($a7, $b9, 22) + self::mul($a8, $b8, 22) + |
|
| 3014 | - self::mul($a9, $b7, 22) + self::mul($a10, $b6, 22) + self::mul($a11, $b5, 22); |
|
| 3015 | - $s17 = self::mul($a6, $b11, 22) + self::mul($a7, $b10, 22) + self::mul($a8, $b9, 22) + self::mul($a9, $b8, 22) + |
|
| 3016 | - self::mul($a10, $b7, 22) + self::mul($a11, $b6, 22); |
|
| 3017 | - $s18 = self::mul($a7, $b11, 22) + self::mul($a8, $b10, 22) + self::mul($a9, $b9, 22) + self::mul($a10, $b8, 22) |
|
| 3018 | - + self::mul($a11, $b7, 22); |
|
| 3019 | - $s19 = self::mul($a8, $b11, 22) + self::mul($a9, $b10, 22) + self::mul($a10, $b9, 22) + |
|
| 3020 | - self::mul($a11, $b8, 22); |
|
| 3021 | - $s20 = self::mul($a9, $b11, 22) + self::mul($a10, $b10, 22) + self::mul($a11, $b9, 22); |
|
| 3022 | - $s21 = self::mul($a10, $b11, 22) + self::mul($a11, $b10, 22); |
|
| 3023 | - $s22 = self::mul($a11, $b11, 22); |
|
| 3024 | - $s23 = 0; |
|
| 3025 | - |
|
| 3026 | - // carry0 = (s0 + (int64_t) (1L << 20)) >> 21; |
|
| 3027 | - // s1 += carry0; |
|
| 3028 | - // s0 -= carry0 * ((uint64_t) 1L << 21); |
|
| 3029 | - $carry0 = ($s0 + (1 << 20)) >> 21; |
|
| 3030 | - $s1 += $carry0; |
|
| 3031 | - $s0 -= $carry0 << 21; |
|
| 3032 | - // carry2 = (s2 + (int64_t) (1L << 20)) >> 21; |
|
| 3033 | - // s3 += carry2; |
|
| 3034 | - // s2 -= carry2 * ((uint64_t) 1L << 21); |
|
| 3035 | - $carry2 = ($s2 + (1 << 20)) >> 21; |
|
| 3036 | - $s3 += $carry2; |
|
| 3037 | - $s2 -= $carry2 << 21; |
|
| 3038 | - // carry4 = (s4 + (int64_t) (1L << 20)) >> 21; |
|
| 3039 | - // s5 += carry4; |
|
| 3040 | - // s4 -= carry4 * ((uint64_t) 1L << 21); |
|
| 3041 | - $carry4 = ($s4 + (1 << 20)) >> 21; |
|
| 3042 | - $s5 += $carry4; |
|
| 3043 | - $s4 -= $carry4 << 21; |
|
| 3044 | - // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; |
|
| 3045 | - // s7 += carry6; |
|
| 3046 | - // s6 -= carry6 * ((uint64_t) 1L << 21); |
|
| 3047 | - $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 3048 | - $s7 += $carry6; |
|
| 3049 | - $s6 -= $carry6 << 21; |
|
| 3050 | - // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; |
|
| 3051 | - // s9 += carry8; |
|
| 3052 | - // s8 -= carry8 * ((uint64_t) 1L << 21); |
|
| 3053 | - $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 3054 | - $s9 += $carry8; |
|
| 3055 | - $s8 -= $carry8 << 21; |
|
| 3056 | - // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; |
|
| 3057 | - // s11 += carry10; |
|
| 3058 | - // s10 -= carry10 * ((uint64_t) 1L << 21); |
|
| 3059 | - $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 3060 | - $s11 += $carry10; |
|
| 3061 | - $s10 -= $carry10 << 21; |
|
| 3062 | - // carry12 = (s12 + (int64_t) (1L << 20)) >> 21; |
|
| 3063 | - // s13 += carry12; |
|
| 3064 | - // s12 -= carry12 * ((uint64_t) 1L << 21); |
|
| 3065 | - $carry12 = ($s12 + (1 << 20)) >> 21; |
|
| 3066 | - $s13 += $carry12; |
|
| 3067 | - $s12 -= $carry12 << 21; |
|
| 3068 | - // carry14 = (s14 + (int64_t) (1L << 20)) >> 21; |
|
| 3069 | - // s15 += carry14; |
|
| 3070 | - // s14 -= carry14 * ((uint64_t) 1L << 21); |
|
| 3071 | - $carry14 = ($s14 + (1 << 20)) >> 21; |
|
| 3072 | - $s15 += $carry14; |
|
| 3073 | - $s14 -= $carry14 << 21; |
|
| 3074 | - // carry16 = (s16 + (int64_t) (1L << 20)) >> 21; |
|
| 3075 | - // s17 += carry16; |
|
| 3076 | - // s16 -= carry16 * ((uint64_t) 1L << 21); |
|
| 3077 | - $carry16 = ($s16 + (1 << 20)) >> 21; |
|
| 3078 | - $s17 += $carry16; |
|
| 3079 | - $s16 -= $carry16 << 21; |
|
| 3080 | - // carry18 = (s18 + (int64_t) (1L << 20)) >> 21; |
|
| 3081 | - // s19 += carry18; |
|
| 3082 | - // s18 -= carry18 * ((uint64_t) 1L << 21); |
|
| 3083 | - $carry18 = ($s18 + (1 << 20)) >> 21; |
|
| 3084 | - $s19 += $carry18; |
|
| 3085 | - $s18 -= $carry18 << 21; |
|
| 3086 | - // carry20 = (s20 + (int64_t) (1L << 20)) >> 21; |
|
| 3087 | - // s21 += carry20; |
|
| 3088 | - // s20 -= carry20 * ((uint64_t) 1L << 21); |
|
| 3089 | - $carry20 = ($s20 + (1 << 20)) >> 21; |
|
| 3090 | - $s21 += $carry20; |
|
| 3091 | - $s20 -= $carry20 << 21; |
|
| 3092 | - // carry22 = (s22 + (int64_t) (1L << 20)) >> 21; |
|
| 3093 | - // s23 += carry22; |
|
| 3094 | - // s22 -= carry22 * ((uint64_t) 1L << 21); |
|
| 3095 | - $carry22 = ($s22 + (1 << 20)) >> 21; |
|
| 3096 | - $s23 += $carry22; |
|
| 3097 | - $s22 -= $carry22 << 21; |
|
| 3098 | - |
|
| 3099 | - // carry1 = (s1 + (int64_t) (1L << 20)) >> 21; |
|
| 3100 | - // s2 += carry1; |
|
| 3101 | - // s1 -= carry1 * ((uint64_t) 1L << 21); |
|
| 3102 | - $carry1 = ($s1 + (1 << 20)) >> 21; |
|
| 3103 | - $s2 += $carry1; |
|
| 3104 | - $s1 -= $carry1 << 21; |
|
| 3105 | - // carry3 = (s3 + (int64_t) (1L << 20)) >> 21; |
|
| 3106 | - // s4 += carry3; |
|
| 3107 | - // s3 -= carry3 * ((uint64_t) 1L << 21); |
|
| 3108 | - $carry3 = ($s3 + (1 << 20)) >> 21; |
|
| 3109 | - $s4 += $carry3; |
|
| 3110 | - $s3 -= $carry3 << 21; |
|
| 3111 | - // carry5 = (s5 + (int64_t) (1L << 20)) >> 21; |
|
| 3112 | - // s6 += carry5; |
|
| 3113 | - // s5 -= carry5 * ((uint64_t) 1L << 21); |
|
| 3114 | - $carry5 = ($s5 + (1 << 20)) >> 21; |
|
| 3115 | - $s6 += $carry5; |
|
| 3116 | - $s5 -= $carry5 << 21; |
|
| 3117 | - // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; |
|
| 3118 | - // s8 += carry7; |
|
| 3119 | - // s7 -= carry7 * ((uint64_t) 1L << 21); |
|
| 3120 | - $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 3121 | - $s8 += $carry7; |
|
| 3122 | - $s7 -= $carry7 << 21; |
|
| 3123 | - // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; |
|
| 3124 | - // s10 += carry9; |
|
| 3125 | - // s9 -= carry9 * ((uint64_t) 1L << 21); |
|
| 3126 | - $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 3127 | - $s10 += $carry9; |
|
| 3128 | - $s9 -= $carry9 << 21; |
|
| 3129 | - // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; |
|
| 3130 | - // s12 += carry11; |
|
| 3131 | - // s11 -= carry11 * ((uint64_t) 1L << 21); |
|
| 3132 | - $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 3133 | - $s12 += $carry11; |
|
| 3134 | - $s11 -= $carry11 << 21; |
|
| 3135 | - // carry13 = (s13 + (int64_t) (1L << 20)) >> 21; |
|
| 3136 | - // s14 += carry13; |
|
| 3137 | - // s13 -= carry13 * ((uint64_t) 1L << 21); |
|
| 3138 | - $carry13 = ($s13 + (1 << 20)) >> 21; |
|
| 3139 | - $s14 += $carry13; |
|
| 3140 | - $s13 -= $carry13 << 21; |
|
| 3141 | - // carry15 = (s15 + (int64_t) (1L << 20)) >> 21; |
|
| 3142 | - // s16 += carry15; |
|
| 3143 | - // s15 -= carry15 * ((uint64_t) 1L << 21); |
|
| 3144 | - $carry15 = ($s15 + (1 << 20)) >> 21; |
|
| 3145 | - $s16 += $carry15; |
|
| 3146 | - $s15 -= $carry15 << 21; |
|
| 3147 | - // carry17 = (s17 + (int64_t) (1L << 20)) >> 21; |
|
| 3148 | - // s18 += carry17; |
|
| 3149 | - // s17 -= carry17 * ((uint64_t) 1L << 21); |
|
| 3150 | - $carry17 = ($s17 + (1 << 20)) >> 21; |
|
| 3151 | - $s18 += $carry17; |
|
| 3152 | - $s17 -= $carry17 << 21; |
|
| 3153 | - // carry19 = (s19 + (int64_t) (1L << 20)) >> 21; |
|
| 3154 | - // s20 += carry19; |
|
| 3155 | - // s19 -= carry19 * ((uint64_t) 1L << 21); |
|
| 3156 | - $carry19 = ($s19 + (1 << 20)) >> 21; |
|
| 3157 | - $s20 += $carry19; |
|
| 3158 | - $s19 -= $carry19 << 21; |
|
| 3159 | - // carry21 = (s21 + (int64_t) (1L << 20)) >> 21; |
|
| 3160 | - // s22 += carry21; |
|
| 3161 | - // s21 -= carry21 * ((uint64_t) 1L << 21); |
|
| 3162 | - $carry21 = ($s21 + (1 << 20)) >> 21; |
|
| 3163 | - $s22 += $carry21; |
|
| 3164 | - $s21 -= $carry21 << 21; |
|
| 3165 | - |
|
| 3166 | - // s11 += s23 * 666643; |
|
| 3167 | - // s12 += s23 * 470296; |
|
| 3168 | - // s13 += s23 * 654183; |
|
| 3169 | - // s14 -= s23 * 997805; |
|
| 3170 | - // s15 += s23 * 136657; |
|
| 3171 | - // s16 -= s23 * 683901; |
|
| 3172 | - $s11 += self::mul($s23, 666643, 20); |
|
| 3173 | - $s12 += self::mul($s23, 470296, 19); |
|
| 3174 | - $s13 += self::mul($s23, 654183, 20); |
|
| 3175 | - $s14 -= self::mul($s23, 997805, 20); |
|
| 3176 | - $s15 += self::mul($s23, 136657, 18); |
|
| 3177 | - $s16 -= self::mul($s23, 683901, 20); |
|
| 3178 | - |
|
| 3179 | - // s10 += s22 * 666643; |
|
| 3180 | - // s11 += s22 * 470296; |
|
| 3181 | - // s12 += s22 * 654183; |
|
| 3182 | - // s13 -= s22 * 997805; |
|
| 3183 | - // s14 += s22 * 136657; |
|
| 3184 | - // s15 -= s22 * 683901; |
|
| 3185 | - $s10 += self::mul($s22, 666643, 20); |
|
| 3186 | - $s11 += self::mul($s22, 470296, 19); |
|
| 3187 | - $s12 += self::mul($s22, 654183, 20); |
|
| 3188 | - $s13 -= self::mul($s22, 997805, 20); |
|
| 3189 | - $s14 += self::mul($s22, 136657, 18); |
|
| 3190 | - $s15 -= self::mul($s22, 683901, 20); |
|
| 3191 | - |
|
| 3192 | - // s9 += s21 * 666643; |
|
| 3193 | - // s10 += s21 * 470296; |
|
| 3194 | - // s11 += s21 * 654183; |
|
| 3195 | - // s12 -= s21 * 997805; |
|
| 3196 | - // s13 += s21 * 136657; |
|
| 3197 | - // s14 -= s21 * 683901; |
|
| 3198 | - $s9 += self::mul($s21, 666643, 20); |
|
| 3199 | - $s10 += self::mul($s21, 470296, 19); |
|
| 3200 | - $s11 += self::mul($s21, 654183, 20); |
|
| 3201 | - $s12 -= self::mul($s21, 997805, 20); |
|
| 3202 | - $s13 += self::mul($s21, 136657, 18); |
|
| 3203 | - $s14 -= self::mul($s21, 683901, 20); |
|
| 3204 | - |
|
| 3205 | - // s8 += s20 * 666643; |
|
| 3206 | - // s9 += s20 * 470296; |
|
| 3207 | - // s10 += s20 * 654183; |
|
| 3208 | - // s11 -= s20 * 997805; |
|
| 3209 | - // s12 += s20 * 136657; |
|
| 3210 | - // s13 -= s20 * 683901; |
|
| 3211 | - $s8 += self::mul($s20, 666643, 20); |
|
| 3212 | - $s9 += self::mul($s20, 470296, 19); |
|
| 3213 | - $s10 += self::mul($s20, 654183, 20); |
|
| 3214 | - $s11 -= self::mul($s20, 997805, 20); |
|
| 3215 | - $s12 += self::mul($s20, 136657, 18); |
|
| 3216 | - $s13 -= self::mul($s20, 683901, 20); |
|
| 3217 | - |
|
| 3218 | - // s7 += s19 * 666643; |
|
| 3219 | - // s8 += s19 * 470296; |
|
| 3220 | - // s9 += s19 * 654183; |
|
| 3221 | - // s10 -= s19 * 997805; |
|
| 3222 | - // s11 += s19 * 136657; |
|
| 3223 | - // s12 -= s19 * 683901; |
|
| 3224 | - $s7 += self::mul($s19, 666643, 20); |
|
| 3225 | - $s8 += self::mul($s19, 470296, 19); |
|
| 3226 | - $s9 += self::mul($s19, 654183, 20); |
|
| 3227 | - $s10 -= self::mul($s19, 997805, 20); |
|
| 3228 | - $s11 += self::mul($s19, 136657, 18); |
|
| 3229 | - $s12 -= self::mul($s19, 683901, 20); |
|
| 3230 | - |
|
| 3231 | - // s6 += s18 * 666643; |
|
| 3232 | - // s7 += s18 * 470296; |
|
| 3233 | - // s8 += s18 * 654183; |
|
| 3234 | - // s9 -= s18 * 997805; |
|
| 3235 | - // s10 += s18 * 136657; |
|
| 3236 | - // s11 -= s18 * 683901; |
|
| 3237 | - $s6 += self::mul($s18, 666643, 20); |
|
| 3238 | - $s7 += self::mul($s18, 470296, 19); |
|
| 3239 | - $s8 += self::mul($s18, 654183, 20); |
|
| 3240 | - $s9 -= self::mul($s18, 997805, 20); |
|
| 3241 | - $s10 += self::mul($s18, 136657, 18); |
|
| 3242 | - $s11 -= self::mul($s18, 683901, 20); |
|
| 3243 | - |
|
| 3244 | - // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; |
|
| 3245 | - // s7 += carry6; |
|
| 3246 | - // s6 -= carry6 * ((uint64_t) 1L << 21); |
|
| 3247 | - $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 3248 | - $s7 += $carry6; |
|
| 3249 | - $s6 -= $carry6 << 21; |
|
| 3250 | - // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; |
|
| 3251 | - // s9 += carry8; |
|
| 3252 | - // s8 -= carry8 * ((uint64_t) 1L << 21); |
|
| 3253 | - $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 3254 | - $s9 += $carry8; |
|
| 3255 | - $s8 -= $carry8 << 21; |
|
| 3256 | - // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; |
|
| 3257 | - // s11 += carry10; |
|
| 3258 | - // s10 -= carry10 * ((uint64_t) 1L << 21); |
|
| 3259 | - $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 3260 | - $s11 += $carry10; |
|
| 3261 | - $s10 -= $carry10 << 21; |
|
| 3262 | - // carry12 = (s12 + (int64_t) (1L << 20)) >> 21; |
|
| 3263 | - // s13 += carry12; |
|
| 3264 | - // s12 -= carry12 * ((uint64_t) 1L << 21); |
|
| 3265 | - $carry12 = ($s12 + (1 << 20)) >> 21; |
|
| 3266 | - $s13 += $carry12; |
|
| 3267 | - $s12 -= $carry12 << 21; |
|
| 3268 | - // carry14 = (s14 + (int64_t) (1L << 20)) >> 21; |
|
| 3269 | - // s15 += carry14; |
|
| 3270 | - // s14 -= carry14 * ((uint64_t) 1L << 21); |
|
| 3271 | - $carry14 = ($s14 + (1 << 20)) >> 21; |
|
| 3272 | - $s15 += $carry14; |
|
| 3273 | - $s14 -= $carry14 << 21; |
|
| 3274 | - // carry16 = (s16 + (int64_t) (1L << 20)) >> 21; |
|
| 3275 | - // s17 += carry16; |
|
| 3276 | - // s16 -= carry16 * ((uint64_t) 1L << 21); |
|
| 3277 | - $carry16 = ($s16 + (1 << 20)) >> 21; |
|
| 3278 | - $s17 += $carry16; |
|
| 3279 | - $s16 -= $carry16 << 21; |
|
| 3280 | - |
|
| 3281 | - // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; |
|
| 3282 | - // s8 += carry7; |
|
| 3283 | - // s7 -= carry7 * ((uint64_t) 1L << 21); |
|
| 3284 | - $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 3285 | - $s8 += $carry7; |
|
| 3286 | - $s7 -= $carry7 << 21; |
|
| 3287 | - // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; |
|
| 3288 | - // s10 += carry9; |
|
| 3289 | - // s9 -= carry9 * ((uint64_t) 1L << 21); |
|
| 3290 | - $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 3291 | - $s10 += $carry9; |
|
| 3292 | - $s9 -= $carry9 << 21; |
|
| 3293 | - // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; |
|
| 3294 | - // s12 += carry11; |
|
| 3295 | - // s11 -= carry11 * ((uint64_t) 1L << 21); |
|
| 3296 | - $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 3297 | - $s12 += $carry11; |
|
| 3298 | - $s11 -= $carry11 << 21; |
|
| 3299 | - // carry13 = (s13 + (int64_t) (1L << 20)) >> 21; |
|
| 3300 | - // s14 += carry13; |
|
| 3301 | - // s13 -= carry13 * ((uint64_t) 1L << 21); |
|
| 3302 | - $carry13 = ($s13 + (1 << 20)) >> 21; |
|
| 3303 | - $s14 += $carry13; |
|
| 3304 | - $s13 -= $carry13 << 21; |
|
| 3305 | - // carry15 = (s15 + (int64_t) (1L << 20)) >> 21; |
|
| 3306 | - // s16 += carry15; |
|
| 3307 | - // s15 -= carry15 * ((uint64_t) 1L << 21); |
|
| 3308 | - $carry15 = ($s15 + (1 << 20)) >> 21; |
|
| 3309 | - $s16 += $carry15; |
|
| 3310 | - $s15 -= $carry15 << 21; |
|
| 3311 | - |
|
| 3312 | - // s5 += s17 * 666643; |
|
| 3313 | - // s6 += s17 * 470296; |
|
| 3314 | - // s7 += s17 * 654183; |
|
| 3315 | - // s8 -= s17 * 997805; |
|
| 3316 | - // s9 += s17 * 136657; |
|
| 3317 | - // s10 -= s17 * 683901; |
|
| 3318 | - $s5 += self::mul($s17, 666643, 20); |
|
| 3319 | - $s6 += self::mul($s17, 470296, 19); |
|
| 3320 | - $s7 += self::mul($s17, 654183, 20); |
|
| 3321 | - $s8 -= self::mul($s17, 997805, 20); |
|
| 3322 | - $s9 += self::mul($s17, 136657, 18); |
|
| 3323 | - $s10 -= self::mul($s17, 683901, 20); |
|
| 3324 | - |
|
| 3325 | - // s4 += s16 * 666643; |
|
| 3326 | - // s5 += s16 * 470296; |
|
| 3327 | - // s6 += s16 * 654183; |
|
| 3328 | - // s7 -= s16 * 997805; |
|
| 3329 | - // s8 += s16 * 136657; |
|
| 3330 | - // s9 -= s16 * 683901; |
|
| 3331 | - $s4 += self::mul($s16, 666643, 20); |
|
| 3332 | - $s5 += self::mul($s16, 470296, 19); |
|
| 3333 | - $s6 += self::mul($s16, 654183, 20); |
|
| 3334 | - $s7 -= self::mul($s16, 997805, 20); |
|
| 3335 | - $s8 += self::mul($s16, 136657, 18); |
|
| 3336 | - $s9 -= self::mul($s16, 683901, 20); |
|
| 3337 | - |
|
| 3338 | - // s3 += s15 * 666643; |
|
| 3339 | - // s4 += s15 * 470296; |
|
| 3340 | - // s5 += s15 * 654183; |
|
| 3341 | - // s6 -= s15 * 997805; |
|
| 3342 | - // s7 += s15 * 136657; |
|
| 3343 | - // s8 -= s15 * 683901; |
|
| 3344 | - $s3 += self::mul($s15, 666643, 20); |
|
| 3345 | - $s4 += self::mul($s15, 470296, 19); |
|
| 3346 | - $s5 += self::mul($s15, 654183, 20); |
|
| 3347 | - $s6 -= self::mul($s15, 997805, 20); |
|
| 3348 | - $s7 += self::mul($s15, 136657, 18); |
|
| 3349 | - $s8 -= self::mul($s15, 683901, 20); |
|
| 3350 | - |
|
| 3351 | - // s2 += s14 * 666643; |
|
| 3352 | - // s3 += s14 * 470296; |
|
| 3353 | - // s4 += s14 * 654183; |
|
| 3354 | - // s5 -= s14 * 997805; |
|
| 3355 | - // s6 += s14 * 136657; |
|
| 3356 | - // s7 -= s14 * 683901; |
|
| 3357 | - $s2 += self::mul($s14, 666643, 20); |
|
| 3358 | - $s3 += self::mul($s14, 470296, 19); |
|
| 3359 | - $s4 += self::mul($s14, 654183, 20); |
|
| 3360 | - $s5 -= self::mul($s14, 997805, 20); |
|
| 3361 | - $s6 += self::mul($s14, 136657, 18); |
|
| 3362 | - $s7 -= self::mul($s14, 683901, 20); |
|
| 3363 | - |
|
| 3364 | - // s1 += s13 * 666643; |
|
| 3365 | - // s2 += s13 * 470296; |
|
| 3366 | - // s3 += s13 * 654183; |
|
| 3367 | - // s4 -= s13 * 997805; |
|
| 3368 | - // s5 += s13 * 136657; |
|
| 3369 | - // s6 -= s13 * 683901; |
|
| 3370 | - $s1 += self::mul($s13, 666643, 20); |
|
| 3371 | - $s2 += self::mul($s13, 470296, 19); |
|
| 3372 | - $s3 += self::mul($s13, 654183, 20); |
|
| 3373 | - $s4 -= self::mul($s13, 997805, 20); |
|
| 3374 | - $s5 += self::mul($s13, 136657, 18); |
|
| 3375 | - $s6 -= self::mul($s13, 683901, 20); |
|
| 3376 | - |
|
| 3377 | - // s0 += s12 * 666643; |
|
| 3378 | - // s1 += s12 * 470296; |
|
| 3379 | - // s2 += s12 * 654183; |
|
| 3380 | - // s3 -= s12 * 997805; |
|
| 3381 | - // s4 += s12 * 136657; |
|
| 3382 | - // s5 -= s12 * 683901; |
|
| 3383 | - // s12 = 0; |
|
| 3384 | - $s0 += self::mul($s12, 666643, 20); |
|
| 3385 | - $s1 += self::mul($s12, 470296, 19); |
|
| 3386 | - $s2 += self::mul($s12, 654183, 20); |
|
| 3387 | - $s3 -= self::mul($s12, 997805, 20); |
|
| 3388 | - $s4 += self::mul($s12, 136657, 18); |
|
| 3389 | - $s5 -= self::mul($s12, 683901, 20); |
|
| 3390 | - $s12 = 0; |
|
| 3391 | - |
|
| 3392 | - // carry0 = (s0 + (int64_t) (1L << 20)) >> 21; |
|
| 3393 | - // s1 += carry0; |
|
| 3394 | - // s0 -= carry0 * ((uint64_t) 1L << 21); |
|
| 3395 | - $carry0 = ($s0 + (1 << 20)) >> 21; |
|
| 3396 | - $s1 += $carry0; |
|
| 3397 | - $s0 -= $carry0 << 21; |
|
| 3398 | - // carry2 = (s2 + (int64_t) (1L << 20)) >> 21; |
|
| 3399 | - // s3 += carry2; |
|
| 3400 | - // s2 -= carry2 * ((uint64_t) 1L << 21); |
|
| 3401 | - $carry2 = ($s2 + (1 << 20)) >> 21; |
|
| 3402 | - $s3 += $carry2; |
|
| 3403 | - $s2 -= $carry2 << 21; |
|
| 3404 | - // carry4 = (s4 + (int64_t) (1L << 20)) >> 21; |
|
| 3405 | - // s5 += carry4; |
|
| 3406 | - // s4 -= carry4 * ((uint64_t) 1L << 21); |
|
| 3407 | - $carry4 = ($s4 + (1 << 20)) >> 21; |
|
| 3408 | - $s5 += $carry4; |
|
| 3409 | - $s4 -= $carry4 << 21; |
|
| 3410 | - // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; |
|
| 3411 | - // s7 += carry6; |
|
| 3412 | - // s6 -= carry6 * ((uint64_t) 1L << 21); |
|
| 3413 | - $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 3414 | - $s7 += $carry6; |
|
| 3415 | - $s6 -= $carry6 << 21; |
|
| 3416 | - // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; |
|
| 3417 | - // s9 += carry8; |
|
| 3418 | - // s8 -= carry8 * ((uint64_t) 1L << 21); |
|
| 3419 | - $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 3420 | - $s9 += $carry8; |
|
| 3421 | - $s8 -= $carry8 << 21; |
|
| 3422 | - // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; |
|
| 3423 | - // s11 += carry10; |
|
| 3424 | - // s10 -= carry10 * ((uint64_t) 1L << 21); |
|
| 3425 | - $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 3426 | - $s11 += $carry10; |
|
| 3427 | - $s10 -= $carry10 << 21; |
|
| 3428 | - |
|
| 3429 | - // carry1 = (s1 + (int64_t) (1L << 20)) >> 21; |
|
| 3430 | - // s2 += carry1; |
|
| 3431 | - // s1 -= carry1 * ((uint64_t) 1L << 21); |
|
| 3432 | - $carry1 = ($s1 + (1 << 20)) >> 21; |
|
| 3433 | - $s2 += $carry1; |
|
| 3434 | - $s1 -= $carry1 << 21; |
|
| 3435 | - // carry3 = (s3 + (int64_t) (1L << 20)) >> 21; |
|
| 3436 | - // s4 += carry3; |
|
| 3437 | - // s3 -= carry3 * ((uint64_t) 1L << 21); |
|
| 3438 | - $carry3 = ($s3 + (1 << 20)) >> 21; |
|
| 3439 | - $s4 += $carry3; |
|
| 3440 | - $s3 -= $carry3 << 21; |
|
| 3441 | - // carry5 = (s5 + (int64_t) (1L << 20)) >> 21; |
|
| 3442 | - // s6 += carry5; |
|
| 3443 | - // s5 -= carry5 * ((uint64_t) 1L << 21); |
|
| 3444 | - $carry5 = ($s5 + (1 << 20)) >> 21; |
|
| 3445 | - $s6 += $carry5; |
|
| 3446 | - $s5 -= $carry5 << 21; |
|
| 3447 | - // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; |
|
| 3448 | - // s8 += carry7; |
|
| 3449 | - // s7 -= carry7 * ((uint64_t) 1L << 21); |
|
| 3450 | - $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 3451 | - $s8 += $carry7; |
|
| 3452 | - $s7 -= $carry7 << 21; |
|
| 3453 | - // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; |
|
| 3454 | - // s10 += carry9; |
|
| 3455 | - // s9 -= carry9 * ((uint64_t) 1L << 21); |
|
| 3456 | - $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 3457 | - $s10 += $carry9; |
|
| 3458 | - $s9 -= $carry9 << 21; |
|
| 3459 | - // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; |
|
| 3460 | - // s12 += carry11; |
|
| 3461 | - // s11 -= carry11 * ((uint64_t) 1L << 21); |
|
| 3462 | - $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 3463 | - $s12 += $carry11; |
|
| 3464 | - $s11 -= $carry11 << 21; |
|
| 3465 | - |
|
| 3466 | - // s0 += s12 * 666643; |
|
| 3467 | - // s1 += s12 * 470296; |
|
| 3468 | - // s2 += s12 * 654183; |
|
| 3469 | - // s3 -= s12 * 997805; |
|
| 3470 | - // s4 += s12 * 136657; |
|
| 3471 | - // s5 -= s12 * 683901; |
|
| 3472 | - // s12 = 0; |
|
| 3473 | - $s0 += self::mul($s12, 666643, 20); |
|
| 3474 | - $s1 += self::mul($s12, 470296, 19); |
|
| 3475 | - $s2 += self::mul($s12, 654183, 20); |
|
| 3476 | - $s3 -= self::mul($s12, 997805, 20); |
|
| 3477 | - $s4 += self::mul($s12, 136657, 18); |
|
| 3478 | - $s5 -= self::mul($s12, 683901, 20); |
|
| 3479 | - $s12 = 0; |
|
| 3480 | - |
|
| 3481 | - // carry0 = s0 >> 21; |
|
| 3482 | - // s1 += carry0; |
|
| 3483 | - // s0 -= carry0 * ((uint64_t) 1L << 21); |
|
| 3484 | - $carry0 = $s0 >> 21; |
|
| 3485 | - $s1 += $carry0; |
|
| 3486 | - $s0 -= $carry0 << 21; |
|
| 3487 | - // carry1 = s1 >> 21; |
|
| 3488 | - // s2 += carry1; |
|
| 3489 | - // s1 -= carry1 * ((uint64_t) 1L << 21); |
|
| 3490 | - $carry1 = $s1 >> 21; |
|
| 3491 | - $s2 += $carry1; |
|
| 3492 | - $s1 -= $carry1 << 21; |
|
| 3493 | - // carry2 = s2 >> 21; |
|
| 3494 | - // s3 += carry2; |
|
| 3495 | - // s2 -= carry2 * ((uint64_t) 1L << 21); |
|
| 3496 | - $carry2 = $s2 >> 21; |
|
| 3497 | - $s3 += $carry2; |
|
| 3498 | - $s2 -= $carry2 << 21; |
|
| 3499 | - // carry3 = s3 >> 21; |
|
| 3500 | - // s4 += carry3; |
|
| 3501 | - // s3 -= carry3 * ((uint64_t) 1L << 21); |
|
| 3502 | - $carry3 = $s3 >> 21; |
|
| 3503 | - $s4 += $carry3; |
|
| 3504 | - $s3 -= $carry3 << 21; |
|
| 3505 | - // carry4 = s4 >> 21; |
|
| 3506 | - // s5 += carry4; |
|
| 3507 | - // s4 -= carry4 * ((uint64_t) 1L << 21); |
|
| 3508 | - $carry4 = $s4 >> 21; |
|
| 3509 | - $s5 += $carry4; |
|
| 3510 | - $s4 -= $carry4 << 21; |
|
| 3511 | - // carry5 = s5 >> 21; |
|
| 3512 | - // s6 += carry5; |
|
| 3513 | - // s5 -= carry5 * ((uint64_t) 1L << 21); |
|
| 3514 | - $carry5 = $s5 >> 21; |
|
| 3515 | - $s6 += $carry5; |
|
| 3516 | - $s5 -= $carry5 << 21; |
|
| 3517 | - // carry6 = s6 >> 21; |
|
| 3518 | - // s7 += carry6; |
|
| 3519 | - // s6 -= carry6 * ((uint64_t) 1L << 21); |
|
| 3520 | - $carry6 = $s6 >> 21; |
|
| 3521 | - $s7 += $carry6; |
|
| 3522 | - $s6 -= $carry6 << 21; |
|
| 3523 | - // carry7 = s7 >> 21; |
|
| 3524 | - // s8 += carry7; |
|
| 3525 | - // s7 -= carry7 * ((uint64_t) 1L << 21); |
|
| 3526 | - $carry7 = $s7 >> 21; |
|
| 3527 | - $s8 += $carry7; |
|
| 3528 | - $s7 -= $carry7 << 21; |
|
| 3529 | - // carry8 = s8 >> 21; |
|
| 3530 | - // s9 += carry8; |
|
| 3531 | - // s8 -= carry8 * ((uint64_t) 1L << 21); |
|
| 3532 | - $carry8 = $s8 >> 21; |
|
| 3533 | - $s9 += $carry8; |
|
| 3534 | - $s8 -= $carry8 << 21; |
|
| 3535 | - // carry9 = s9 >> 21; |
|
| 3536 | - // s10 += carry9; |
|
| 3537 | - // s9 -= carry9 * ((uint64_t) 1L << 21); |
|
| 3538 | - $carry9 = $s9 >> 21; |
|
| 3539 | - $s10 += $carry9; |
|
| 3540 | - $s9 -= $carry9 << 21; |
|
| 3541 | - // carry10 = s10 >> 21; |
|
| 3542 | - // s11 += carry10; |
|
| 3543 | - // s10 -= carry10 * ((uint64_t) 1L << 21); |
|
| 3544 | - $carry10 = $s10 >> 21; |
|
| 3545 | - $s11 += $carry10; |
|
| 3546 | - $s10 -= $carry10 << 21; |
|
| 3547 | - // carry11 = s11 >> 21; |
|
| 3548 | - // s12 += carry11; |
|
| 3549 | - // s11 -= carry11 * ((uint64_t) 1L << 21); |
|
| 3550 | - $carry11 = $s11 >> 21; |
|
| 3551 | - $s12 += $carry11; |
|
| 3552 | - $s11 -= $carry11 << 21; |
|
| 3553 | - |
|
| 3554 | - // s0 += s12 * 666643; |
|
| 3555 | - // s1 += s12 * 470296; |
|
| 3556 | - // s2 += s12 * 654183; |
|
| 3557 | - // s3 -= s12 * 997805; |
|
| 3558 | - // s4 += s12 * 136657; |
|
| 3559 | - // s5 -= s12 * 683901; |
|
| 3560 | - $s0 += self::mul($s12, 666643, 20); |
|
| 3561 | - $s1 += self::mul($s12, 470296, 19); |
|
| 3562 | - $s2 += self::mul($s12, 654183, 20); |
|
| 3563 | - $s3 -= self::mul($s12, 997805, 20); |
|
| 3564 | - $s4 += self::mul($s12, 136657, 18); |
|
| 3565 | - $s5 -= self::mul($s12, 683901, 20); |
|
| 3566 | - |
|
| 3567 | - // carry0 = s0 >> 21; |
|
| 3568 | - // s1 += carry0; |
|
| 3569 | - // s0 -= carry0 * ((uint64_t) 1L << 21); |
|
| 3570 | - $carry0 = $s0 >> 21; |
|
| 3571 | - $s1 += $carry0; |
|
| 3572 | - $s0 -= $carry0 << 21; |
|
| 3573 | - // carry1 = s1 >> 21; |
|
| 3574 | - // s2 += carry1; |
|
| 3575 | - // s1 -= carry1 * ((uint64_t) 1L << 21); |
|
| 3576 | - $carry1 = $s1 >> 21; |
|
| 3577 | - $s2 += $carry1; |
|
| 3578 | - $s1 -= $carry1 << 21; |
|
| 3579 | - // carry2 = s2 >> 21; |
|
| 3580 | - // s3 += carry2; |
|
| 3581 | - // s2 -= carry2 * ((uint64_t) 1L << 21); |
|
| 3582 | - $carry2 = $s2 >> 21; |
|
| 3583 | - $s3 += $carry2; |
|
| 3584 | - $s2 -= $carry2 << 21; |
|
| 3585 | - // carry3 = s3 >> 21; |
|
| 3586 | - // s4 += carry3; |
|
| 3587 | - // s3 -= carry3 * ((uint64_t) 1L << 21); |
|
| 3588 | - $carry3 = $s3 >> 21; |
|
| 3589 | - $s4 += $carry3; |
|
| 3590 | - $s3 -= $carry3 << 21; |
|
| 3591 | - // carry4 = s4 >> 21; |
|
| 3592 | - // s5 += carry4; |
|
| 3593 | - // s4 -= carry4 * ((uint64_t) 1L << 21); |
|
| 3594 | - $carry4 = $s4 >> 21; |
|
| 3595 | - $s5 += $carry4; |
|
| 3596 | - $s4 -= $carry4 << 21; |
|
| 3597 | - // carry5 = s5 >> 21; |
|
| 3598 | - // s6 += carry5; |
|
| 3599 | - // s5 -= carry5 * ((uint64_t) 1L << 21); |
|
| 3600 | - $carry5 = $s5 >> 21; |
|
| 3601 | - $s6 += $carry5; |
|
| 3602 | - $s5 -= $carry5 << 21; |
|
| 3603 | - // carry6 = s6 >> 21; |
|
| 3604 | - // s7 += carry6; |
|
| 3605 | - // s6 -= carry6 * ((uint64_t) 1L << 21); |
|
| 3606 | - $carry6 = $s6 >> 21; |
|
| 3607 | - $s7 += $carry6; |
|
| 3608 | - $s6 -= $carry6 << 21; |
|
| 3609 | - // carry7 = s7 >> 21; |
|
| 3610 | - // s8 += carry7; |
|
| 3611 | - // s7 -= carry7 * ((uint64_t) 1L << 21); |
|
| 3612 | - $carry7 = $s7 >> 21; |
|
| 3613 | - $s8 += $carry7; |
|
| 3614 | - $s7 -= $carry7 << 21; |
|
| 3615 | - // carry8 = s8 >> 21; |
|
| 3616 | - // s9 += carry8; |
|
| 3617 | - // s8 -= carry8 * ((uint64_t) 1L << 21); |
|
| 3618 | - $carry8 = $s8 >> 21; |
|
| 3619 | - $s9 += $carry8; |
|
| 3620 | - $s8 -= $carry8 << 21; |
|
| 3621 | - // carry9 = s9 >> 21; |
|
| 3622 | - // s10 += carry9; |
|
| 3623 | - // s9 -= carry9 * ((uint64_t) 1L << 21); |
|
| 3624 | - $carry9 = $s9 >> 21; |
|
| 3625 | - $s10 += $carry9; |
|
| 3626 | - $s9 -= $carry9 << 21; |
|
| 3627 | - // carry10 = s10 >> 21; |
|
| 3628 | - // s11 += carry10; |
|
| 3629 | - // s10 -= carry10 * ((uint64_t) 1L << 21); |
|
| 3630 | - $carry10 = $s10 >> 21; |
|
| 3631 | - $s11 += $carry10; |
|
| 3632 | - $s10 -= $carry10 << 21; |
|
| 3633 | - |
|
| 3634 | - $s = array_fill(0, 32, 0); |
|
| 3635 | - // s[0] = s0 >> 0; |
|
| 3636 | - $s[0] = $s0 >> 0; |
|
| 3637 | - // s[1] = s0 >> 8; |
|
| 3638 | - $s[1] = $s0 >> 8; |
|
| 3639 | - // s[2] = (s0 >> 16) | (s1 * ((uint64_t) 1 << 5)); |
|
| 3640 | - $s[2] = ($s0 >> 16) | ($s1 << 5); |
|
| 3641 | - // s[3] = s1 >> 3; |
|
| 3642 | - $s[3] = $s1 >> 3; |
|
| 3643 | - // s[4] = s1 >> 11; |
|
| 3644 | - $s[4] = $s1 >> 11; |
|
| 3645 | - // s[5] = (s1 >> 19) | (s2 * ((uint64_t) 1 << 2)); |
|
| 3646 | - $s[5] = ($s1 >> 19) | ($s2 << 2); |
|
| 3647 | - // s[6] = s2 >> 6; |
|
| 3648 | - $s[6] = $s2 >> 6; |
|
| 3649 | - // s[7] = (s2 >> 14) | (s3 * ((uint64_t) 1 << 7)); |
|
| 3650 | - $s[7] = ($s2 >> 14) | ($s3 << 7); |
|
| 3651 | - // s[8] = s3 >> 1; |
|
| 3652 | - $s[8] = $s3 >> 1; |
|
| 3653 | - // s[9] = s3 >> 9; |
|
| 3654 | - $s[9] = $s3 >> 9; |
|
| 3655 | - // s[10] = (s3 >> 17) | (s4 * ((uint64_t) 1 << 4)); |
|
| 3656 | - $s[10] = ($s3 >> 17) | ($s4 << 4); |
|
| 3657 | - // s[11] = s4 >> 4; |
|
| 3658 | - $s[11] = $s4 >> 4; |
|
| 3659 | - // s[12] = s4 >> 12; |
|
| 3660 | - $s[12] = $s4 >> 12; |
|
| 3661 | - // s[13] = (s4 >> 20) | (s5 * ((uint64_t) 1 << 1)); |
|
| 3662 | - $s[13] = ($s4 >> 20) | ($s5 << 1); |
|
| 3663 | - // s[14] = s5 >> 7; |
|
| 3664 | - $s[14] = $s5 >> 7; |
|
| 3665 | - // s[15] = (s5 >> 15) | (s6 * ((uint64_t) 1 << 6)); |
|
| 3666 | - $s[15] = ($s5 >> 15) | ($s6 << 6); |
|
| 3667 | - // s[16] = s6 >> 2; |
|
| 3668 | - $s[16] = $s6 >> 2; |
|
| 3669 | - // s[17] = s6 >> 10; |
|
| 3670 | - $s[17] = $s6 >> 10; |
|
| 3671 | - // s[18] = (s6 >> 18) | (s7 * ((uint64_t) 1 << 3)); |
|
| 3672 | - $s[18] = ($s6 >> 18) | ($s7 << 3); |
|
| 3673 | - // s[19] = s7 >> 5; |
|
| 3674 | - $s[19] = $s7 >> 5; |
|
| 3675 | - // s[20] = s7 >> 13; |
|
| 3676 | - $s[20] = $s7 >> 13; |
|
| 3677 | - // s[21] = s8 >> 0; |
|
| 3678 | - $s[21] = $s8 >> 0; |
|
| 3679 | - // s[22] = s8 >> 8; |
|
| 3680 | - $s[22] = $s8 >> 8; |
|
| 3681 | - // s[23] = (s8 >> 16) | (s9 * ((uint64_t) 1 << 5)); |
|
| 3682 | - $s[23] = ($s8 >> 16) | ($s9 << 5); |
|
| 3683 | - // s[24] = s9 >> 3; |
|
| 3684 | - $s[24] = $s9 >> 3; |
|
| 3685 | - // s[25] = s9 >> 11; |
|
| 3686 | - $s[25] = $s9 >> 11; |
|
| 3687 | - // s[26] = (s9 >> 19) | (s10 * ((uint64_t) 1 << 2)); |
|
| 3688 | - $s[26] = ($s9 >> 19) | ($s10 << 2); |
|
| 3689 | - // s[27] = s10 >> 6; |
|
| 3690 | - $s[27] = $s10 >> 6; |
|
| 3691 | - // s[28] = (s10 >> 14) | (s11 * ((uint64_t) 1 << 7)); |
|
| 3692 | - $s[28] = ($s10 >> 14) | ($s11 << 7); |
|
| 3693 | - // s[29] = s11 >> 1; |
|
| 3694 | - $s[29] = $s11 >> 1; |
|
| 3695 | - // s[30] = s11 >> 9; |
|
| 3696 | - $s[30] = $s11 >> 9; |
|
| 3697 | - // s[31] = s11 >> 17; |
|
| 3698 | - $s[31] = $s11 >> 17; |
|
| 3699 | - return self::intArrayToString($s); |
|
| 3700 | - } |
|
| 3701 | - |
|
| 3702 | - /** |
|
| 3703 | - * @param string $s |
|
| 3704 | - * @return string |
|
| 3705 | - */ |
|
| 3706 | - public static function sc25519_sq($s) |
|
| 3707 | - { |
|
| 3708 | - return self::sc25519_mul($s, $s); |
|
| 3709 | - } |
|
| 3710 | - |
|
| 3711 | - /** |
|
| 3712 | - * @param string $s |
|
| 3713 | - * @param int $n |
|
| 3714 | - * @param string $a |
|
| 3715 | - * @return string |
|
| 3716 | - */ |
|
| 3717 | - public static function sc25519_sqmul($s, $n, $a) |
|
| 3718 | - { |
|
| 3719 | - for ($i = 0; $i < $n; ++$i) { |
|
| 3720 | - $s = self::sc25519_sq($s); |
|
| 3721 | - } |
|
| 3722 | - return self::sc25519_mul($s, $a); |
|
| 3723 | - } |
|
| 3724 | - |
|
| 3725 | - /** |
|
| 3726 | - * @param string $s |
|
| 3727 | - * @return string |
|
| 3728 | - */ |
|
| 3729 | - public static function sc25519_invert($s) |
|
| 3730 | - { |
|
| 3731 | - $_10 = self::sc25519_sq($s); |
|
| 3732 | - $_11 = self::sc25519_mul($s, $_10); |
|
| 3733 | - $_100 = self::sc25519_mul($s, $_11); |
|
| 3734 | - $_1000 = self::sc25519_sq($_100); |
|
| 3735 | - $_1010 = self::sc25519_mul($_10, $_1000); |
|
| 3736 | - $_1011 = self::sc25519_mul($s, $_1010); |
|
| 3737 | - $_10000 = self::sc25519_sq($_1000); |
|
| 3738 | - $_10110 = self::sc25519_sq($_1011); |
|
| 3739 | - $_100000 = self::sc25519_mul($_1010, $_10110); |
|
| 3740 | - $_100110 = self::sc25519_mul($_10000, $_10110); |
|
| 3741 | - $_1000000 = self::sc25519_sq($_100000); |
|
| 3742 | - $_1010000 = self::sc25519_mul($_10000, $_1000000); |
|
| 3743 | - $_1010011 = self::sc25519_mul($_11, $_1010000); |
|
| 3744 | - $_1100011 = self::sc25519_mul($_10000, $_1010011); |
|
| 3745 | - $_1100111 = self::sc25519_mul($_100, $_1100011); |
|
| 3746 | - $_1101011 = self::sc25519_mul($_100, $_1100111); |
|
| 3747 | - $_10010011 = self::sc25519_mul($_1000000, $_1010011); |
|
| 3748 | - $_10010111 = self::sc25519_mul($_100, $_10010011); |
|
| 3749 | - $_10111101 = self::sc25519_mul($_100110, $_10010111); |
|
| 3750 | - $_11010011 = self::sc25519_mul($_10110, $_10111101); |
|
| 3751 | - $_11100111 = self::sc25519_mul($_1010000, $_10010111); |
|
| 3752 | - $_11101011 = self::sc25519_mul($_100, $_11100111); |
|
| 3753 | - $_11110101 = self::sc25519_mul($_1010, $_11101011); |
|
| 3754 | - |
|
| 3755 | - $recip = self::sc25519_mul($_1011, $_11110101); |
|
| 3756 | - $recip = self::sc25519_sqmul($recip, 126, $_1010011); |
|
| 3757 | - $recip = self::sc25519_sqmul($recip, 9, $_10); |
|
| 3758 | - $recip = self::sc25519_mul($recip, $_11110101); |
|
| 3759 | - $recip = self::sc25519_sqmul($recip, 7, $_1100111); |
|
| 3760 | - $recip = self::sc25519_sqmul($recip, 9, $_11110101); |
|
| 3761 | - $recip = self::sc25519_sqmul($recip, 11, $_10111101); |
|
| 3762 | - $recip = self::sc25519_sqmul($recip, 8, $_11100111); |
|
| 3763 | - $recip = self::sc25519_sqmul($recip, 9, $_1101011); |
|
| 3764 | - $recip = self::sc25519_sqmul($recip, 6, $_1011); |
|
| 3765 | - $recip = self::sc25519_sqmul($recip, 14, $_10010011); |
|
| 3766 | - $recip = self::sc25519_sqmul($recip, 10, $_1100011); |
|
| 3767 | - $recip = self::sc25519_sqmul($recip, 9, $_10010111); |
|
| 3768 | - $recip = self::sc25519_sqmul($recip, 10, $_11110101); |
|
| 3769 | - $recip = self::sc25519_sqmul($recip, 8, $_11010011); |
|
| 3770 | - return self::sc25519_sqmul($recip, 8, $_11101011); |
|
| 3771 | - } |
|
| 3772 | - |
|
| 3773 | - /** |
|
| 3774 | - * @param string $s |
|
| 3775 | - * @return string |
|
| 3776 | - */ |
|
| 3777 | - public static function clamp($s) |
|
| 3778 | - { |
|
| 3779 | - $s_ = self::stringToIntArray($s); |
|
| 3780 | - $s_[0] &= 248; |
|
| 3781 | - $s_[31] |= 64; |
|
| 3782 | - $s_[31] &= 128; |
|
| 3783 | - return self::intArrayToString($s_); |
|
| 3784 | - } |
|
| 18 | + /** |
|
| 19 | + * Get a field element of size 10 with a value of 0 |
|
| 20 | + * |
|
| 21 | + * @internal You should not use this directly from another application |
|
| 22 | + * |
|
| 23 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 24 | + */ |
|
| 25 | + public static function fe_0() |
|
| 26 | + { |
|
| 27 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 28 | + array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) |
|
| 29 | + ); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Get a field element of size 10 with a value of 1 |
|
| 34 | + * |
|
| 35 | + * @internal You should not use this directly from another application |
|
| 36 | + * |
|
| 37 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 38 | + */ |
|
| 39 | + public static function fe_1() |
|
| 40 | + { |
|
| 41 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 42 | + array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0) |
|
| 43 | + ); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Add two field elements. |
|
| 48 | + * |
|
| 49 | + * @internal You should not use this directly from another application |
|
| 50 | + * |
|
| 51 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 52 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 53 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 54 | + * @psalm-suppress MixedAssignment |
|
| 55 | + * @psalm-suppress MixedOperand |
|
| 56 | + */ |
|
| 57 | + public static function fe_add( |
|
| 58 | + ParagonIE_Sodium_Core_Curve25519_Fe $f, |
|
| 59 | + ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 60 | + ) { |
|
| 61 | + /** @var array<int, int> $arr */ |
|
| 62 | + $arr = array(); |
|
| 63 | + for ($i = 0; $i < 10; ++$i) { |
|
| 64 | + $arr[$i] = (int) ($f[$i] + $g[$i]); |
|
| 65 | + } |
|
| 66 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($arr); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Constant-time conditional move. |
|
| 71 | + * |
|
| 72 | + * @internal You should not use this directly from another application |
|
| 73 | + * |
|
| 74 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 75 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 76 | + * @param int $b |
|
| 77 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 78 | + * @psalm-suppress MixedAssignment |
|
| 79 | + */ |
|
| 80 | + public static function fe_cmov( |
|
| 81 | + ParagonIE_Sodium_Core_Curve25519_Fe $f, |
|
| 82 | + ParagonIE_Sodium_Core_Curve25519_Fe $g, |
|
| 83 | + $b = 0 |
|
| 84 | + ) { |
|
| 85 | + /** @var array<int, int> $h */ |
|
| 86 | + $h = array(); |
|
| 87 | + $b *= -1; |
|
| 88 | + for ($i = 0; $i < 10; ++$i) { |
|
| 89 | + $x = (($f[$i] ^ $g[$i]) & $b); |
|
| 90 | + $h[$i] = ($f[$i]) ^ $x; |
|
| 91 | + } |
|
| 92 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($h); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Create a copy of a field element. |
|
| 97 | + * |
|
| 98 | + * @internal You should not use this directly from another application |
|
| 99 | + * |
|
| 100 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 101 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 102 | + */ |
|
| 103 | + public static function fe_copy(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 104 | + { |
|
| 105 | + $h = clone $f; |
|
| 106 | + return $h; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Give: 32-byte string. |
|
| 111 | + * Receive: A field element object to use for internal calculations. |
|
| 112 | + * |
|
| 113 | + * @internal You should not use this directly from another application |
|
| 114 | + * |
|
| 115 | + * @param string $s |
|
| 116 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 117 | + * @throws RangeException |
|
| 118 | + * @throws TypeError |
|
| 119 | + */ |
|
| 120 | + public static function fe_frombytes($s) |
|
| 121 | + { |
|
| 122 | + if (self::strlen($s) !== 32) { |
|
| 123 | + throw new RangeException('Expected a 32-byte string.'); |
|
| 124 | + } |
|
| 125 | + $h0 = self::load_4($s); |
|
| 126 | + $h1 = self::load_3(self::substr($s, 4, 3)) << 6; |
|
| 127 | + $h2 = self::load_3(self::substr($s, 7, 3)) << 5; |
|
| 128 | + $h3 = self::load_3(self::substr($s, 10, 3)) << 3; |
|
| 129 | + $h4 = self::load_3(self::substr($s, 13, 3)) << 2; |
|
| 130 | + $h5 = self::load_4(self::substr($s, 16, 4)); |
|
| 131 | + $h6 = self::load_3(self::substr($s, 20, 3)) << 7; |
|
| 132 | + $h7 = self::load_3(self::substr($s, 23, 3)) << 5; |
|
| 133 | + $h8 = self::load_3(self::substr($s, 26, 3)) << 4; |
|
| 134 | + $h9 = (self::load_3(self::substr($s, 29, 3)) & 8388607) << 2; |
|
| 135 | + |
|
| 136 | + $carry9 = ($h9 + (1 << 24)) >> 25; |
|
| 137 | + $h0 += self::mul($carry9, 19, 5); |
|
| 138 | + $h9 -= $carry9 << 25; |
|
| 139 | + $carry1 = ($h1 + (1 << 24)) >> 25; |
|
| 140 | + $h2 += $carry1; |
|
| 141 | + $h1 -= $carry1 << 25; |
|
| 142 | + $carry3 = ($h3 + (1 << 24)) >> 25; |
|
| 143 | + $h4 += $carry3; |
|
| 144 | + $h3 -= $carry3 << 25; |
|
| 145 | + $carry5 = ($h5 + (1 << 24)) >> 25; |
|
| 146 | + $h6 += $carry5; |
|
| 147 | + $h5 -= $carry5 << 25; |
|
| 148 | + $carry7 = ($h7 + (1 << 24)) >> 25; |
|
| 149 | + $h8 += $carry7; |
|
| 150 | + $h7 -= $carry7 << 25; |
|
| 151 | + |
|
| 152 | + $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 153 | + $h1 += $carry0; |
|
| 154 | + $h0 -= $carry0 << 26; |
|
| 155 | + $carry2 = ($h2 + (1 << 25)) >> 26; |
|
| 156 | + $h3 += $carry2; |
|
| 157 | + $h2 -= $carry2 << 26; |
|
| 158 | + $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 159 | + $h5 += $carry4; |
|
| 160 | + $h4 -= $carry4 << 26; |
|
| 161 | + $carry6 = ($h6 + (1 << 25)) >> 26; |
|
| 162 | + $h7 += $carry6; |
|
| 163 | + $h6 -= $carry6 << 26; |
|
| 164 | + $carry8 = ($h8 + (1 << 25)) >> 26; |
|
| 165 | + $h9 += $carry8; |
|
| 166 | + $h8 -= $carry8 << 26; |
|
| 167 | + |
|
| 168 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 169 | + array( |
|
| 170 | + (int) $h0, |
|
| 171 | + (int) $h1, |
|
| 172 | + (int) $h2, |
|
| 173 | + (int) $h3, |
|
| 174 | + (int) $h4, |
|
| 175 | + (int) $h5, |
|
| 176 | + (int) $h6, |
|
| 177 | + (int) $h7, |
|
| 178 | + (int) $h8, |
|
| 179 | + (int) $h9 |
|
| 180 | + ) |
|
| 181 | + ); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Convert a field element to a byte string. |
|
| 186 | + * |
|
| 187 | + * @internal You should not use this directly from another application |
|
| 188 | + * |
|
| 189 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $h |
|
| 190 | + * @return string |
|
| 191 | + */ |
|
| 192 | + public static function fe_tobytes(ParagonIE_Sodium_Core_Curve25519_Fe $h) |
|
| 193 | + { |
|
| 194 | + $h0 = (int) $h[0]; |
|
| 195 | + $h1 = (int) $h[1]; |
|
| 196 | + $h2 = (int) $h[2]; |
|
| 197 | + $h3 = (int) $h[3]; |
|
| 198 | + $h4 = (int) $h[4]; |
|
| 199 | + $h5 = (int) $h[5]; |
|
| 200 | + $h6 = (int) $h[6]; |
|
| 201 | + $h7 = (int) $h[7]; |
|
| 202 | + $h8 = (int) $h[8]; |
|
| 203 | + $h9 = (int) $h[9]; |
|
| 204 | + |
|
| 205 | + $q = (self::mul($h9, 19, 5) + (1 << 24)) >> 25; |
|
| 206 | + $q = ($h0 + $q) >> 26; |
|
| 207 | + $q = ($h1 + $q) >> 25; |
|
| 208 | + $q = ($h2 + $q) >> 26; |
|
| 209 | + $q = ($h3 + $q) >> 25; |
|
| 210 | + $q = ($h4 + $q) >> 26; |
|
| 211 | + $q = ($h5 + $q) >> 25; |
|
| 212 | + $q = ($h6 + $q) >> 26; |
|
| 213 | + $q = ($h7 + $q) >> 25; |
|
| 214 | + $q = ($h8 + $q) >> 26; |
|
| 215 | + $q = ($h9 + $q) >> 25; |
|
| 216 | + |
|
| 217 | + $h0 += self::mul($q, 19, 5); |
|
| 218 | + |
|
| 219 | + $carry0 = $h0 >> 26; |
|
| 220 | + $h1 += $carry0; |
|
| 221 | + $h0 -= $carry0 << 26; |
|
| 222 | + $carry1 = $h1 >> 25; |
|
| 223 | + $h2 += $carry1; |
|
| 224 | + $h1 -= $carry1 << 25; |
|
| 225 | + $carry2 = $h2 >> 26; |
|
| 226 | + $h3 += $carry2; |
|
| 227 | + $h2 -= $carry2 << 26; |
|
| 228 | + $carry3 = $h3 >> 25; |
|
| 229 | + $h4 += $carry3; |
|
| 230 | + $h3 -= $carry3 << 25; |
|
| 231 | + $carry4 = $h4 >> 26; |
|
| 232 | + $h5 += $carry4; |
|
| 233 | + $h4 -= $carry4 << 26; |
|
| 234 | + $carry5 = $h5 >> 25; |
|
| 235 | + $h6 += $carry5; |
|
| 236 | + $h5 -= $carry5 << 25; |
|
| 237 | + $carry6 = $h6 >> 26; |
|
| 238 | + $h7 += $carry6; |
|
| 239 | + $h6 -= $carry6 << 26; |
|
| 240 | + $carry7 = $h7 >> 25; |
|
| 241 | + $h8 += $carry7; |
|
| 242 | + $h7 -= $carry7 << 25; |
|
| 243 | + $carry8 = $h8 >> 26; |
|
| 244 | + $h9 += $carry8; |
|
| 245 | + $h8 -= $carry8 << 26; |
|
| 246 | + $carry9 = $h9 >> 25; |
|
| 247 | + $h9 -= $carry9 << 25; |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * @var array<int, int> |
|
| 251 | + */ |
|
| 252 | + $s = array( |
|
| 253 | + (int) (($h0 >> 0) & 0xff), |
|
| 254 | + (int) (($h0 >> 8) & 0xff), |
|
| 255 | + (int) (($h0 >> 16) & 0xff), |
|
| 256 | + (int) ((($h0 >> 24) | ($h1 << 2)) & 0xff), |
|
| 257 | + (int) (($h1 >> 6) & 0xff), |
|
| 258 | + (int) (($h1 >> 14) & 0xff), |
|
| 259 | + (int) ((($h1 >> 22) | ($h2 << 3)) & 0xff), |
|
| 260 | + (int) (($h2 >> 5) & 0xff), |
|
| 261 | + (int) (($h2 >> 13) & 0xff), |
|
| 262 | + (int) ((($h2 >> 21) | ($h3 << 5)) & 0xff), |
|
| 263 | + (int) (($h3 >> 3) & 0xff), |
|
| 264 | + (int) (($h3 >> 11) & 0xff), |
|
| 265 | + (int) ((($h3 >> 19) | ($h4 << 6)) & 0xff), |
|
| 266 | + (int) (($h4 >> 2) & 0xff), |
|
| 267 | + (int) (($h4 >> 10) & 0xff), |
|
| 268 | + (int) (($h4 >> 18) & 0xff), |
|
| 269 | + (int) (($h5 >> 0) & 0xff), |
|
| 270 | + (int) (($h5 >> 8) & 0xff), |
|
| 271 | + (int) (($h5 >> 16) & 0xff), |
|
| 272 | + (int) ((($h5 >> 24) | ($h6 << 1)) & 0xff), |
|
| 273 | + (int) (($h6 >> 7) & 0xff), |
|
| 274 | + (int) (($h6 >> 15) & 0xff), |
|
| 275 | + (int) ((($h6 >> 23) | ($h7 << 3)) & 0xff), |
|
| 276 | + (int) (($h7 >> 5) & 0xff), |
|
| 277 | + (int) (($h7 >> 13) & 0xff), |
|
| 278 | + (int) ((($h7 >> 21) | ($h8 << 4)) & 0xff), |
|
| 279 | + (int) (($h8 >> 4) & 0xff), |
|
| 280 | + (int) (($h8 >> 12) & 0xff), |
|
| 281 | + (int) ((($h8 >> 20) | ($h9 << 6)) & 0xff), |
|
| 282 | + (int) (($h9 >> 2) & 0xff), |
|
| 283 | + (int) (($h9 >> 10) & 0xff), |
|
| 284 | + (int) (($h9 >> 18) & 0xff) |
|
| 285 | + ); |
|
| 286 | + return self::intArrayToString($s); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Is a field element negative? (1 = yes, 0 = no. Used in calculations.) |
|
| 291 | + * |
|
| 292 | + * @internal You should not use this directly from another application |
|
| 293 | + * |
|
| 294 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 295 | + * @return int |
|
| 296 | + * @throws SodiumException |
|
| 297 | + * @throws TypeError |
|
| 298 | + */ |
|
| 299 | + public static function fe_isnegative(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 300 | + { |
|
| 301 | + $str = self::fe_tobytes($f); |
|
| 302 | + return (int) (self::chrToInt($str[0]) & 1); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Returns 0 if this field element results in all NUL bytes. |
|
| 307 | + * |
|
| 308 | + * @internal You should not use this directly from another application |
|
| 309 | + * |
|
| 310 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 311 | + * @return bool |
|
| 312 | + * @throws SodiumException |
|
| 313 | + * @throws TypeError |
|
| 314 | + */ |
|
| 315 | + public static function fe_isnonzero(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 316 | + { |
|
| 317 | + static $zero; |
|
| 318 | + if ($zero === null) { |
|
| 319 | + $zero = str_repeat("\x00", 32); |
|
| 320 | + } |
|
| 321 | + /** @var string $zero */ |
|
| 322 | + /** @var string $str */ |
|
| 323 | + $str = self::fe_tobytes($f); |
|
| 324 | + return !self::verify_32($str, (string) $zero); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Multiply two field elements |
|
| 329 | + * |
|
| 330 | + * h = f * g |
|
| 331 | + * |
|
| 332 | + * @internal You should not use this directly from another application |
|
| 333 | + * |
|
| 334 | + * @security Is multiplication a source of timing leaks? If so, can we do |
|
| 335 | + * anything to prevent that from happening? |
|
| 336 | + * |
|
| 337 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 338 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 339 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 340 | + */ |
|
| 341 | + public static function fe_mul( |
|
| 342 | + ParagonIE_Sodium_Core_Curve25519_Fe $f, |
|
| 343 | + ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 344 | + ) { |
|
| 345 | + $f0 = $f[0]; |
|
| 346 | + $f1 = $f[1]; |
|
| 347 | + $f2 = $f[2]; |
|
| 348 | + $f3 = $f[3]; |
|
| 349 | + $f4 = $f[4]; |
|
| 350 | + $f5 = $f[5]; |
|
| 351 | + $f6 = $f[6]; |
|
| 352 | + $f7 = $f[7]; |
|
| 353 | + $f8 = $f[8]; |
|
| 354 | + $f9 = $f[9]; |
|
| 355 | + $g0 = $g[0]; |
|
| 356 | + $g1 = $g[1]; |
|
| 357 | + $g2 = $g[2]; |
|
| 358 | + $g3 = $g[3]; |
|
| 359 | + $g4 = $g[4]; |
|
| 360 | + $g5 = $g[5]; |
|
| 361 | + $g6 = $g[6]; |
|
| 362 | + $g7 = $g[7]; |
|
| 363 | + $g8 = $g[8]; |
|
| 364 | + $g9 = $g[9]; |
|
| 365 | + $g1_19 = self::mul($g1, 19, 5); |
|
| 366 | + $g2_19 = self::mul($g2, 19, 5); |
|
| 367 | + $g3_19 = self::mul($g3, 19, 5); |
|
| 368 | + $g4_19 = self::mul($g4, 19, 5); |
|
| 369 | + $g5_19 = self::mul($g5, 19, 5); |
|
| 370 | + $g6_19 = self::mul($g6, 19, 5); |
|
| 371 | + $g7_19 = self::mul($g7, 19, 5); |
|
| 372 | + $g8_19 = self::mul($g8, 19, 5); |
|
| 373 | + $g9_19 = self::mul($g9, 19, 5); |
|
| 374 | + $f1_2 = $f1 << 1; |
|
| 375 | + $f3_2 = $f3 << 1; |
|
| 376 | + $f5_2 = $f5 << 1; |
|
| 377 | + $f7_2 = $f7 << 1; |
|
| 378 | + $f9_2 = $f9 << 1; |
|
| 379 | + $f0g0 = self::mul($f0, $g0, 26); |
|
| 380 | + $f0g1 = self::mul($f0, $g1, 25); |
|
| 381 | + $f0g2 = self::mul($f0, $g2, 26); |
|
| 382 | + $f0g3 = self::mul($f0, $g3, 25); |
|
| 383 | + $f0g4 = self::mul($f0, $g4, 26); |
|
| 384 | + $f0g5 = self::mul($f0, $g5, 25); |
|
| 385 | + $f0g6 = self::mul($f0, $g6, 26); |
|
| 386 | + $f0g7 = self::mul($f0, $g7, 25); |
|
| 387 | + $f0g8 = self::mul($f0, $g8, 26); |
|
| 388 | + $f0g9 = self::mul($f0, $g9, 26); |
|
| 389 | + $f1g0 = self::mul($f1, $g0, 26); |
|
| 390 | + $f1g1_2 = self::mul($f1_2, $g1, 25); |
|
| 391 | + $f1g2 = self::mul($f1, $g2, 26); |
|
| 392 | + $f1g3_2 = self::mul($f1_2, $g3, 25); |
|
| 393 | + $f1g4 = self::mul($f1, $g4, 26); |
|
| 394 | + $f1g5_2 = self::mul($f1_2, $g5, 25); |
|
| 395 | + $f1g6 = self::mul($f1, $g6, 26); |
|
| 396 | + $f1g7_2 = self::mul($f1_2, $g7, 25); |
|
| 397 | + $f1g8 = self::mul($f1, $g8, 26); |
|
| 398 | + $f1g9_38 = self::mul($g9_19, $f1_2, 26); |
|
| 399 | + $f2g0 = self::mul($f2, $g0, 26); |
|
| 400 | + $f2g1 = self::mul($f2, $g1, 25); |
|
| 401 | + $f2g2 = self::mul($f2, $g2, 26); |
|
| 402 | + $f2g3 = self::mul($f2, $g3, 25); |
|
| 403 | + $f2g4 = self::mul($f2, $g4, 26); |
|
| 404 | + $f2g5 = self::mul($f2, $g5, 25); |
|
| 405 | + $f2g6 = self::mul($f2, $g6, 26); |
|
| 406 | + $f2g7 = self::mul($f2, $g7, 25); |
|
| 407 | + $f2g8_19 = self::mul($g8_19, $f2, 26); |
|
| 408 | + $f2g9_19 = self::mul($g9_19, $f2, 26); |
|
| 409 | + $f3g0 = self::mul($f3, $g0, 26); |
|
| 410 | + $f3g1_2 = self::mul($f3_2, $g1, 25); |
|
| 411 | + $f3g2 = self::mul($f3, $g2, 26); |
|
| 412 | + $f3g3_2 = self::mul($f3_2, $g3, 25); |
|
| 413 | + $f3g4 = self::mul($f3, $g4, 26); |
|
| 414 | + $f3g5_2 = self::mul($f3_2, $g5, 25); |
|
| 415 | + $f3g6 = self::mul($f3, $g6, 26); |
|
| 416 | + $f3g7_38 = self::mul($g7_19, $f3_2, 26); |
|
| 417 | + $f3g8_19 = self::mul($g8_19, $f3, 25); |
|
| 418 | + $f3g9_38 = self::mul($g9_19, $f3_2, 26); |
|
| 419 | + $f4g0 = self::mul($f4, $g0, 26); |
|
| 420 | + $f4g1 = self::mul($f4, $g1, 25); |
|
| 421 | + $f4g2 = self::mul($f4, $g2, 26); |
|
| 422 | + $f4g3 = self::mul($f4, $g3, 25); |
|
| 423 | + $f4g4 = self::mul($f4, $g4, 26); |
|
| 424 | + $f4g5 = self::mul($f4, $g5, 25); |
|
| 425 | + $f4g6_19 = self::mul($g6_19, $f4, 26); |
|
| 426 | + $f4g7_19 = self::mul($g7_19, $f4, 26); |
|
| 427 | + $f4g8_19 = self::mul($g8_19, $f4, 26); |
|
| 428 | + $f4g9_19 = self::mul($g9_19, $f4, 26); |
|
| 429 | + $f5g0 = self::mul($f5, $g0, 26); |
|
| 430 | + $f5g1_2 = self::mul($f5_2, $g1, 25); |
|
| 431 | + $f5g2 = self::mul($f5, $g2, 26); |
|
| 432 | + $f5g3_2 = self::mul($f5_2, $g3, 25); |
|
| 433 | + $f5g4 = self::mul($f5, $g4, 26); |
|
| 434 | + $f5g5_38 = self::mul($g5_19, $f5_2, 26); |
|
| 435 | + $f5g6_19 = self::mul($g6_19, $f5, 25); |
|
| 436 | + $f5g7_38 = self::mul($g7_19, $f5_2, 26); |
|
| 437 | + $f5g8_19 = self::mul($g8_19, $f5, 25); |
|
| 438 | + $f5g9_38 = self::mul($g9_19, $f5_2, 26); |
|
| 439 | + $f6g0 = self::mul($f6, $g0, 26); |
|
| 440 | + $f6g1 = self::mul($f6, $g1, 25); |
|
| 441 | + $f6g2 = self::mul($f6, $g2, 26); |
|
| 442 | + $f6g3 = self::mul($f6, $g3, 25); |
|
| 443 | + $f6g4_19 = self::mul($g4_19, $f6, 26); |
|
| 444 | + $f6g5_19 = self::mul($g5_19, $f6, 26); |
|
| 445 | + $f6g6_19 = self::mul($g6_19, $f6, 26); |
|
| 446 | + $f6g7_19 = self::mul($g7_19, $f6, 26); |
|
| 447 | + $f6g8_19 = self::mul($g8_19, $f6, 26); |
|
| 448 | + $f6g9_19 = self::mul($g9_19, $f6, 26); |
|
| 449 | + $f7g0 = self::mul($f7, $g0, 26); |
|
| 450 | + $f7g1_2 = self::mul($f7_2, $g1, 25); |
|
| 451 | + $f7g2 = self::mul($f7, $g2, 26); |
|
| 452 | + $f7g3_38 = self::mul($g3_19, $f7_2, 26); |
|
| 453 | + $f7g4_19 = self::mul($g4_19, $f7, 26); |
|
| 454 | + $f7g5_38 = self::mul($g5_19, $f7_2, 26); |
|
| 455 | + $f7g6_19 = self::mul($g6_19, $f7, 25); |
|
| 456 | + $f7g7_38 = self::mul($g7_19, $f7_2, 26); |
|
| 457 | + $f7g8_19 = self::mul($g8_19, $f7, 25); |
|
| 458 | + $f7g9_38 = self::mul($g9_19,$f7_2, 26); |
|
| 459 | + $f8g0 = self::mul($f8, $g0, 26); |
|
| 460 | + $f8g1 = self::mul($f8, $g1, 25); |
|
| 461 | + $f8g2_19 = self::mul($g2_19, $f8, 26); |
|
| 462 | + $f8g3_19 = self::mul($g3_19, $f8, 26); |
|
| 463 | + $f8g4_19 = self::mul($g4_19, $f8, 26); |
|
| 464 | + $f8g5_19 = self::mul($g5_19, $f8, 26); |
|
| 465 | + $f8g6_19 = self::mul($g6_19, $f8, 26); |
|
| 466 | + $f8g7_19 = self::mul($g7_19, $f8, 26); |
|
| 467 | + $f8g8_19 = self::mul($g8_19, $f8, 26); |
|
| 468 | + $f8g9_19 = self::mul($g9_19, $f8, 26); |
|
| 469 | + $f9g0 = self::mul($f9, $g0, 26); |
|
| 470 | + $f9g1_38 = self::mul($g1_19, $f9_2, 26); |
|
| 471 | + $f9g2_19 = self::mul($g2_19, $f9, 25); |
|
| 472 | + $f9g3_38 = self::mul($g3_19, $f9_2, 26); |
|
| 473 | + $f9g4_19 = self::mul($g4_19, $f9, 25); |
|
| 474 | + $f9g5_38 = self::mul($g5_19, $f9_2, 26); |
|
| 475 | + $f9g6_19 = self::mul($g6_19, $f9, 25); |
|
| 476 | + $f9g7_38 = self::mul($g7_19, $f9_2, 26); |
|
| 477 | + $f9g8_19 = self::mul($g8_19, $f9, 25); |
|
| 478 | + $f9g9_38 = self::mul($g9_19, $f9_2, 26); |
|
| 479 | + $h0 = $f0g0 + $f1g9_38 + $f2g8_19 + $f3g7_38 + $f4g6_19 + $f5g5_38 + $f6g4_19 + $f7g3_38 + $f8g2_19 + $f9g1_38; |
|
| 480 | + $h1 = $f0g1 + $f1g0 + $f2g9_19 + $f3g8_19 + $f4g7_19 + $f5g6_19 + $f6g5_19 + $f7g4_19 + $f8g3_19 + $f9g2_19; |
|
| 481 | + $h2 = $f0g2 + $f1g1_2 + $f2g0 + $f3g9_38 + $f4g8_19 + $f5g7_38 + $f6g6_19 + $f7g5_38 + $f8g4_19 + $f9g3_38; |
|
| 482 | + $h3 = $f0g3 + $f1g2 + $f2g1 + $f3g0 + $f4g9_19 + $f5g8_19 + $f6g7_19 + $f7g6_19 + $f8g5_19 + $f9g4_19; |
|
| 483 | + $h4 = $f0g4 + $f1g3_2 + $f2g2 + $f3g1_2 + $f4g0 + $f5g9_38 + $f6g8_19 + $f7g7_38 + $f8g6_19 + $f9g5_38; |
|
| 484 | + $h5 = $f0g5 + $f1g4 + $f2g3 + $f3g2 + $f4g1 + $f5g0 + $f6g9_19 + $f7g8_19 + $f8g7_19 + $f9g6_19; |
|
| 485 | + $h6 = $f0g6 + $f1g5_2 + $f2g4 + $f3g3_2 + $f4g2 + $f5g1_2 + $f6g0 + $f7g9_38 + $f8g8_19 + $f9g7_38; |
|
| 486 | + $h7 = $f0g7 + $f1g6 + $f2g5 + $f3g4 + $f4g3 + $f5g2 + $f6g1 + $f7g0 + $f8g9_19 + $f9g8_19; |
|
| 487 | + $h8 = $f0g8 + $f1g7_2 + $f2g6 + $f3g5_2 + $f4g4 + $f5g3_2 + $f6g2 + $f7g1_2 + $f8g0 + $f9g9_38; |
|
| 488 | + $h9 = $f0g9 + $f1g8 + $f2g7 + $f3g6 + $f4g5 + $f5g4 + $f6g3 + $f7g2 + $f8g1 + $f9g0 ; |
|
| 489 | + |
|
| 490 | + $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 491 | + $h1 += $carry0; |
|
| 492 | + $h0 -= $carry0 << 26; |
|
| 493 | + $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 494 | + $h5 += $carry4; |
|
| 495 | + $h4 -= $carry4 << 26; |
|
| 496 | + |
|
| 497 | + $carry1 = ($h1 + (1 << 24)) >> 25; |
|
| 498 | + $h2 += $carry1; |
|
| 499 | + $h1 -= $carry1 << 25; |
|
| 500 | + $carry5 = ($h5 + (1 << 24)) >> 25; |
|
| 501 | + $h6 += $carry5; |
|
| 502 | + $h5 -= $carry5 << 25; |
|
| 503 | + |
|
| 504 | + $carry2 = ($h2 + (1 << 25)) >> 26; |
|
| 505 | + $h3 += $carry2; |
|
| 506 | + $h2 -= $carry2 << 26; |
|
| 507 | + $carry6 = ($h6 + (1 << 25)) >> 26; |
|
| 508 | + $h7 += $carry6; |
|
| 509 | + $h6 -= $carry6 << 26; |
|
| 510 | + |
|
| 511 | + $carry3 = ($h3 + (1 << 24)) >> 25; |
|
| 512 | + $h4 += $carry3; |
|
| 513 | + $h3 -= $carry3 << 25; |
|
| 514 | + $carry7 = ($h7 + (1 << 24)) >> 25; |
|
| 515 | + $h8 += $carry7; |
|
| 516 | + $h7 -= $carry7 << 25; |
|
| 517 | + |
|
| 518 | + $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 519 | + $h5 += $carry4; |
|
| 520 | + $h4 -= $carry4 << 26; |
|
| 521 | + $carry8 = ($h8 + (1 << 25)) >> 26; |
|
| 522 | + $h9 += $carry8; |
|
| 523 | + $h8 -= $carry8 << 26; |
|
| 524 | + |
|
| 525 | + $carry9 = ($h9 + (1 << 24)) >> 25; |
|
| 526 | + $h0 += self::mul($carry9, 19, 5); |
|
| 527 | + $h9 -= $carry9 << 25; |
|
| 528 | + |
|
| 529 | + $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 530 | + $h1 += $carry0; |
|
| 531 | + $h0 -= $carry0 << 26; |
|
| 532 | + |
|
| 533 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 534 | + array( |
|
| 535 | + (int) $h0, |
|
| 536 | + (int) $h1, |
|
| 537 | + (int) $h2, |
|
| 538 | + (int) $h3, |
|
| 539 | + (int) $h4, |
|
| 540 | + (int) $h5, |
|
| 541 | + (int) $h6, |
|
| 542 | + (int) $h7, |
|
| 543 | + (int) $h8, |
|
| 544 | + (int) $h9 |
|
| 545 | + ) |
|
| 546 | + ); |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + /** |
|
| 550 | + * Get the negative values for each piece of the field element. |
|
| 551 | + * |
|
| 552 | + * h = -f |
|
| 553 | + * |
|
| 554 | + * @internal You should not use this directly from another application |
|
| 555 | + * |
|
| 556 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 557 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 558 | + * @psalm-suppress MixedAssignment |
|
| 559 | + */ |
|
| 560 | + public static function fe_neg(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 561 | + { |
|
| 562 | + $h = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 563 | + for ($i = 0; $i < 10; ++$i) { |
|
| 564 | + $h[$i] = -$f[$i]; |
|
| 565 | + } |
|
| 566 | + return $h; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Square a field element |
|
| 571 | + * |
|
| 572 | + * h = f * f |
|
| 573 | + * |
|
| 574 | + * @internal You should not use this directly from another application |
|
| 575 | + * |
|
| 576 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 577 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 578 | + */ |
|
| 579 | + public static function fe_sq(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 580 | + { |
|
| 581 | + $f0 = (int) $f[0]; |
|
| 582 | + $f1 = (int) $f[1]; |
|
| 583 | + $f2 = (int) $f[2]; |
|
| 584 | + $f3 = (int) $f[3]; |
|
| 585 | + $f4 = (int) $f[4]; |
|
| 586 | + $f5 = (int) $f[5]; |
|
| 587 | + $f6 = (int) $f[6]; |
|
| 588 | + $f7 = (int) $f[7]; |
|
| 589 | + $f8 = (int) $f[8]; |
|
| 590 | + $f9 = (int) $f[9]; |
|
| 591 | + |
|
| 592 | + $f0_2 = $f0 << 1; |
|
| 593 | + $f1_2 = $f1 << 1; |
|
| 594 | + $f2_2 = $f2 << 1; |
|
| 595 | + $f3_2 = $f3 << 1; |
|
| 596 | + $f4_2 = $f4 << 1; |
|
| 597 | + $f5_2 = $f5 << 1; |
|
| 598 | + $f6_2 = $f6 << 1; |
|
| 599 | + $f7_2 = $f7 << 1; |
|
| 600 | + $f5_38 = self::mul($f5, 38, 6); |
|
| 601 | + $f6_19 = self::mul($f6, 19, 5); |
|
| 602 | + $f7_38 = self::mul($f7, 38, 6); |
|
| 603 | + $f8_19 = self::mul($f8, 19, 5); |
|
| 604 | + $f9_38 = self::mul($f9, 38, 6); |
|
| 605 | + $f0f0 = self::mul($f0, $f0, 26); |
|
| 606 | + $f0f1_2 = self::mul($f0_2, $f1, 26); |
|
| 607 | + $f0f2_2 = self::mul($f0_2, $f2, 26); |
|
| 608 | + $f0f3_2 = self::mul($f0_2, $f3, 26); |
|
| 609 | + $f0f4_2 = self::mul($f0_2, $f4, 26); |
|
| 610 | + $f0f5_2 = self::mul($f0_2, $f5, 26); |
|
| 611 | + $f0f6_2 = self::mul($f0_2, $f6, 26); |
|
| 612 | + $f0f7_2 = self::mul($f0_2, $f7, 26); |
|
| 613 | + $f0f8_2 = self::mul($f0_2, $f8, 26); |
|
| 614 | + $f0f9_2 = self::mul($f0_2, $f9, 26); |
|
| 615 | + $f1f1_2 = self::mul($f1_2, $f1, 26); |
|
| 616 | + $f1f2_2 = self::mul($f1_2, $f2, 26); |
|
| 617 | + $f1f3_4 = self::mul($f1_2, $f3_2, 26); |
|
| 618 | + $f1f4_2 = self::mul($f1_2, $f4, 26); |
|
| 619 | + $f1f5_4 = self::mul($f1_2, $f5_2, 26); |
|
| 620 | + $f1f6_2 = self::mul($f1_2, $f6, 26); |
|
| 621 | + $f1f7_4 = self::mul($f1_2, $f7_2, 26); |
|
| 622 | + $f1f8_2 = self::mul($f1_2, $f8, 26); |
|
| 623 | + $f1f9_76 = self::mul($f9_38, $f1_2, 27); |
|
| 624 | + $f2f2 = self::mul($f2, $f2, 27); |
|
| 625 | + $f2f3_2 = self::mul($f2_2, $f3, 27); |
|
| 626 | + $f2f4_2 = self::mul($f2_2, $f4, 27); |
|
| 627 | + $f2f5_2 = self::mul($f2_2, $f5, 27); |
|
| 628 | + $f2f6_2 = self::mul($f2_2, $f6, 27); |
|
| 629 | + $f2f7_2 = self::mul($f2_2, $f7, 27); |
|
| 630 | + $f2f8_38 = self::mul($f8_19, $f2_2, 27); |
|
| 631 | + $f2f9_38 = self::mul($f9_38, $f2, 26); |
|
| 632 | + $f3f3_2 = self::mul($f3_2, $f3, 26); |
|
| 633 | + $f3f4_2 = self::mul($f3_2, $f4, 26); |
|
| 634 | + $f3f5_4 = self::mul($f3_2, $f5_2, 26); |
|
| 635 | + $f3f6_2 = self::mul($f3_2, $f6, 26); |
|
| 636 | + $f3f7_76 = self::mul($f7_38, $f3_2, 26); |
|
| 637 | + $f3f8_38 = self::mul($f8_19, $f3_2, 26); |
|
| 638 | + $f3f9_76 = self::mul($f9_38, $f3_2, 26); |
|
| 639 | + $f4f4 = self::mul($f4, $f4, 26); |
|
| 640 | + $f4f5_2 = self::mul($f4_2, $f5, 26); |
|
| 641 | + $f4f6_38 = self::mul($f6_19, $f4_2, 27); |
|
| 642 | + $f4f7_38 = self::mul($f7_38, $f4, 26); |
|
| 643 | + $f4f8_38 = self::mul($f8_19, $f4_2, 27); |
|
| 644 | + $f4f9_38 = self::mul($f9_38, $f4, 26); |
|
| 645 | + $f5f5_38 = self::mul($f5_38, $f5, 26); |
|
| 646 | + $f5f6_38 = self::mul($f6_19, $f5_2, 26); |
|
| 647 | + $f5f7_76 = self::mul($f7_38, $f5_2, 26); |
|
| 648 | + $f5f8_38 = self::mul($f8_19, $f5_2, 26); |
|
| 649 | + $f5f9_76 = self::mul($f9_38, $f5_2, 26); |
|
| 650 | + $f6f6_19 = self::mul($f6_19, $f6, 26); |
|
| 651 | + $f6f7_38 = self::mul($f7_38, $f6, 26); |
|
| 652 | + $f6f8_38 = self::mul($f8_19, $f6_2, 27); |
|
| 653 | + $f6f9_38 = self::mul($f9_38, $f6, 26); |
|
| 654 | + $f7f7_38 = self::mul($f7_38, $f7, 26); |
|
| 655 | + $f7f8_38 = self::mul($f8_19, $f7_2, 26); |
|
| 656 | + $f7f9_76 = self::mul($f9_38, $f7_2, 26); |
|
| 657 | + $f8f8_19 = self::mul($f8_19, $f8, 26); |
|
| 658 | + $f8f9_38 = self::mul($f9_38, $f8, 26); |
|
| 659 | + $f9f9_38 = self::mul($f9_38, $f9, 26); |
|
| 660 | + $h0 = $f0f0 + $f1f9_76 + $f2f8_38 + $f3f7_76 + $f4f6_38 + $f5f5_38; |
|
| 661 | + $h1 = $f0f1_2 + $f2f9_38 + $f3f8_38 + $f4f7_38 + $f5f6_38; |
|
| 662 | + $h2 = $f0f2_2 + $f1f1_2 + $f3f9_76 + $f4f8_38 + $f5f7_76 + $f6f6_19; |
|
| 663 | + $h3 = $f0f3_2 + $f1f2_2 + $f4f9_38 + $f5f8_38 + $f6f7_38; |
|
| 664 | + $h4 = $f0f4_2 + $f1f3_4 + $f2f2 + $f5f9_76 + $f6f8_38 + $f7f7_38; |
|
| 665 | + $h5 = $f0f5_2 + $f1f4_2 + $f2f3_2 + $f6f9_38 + $f7f8_38; |
|
| 666 | + $h6 = $f0f6_2 + $f1f5_4 + $f2f4_2 + $f3f3_2 + $f7f9_76 + $f8f8_19; |
|
| 667 | + $h7 = $f0f7_2 + $f1f6_2 + $f2f5_2 + $f3f4_2 + $f8f9_38; |
|
| 668 | + $h8 = $f0f8_2 + $f1f7_4 + $f2f6_2 + $f3f5_4 + $f4f4 + $f9f9_38; |
|
| 669 | + $h9 = $f0f9_2 + $f1f8_2 + $f2f7_2 + $f3f6_2 + $f4f5_2; |
|
| 670 | + |
|
| 671 | + $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 672 | + $h1 += $carry0; |
|
| 673 | + $h0 -= $carry0 << 26; |
|
| 674 | + $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 675 | + $h5 += $carry4; |
|
| 676 | + $h4 -= $carry4 << 26; |
|
| 677 | + |
|
| 678 | + $carry1 = ($h1 + (1 << 24)) >> 25; |
|
| 679 | + $h2 += $carry1; |
|
| 680 | + $h1 -= $carry1 << 25; |
|
| 681 | + $carry5 = ($h5 + (1 << 24)) >> 25; |
|
| 682 | + $h6 += $carry5; |
|
| 683 | + $h5 -= $carry5 << 25; |
|
| 684 | + |
|
| 685 | + $carry2 = ($h2 + (1 << 25)) >> 26; |
|
| 686 | + $h3 += $carry2; |
|
| 687 | + $h2 -= $carry2 << 26; |
|
| 688 | + $carry6 = ($h6 + (1 << 25)) >> 26; |
|
| 689 | + $h7 += $carry6; |
|
| 690 | + $h6 -= $carry6 << 26; |
|
| 691 | + |
|
| 692 | + $carry3 = ($h3 + (1 << 24)) >> 25; |
|
| 693 | + $h4 += $carry3; |
|
| 694 | + $h3 -= $carry3 << 25; |
|
| 695 | + $carry7 = ($h7 + (1 << 24)) >> 25; |
|
| 696 | + $h8 += $carry7; |
|
| 697 | + $h7 -= $carry7 << 25; |
|
| 698 | + |
|
| 699 | + $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 700 | + $h5 += $carry4; |
|
| 701 | + $h4 -= $carry4 << 26; |
|
| 702 | + $carry8 = ($h8 + (1 << 25)) >> 26; |
|
| 703 | + $h9 += $carry8; |
|
| 704 | + $h8 -= $carry8 << 26; |
|
| 705 | + |
|
| 706 | + $carry9 = ($h9 + (1 << 24)) >> 25; |
|
| 707 | + $h0 += self::mul($carry9, 19, 5); |
|
| 708 | + $h9 -= $carry9 << 25; |
|
| 709 | + |
|
| 710 | + $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 711 | + $h1 += $carry0; |
|
| 712 | + $h0 -= $carry0 << 26; |
|
| 713 | + |
|
| 714 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 715 | + array( |
|
| 716 | + (int) $h0, |
|
| 717 | + (int) $h1, |
|
| 718 | + (int) $h2, |
|
| 719 | + (int) $h3, |
|
| 720 | + (int) $h4, |
|
| 721 | + (int) $h5, |
|
| 722 | + (int) $h6, |
|
| 723 | + (int) $h7, |
|
| 724 | + (int) $h8, |
|
| 725 | + (int) $h9 |
|
| 726 | + ) |
|
| 727 | + ); |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + |
|
| 731 | + /** |
|
| 732 | + * Square and double a field element |
|
| 733 | + * |
|
| 734 | + * h = 2 * f * f |
|
| 735 | + * |
|
| 736 | + * @internal You should not use this directly from another application |
|
| 737 | + * |
|
| 738 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 739 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 740 | + */ |
|
| 741 | + public static function fe_sq2(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 742 | + { |
|
| 743 | + $f0 = (int) $f[0]; |
|
| 744 | + $f1 = (int) $f[1]; |
|
| 745 | + $f2 = (int) $f[2]; |
|
| 746 | + $f3 = (int) $f[3]; |
|
| 747 | + $f4 = (int) $f[4]; |
|
| 748 | + $f5 = (int) $f[5]; |
|
| 749 | + $f6 = (int) $f[6]; |
|
| 750 | + $f7 = (int) $f[7]; |
|
| 751 | + $f8 = (int) $f[8]; |
|
| 752 | + $f9 = (int) $f[9]; |
|
| 753 | + |
|
| 754 | + $f0_2 = $f0 << 1; |
|
| 755 | + $f1_2 = $f1 << 1; |
|
| 756 | + $f2_2 = $f2 << 1; |
|
| 757 | + $f3_2 = $f3 << 1; |
|
| 758 | + $f4_2 = $f4 << 1; |
|
| 759 | + $f5_2 = $f5 << 1; |
|
| 760 | + $f6_2 = $f6 << 1; |
|
| 761 | + $f7_2 = $f7 << 1; |
|
| 762 | + $f5_38 = self::mul($f5, 38, 6); /* 1.959375*2^30 */ |
|
| 763 | + $f6_19 = self::mul($f6, 19, 5); /* 1.959375*2^30 */ |
|
| 764 | + $f7_38 = self::mul($f7, 38, 6); /* 1.959375*2^30 */ |
|
| 765 | + $f8_19 = self::mul($f8, 19, 5); /* 1.959375*2^30 */ |
|
| 766 | + $f9_38 = self::mul($f9, 38, 6); /* 1.959375*2^30 */ |
|
| 767 | + $f0f0 = self::mul($f0, $f0, 24); |
|
| 768 | + $f0f1_2 = self::mul($f0_2, $f1, 24); |
|
| 769 | + $f0f2_2 = self::mul($f0_2, $f2, 24); |
|
| 770 | + $f0f3_2 = self::mul($f0_2, $f3, 24); |
|
| 771 | + $f0f4_2 = self::mul($f0_2, $f4, 24); |
|
| 772 | + $f0f5_2 = self::mul($f0_2, $f5, 24); |
|
| 773 | + $f0f6_2 = self::mul($f0_2, $f6, 24); |
|
| 774 | + $f0f7_2 = self::mul($f0_2, $f7, 24); |
|
| 775 | + $f0f8_2 = self::mul($f0_2, $f8, 24); |
|
| 776 | + $f0f9_2 = self::mul($f0_2, $f9, 24); |
|
| 777 | + $f1f1_2 = self::mul($f1_2, $f1, 24); |
|
| 778 | + $f1f2_2 = self::mul($f1_2, $f2, 24); |
|
| 779 | + $f1f3_4 = self::mul($f1_2, $f3_2, 24); |
|
| 780 | + $f1f4_2 = self::mul($f1_2, $f4, 24); |
|
| 781 | + $f1f5_4 = self::mul($f1_2, $f5_2, 24); |
|
| 782 | + $f1f6_2 = self::mul($f1_2, $f6, 24); |
|
| 783 | + $f1f7_4 = self::mul($f1_2, $f7_2, 24); |
|
| 784 | + $f1f8_2 = self::mul($f1_2, $f8, 24); |
|
| 785 | + $f1f9_76 = self::mul($f9_38, $f1_2, 24); |
|
| 786 | + $f2f2 = self::mul($f2, $f2, 24); |
|
| 787 | + $f2f3_2 = self::mul($f2_2, $f3, 24); |
|
| 788 | + $f2f4_2 = self::mul($f2_2, $f4, 24); |
|
| 789 | + $f2f5_2 = self::mul($f2_2, $f5, 24); |
|
| 790 | + $f2f6_2 = self::mul($f2_2, $f6, 24); |
|
| 791 | + $f2f7_2 = self::mul($f2_2, $f7, 24); |
|
| 792 | + $f2f8_38 = self::mul($f8_19, $f2_2, 25); |
|
| 793 | + $f2f9_38 = self::mul($f9_38, $f2, 24); |
|
| 794 | + $f3f3_2 = self::mul($f3_2, $f3, 24); |
|
| 795 | + $f3f4_2 = self::mul($f3_2, $f4, 24); |
|
| 796 | + $f3f5_4 = self::mul($f3_2, $f5_2, 24); |
|
| 797 | + $f3f6_2 = self::mul($f3_2, $f6, 24); |
|
| 798 | + $f3f7_76 = self::mul($f7_38, $f3_2, 24); |
|
| 799 | + $f3f8_38 = self::mul($f8_19, $f3_2, 24); |
|
| 800 | + $f3f9_76 = self::mul($f9_38, $f3_2, 24); |
|
| 801 | + $f4f4 = self::mul($f4, $f4, 24); |
|
| 802 | + $f4f5_2 = self::mul($f4_2, $f5, 24); |
|
| 803 | + $f4f6_38 = self::mul($f6_19, $f4_2, 25); |
|
| 804 | + $f4f7_38 = self::mul($f7_38, $f4, 24); |
|
| 805 | + $f4f8_38 = self::mul($f8_19, $f4_2, 25); |
|
| 806 | + $f4f9_38 = self::mul($f9_38, $f4, 24); |
|
| 807 | + $f5f5_38 = self::mul($f5_38, $f5, 24); |
|
| 808 | + $f5f6_38 = self::mul($f6_19, $f5_2, 24); |
|
| 809 | + $f5f7_76 = self::mul($f7_38, $f5_2, 24); |
|
| 810 | + $f5f8_38 = self::mul($f8_19, $f5_2, 24); |
|
| 811 | + $f5f9_76 = self::mul($f9_38, $f5_2, 24); |
|
| 812 | + $f6f6_19 = self::mul($f6_19, $f6, 24); |
|
| 813 | + $f6f7_38 = self::mul($f7_38, $f6, 24); |
|
| 814 | + $f6f8_38 = self::mul($f8_19, $f6_2, 25); |
|
| 815 | + $f6f9_38 = self::mul($f9_38, $f6, 24); |
|
| 816 | + $f7f7_38 = self::mul($f7_38, $f7, 24); |
|
| 817 | + $f7f8_38 = self::mul($f8_19, $f7_2, 24); |
|
| 818 | + $f7f9_76 = self::mul($f9_38, $f7_2, 24); |
|
| 819 | + $f8f8_19 = self::mul($f8_19, $f8, 24); |
|
| 820 | + $f8f9_38 = self::mul($f9_38, $f8, 24); |
|
| 821 | + $f9f9_38 = self::mul($f9_38, $f9, 24); |
|
| 822 | + |
|
| 823 | + $h0 = (int) ($f0f0 + $f1f9_76 + $f2f8_38 + $f3f7_76 + $f4f6_38 + $f5f5_38) << 1; |
|
| 824 | + $h1 = (int) ($f0f1_2 + $f2f9_38 + $f3f8_38 + $f4f7_38 + $f5f6_38) << 1; |
|
| 825 | + $h2 = (int) ($f0f2_2 + $f1f1_2 + $f3f9_76 + $f4f8_38 + $f5f7_76 + $f6f6_19) << 1; |
|
| 826 | + $h3 = (int) ($f0f3_2 + $f1f2_2 + $f4f9_38 + $f5f8_38 + $f6f7_38) << 1; |
|
| 827 | + $h4 = (int) ($f0f4_2 + $f1f3_4 + $f2f2 + $f5f9_76 + $f6f8_38 + $f7f7_38) << 1; |
|
| 828 | + $h5 = (int) ($f0f5_2 + $f1f4_2 + $f2f3_2 + $f6f9_38 + $f7f8_38) << 1; |
|
| 829 | + $h6 = (int) ($f0f6_2 + $f1f5_4 + $f2f4_2 + $f3f3_2 + $f7f9_76 + $f8f8_19) << 1; |
|
| 830 | + $h7 = (int) ($f0f7_2 + $f1f6_2 + $f2f5_2 + $f3f4_2 + $f8f9_38) << 1; |
|
| 831 | + $h8 = (int) ($f0f8_2 + $f1f7_4 + $f2f6_2 + $f3f5_4 + $f4f4 + $f9f9_38) << 1; |
|
| 832 | + $h9 = (int) ($f0f9_2 + $f1f8_2 + $f2f7_2 + $f3f6_2 + $f4f5_2) << 1; |
|
| 833 | + |
|
| 834 | + $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 835 | + $h1 += $carry0; |
|
| 836 | + $h0 -= $carry0 << 26; |
|
| 837 | + $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 838 | + $h5 += $carry4; |
|
| 839 | + $h4 -= $carry4 << 26; |
|
| 840 | + |
|
| 841 | + $carry1 = ($h1 + (1 << 24)) >> 25; |
|
| 842 | + $h2 += $carry1; |
|
| 843 | + $h1 -= $carry1 << 25; |
|
| 844 | + $carry5 = ($h5 + (1 << 24)) >> 25; |
|
| 845 | + $h6 += $carry5; |
|
| 846 | + $h5 -= $carry5 << 25; |
|
| 847 | + |
|
| 848 | + $carry2 = ($h2 + (1 << 25)) >> 26; |
|
| 849 | + $h3 += $carry2; |
|
| 850 | + $h2 -= $carry2 << 26; |
|
| 851 | + $carry6 = ($h6 + (1 << 25)) >> 26; |
|
| 852 | + $h7 += $carry6; |
|
| 853 | + $h6 -= $carry6 << 26; |
|
| 854 | + |
|
| 855 | + $carry3 = ($h3 + (1 << 24)) >> 25; |
|
| 856 | + $h4 += $carry3; |
|
| 857 | + $h3 -= $carry3 << 25; |
|
| 858 | + $carry7 = ($h7 + (1 << 24)) >> 25; |
|
| 859 | + $h8 += $carry7; |
|
| 860 | + $h7 -= $carry7 << 25; |
|
| 861 | + |
|
| 862 | + $carry4 = ($h4 + (1 << 25)) >> 26; |
|
| 863 | + $h5 += $carry4; |
|
| 864 | + $h4 -= $carry4 << 26; |
|
| 865 | + $carry8 = ($h8 + (1 << 25)) >> 26; |
|
| 866 | + $h9 += $carry8; |
|
| 867 | + $h8 -= $carry8 << 26; |
|
| 868 | + |
|
| 869 | + $carry9 = ($h9 + (1 << 24)) >> 25; |
|
| 870 | + $h0 += self::mul($carry9, 19, 5); |
|
| 871 | + $h9 -= $carry9 << 25; |
|
| 872 | + |
|
| 873 | + $carry0 = ($h0 + (1 << 25)) >> 26; |
|
| 874 | + $h1 += $carry0; |
|
| 875 | + $h0 -= $carry0 << 26; |
|
| 876 | + |
|
| 877 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 878 | + array( |
|
| 879 | + (int) $h0, |
|
| 880 | + (int) $h1, |
|
| 881 | + (int) $h2, |
|
| 882 | + (int) $h3, |
|
| 883 | + (int) $h4, |
|
| 884 | + (int) $h5, |
|
| 885 | + (int) $h6, |
|
| 886 | + (int) $h7, |
|
| 887 | + (int) $h8, |
|
| 888 | + (int) $h9 |
|
| 889 | + ) |
|
| 890 | + ); |
|
| 891 | + } |
|
| 892 | + |
|
| 893 | + /** |
|
| 894 | + * @internal You should not use this directly from another application |
|
| 895 | + * |
|
| 896 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $Z |
|
| 897 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 898 | + */ |
|
| 899 | + public static function fe_invert(ParagonIE_Sodium_Core_Curve25519_Fe $Z) |
|
| 900 | + { |
|
| 901 | + $z = clone $Z; |
|
| 902 | + $t0 = self::fe_sq($z); |
|
| 903 | + $t1 = self::fe_sq($t0); |
|
| 904 | + $t1 = self::fe_sq($t1); |
|
| 905 | + $t1 = self::fe_mul($z, $t1); |
|
| 906 | + $t0 = self::fe_mul($t0, $t1); |
|
| 907 | + $t2 = self::fe_sq($t0); |
|
| 908 | + $t1 = self::fe_mul($t1, $t2); |
|
| 909 | + $t2 = self::fe_sq($t1); |
|
| 910 | + for ($i = 1; $i < 5; ++$i) { |
|
| 911 | + $t2 = self::fe_sq($t2); |
|
| 912 | + } |
|
| 913 | + $t1 = self::fe_mul($t2, $t1); |
|
| 914 | + $t2 = self::fe_sq($t1); |
|
| 915 | + for ($i = 1; $i < 10; ++$i) { |
|
| 916 | + $t2 = self::fe_sq($t2); |
|
| 917 | + } |
|
| 918 | + $t2 = self::fe_mul($t2, $t1); |
|
| 919 | + $t3 = self::fe_sq($t2); |
|
| 920 | + for ($i = 1; $i < 20; ++$i) { |
|
| 921 | + $t3 = self::fe_sq($t3); |
|
| 922 | + } |
|
| 923 | + $t2 = self::fe_mul($t3, $t2); |
|
| 924 | + $t2 = self::fe_sq($t2); |
|
| 925 | + for ($i = 1; $i < 10; ++$i) { |
|
| 926 | + $t2 = self::fe_sq($t2); |
|
| 927 | + } |
|
| 928 | + $t1 = self::fe_mul($t2, $t1); |
|
| 929 | + $t2 = self::fe_sq($t1); |
|
| 930 | + for ($i = 1; $i < 50; ++$i) { |
|
| 931 | + $t2 = self::fe_sq($t2); |
|
| 932 | + } |
|
| 933 | + $t2 = self::fe_mul($t2, $t1); |
|
| 934 | + $t3 = self::fe_sq($t2); |
|
| 935 | + for ($i = 1; $i < 100; ++$i) { |
|
| 936 | + $t3 = self::fe_sq($t3); |
|
| 937 | + } |
|
| 938 | + $t2 = self::fe_mul($t3, $t2); |
|
| 939 | + $t2 = self::fe_sq($t2); |
|
| 940 | + for ($i = 1; $i < 50; ++$i) { |
|
| 941 | + $t2 = self::fe_sq($t2); |
|
| 942 | + } |
|
| 943 | + $t1 = self::fe_mul($t2, $t1); |
|
| 944 | + $t1 = self::fe_sq($t1); |
|
| 945 | + for ($i = 1; $i < 5; ++$i) { |
|
| 946 | + $t1 = self::fe_sq($t1); |
|
| 947 | + } |
|
| 948 | + return self::fe_mul($t1, $t0); |
|
| 949 | + } |
|
| 950 | + |
|
| 951 | + /** |
|
| 952 | + * @internal You should not use this directly from another application |
|
| 953 | + * |
|
| 954 | + * @ref https://github.com/jedisct1/libsodium/blob/68564326e1e9dc57ef03746f85734232d20ca6fb/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c#L1054-L1106 |
|
| 955 | + * |
|
| 956 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $z |
|
| 957 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 958 | + */ |
|
| 959 | + public static function fe_pow22523(ParagonIE_Sodium_Core_Curve25519_Fe $z) |
|
| 960 | + { |
|
| 961 | + # fe_sq(t0, z); |
|
| 962 | + # fe_sq(t1, t0); |
|
| 963 | + # fe_sq(t1, t1); |
|
| 964 | + # fe_mul(t1, z, t1); |
|
| 965 | + # fe_mul(t0, t0, t1); |
|
| 966 | + # fe_sq(t0, t0); |
|
| 967 | + # fe_mul(t0, t1, t0); |
|
| 968 | + # fe_sq(t1, t0); |
|
| 969 | + $t0 = self::fe_sq($z); |
|
| 970 | + $t1 = self::fe_sq($t0); |
|
| 971 | + $t1 = self::fe_sq($t1); |
|
| 972 | + $t1 = self::fe_mul($z, $t1); |
|
| 973 | + $t0 = self::fe_mul($t0, $t1); |
|
| 974 | + $t0 = self::fe_sq($t0); |
|
| 975 | + $t0 = self::fe_mul($t1, $t0); |
|
| 976 | + $t1 = self::fe_sq($t0); |
|
| 977 | + |
|
| 978 | + # for (i = 1; i < 5; ++i) { |
|
| 979 | + # fe_sq(t1, t1); |
|
| 980 | + # } |
|
| 981 | + for ($i = 1; $i < 5; ++$i) { |
|
| 982 | + $t1 = self::fe_sq($t1); |
|
| 983 | + } |
|
| 984 | + |
|
| 985 | + # fe_mul(t0, t1, t0); |
|
| 986 | + # fe_sq(t1, t0); |
|
| 987 | + $t0 = self::fe_mul($t1, $t0); |
|
| 988 | + $t1 = self::fe_sq($t0); |
|
| 989 | + |
|
| 990 | + # for (i = 1; i < 10; ++i) { |
|
| 991 | + # fe_sq(t1, t1); |
|
| 992 | + # } |
|
| 993 | + for ($i = 1; $i < 10; ++$i) { |
|
| 994 | + $t1 = self::fe_sq($t1); |
|
| 995 | + } |
|
| 996 | + |
|
| 997 | + # fe_mul(t1, t1, t0); |
|
| 998 | + # fe_sq(t2, t1); |
|
| 999 | + $t1 = self::fe_mul($t1, $t0); |
|
| 1000 | + $t2 = self::fe_sq($t1); |
|
| 1001 | + |
|
| 1002 | + # for (i = 1; i < 20; ++i) { |
|
| 1003 | + # fe_sq(t2, t2); |
|
| 1004 | + # } |
|
| 1005 | + for ($i = 1; $i < 20; ++$i) { |
|
| 1006 | + $t2 = self::fe_sq($t2); |
|
| 1007 | + } |
|
| 1008 | + |
|
| 1009 | + # fe_mul(t1, t2, t1); |
|
| 1010 | + # fe_sq(t1, t1); |
|
| 1011 | + $t1 = self::fe_mul($t2, $t1); |
|
| 1012 | + $t1 = self::fe_sq($t1); |
|
| 1013 | + |
|
| 1014 | + # for (i = 1; i < 10; ++i) { |
|
| 1015 | + # fe_sq(t1, t1); |
|
| 1016 | + # } |
|
| 1017 | + for ($i = 1; $i < 10; ++$i) { |
|
| 1018 | + $t1 = self::fe_sq($t1); |
|
| 1019 | + } |
|
| 1020 | + |
|
| 1021 | + # fe_mul(t0, t1, t0); |
|
| 1022 | + # fe_sq(t1, t0); |
|
| 1023 | + $t0 = self::fe_mul($t1, $t0); |
|
| 1024 | + $t1 = self::fe_sq($t0); |
|
| 1025 | + |
|
| 1026 | + # for (i = 1; i < 50; ++i) { |
|
| 1027 | + # fe_sq(t1, t1); |
|
| 1028 | + # } |
|
| 1029 | + for ($i = 1; $i < 50; ++$i) { |
|
| 1030 | + $t1 = self::fe_sq($t1); |
|
| 1031 | + } |
|
| 1032 | + |
|
| 1033 | + # fe_mul(t1, t1, t0); |
|
| 1034 | + # fe_sq(t2, t1); |
|
| 1035 | + $t1 = self::fe_mul($t1, $t0); |
|
| 1036 | + $t2 = self::fe_sq($t1); |
|
| 1037 | + |
|
| 1038 | + # for (i = 1; i < 100; ++i) { |
|
| 1039 | + # fe_sq(t2, t2); |
|
| 1040 | + # } |
|
| 1041 | + for ($i = 1; $i < 100; ++$i) { |
|
| 1042 | + $t2 = self::fe_sq($t2); |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + # fe_mul(t1, t2, t1); |
|
| 1046 | + # fe_sq(t1, t1); |
|
| 1047 | + $t1 = self::fe_mul($t2, $t1); |
|
| 1048 | + $t1 = self::fe_sq($t1); |
|
| 1049 | + |
|
| 1050 | + # for (i = 1; i < 50; ++i) { |
|
| 1051 | + # fe_sq(t1, t1); |
|
| 1052 | + # } |
|
| 1053 | + for ($i = 1; $i < 50; ++$i) { |
|
| 1054 | + $t1 = self::fe_sq($t1); |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + # fe_mul(t0, t1, t0); |
|
| 1058 | + # fe_sq(t0, t0); |
|
| 1059 | + # fe_sq(t0, t0); |
|
| 1060 | + # fe_mul(out, t0, z); |
|
| 1061 | + $t0 = self::fe_mul($t1, $t0); |
|
| 1062 | + $t0 = self::fe_sq($t0); |
|
| 1063 | + $t0 = self::fe_sq($t0); |
|
| 1064 | + return self::fe_mul($t0, $z); |
|
| 1065 | + } |
|
| 1066 | + |
|
| 1067 | + /** |
|
| 1068 | + * Subtract two field elements. |
|
| 1069 | + * |
|
| 1070 | + * h = f - g |
|
| 1071 | + * |
|
| 1072 | + * Preconditions: |
|
| 1073 | + * |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. |
|
| 1074 | + * |g| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. |
|
| 1075 | + * |
|
| 1076 | + * Postconditions: |
|
| 1077 | + * |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. |
|
| 1078 | + * |
|
| 1079 | + * @internal You should not use this directly from another application |
|
| 1080 | + * |
|
| 1081 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 1082 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
| 1083 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 1084 | + * @psalm-suppress MixedOperand |
|
| 1085 | + */ |
|
| 1086 | + public static function fe_sub(ParagonIE_Sodium_Core_Curve25519_Fe $f, ParagonIE_Sodium_Core_Curve25519_Fe $g) |
|
| 1087 | + { |
|
| 1088 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( |
|
| 1089 | + array( |
|
| 1090 | + (int) ($f[0] - $g[0]), |
|
| 1091 | + (int) ($f[1] - $g[1]), |
|
| 1092 | + (int) ($f[2] - $g[2]), |
|
| 1093 | + (int) ($f[3] - $g[3]), |
|
| 1094 | + (int) ($f[4] - $g[4]), |
|
| 1095 | + (int) ($f[5] - $g[5]), |
|
| 1096 | + (int) ($f[6] - $g[6]), |
|
| 1097 | + (int) ($f[7] - $g[7]), |
|
| 1098 | + (int) ($f[8] - $g[8]), |
|
| 1099 | + (int) ($f[9] - $g[9]) |
|
| 1100 | + ) |
|
| 1101 | + ); |
|
| 1102 | + } |
|
| 1103 | + |
|
| 1104 | + /** |
|
| 1105 | + * Add two group elements. |
|
| 1106 | + * |
|
| 1107 | + * r = p + q |
|
| 1108 | + * |
|
| 1109 | + * @internal You should not use this directly from another application |
|
| 1110 | + * |
|
| 1111 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1112 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q |
|
| 1113 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1114 | + */ |
|
| 1115 | + public static function ge_add( |
|
| 1116 | + ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, |
|
| 1117 | + ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q |
|
| 1118 | + ) { |
|
| 1119 | + $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); |
|
| 1120 | + $r->X = self::fe_add($p->Y, $p->X); |
|
| 1121 | + $r->Y = self::fe_sub($p->Y, $p->X); |
|
| 1122 | + $r->Z = self::fe_mul($r->X, $q->YplusX); |
|
| 1123 | + $r->Y = self::fe_mul($r->Y, $q->YminusX); |
|
| 1124 | + $r->T = self::fe_mul($q->T2d, $p->T); |
|
| 1125 | + $r->X = self::fe_mul($p->Z, $q->Z); |
|
| 1126 | + $t0 = self::fe_add($r->X, $r->X); |
|
| 1127 | + $r->X = self::fe_sub($r->Z, $r->Y); |
|
| 1128 | + $r->Y = self::fe_add($r->Z, $r->Y); |
|
| 1129 | + $r->Z = self::fe_add($t0, $r->T); |
|
| 1130 | + $r->T = self::fe_sub($t0, $r->T); |
|
| 1131 | + return $r; |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + /** |
|
| 1135 | + * @internal You should not use this directly from another application |
|
| 1136 | + * |
|
| 1137 | + * @ref https://github.com/jedisct1/libsodium/blob/157c4a80c13b117608aeae12178b2d38825f9f8f/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c#L1185-L1215 |
|
| 1138 | + * @param string $a |
|
| 1139 | + * @return array<int, mixed> |
|
| 1140 | + * @throws SodiumException |
|
| 1141 | + * @throws TypeError |
|
| 1142 | + */ |
|
| 1143 | + public static function slide($a) |
|
| 1144 | + { |
|
| 1145 | + if (self::strlen($a) < 256) { |
|
| 1146 | + if (self::strlen($a) < 16) { |
|
| 1147 | + $a = str_pad($a, 256, '0', STR_PAD_RIGHT); |
|
| 1148 | + } |
|
| 1149 | + } |
|
| 1150 | + /** @var array<int, int> $r */ |
|
| 1151 | + $r = array(); |
|
| 1152 | + |
|
| 1153 | + /** @var int $i */ |
|
| 1154 | + for ($i = 0; $i < 256; ++$i) { |
|
| 1155 | + $r[$i] = (int) ( |
|
| 1156 | + 1 & ( |
|
| 1157 | + self::chrToInt($a[(int) ($i >> 3)]) |
|
| 1158 | + >> |
|
| 1159 | + ($i & 7) |
|
| 1160 | + ) |
|
| 1161 | + ); |
|
| 1162 | + } |
|
| 1163 | + |
|
| 1164 | + for ($i = 0;$i < 256;++$i) { |
|
| 1165 | + if ($r[$i]) { |
|
| 1166 | + for ($b = 1;$b <= 6 && $i + $b < 256;++$b) { |
|
| 1167 | + if ($r[$i + $b]) { |
|
| 1168 | + if ($r[$i] + ($r[$i + $b] << $b) <= 15) { |
|
| 1169 | + $r[$i] += $r[$i + $b] << $b; |
|
| 1170 | + $r[$i + $b] = 0; |
|
| 1171 | + } elseif ($r[$i] - ($r[$i + $b] << $b) >= -15) { |
|
| 1172 | + $r[$i] -= $r[$i + $b] << $b; |
|
| 1173 | + for ($k = $i + $b; $k < 256; ++$k) { |
|
| 1174 | + if (!$r[$k]) { |
|
| 1175 | + $r[$k] = 1; |
|
| 1176 | + break; |
|
| 1177 | + } |
|
| 1178 | + $r[$k] = 0; |
|
| 1179 | + } |
|
| 1180 | + } else { |
|
| 1181 | + break; |
|
| 1182 | + } |
|
| 1183 | + } |
|
| 1184 | + } |
|
| 1185 | + } |
|
| 1186 | + } |
|
| 1187 | + return $r; |
|
| 1188 | + } |
|
| 1189 | + |
|
| 1190 | + /** |
|
| 1191 | + * @internal You should not use this directly from another application |
|
| 1192 | + * |
|
| 1193 | + * @param string $s |
|
| 1194 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 1195 | + * @throws SodiumException |
|
| 1196 | + * @throws TypeError |
|
| 1197 | + */ |
|
| 1198 | + public static function ge_frombytes_negate_vartime($s) |
|
| 1199 | + { |
|
| 1200 | + static $d = null; |
|
| 1201 | + if (!$d) { |
|
| 1202 | + $d = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d); |
|
| 1203 | + } |
|
| 1204 | + |
|
| 1205 | + # fe_frombytes(h->Y,s); |
|
| 1206 | + # fe_1(h->Z); |
|
| 1207 | + $h = new ParagonIE_Sodium_Core_Curve25519_Ge_P3( |
|
| 1208 | + self::fe_0(), |
|
| 1209 | + self::fe_frombytes($s), |
|
| 1210 | + self::fe_1() |
|
| 1211 | + ); |
|
| 1212 | + |
|
| 1213 | + # fe_sq(u,h->Y); |
|
| 1214 | + # fe_mul(v,u,d); |
|
| 1215 | + # fe_sub(u,u,h->Z); /* u = y^2-1 */ |
|
| 1216 | + # fe_add(v,v,h->Z); /* v = dy^2+1 */ |
|
| 1217 | + $u = self::fe_sq($h->Y); |
|
| 1218 | + /** @var ParagonIE_Sodium_Core_Curve25519_Fe $d */ |
|
| 1219 | + $v = self::fe_mul($u, $d); |
|
| 1220 | + $u = self::fe_sub($u, $h->Z); /* u = y^2 - 1 */ |
|
| 1221 | + $v = self::fe_add($v, $h->Z); /* v = dy^2 + 1 */ |
|
| 1222 | + |
|
| 1223 | + # fe_sq(v3,v); |
|
| 1224 | + # fe_mul(v3,v3,v); /* v3 = v^3 */ |
|
| 1225 | + # fe_sq(h->X,v3); |
|
| 1226 | + # fe_mul(h->X,h->X,v); |
|
| 1227 | + # fe_mul(h->X,h->X,u); /* x = uv^7 */ |
|
| 1228 | + $v3 = self::fe_sq($v); |
|
| 1229 | + $v3 = self::fe_mul($v3, $v); /* v3 = v^3 */ |
|
| 1230 | + $h->X = self::fe_sq($v3); |
|
| 1231 | + $h->X = self::fe_mul($h->X, $v); |
|
| 1232 | + $h->X = self::fe_mul($h->X, $u); /* x = uv^7 */ |
|
| 1233 | + |
|
| 1234 | + # fe_pow22523(h->X,h->X); /* x = (uv^7)^((q-5)/8) */ |
|
| 1235 | + # fe_mul(h->X,h->X,v3); |
|
| 1236 | + # fe_mul(h->X,h->X,u); /* x = uv^3(uv^7)^((q-5)/8) */ |
|
| 1237 | + $h->X = self::fe_pow22523($h->X); /* x = (uv^7)^((q-5)/8) */ |
|
| 1238 | + $h->X = self::fe_mul($h->X, $v3); |
|
| 1239 | + $h->X = self::fe_mul($h->X, $u); /* x = uv^3(uv^7)^((q-5)/8) */ |
|
| 1240 | + |
|
| 1241 | + # fe_sq(vxx,h->X); |
|
| 1242 | + # fe_mul(vxx,vxx,v); |
|
| 1243 | + # fe_sub(check,vxx,u); /* vx^2-u */ |
|
| 1244 | + $vxx = self::fe_sq($h->X); |
|
| 1245 | + $vxx = self::fe_mul($vxx, $v); |
|
| 1246 | + $check = self::fe_sub($vxx, $u); /* vx^2 - u */ |
|
| 1247 | + |
|
| 1248 | + # if (fe_isnonzero(check)) { |
|
| 1249 | + # fe_add(check,vxx,u); /* vx^2+u */ |
|
| 1250 | + # if (fe_isnonzero(check)) { |
|
| 1251 | + # return -1; |
|
| 1252 | + # } |
|
| 1253 | + # fe_mul(h->X,h->X,sqrtm1); |
|
| 1254 | + # } |
|
| 1255 | + if (self::fe_isnonzero($check)) { |
|
| 1256 | + $check = self::fe_add($vxx, $u); /* vx^2 + u */ |
|
| 1257 | + if (self::fe_isnonzero($check)) { |
|
| 1258 | + throw new RangeException('Internal check failed.'); |
|
| 1259 | + } |
|
| 1260 | + $h->X = self::fe_mul( |
|
| 1261 | + $h->X, |
|
| 1262 | + ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1) |
|
| 1263 | + ); |
|
| 1264 | + } |
|
| 1265 | + |
|
| 1266 | + # if (fe_isnegative(h->X) == (s[31] >> 7)) { |
|
| 1267 | + # fe_neg(h->X,h->X); |
|
| 1268 | + # } |
|
| 1269 | + $i = self::chrToInt($s[31]); |
|
| 1270 | + if (self::fe_isnegative($h->X) === ($i >> 7)) { |
|
| 1271 | + $h->X = self::fe_neg($h->X); |
|
| 1272 | + } |
|
| 1273 | + |
|
| 1274 | + # fe_mul(h->T,h->X,h->Y); |
|
| 1275 | + $h->T = self::fe_mul($h->X, $h->Y); |
|
| 1276 | + return $h; |
|
| 1277 | + } |
|
| 1278 | + |
|
| 1279 | + /** |
|
| 1280 | + * @internal You should not use this directly from another application |
|
| 1281 | + * |
|
| 1282 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R |
|
| 1283 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1284 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q |
|
| 1285 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1286 | + */ |
|
| 1287 | + public static function ge_madd( |
|
| 1288 | + ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R, |
|
| 1289 | + ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, |
|
| 1290 | + ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q |
|
| 1291 | + ) { |
|
| 1292 | + $r = clone $R; |
|
| 1293 | + $r->X = self::fe_add($p->Y, $p->X); |
|
| 1294 | + $r->Y = self::fe_sub($p->Y, $p->X); |
|
| 1295 | + $r->Z = self::fe_mul($r->X, $q->yplusx); |
|
| 1296 | + $r->Y = self::fe_mul($r->Y, $q->yminusx); |
|
| 1297 | + $r->T = self::fe_mul($q->xy2d, $p->T); |
|
| 1298 | + $t0 = self::fe_add(clone $p->Z, clone $p->Z); |
|
| 1299 | + $r->X = self::fe_sub($r->Z, $r->Y); |
|
| 1300 | + $r->Y = self::fe_add($r->Z, $r->Y); |
|
| 1301 | + $r->Z = self::fe_add($t0, $r->T); |
|
| 1302 | + $r->T = self::fe_sub($t0, $r->T); |
|
| 1303 | + |
|
| 1304 | + return $r; |
|
| 1305 | + } |
|
| 1306 | + |
|
| 1307 | + /** |
|
| 1308 | + * @internal You should not use this directly from another application |
|
| 1309 | + * |
|
| 1310 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R |
|
| 1311 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1312 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q |
|
| 1313 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1314 | + */ |
|
| 1315 | + public static function ge_msub( |
|
| 1316 | + ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R, |
|
| 1317 | + ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, |
|
| 1318 | + ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q |
|
| 1319 | + ) { |
|
| 1320 | + $r = clone $R; |
|
| 1321 | + |
|
| 1322 | + $r->X = self::fe_add($p->Y, $p->X); |
|
| 1323 | + $r->Y = self::fe_sub($p->Y, $p->X); |
|
| 1324 | + $r->Z = self::fe_mul($r->X, $q->yminusx); |
|
| 1325 | + $r->Y = self::fe_mul($r->Y, $q->yplusx); |
|
| 1326 | + $r->T = self::fe_mul($q->xy2d, $p->T); |
|
| 1327 | + $t0 = self::fe_add($p->Z, $p->Z); |
|
| 1328 | + $r->X = self::fe_sub($r->Z, $r->Y); |
|
| 1329 | + $r->Y = self::fe_add($r->Z, $r->Y); |
|
| 1330 | + $r->Z = self::fe_sub($t0, $r->T); |
|
| 1331 | + $r->T = self::fe_add($t0, $r->T); |
|
| 1332 | + |
|
| 1333 | + return $r; |
|
| 1334 | + } |
|
| 1335 | + |
|
| 1336 | + /** |
|
| 1337 | + * @internal You should not use this directly from another application |
|
| 1338 | + * |
|
| 1339 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p |
|
| 1340 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 |
|
| 1341 | + */ |
|
| 1342 | + public static function ge_p1p1_to_p2(ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p) |
|
| 1343 | + { |
|
| 1344 | + $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P2(); |
|
| 1345 | + $r->X = self::fe_mul($p->X, $p->T); |
|
| 1346 | + $r->Y = self::fe_mul($p->Y, $p->Z); |
|
| 1347 | + $r->Z = self::fe_mul($p->Z, $p->T); |
|
| 1348 | + return $r; |
|
| 1349 | + } |
|
| 1350 | + |
|
| 1351 | + /** |
|
| 1352 | + * @internal You should not use this directly from another application |
|
| 1353 | + * |
|
| 1354 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p |
|
| 1355 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 1356 | + */ |
|
| 1357 | + public static function ge_p1p1_to_p3(ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p) |
|
| 1358 | + { |
|
| 1359 | + $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P3(); |
|
| 1360 | + $r->X = self::fe_mul($p->X, $p->T); |
|
| 1361 | + $r->Y = self::fe_mul($p->Y, $p->Z); |
|
| 1362 | + $r->Z = self::fe_mul($p->Z, $p->T); |
|
| 1363 | + $r->T = self::fe_mul($p->X, $p->Y); |
|
| 1364 | + return $r; |
|
| 1365 | + } |
|
| 1366 | + |
|
| 1367 | + /** |
|
| 1368 | + * @internal You should not use this directly from another application |
|
| 1369 | + * |
|
| 1370 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 |
|
| 1371 | + */ |
|
| 1372 | + public static function ge_p2_0() |
|
| 1373 | + { |
|
| 1374 | + return new ParagonIE_Sodium_Core_Curve25519_Ge_P2( |
|
| 1375 | + self::fe_0(), |
|
| 1376 | + self::fe_1(), |
|
| 1377 | + self::fe_1() |
|
| 1378 | + ); |
|
| 1379 | + } |
|
| 1380 | + |
|
| 1381 | + /** |
|
| 1382 | + * @internal You should not use this directly from another application |
|
| 1383 | + * |
|
| 1384 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P2 $p |
|
| 1385 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1386 | + */ |
|
| 1387 | + public static function ge_p2_dbl(ParagonIE_Sodium_Core_Curve25519_Ge_P2 $p) |
|
| 1388 | + { |
|
| 1389 | + $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); |
|
| 1390 | + |
|
| 1391 | + $r->X = self::fe_sq($p->X); |
|
| 1392 | + $r->Z = self::fe_sq($p->Y); |
|
| 1393 | + $r->T = self::fe_sq2($p->Z); |
|
| 1394 | + $r->Y = self::fe_add($p->X, $p->Y); |
|
| 1395 | + $t0 = self::fe_sq($r->Y); |
|
| 1396 | + $r->Y = self::fe_add($r->Z, $r->X); |
|
| 1397 | + $r->Z = self::fe_sub($r->Z, $r->X); |
|
| 1398 | + $r->X = self::fe_sub($t0, $r->Y); |
|
| 1399 | + $r->T = self::fe_sub($r->T, $r->Z); |
|
| 1400 | + |
|
| 1401 | + return $r; |
|
| 1402 | + } |
|
| 1403 | + |
|
| 1404 | + /** |
|
| 1405 | + * @internal You should not use this directly from another application |
|
| 1406 | + * |
|
| 1407 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 1408 | + */ |
|
| 1409 | + public static function ge_p3_0() |
|
| 1410 | + { |
|
| 1411 | + return new ParagonIE_Sodium_Core_Curve25519_Ge_P3( |
|
| 1412 | + self::fe_0(), |
|
| 1413 | + self::fe_1(), |
|
| 1414 | + self::fe_1(), |
|
| 1415 | + self::fe_0() |
|
| 1416 | + ); |
|
| 1417 | + } |
|
| 1418 | + |
|
| 1419 | + /** |
|
| 1420 | + * @internal You should not use this directly from another application |
|
| 1421 | + * |
|
| 1422 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1423 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_Cached |
|
| 1424 | + */ |
|
| 1425 | + public static function ge_p3_to_cached(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p) |
|
| 1426 | + { |
|
| 1427 | + static $d2 = null; |
|
| 1428 | + if ($d2 === null) { |
|
| 1429 | + $d2 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d2); |
|
| 1430 | + } |
|
| 1431 | + /** @var ParagonIE_Sodium_Core_Curve25519_Fe $d2 */ |
|
| 1432 | + $r = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached(); |
|
| 1433 | + $r->YplusX = self::fe_add($p->Y, $p->X); |
|
| 1434 | + $r->YminusX = self::fe_sub($p->Y, $p->X); |
|
| 1435 | + $r->Z = self::fe_copy($p->Z); |
|
| 1436 | + $r->T2d = self::fe_mul($p->T, $d2); |
|
| 1437 | + return $r; |
|
| 1438 | + } |
|
| 1439 | + |
|
| 1440 | + /** |
|
| 1441 | + * @internal You should not use this directly from another application |
|
| 1442 | + * |
|
| 1443 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1444 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 |
|
| 1445 | + */ |
|
| 1446 | + public static function ge_p3_to_p2(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p) |
|
| 1447 | + { |
|
| 1448 | + return new ParagonIE_Sodium_Core_Curve25519_Ge_P2( |
|
| 1449 | + self::fe_copy($p->X), |
|
| 1450 | + self::fe_copy($p->Y), |
|
| 1451 | + self::fe_copy($p->Z) |
|
| 1452 | + ); |
|
| 1453 | + } |
|
| 1454 | + |
|
| 1455 | + /** |
|
| 1456 | + * @internal You should not use this directly from another application |
|
| 1457 | + * |
|
| 1458 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h |
|
| 1459 | + * @return string |
|
| 1460 | + * @throws SodiumException |
|
| 1461 | + * @throws TypeError |
|
| 1462 | + */ |
|
| 1463 | + public static function ge_p3_tobytes(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h) |
|
| 1464 | + { |
|
| 1465 | + $recip = self::fe_invert($h->Z); |
|
| 1466 | + $x = self::fe_mul($h->X, $recip); |
|
| 1467 | + $y = self::fe_mul($h->Y, $recip); |
|
| 1468 | + $s = self::fe_tobytes($y); |
|
| 1469 | + $s[31] = self::intToChr( |
|
| 1470 | + self::chrToInt($s[31]) ^ (self::fe_isnegative($x) << 7) |
|
| 1471 | + ); |
|
| 1472 | + return $s; |
|
| 1473 | + } |
|
| 1474 | + |
|
| 1475 | + /** |
|
| 1476 | + * @internal You should not use this directly from another application |
|
| 1477 | + * |
|
| 1478 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1479 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1480 | + */ |
|
| 1481 | + public static function ge_p3_dbl(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p) |
|
| 1482 | + { |
|
| 1483 | + $q = self::ge_p3_to_p2($p); |
|
| 1484 | + return self::ge_p2_dbl($q); |
|
| 1485 | + } |
|
| 1486 | + |
|
| 1487 | + /** |
|
| 1488 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_Precomp |
|
| 1489 | + */ |
|
| 1490 | + public static function ge_precomp_0() |
|
| 1491 | + { |
|
| 1492 | + return new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( |
|
| 1493 | + self::fe_1(), |
|
| 1494 | + self::fe_1(), |
|
| 1495 | + self::fe_0() |
|
| 1496 | + ); |
|
| 1497 | + } |
|
| 1498 | + |
|
| 1499 | + /** |
|
| 1500 | + * @internal You should not use this directly from another application |
|
| 1501 | + * |
|
| 1502 | + * @param int $b |
|
| 1503 | + * @param int $c |
|
| 1504 | + * @return int |
|
| 1505 | + */ |
|
| 1506 | + public static function equal($b, $c) |
|
| 1507 | + { |
|
| 1508 | + return (int) ((($b ^ $c) - 1) >> 31) & 1; |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + /** |
|
| 1512 | + * @internal You should not use this directly from another application |
|
| 1513 | + * |
|
| 1514 | + * @param int|string $char |
|
| 1515 | + * @return int (1 = yes, 0 = no) |
|
| 1516 | + * @throws SodiumException |
|
| 1517 | + * @throws TypeError |
|
| 1518 | + */ |
|
| 1519 | + public static function negative($char) |
|
| 1520 | + { |
|
| 1521 | + if (is_int($char)) { |
|
| 1522 | + return ($char >> 63) & 1; |
|
| 1523 | + } |
|
| 1524 | + $x = self::chrToInt(self::substr($char, 0, 1)); |
|
| 1525 | + return (int) ($x >> 63); |
|
| 1526 | + } |
|
| 1527 | + |
|
| 1528 | + /** |
|
| 1529 | + * Conditional move |
|
| 1530 | + * |
|
| 1531 | + * @internal You should not use this directly from another application |
|
| 1532 | + * |
|
| 1533 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $t |
|
| 1534 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $u |
|
| 1535 | + * @param int $b |
|
| 1536 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_Precomp |
|
| 1537 | + */ |
|
| 1538 | + public static function cmov( |
|
| 1539 | + ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $t, |
|
| 1540 | + ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $u, |
|
| 1541 | + $b |
|
| 1542 | + ) { |
|
| 1543 | + if (!is_int($b)) { |
|
| 1544 | + throw new InvalidArgumentException('Expected an integer.'); |
|
| 1545 | + } |
|
| 1546 | + return new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( |
|
| 1547 | + self::fe_cmov($t->yplusx, $u->yplusx, $b), |
|
| 1548 | + self::fe_cmov($t->yminusx, $u->yminusx, $b), |
|
| 1549 | + self::fe_cmov($t->xy2d, $u->xy2d, $b) |
|
| 1550 | + ); |
|
| 1551 | + } |
|
| 1552 | + |
|
| 1553 | + /** |
|
| 1554 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $t |
|
| 1555 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $u |
|
| 1556 | + * @param int $b |
|
| 1557 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_Cached |
|
| 1558 | + */ |
|
| 1559 | + public static function ge_cmov_cached( |
|
| 1560 | + ParagonIE_Sodium_Core_Curve25519_Ge_Cached $t, |
|
| 1561 | + ParagonIE_Sodium_Core_Curve25519_Ge_Cached $u, |
|
| 1562 | + $b |
|
| 1563 | + ) { |
|
| 1564 | + $b &= 1; |
|
| 1565 | + $ret = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached(); |
|
| 1566 | + $ret->YplusX = self::fe_cmov($t->YplusX, $u->YplusX, $b); |
|
| 1567 | + $ret->YminusX = self::fe_cmov($t->YminusX, $u->YminusX, $b); |
|
| 1568 | + $ret->Z = self::fe_cmov($t->Z, $u->Z, $b); |
|
| 1569 | + $ret->T2d = self::fe_cmov($t->T2d, $u->T2d, $b); |
|
| 1570 | + return $ret; |
|
| 1571 | + } |
|
| 1572 | + |
|
| 1573 | + /** |
|
| 1574 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached[] $cached |
|
| 1575 | + * @param int $b |
|
| 1576 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_Cached |
|
| 1577 | + * @throws SodiumException |
|
| 1578 | + */ |
|
| 1579 | + public static function ge_cmov8_cached(array $cached, $b) |
|
| 1580 | + { |
|
| 1581 | + // const unsigned char bnegative = negative(b); |
|
| 1582 | + // const unsigned char babs = b - (((-bnegative) & b) * ((signed char) 1 << 1)); |
|
| 1583 | + $bnegative = self::negative($b); |
|
| 1584 | + $babs = $b - (((-$bnegative) & $b) << 1); |
|
| 1585 | + |
|
| 1586 | + // ge25519_cached_0(t); |
|
| 1587 | + $t = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( |
|
| 1588 | + self::fe_1(), |
|
| 1589 | + self::fe_1(), |
|
| 1590 | + self::fe_1(), |
|
| 1591 | + self::fe_0() |
|
| 1592 | + ); |
|
| 1593 | + |
|
| 1594 | + // ge25519_cmov_cached(t, &cached[0], equal(babs, 1)); |
|
| 1595 | + // ge25519_cmov_cached(t, &cached[1], equal(babs, 2)); |
|
| 1596 | + // ge25519_cmov_cached(t, &cached[2], equal(babs, 3)); |
|
| 1597 | + // ge25519_cmov_cached(t, &cached[3], equal(babs, 4)); |
|
| 1598 | + // ge25519_cmov_cached(t, &cached[4], equal(babs, 5)); |
|
| 1599 | + // ge25519_cmov_cached(t, &cached[5], equal(babs, 6)); |
|
| 1600 | + // ge25519_cmov_cached(t, &cached[6], equal(babs, 7)); |
|
| 1601 | + // ge25519_cmov_cached(t, &cached[7], equal(babs, 8)); |
|
| 1602 | + for ($x = 0; $x < 8; ++$x) { |
|
| 1603 | + $t = self::ge_cmov_cached($t, $cached[$x], self::equal($babs, $x + 1)); |
|
| 1604 | + } |
|
| 1605 | + |
|
| 1606 | + // fe25519_copy(minust.YplusX, t->YminusX); |
|
| 1607 | + // fe25519_copy(minust.YminusX, t->YplusX); |
|
| 1608 | + // fe25519_copy(minust.Z, t->Z); |
|
| 1609 | + // fe25519_neg(minust.T2d, t->T2d); |
|
| 1610 | + $minust = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( |
|
| 1611 | + self::fe_copy($t->YminusX), |
|
| 1612 | + self::fe_copy($t->YplusX), |
|
| 1613 | + self::fe_copy($t->Z), |
|
| 1614 | + self::fe_neg($t->T2d) |
|
| 1615 | + ); |
|
| 1616 | + return self::ge_cmov_cached($t, $minust, $bnegative); |
|
| 1617 | + } |
|
| 1618 | + |
|
| 1619 | + /** |
|
| 1620 | + * @internal You should not use this directly from another application |
|
| 1621 | + * |
|
| 1622 | + * @param int $pos |
|
| 1623 | + * @param int $b |
|
| 1624 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_Precomp |
|
| 1625 | + * @throws SodiumException |
|
| 1626 | + * @throws TypeError |
|
| 1627 | + * @psalm-suppress MixedArgument |
|
| 1628 | + * @psalm-suppress MixedArrayAccess |
|
| 1629 | + * @psalm-suppress MixedArrayOffset |
|
| 1630 | + */ |
|
| 1631 | + public static function ge_select($pos = 0, $b = 0) |
|
| 1632 | + { |
|
| 1633 | + static $base = null; |
|
| 1634 | + if ($base === null) { |
|
| 1635 | + $base = array(); |
|
| 1636 | + /** @var int $i */ |
|
| 1637 | + foreach (self::$base as $i => $bas) { |
|
| 1638 | + for ($j = 0; $j < 8; ++$j) { |
|
| 1639 | + $base[$i][$j] = new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( |
|
| 1640 | + ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($bas[$j][0]), |
|
| 1641 | + ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($bas[$j][1]), |
|
| 1642 | + ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($bas[$j][2]) |
|
| 1643 | + ); |
|
| 1644 | + } |
|
| 1645 | + } |
|
| 1646 | + } |
|
| 1647 | + /** @var array<int, array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Precomp>> $base */ |
|
| 1648 | + if (!is_int($pos)) { |
|
| 1649 | + throw new InvalidArgumentException('Position must be an integer'); |
|
| 1650 | + } |
|
| 1651 | + if ($pos < 0 || $pos > 31) { |
|
| 1652 | + throw new RangeException('Position is out of range [0, 31]'); |
|
| 1653 | + } |
|
| 1654 | + |
|
| 1655 | + $bnegative = self::negative($b); |
|
| 1656 | + $babs = $b - (((-$bnegative) & $b) << 1); |
|
| 1657 | + |
|
| 1658 | + $t = self::ge_precomp_0(); |
|
| 1659 | + for ($i = 0; $i < 8; ++$i) { |
|
| 1660 | + $t = self::cmov( |
|
| 1661 | + $t, |
|
| 1662 | + $base[$pos][$i], |
|
| 1663 | + self::equal($babs, $i + 1) |
|
| 1664 | + ); |
|
| 1665 | + } |
|
| 1666 | + $minusT = new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( |
|
| 1667 | + self::fe_copy($t->yminusx), |
|
| 1668 | + self::fe_copy($t->yplusx), |
|
| 1669 | + self::fe_neg($t->xy2d) |
|
| 1670 | + ); |
|
| 1671 | + return self::cmov($t, $minusT, $bnegative); |
|
| 1672 | + } |
|
| 1673 | + |
|
| 1674 | + /** |
|
| 1675 | + * Subtract two group elements. |
|
| 1676 | + * |
|
| 1677 | + * r = p - q |
|
| 1678 | + * |
|
| 1679 | + * @internal You should not use this directly from another application |
|
| 1680 | + * |
|
| 1681 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1682 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q |
|
| 1683 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 1684 | + */ |
|
| 1685 | + public static function ge_sub( |
|
| 1686 | + ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, |
|
| 1687 | + ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q |
|
| 1688 | + ) { |
|
| 1689 | + $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); |
|
| 1690 | + |
|
| 1691 | + $r->X = self::fe_add($p->Y, $p->X); |
|
| 1692 | + $r->Y = self::fe_sub($p->Y, $p->X); |
|
| 1693 | + $r->Z = self::fe_mul($r->X, $q->YminusX); |
|
| 1694 | + $r->Y = self::fe_mul($r->Y, $q->YplusX); |
|
| 1695 | + $r->T = self::fe_mul($q->T2d, $p->T); |
|
| 1696 | + $r->X = self::fe_mul($p->Z, $q->Z); |
|
| 1697 | + $t0 = self::fe_add($r->X, $r->X); |
|
| 1698 | + $r->X = self::fe_sub($r->Z, $r->Y); |
|
| 1699 | + $r->Y = self::fe_add($r->Z, $r->Y); |
|
| 1700 | + $r->Z = self::fe_sub($t0, $r->T); |
|
| 1701 | + $r->T = self::fe_add($t0, $r->T); |
|
| 1702 | + |
|
| 1703 | + return $r; |
|
| 1704 | + } |
|
| 1705 | + |
|
| 1706 | + /** |
|
| 1707 | + * Convert a group element to a byte string. |
|
| 1708 | + * |
|
| 1709 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P2 $h |
|
| 1710 | + * @return string |
|
| 1711 | + * @throws SodiumException |
|
| 1712 | + * @throws TypeError |
|
| 1713 | + */ |
|
| 1714 | + public static function ge_tobytes(ParagonIE_Sodium_Core_Curve25519_Ge_P2 $h) |
|
| 1715 | + { |
|
| 1716 | + $recip = self::fe_invert($h->Z); |
|
| 1717 | + $x = self::fe_mul($h->X, $recip); |
|
| 1718 | + $y = self::fe_mul($h->Y, $recip); |
|
| 1719 | + $s = self::fe_tobytes($y); |
|
| 1720 | + $s[31] = self::intToChr( |
|
| 1721 | + self::chrToInt($s[31]) ^ (self::fe_isnegative($x) << 7) |
|
| 1722 | + ); |
|
| 1723 | + return $s; |
|
| 1724 | + } |
|
| 1725 | + |
|
| 1726 | + /** |
|
| 1727 | + * @internal You should not use this directly from another application |
|
| 1728 | + * |
|
| 1729 | + * @param string $a |
|
| 1730 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A |
|
| 1731 | + * @param string $b |
|
| 1732 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 |
|
| 1733 | + * @throws SodiumException |
|
| 1734 | + * @throws TypeError |
|
| 1735 | + * @psalm-suppress MixedArgument |
|
| 1736 | + * @psalm-suppress MixedArrayAccess |
|
| 1737 | + */ |
|
| 1738 | + public static function ge_double_scalarmult_vartime( |
|
| 1739 | + $a, |
|
| 1740 | + ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A, |
|
| 1741 | + $b |
|
| 1742 | + ) { |
|
| 1743 | + /** @var array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Cached> $Ai */ |
|
| 1744 | + $Ai = array(); |
|
| 1745 | + |
|
| 1746 | + /** @var array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Precomp> $Bi */ |
|
| 1747 | + static $Bi = array(); |
|
| 1748 | + if (!$Bi) { |
|
| 1749 | + for ($i = 0; $i < 8; ++$i) { |
|
| 1750 | + $Bi[$i] = new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( |
|
| 1751 | + ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$base2[$i][0]), |
|
| 1752 | + ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$base2[$i][1]), |
|
| 1753 | + ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$base2[$i][2]) |
|
| 1754 | + ); |
|
| 1755 | + } |
|
| 1756 | + } |
|
| 1757 | + for ($i = 0; $i < 8; ++$i) { |
|
| 1758 | + $Ai[$i] = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( |
|
| 1759 | + self::fe_0(), |
|
| 1760 | + self::fe_0(), |
|
| 1761 | + self::fe_0(), |
|
| 1762 | + self::fe_0() |
|
| 1763 | + ); |
|
| 1764 | + } |
|
| 1765 | + |
|
| 1766 | + # slide(aslide,a); |
|
| 1767 | + # slide(bslide,b); |
|
| 1768 | + /** @var array<int, int> $aslide */ |
|
| 1769 | + $aslide = self::slide($a); |
|
| 1770 | + /** @var array<int, int> $bslide */ |
|
| 1771 | + $bslide = self::slide($b); |
|
| 1772 | + |
|
| 1773 | + # ge_p3_to_cached(&Ai[0],A); |
|
| 1774 | + # ge_p3_dbl(&t,A); ge_p1p1_to_p3(&A2,&t); |
|
| 1775 | + $Ai[0] = self::ge_p3_to_cached($A); |
|
| 1776 | + $t = self::ge_p3_dbl($A); |
|
| 1777 | + $A2 = self::ge_p1p1_to_p3($t); |
|
| 1778 | + |
|
| 1779 | + # ge_add(&t,&A2,&Ai[0]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[1],&u); |
|
| 1780 | + # ge_add(&t,&A2,&Ai[1]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[2],&u); |
|
| 1781 | + # ge_add(&t,&A2,&Ai[2]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[3],&u); |
|
| 1782 | + # ge_add(&t,&A2,&Ai[3]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[4],&u); |
|
| 1783 | + # ge_add(&t,&A2,&Ai[4]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[5],&u); |
|
| 1784 | + # ge_add(&t,&A2,&Ai[5]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[6],&u); |
|
| 1785 | + # ge_add(&t,&A2,&Ai[6]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[7],&u); |
|
| 1786 | + for ($i = 0; $i < 7; ++$i) { |
|
| 1787 | + $t = self::ge_add($A2, $Ai[$i]); |
|
| 1788 | + $u = self::ge_p1p1_to_p3($t); |
|
| 1789 | + $Ai[$i + 1] = self::ge_p3_to_cached($u); |
|
| 1790 | + } |
|
| 1791 | + |
|
| 1792 | + # ge_p2_0(r); |
|
| 1793 | + $r = self::ge_p2_0(); |
|
| 1794 | + |
|
| 1795 | + # for (i = 255;i >= 0;--i) { |
|
| 1796 | + # if (aslide[i] || bslide[i]) break; |
|
| 1797 | + # } |
|
| 1798 | + $i = 255; |
|
| 1799 | + for (; $i >= 0; --$i) { |
|
| 1800 | + if ($aslide[$i] || $bslide[$i]) { |
|
| 1801 | + break; |
|
| 1802 | + } |
|
| 1803 | + } |
|
| 1804 | + |
|
| 1805 | + # for (;i >= 0;--i) { |
|
| 1806 | + for (; $i >= 0; --$i) { |
|
| 1807 | + # ge_p2_dbl(&t,r); |
|
| 1808 | + $t = self::ge_p2_dbl($r); |
|
| 1809 | + |
|
| 1810 | + # if (aslide[i] > 0) { |
|
| 1811 | + if ($aslide[$i] > 0) { |
|
| 1812 | + # ge_p1p1_to_p3(&u,&t); |
|
| 1813 | + # ge_add(&t,&u,&Ai[aslide[i]/2]); |
|
| 1814 | + $u = self::ge_p1p1_to_p3($t); |
|
| 1815 | + $t = self::ge_add( |
|
| 1816 | + $u, |
|
| 1817 | + $Ai[(int) floor($aslide[$i] / 2)] |
|
| 1818 | + ); |
|
| 1819 | + # } else if (aslide[i] < 0) { |
|
| 1820 | + } elseif ($aslide[$i] < 0) { |
|
| 1821 | + # ge_p1p1_to_p3(&u,&t); |
|
| 1822 | + # ge_sub(&t,&u,&Ai[(-aslide[i])/2]); |
|
| 1823 | + $u = self::ge_p1p1_to_p3($t); |
|
| 1824 | + $t = self::ge_sub( |
|
| 1825 | + $u, |
|
| 1826 | + $Ai[(int) floor(-$aslide[$i] / 2)] |
|
| 1827 | + ); |
|
| 1828 | + } |
|
| 1829 | + |
|
| 1830 | + # if (bslide[i] > 0) { |
|
| 1831 | + if ($bslide[$i] > 0) { |
|
| 1832 | + /** @var int $index */ |
|
| 1833 | + $index = (int) floor($bslide[$i] / 2); |
|
| 1834 | + # ge_p1p1_to_p3(&u,&t); |
|
| 1835 | + # ge_madd(&t,&u,&Bi[bslide[i]/2]); |
|
| 1836 | + $u = self::ge_p1p1_to_p3($t); |
|
| 1837 | + $t = self::ge_madd($t, $u, $Bi[$index]); |
|
| 1838 | + # } else if (bslide[i] < 0) { |
|
| 1839 | + } elseif ($bslide[$i] < 0) { |
|
| 1840 | + /** @var int $index */ |
|
| 1841 | + $index = (int) floor(-$bslide[$i] / 2); |
|
| 1842 | + # ge_p1p1_to_p3(&u,&t); |
|
| 1843 | + # ge_msub(&t,&u,&Bi[(-bslide[i])/2]); |
|
| 1844 | + $u = self::ge_p1p1_to_p3($t); |
|
| 1845 | + $t = self::ge_msub($t, $u, $Bi[$index]); |
|
| 1846 | + } |
|
| 1847 | + # ge_p1p1_to_p2(r,&t); |
|
| 1848 | + $r = self::ge_p1p1_to_p2($t); |
|
| 1849 | + } |
|
| 1850 | + return $r; |
|
| 1851 | + } |
|
| 1852 | + |
|
| 1853 | + /** |
|
| 1854 | + * @internal You should not use this directly from another application |
|
| 1855 | + * |
|
| 1856 | + * @param string $a |
|
| 1857 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p |
|
| 1858 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 1859 | + * @throws SodiumException |
|
| 1860 | + * @throws TypeError |
|
| 1861 | + * @psalm-suppress MixedAssignment |
|
| 1862 | + * @psalm-suppress MixedOperand |
|
| 1863 | + */ |
|
| 1864 | + public static function ge_scalarmult($a, $p) |
|
| 1865 | + { |
|
| 1866 | + $e = array_fill(0, 64, 0); |
|
| 1867 | + |
|
| 1868 | + /** @var ParagonIE_Sodium_Core_Curve25519_Ge_Cached[] $pi */ |
|
| 1869 | + $pi = array(); |
|
| 1870 | + |
|
| 1871 | + // ge25519_p3_to_cached(&pi[1 - 1], p); /* p */ |
|
| 1872 | + $pi[0] = self::ge_p3_to_cached($p); |
|
| 1873 | + |
|
| 1874 | + // ge25519_p3_dbl(&t2, p); |
|
| 1875 | + // ge25519_p1p1_to_p3(&p2, &t2); |
|
| 1876 | + // ge25519_p3_to_cached(&pi[2 - 1], &p2); /* 2p = 2*p */ |
|
| 1877 | + $t2 = self::ge_p3_dbl($p); |
|
| 1878 | + $p2 = self::ge_p1p1_to_p3($t2); |
|
| 1879 | + $pi[1] = self::ge_p3_to_cached($p2); |
|
| 1880 | + |
|
| 1881 | + // ge25519_add_cached(&t3, p, &pi[2 - 1]); |
|
| 1882 | + // ge25519_p1p1_to_p3(&p3, &t3); |
|
| 1883 | + // ge25519_p3_to_cached(&pi[3 - 1], &p3); /* 3p = 2p+p */ |
|
| 1884 | + $t3 = self::ge_add($p, $pi[1]); |
|
| 1885 | + $p3 = self::ge_p1p1_to_p3($t3); |
|
| 1886 | + $pi[2] = self::ge_p3_to_cached($p3); |
|
| 1887 | + |
|
| 1888 | + // ge25519_p3_dbl(&t4, &p2); |
|
| 1889 | + // ge25519_p1p1_to_p3(&p4, &t4); |
|
| 1890 | + // ge25519_p3_to_cached(&pi[4 - 1], &p4); /* 4p = 2*2p */ |
|
| 1891 | + $t4 = self::ge_p3_dbl($p2); |
|
| 1892 | + $p4 = self::ge_p1p1_to_p3($t4); |
|
| 1893 | + $pi[3] = self::ge_p3_to_cached($p4); |
|
| 1894 | + |
|
| 1895 | + // ge25519_add_cached(&t5, p, &pi[4 - 1]); |
|
| 1896 | + // ge25519_p1p1_to_p3(&p5, &t5); |
|
| 1897 | + // ge25519_p3_to_cached(&pi[5 - 1], &p5); /* 5p = 4p+p */ |
|
| 1898 | + $t5 = self::ge_add($p, $pi[3]); |
|
| 1899 | + $p5 = self::ge_p1p1_to_p3($t5); |
|
| 1900 | + $pi[4] = self::ge_p3_to_cached($p5); |
|
| 1901 | + |
|
| 1902 | + // ge25519_p3_dbl(&t6, &p3); |
|
| 1903 | + // ge25519_p1p1_to_p3(&p6, &t6); |
|
| 1904 | + // ge25519_p3_to_cached(&pi[6 - 1], &p6); /* 6p = 2*3p */ |
|
| 1905 | + $t6 = self::ge_p3_dbl($p3); |
|
| 1906 | + $p6 = self::ge_p1p1_to_p3($t6); |
|
| 1907 | + $pi[5] = self::ge_p3_to_cached($p6); |
|
| 1908 | + |
|
| 1909 | + // ge25519_add_cached(&t7, p, &pi[6 - 1]); |
|
| 1910 | + // ge25519_p1p1_to_p3(&p7, &t7); |
|
| 1911 | + // ge25519_p3_to_cached(&pi[7 - 1], &p7); /* 7p = 6p+p */ |
|
| 1912 | + $t7 = self::ge_add($p, $pi[5]); |
|
| 1913 | + $p7 = self::ge_p1p1_to_p3($t7); |
|
| 1914 | + $pi[6] = self::ge_p3_to_cached($p7); |
|
| 1915 | + |
|
| 1916 | + // ge25519_p3_dbl(&t8, &p4); |
|
| 1917 | + // ge25519_p1p1_to_p3(&p8, &t8); |
|
| 1918 | + // ge25519_p3_to_cached(&pi[8 - 1], &p8); /* 8p = 2*4p */ |
|
| 1919 | + $t8 = self::ge_p3_dbl($p4); |
|
| 1920 | + $p8 = self::ge_p1p1_to_p3($t8); |
|
| 1921 | + $pi[7] = self::ge_p3_to_cached($p8); |
|
| 1922 | + |
|
| 1923 | + |
|
| 1924 | + // for (i = 0; i < 32; ++i) { |
|
| 1925 | + // e[2 * i + 0] = (a[i] >> 0) & 15; |
|
| 1926 | + // e[2 * i + 1] = (a[i] >> 4) & 15; |
|
| 1927 | + // } |
|
| 1928 | + for ($i = 0; $i < 32; ++$i) { |
|
| 1929 | + $e[($i << 1) ] = self::chrToInt($a[$i]) & 15; |
|
| 1930 | + $e[($i << 1) + 1] = (self::chrToInt($a[$i]) >> 4) & 15; |
|
| 1931 | + } |
|
| 1932 | + // /* each e[i] is between 0 and 15 */ |
|
| 1933 | + // /* e[63] is between 0 and 7 */ |
|
| 1934 | + |
|
| 1935 | + // carry = 0; |
|
| 1936 | + // for (i = 0; i < 63; ++i) { |
|
| 1937 | + // e[i] += carry; |
|
| 1938 | + // carry = e[i] + 8; |
|
| 1939 | + // carry >>= 4; |
|
| 1940 | + // e[i] -= carry * ((signed char) 1 << 4); |
|
| 1941 | + // } |
|
| 1942 | + $carry = 0; |
|
| 1943 | + for ($i = 0; $i < 63; ++$i) { |
|
| 1944 | + $e[$i] += $carry; |
|
| 1945 | + $carry = $e[$i] + 8; |
|
| 1946 | + $carry >>= 4; |
|
| 1947 | + $e[$i] -= $carry << 4; |
|
| 1948 | + } |
|
| 1949 | + // e[63] += carry; |
|
| 1950 | + // /* each e[i] is between -8 and 8 */ |
|
| 1951 | + $e[63] += $carry; |
|
| 1952 | + |
|
| 1953 | + // ge25519_p3_0(h); |
|
| 1954 | + $h = self::ge_p3_0(); |
|
| 1955 | + |
|
| 1956 | + // for (i = 63; i != 0; i--) { |
|
| 1957 | + for ($i = 63; $i != 0; --$i) { |
|
| 1958 | + // ge25519_cmov8_cached(&t, pi, e[i]); |
|
| 1959 | + $t = self::ge_cmov8_cached($pi, $e[$i]); |
|
| 1960 | + // ge25519_add_cached(&r, h, &t); |
|
| 1961 | + $r = self::ge_add($h, $t); |
|
| 1962 | + |
|
| 1963 | + // ge25519_p1p1_to_p2(&s, &r); |
|
| 1964 | + // ge25519_p2_dbl(&r, &s); |
|
| 1965 | + // ge25519_p1p1_to_p2(&s, &r); |
|
| 1966 | + // ge25519_p2_dbl(&r, &s); |
|
| 1967 | + // ge25519_p1p1_to_p2(&s, &r); |
|
| 1968 | + // ge25519_p2_dbl(&r, &s); |
|
| 1969 | + // ge25519_p1p1_to_p2(&s, &r); |
|
| 1970 | + // ge25519_p2_dbl(&r, &s); |
|
| 1971 | + $s = self::ge_p1p1_to_p2($r); |
|
| 1972 | + $r = self::ge_p2_dbl($s); |
|
| 1973 | + $s = self::ge_p1p1_to_p2($r); |
|
| 1974 | + $r = self::ge_p2_dbl($s); |
|
| 1975 | + $s = self::ge_p1p1_to_p2($r); |
|
| 1976 | + $r = self::ge_p2_dbl($s); |
|
| 1977 | + $s = self::ge_p1p1_to_p2($r); |
|
| 1978 | + $r = self::ge_p2_dbl($s); |
|
| 1979 | + |
|
| 1980 | + // ge25519_p1p1_to_p3(h, &r); /* *16 */ |
|
| 1981 | + $h = self::ge_p1p1_to_p3($r); /* *16 */ |
|
| 1982 | + } |
|
| 1983 | + |
|
| 1984 | + // ge25519_cmov8_cached(&t, pi, e[i]); |
|
| 1985 | + // ge25519_add_cached(&r, h, &t); |
|
| 1986 | + // ge25519_p1p1_to_p3(h, &r); |
|
| 1987 | + $t = self::ge_cmov8_cached($pi, $e[0]); |
|
| 1988 | + $r = self::ge_add($h, $t); |
|
| 1989 | + return self::ge_p1p1_to_p3($r); |
|
| 1990 | + } |
|
| 1991 | + |
|
| 1992 | + /** |
|
| 1993 | + * @internal You should not use this directly from another application |
|
| 1994 | + * |
|
| 1995 | + * @param string $a |
|
| 1996 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 1997 | + * @throws SodiumException |
|
| 1998 | + * @throws TypeError |
|
| 1999 | + * @psalm-suppress MixedAssignment |
|
| 2000 | + * @psalm-suppress MixedOperand |
|
| 2001 | + */ |
|
| 2002 | + public static function ge_scalarmult_base($a) |
|
| 2003 | + { |
|
| 2004 | + /** @var array<int, int> $e */ |
|
| 2005 | + $e = array(); |
|
| 2006 | + $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); |
|
| 2007 | + |
|
| 2008 | + for ($i = 0; $i < 32; ++$i) { |
|
| 2009 | + $dbl = (int) $i << 1; |
|
| 2010 | + $e[$dbl] = (int) self::chrToInt($a[$i]) & 15; |
|
| 2011 | + $e[$dbl + 1] = (int) (self::chrToInt($a[$i]) >> 4) & 15; |
|
| 2012 | + } |
|
| 2013 | + |
|
| 2014 | + $carry = 0; |
|
| 2015 | + for ($i = 0; $i < 63; ++$i) { |
|
| 2016 | + $e[$i] += $carry; |
|
| 2017 | + $carry = $e[$i] + 8; |
|
| 2018 | + $carry >>= 4; |
|
| 2019 | + $e[$i] -= $carry << 4; |
|
| 2020 | + } |
|
| 2021 | + $e[63] += (int) $carry; |
|
| 2022 | + |
|
| 2023 | + $h = self::ge_p3_0(); |
|
| 2024 | + |
|
| 2025 | + for ($i = 1; $i < 64; $i += 2) { |
|
| 2026 | + $t = self::ge_select((int) floor($i / 2), (int) $e[$i]); |
|
| 2027 | + $r = self::ge_madd($r, $h, $t); |
|
| 2028 | + $h = self::ge_p1p1_to_p3($r); |
|
| 2029 | + } |
|
| 2030 | + |
|
| 2031 | + $r = self::ge_p3_dbl($h); |
|
| 2032 | + |
|
| 2033 | + $s = self::ge_p1p1_to_p2($r); |
|
| 2034 | + $r = self::ge_p2_dbl($s); |
|
| 2035 | + $s = self::ge_p1p1_to_p2($r); |
|
| 2036 | + $r = self::ge_p2_dbl($s); |
|
| 2037 | + $s = self::ge_p1p1_to_p2($r); |
|
| 2038 | + $r = self::ge_p2_dbl($s); |
|
| 2039 | + |
|
| 2040 | + $h = self::ge_p1p1_to_p3($r); |
|
| 2041 | + |
|
| 2042 | + for ($i = 0; $i < 64; $i += 2) { |
|
| 2043 | + $t = self::ge_select($i >> 1, (int) $e[$i]); |
|
| 2044 | + $r = self::ge_madd($r, $h, $t); |
|
| 2045 | + $h = self::ge_p1p1_to_p3($r); |
|
| 2046 | + } |
|
| 2047 | + return $h; |
|
| 2048 | + } |
|
| 2049 | + |
|
| 2050 | + /** |
|
| 2051 | + * Calculates (ab + c) mod l |
|
| 2052 | + * where l = 2^252 + 27742317777372353535851937790883648493 |
|
| 2053 | + * |
|
| 2054 | + * @internal You should not use this directly from another application |
|
| 2055 | + * |
|
| 2056 | + * @param string $a |
|
| 2057 | + * @param string $b |
|
| 2058 | + * @param string $c |
|
| 2059 | + * @return string |
|
| 2060 | + * @throws TypeError |
|
| 2061 | + */ |
|
| 2062 | + public static function sc_muladd($a, $b, $c) |
|
| 2063 | + { |
|
| 2064 | + $a0 = 2097151 & self::load_3(self::substr($a, 0, 3)); |
|
| 2065 | + $a1 = 2097151 & (self::load_4(self::substr($a, 2, 4)) >> 5); |
|
| 2066 | + $a2 = 2097151 & (self::load_3(self::substr($a, 5, 3)) >> 2); |
|
| 2067 | + $a3 = 2097151 & (self::load_4(self::substr($a, 7, 4)) >> 7); |
|
| 2068 | + $a4 = 2097151 & (self::load_4(self::substr($a, 10, 4)) >> 4); |
|
| 2069 | + $a5 = 2097151 & (self::load_3(self::substr($a, 13, 3)) >> 1); |
|
| 2070 | + $a6 = 2097151 & (self::load_4(self::substr($a, 15, 4)) >> 6); |
|
| 2071 | + $a7 = 2097151 & (self::load_3(self::substr($a, 18, 3)) >> 3); |
|
| 2072 | + $a8 = 2097151 & self::load_3(self::substr($a, 21, 3)); |
|
| 2073 | + $a9 = 2097151 & (self::load_4(self::substr($a, 23, 4)) >> 5); |
|
| 2074 | + $a10 = 2097151 & (self::load_3(self::substr($a, 26, 3)) >> 2); |
|
| 2075 | + $a11 = (self::load_4(self::substr($a, 28, 4)) >> 7); |
|
| 2076 | + |
|
| 2077 | + $b0 = 2097151 & self::load_3(self::substr($b, 0, 3)); |
|
| 2078 | + $b1 = 2097151 & (self::load_4(self::substr($b, 2, 4)) >> 5); |
|
| 2079 | + $b2 = 2097151 & (self::load_3(self::substr($b, 5, 3)) >> 2); |
|
| 2080 | + $b3 = 2097151 & (self::load_4(self::substr($b, 7, 4)) >> 7); |
|
| 2081 | + $b4 = 2097151 & (self::load_4(self::substr($b, 10, 4)) >> 4); |
|
| 2082 | + $b5 = 2097151 & (self::load_3(self::substr($b, 13, 3)) >> 1); |
|
| 2083 | + $b6 = 2097151 & (self::load_4(self::substr($b, 15, 4)) >> 6); |
|
| 2084 | + $b7 = 2097151 & (self::load_3(self::substr($b, 18, 3)) >> 3); |
|
| 2085 | + $b8 = 2097151 & self::load_3(self::substr($b, 21, 3)); |
|
| 2086 | + $b9 = 2097151 & (self::load_4(self::substr($b, 23, 4)) >> 5); |
|
| 2087 | + $b10 = 2097151 & (self::load_3(self::substr($b, 26, 3)) >> 2); |
|
| 2088 | + $b11 = (self::load_4(self::substr($b, 28, 4)) >> 7); |
|
| 2089 | + |
|
| 2090 | + $c0 = 2097151 & self::load_3(self::substr($c, 0, 3)); |
|
| 2091 | + $c1 = 2097151 & (self::load_4(self::substr($c, 2, 4)) >> 5); |
|
| 2092 | + $c2 = 2097151 & (self::load_3(self::substr($c, 5, 3)) >> 2); |
|
| 2093 | + $c3 = 2097151 & (self::load_4(self::substr($c, 7, 4)) >> 7); |
|
| 2094 | + $c4 = 2097151 & (self::load_4(self::substr($c, 10, 4)) >> 4); |
|
| 2095 | + $c5 = 2097151 & (self::load_3(self::substr($c, 13, 3)) >> 1); |
|
| 2096 | + $c6 = 2097151 & (self::load_4(self::substr($c, 15, 4)) >> 6); |
|
| 2097 | + $c7 = 2097151 & (self::load_3(self::substr($c, 18, 3)) >> 3); |
|
| 2098 | + $c8 = 2097151 & self::load_3(self::substr($c, 21, 3)); |
|
| 2099 | + $c9 = 2097151 & (self::load_4(self::substr($c, 23, 4)) >> 5); |
|
| 2100 | + $c10 = 2097151 & (self::load_3(self::substr($c, 26, 3)) >> 2); |
|
| 2101 | + $c11 = (self::load_4(self::substr($c, 28, 4)) >> 7); |
|
| 2102 | + |
|
| 2103 | + /* Can't really avoid the pyramid here: */ |
|
| 2104 | + $s0 = $c0 + self::mul($a0, $b0, 24); |
|
| 2105 | + $s1 = $c1 + self::mul($a0, $b1, 24) + self::mul($a1, $b0, 24); |
|
| 2106 | + $s2 = $c2 + self::mul($a0, $b2, 24) + self::mul($a1, $b1, 24) + self::mul($a2, $b0, 24); |
|
| 2107 | + $s3 = $c3 + self::mul($a0, $b3, 24) + self::mul($a1, $b2, 24) + self::mul($a2, $b1, 24) + self::mul($a3, $b0, 24); |
|
| 2108 | + $s4 = $c4 + self::mul($a0, $b4, 24) + self::mul($a1, $b3, 24) + self::mul($a2, $b2, 24) + self::mul($a3, $b1, 24) + |
|
| 2109 | + self::mul($a4, $b0, 24); |
|
| 2110 | + $s5 = $c5 + self::mul($a0, $b5, 24) + self::mul($a1, $b4, 24) + self::mul($a2, $b3, 24) + self::mul($a3, $b2, 24) + |
|
| 2111 | + self::mul($a4, $b1, 24) + self::mul($a5, $b0, 24); |
|
| 2112 | + $s6 = $c6 + self::mul($a0, $b6, 24) + self::mul($a1, $b5, 24) + self::mul($a2, $b4, 24) + self::mul($a3, $b3, 24) + |
|
| 2113 | + self::mul($a4, $b2, 24) + self::mul($a5, $b1, 24) + self::mul($a6, $b0, 24); |
|
| 2114 | + $s7 = $c7 + self::mul($a0, $b7, 24) + self::mul($a1, $b6, 24) + self::mul($a2, $b5, 24) + self::mul($a3, $b4, 24) + |
|
| 2115 | + self::mul($a4, $b3, 24) + self::mul($a5, $b2, 24) + self::mul($a6, $b1, 24) + self::mul($a7, $b0, 24); |
|
| 2116 | + $s8 = $c8 + self::mul($a0, $b8, 24) + self::mul($a1, $b7, 24) + self::mul($a2, $b6, 24) + self::mul($a3, $b5, 24) + |
|
| 2117 | + self::mul($a4, $b4, 24) + self::mul($a5, $b3, 24) + self::mul($a6, $b2, 24) + self::mul($a7, $b1, 24) + |
|
| 2118 | + self::mul($a8, $b0, 24); |
|
| 2119 | + $s9 = $c9 + self::mul($a0, $b9, 24) + self::mul($a1, $b8, 24) + self::mul($a2, $b7, 24) + self::mul($a3, $b6, 24) + |
|
| 2120 | + self::mul($a4, $b5, 24) + self::mul($a5, $b4, 24) + self::mul($a6, $b3, 24) + self::mul($a7, $b2, 24) + |
|
| 2121 | + self::mul($a8, $b1, 24) + self::mul($a9, $b0, 24); |
|
| 2122 | + $s10 = $c10 + self::mul($a0, $b10, 24) + self::mul($a1, $b9, 24) + self::mul($a2, $b8, 24) + self::mul($a3, $b7, 24) + |
|
| 2123 | + self::mul($a4, $b6, 24) + self::mul($a5, $b5, 24) + self::mul($a6, $b4, 24) + self::mul($a7, $b3, 24) + |
|
| 2124 | + self::mul($a8, $b2, 24) + self::mul($a9, $b1, 24) + self::mul($a10, $b0, 24); |
|
| 2125 | + $s11 = $c11 + self::mul($a0, $b11, 24) + self::mul($a1, $b10, 24) + self::mul($a2, $b9, 24) + self::mul($a3, $b8, 24) + |
|
| 2126 | + self::mul($a4, $b7, 24) + self::mul($a5, $b6, 24) + self::mul($a6, $b5, 24) + self::mul($a7, $b4, 24) + |
|
| 2127 | + self::mul($a8, $b3, 24) + self::mul($a9, $b2, 24) + self::mul($a10, $b1, 24) + self::mul($a11, $b0, 24); |
|
| 2128 | + $s12 = self::mul($a1, $b11, 24) + self::mul($a2, $b10, 24) + self::mul($a3, $b9, 24) + self::mul($a4, $b8, 24) + |
|
| 2129 | + self::mul($a5, $b7, 24) + self::mul($a6, $b6, 24) + self::mul($a7, $b5, 24) + self::mul($a8, $b4, 24) + |
|
| 2130 | + self::mul($a9, $b3, 24) + self::mul($a10, $b2, 24) + self::mul($a11, $b1, 24); |
|
| 2131 | + $s13 = self::mul($a2, $b11, 24) + self::mul($a3, $b10, 24) + self::mul($a4, $b9, 24) + self::mul($a5, $b8, 24) + |
|
| 2132 | + self::mul($a6, $b7, 24) + self::mul($a7, $b6, 24) + self::mul($a8, $b5, 24) + self::mul($a9, $b4, 24) + |
|
| 2133 | + self::mul($a10, $b3, 24) + self::mul($a11, $b2, 24); |
|
| 2134 | + $s14 = self::mul($a3, $b11, 24) + self::mul($a4, $b10, 24) + self::mul($a5, $b9, 24) + self::mul($a6, $b8, 24) + |
|
| 2135 | + self::mul($a7, $b7, 24) + self::mul($a8, $b6, 24) + self::mul($a9, $b5, 24) + self::mul($a10, $b4, 24) + |
|
| 2136 | + self::mul($a11, $b3, 24); |
|
| 2137 | + $s15 = self::mul($a4, $b11, 24) + self::mul($a5, $b10, 24) + self::mul($a6, $b9, 24) + self::mul($a7, $b8, 24) + |
|
| 2138 | + self::mul($a8, $b7, 24) + self::mul($a9, $b6, 24) + self::mul($a10, $b5, 24) + self::mul($a11, $b4, 24); |
|
| 2139 | + $s16 = self::mul($a5, $b11, 24) + self::mul($a6, $b10, 24) + self::mul($a7, $b9, 24) + self::mul($a8, $b8, 24) + |
|
| 2140 | + self::mul($a9, $b7, 24) + self::mul($a10, $b6, 24) + self::mul($a11, $b5, 24); |
|
| 2141 | + $s17 = self::mul($a6, $b11, 24) + self::mul($a7, $b10, 24) + self::mul($a8, $b9, 24) + self::mul($a9, $b8, 24) + |
|
| 2142 | + self::mul($a10, $b7, 24) + self::mul($a11, $b6, 24); |
|
| 2143 | + $s18 = self::mul($a7, $b11, 24) + self::mul($a8, $b10, 24) + self::mul($a9, $b9, 24) + self::mul($a10, $b8, 24) + |
|
| 2144 | + self::mul($a11, $b7, 24); |
|
| 2145 | + $s19 = self::mul($a8, $b11, 24) + self::mul($a9, $b10, 24) + self::mul($a10, $b9, 24) + self::mul($a11, $b8, 24); |
|
| 2146 | + $s20 = self::mul($a9, $b11, 24) + self::mul($a10, $b10, 24) + self::mul($a11, $b9, 24); |
|
| 2147 | + $s21 = self::mul($a10, $b11, 24) + self::mul($a11, $b10, 24); |
|
| 2148 | + $s22 = self::mul($a11, $b11, 24); |
|
| 2149 | + $s23 = 0; |
|
| 2150 | + |
|
| 2151 | + $carry0 = ($s0 + (1 << 20)) >> 21; |
|
| 2152 | + $s1 += $carry0; |
|
| 2153 | + $s0 -= $carry0 << 21; |
|
| 2154 | + $carry2 = ($s2 + (1 << 20)) >> 21; |
|
| 2155 | + $s3 += $carry2; |
|
| 2156 | + $s2 -= $carry2 << 21; |
|
| 2157 | + $carry4 = ($s4 + (1 << 20)) >> 21; |
|
| 2158 | + $s5 += $carry4; |
|
| 2159 | + $s4 -= $carry4 << 21; |
|
| 2160 | + $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 2161 | + $s7 += $carry6; |
|
| 2162 | + $s6 -= $carry6 << 21; |
|
| 2163 | + $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 2164 | + $s9 += $carry8; |
|
| 2165 | + $s8 -= $carry8 << 21; |
|
| 2166 | + $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 2167 | + $s11 += $carry10; |
|
| 2168 | + $s10 -= $carry10 << 21; |
|
| 2169 | + $carry12 = ($s12 + (1 << 20)) >> 21; |
|
| 2170 | + $s13 += $carry12; |
|
| 2171 | + $s12 -= $carry12 << 21; |
|
| 2172 | + $carry14 = ($s14 + (1 << 20)) >> 21; |
|
| 2173 | + $s15 += $carry14; |
|
| 2174 | + $s14 -= $carry14 << 21; |
|
| 2175 | + $carry16 = ($s16 + (1 << 20)) >> 21; |
|
| 2176 | + $s17 += $carry16; |
|
| 2177 | + $s16 -= $carry16 << 21; |
|
| 2178 | + $carry18 = ($s18 + (1 << 20)) >> 21; |
|
| 2179 | + $s19 += $carry18; |
|
| 2180 | + $s18 -= $carry18 << 21; |
|
| 2181 | + $carry20 = ($s20 + (1 << 20)) >> 21; |
|
| 2182 | + $s21 += $carry20; |
|
| 2183 | + $s20 -= $carry20 << 21; |
|
| 2184 | + $carry22 = ($s22 + (1 << 20)) >> 21; |
|
| 2185 | + $s23 += $carry22; |
|
| 2186 | + $s22 -= $carry22 << 21; |
|
| 2187 | + |
|
| 2188 | + $carry1 = ($s1 + (1 << 20)) >> 21; |
|
| 2189 | + $s2 += $carry1; |
|
| 2190 | + $s1 -= $carry1 << 21; |
|
| 2191 | + $carry3 = ($s3 + (1 << 20)) >> 21; |
|
| 2192 | + $s4 += $carry3; |
|
| 2193 | + $s3 -= $carry3 << 21; |
|
| 2194 | + $carry5 = ($s5 + (1 << 20)) >> 21; |
|
| 2195 | + $s6 += $carry5; |
|
| 2196 | + $s5 -= $carry5 << 21; |
|
| 2197 | + $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 2198 | + $s8 += $carry7; |
|
| 2199 | + $s7 -= $carry7 << 21; |
|
| 2200 | + $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 2201 | + $s10 += $carry9; |
|
| 2202 | + $s9 -= $carry9 << 21; |
|
| 2203 | + $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 2204 | + $s12 += $carry11; |
|
| 2205 | + $s11 -= $carry11 << 21; |
|
| 2206 | + $carry13 = ($s13 + (1 << 20)) >> 21; |
|
| 2207 | + $s14 += $carry13; |
|
| 2208 | + $s13 -= $carry13 << 21; |
|
| 2209 | + $carry15 = ($s15 + (1 << 20)) >> 21; |
|
| 2210 | + $s16 += $carry15; |
|
| 2211 | + $s15 -= $carry15 << 21; |
|
| 2212 | + $carry17 = ($s17 + (1 << 20)) >> 21; |
|
| 2213 | + $s18 += $carry17; |
|
| 2214 | + $s17 -= $carry17 << 21; |
|
| 2215 | + $carry19 = ($s19 + (1 << 20)) >> 21; |
|
| 2216 | + $s20 += $carry19; |
|
| 2217 | + $s19 -= $carry19 << 21; |
|
| 2218 | + $carry21 = ($s21 + (1 << 20)) >> 21; |
|
| 2219 | + $s22 += $carry21; |
|
| 2220 | + $s21 -= $carry21 << 21; |
|
| 2221 | + |
|
| 2222 | + $s11 += self::mul($s23, 666643, 20); |
|
| 2223 | + $s12 += self::mul($s23, 470296, 19); |
|
| 2224 | + $s13 += self::mul($s23, 654183, 20); |
|
| 2225 | + $s14 -= self::mul($s23, 997805, 20); |
|
| 2226 | + $s15 += self::mul($s23, 136657, 18); |
|
| 2227 | + $s16 -= self::mul($s23, 683901, 20); |
|
| 2228 | + |
|
| 2229 | + $s10 += self::mul($s22, 666643, 20); |
|
| 2230 | + $s11 += self::mul($s22, 470296, 19); |
|
| 2231 | + $s12 += self::mul($s22, 654183, 20); |
|
| 2232 | + $s13 -= self::mul($s22, 997805, 20); |
|
| 2233 | + $s14 += self::mul($s22, 136657, 18); |
|
| 2234 | + $s15 -= self::mul($s22, 683901, 20); |
|
| 2235 | + |
|
| 2236 | + $s9 += self::mul($s21, 666643, 20); |
|
| 2237 | + $s10 += self::mul($s21, 470296, 19); |
|
| 2238 | + $s11 += self::mul($s21, 654183, 20); |
|
| 2239 | + $s12 -= self::mul($s21, 997805, 20); |
|
| 2240 | + $s13 += self::mul($s21, 136657, 18); |
|
| 2241 | + $s14 -= self::mul($s21, 683901, 20); |
|
| 2242 | + |
|
| 2243 | + $s8 += self::mul($s20, 666643, 20); |
|
| 2244 | + $s9 += self::mul($s20, 470296, 19); |
|
| 2245 | + $s10 += self::mul($s20, 654183, 20); |
|
| 2246 | + $s11 -= self::mul($s20, 997805, 20); |
|
| 2247 | + $s12 += self::mul($s20, 136657, 18); |
|
| 2248 | + $s13 -= self::mul($s20, 683901, 20); |
|
| 2249 | + |
|
| 2250 | + $s7 += self::mul($s19, 666643, 20); |
|
| 2251 | + $s8 += self::mul($s19, 470296, 19); |
|
| 2252 | + $s9 += self::mul($s19, 654183, 20); |
|
| 2253 | + $s10 -= self::mul($s19, 997805, 20); |
|
| 2254 | + $s11 += self::mul($s19, 136657, 18); |
|
| 2255 | + $s12 -= self::mul($s19, 683901, 20); |
|
| 2256 | + |
|
| 2257 | + $s6 += self::mul($s18, 666643, 20); |
|
| 2258 | + $s7 += self::mul($s18, 470296, 19); |
|
| 2259 | + $s8 += self::mul($s18, 654183, 20); |
|
| 2260 | + $s9 -= self::mul($s18, 997805, 20); |
|
| 2261 | + $s10 += self::mul($s18, 136657, 18); |
|
| 2262 | + $s11 -= self::mul($s18, 683901, 20); |
|
| 2263 | + |
|
| 2264 | + $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 2265 | + $s7 += $carry6; |
|
| 2266 | + $s6 -= $carry6 << 21; |
|
| 2267 | + $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 2268 | + $s9 += $carry8; |
|
| 2269 | + $s8 -= $carry8 << 21; |
|
| 2270 | + $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 2271 | + $s11 += $carry10; |
|
| 2272 | + $s10 -= $carry10 << 21; |
|
| 2273 | + $carry12 = ($s12 + (1 << 20)) >> 21; |
|
| 2274 | + $s13 += $carry12; |
|
| 2275 | + $s12 -= $carry12 << 21; |
|
| 2276 | + $carry14 = ($s14 + (1 << 20)) >> 21; |
|
| 2277 | + $s15 += $carry14; |
|
| 2278 | + $s14 -= $carry14 << 21; |
|
| 2279 | + $carry16 = ($s16 + (1 << 20)) >> 21; |
|
| 2280 | + $s17 += $carry16; |
|
| 2281 | + $s16 -= $carry16 << 21; |
|
| 2282 | + |
|
| 2283 | + $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 2284 | + $s8 += $carry7; |
|
| 2285 | + $s7 -= $carry7 << 21; |
|
| 2286 | + $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 2287 | + $s10 += $carry9; |
|
| 2288 | + $s9 -= $carry9 << 21; |
|
| 2289 | + $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 2290 | + $s12 += $carry11; |
|
| 2291 | + $s11 -= $carry11 << 21; |
|
| 2292 | + $carry13 = ($s13 + (1 << 20)) >> 21; |
|
| 2293 | + $s14 += $carry13; |
|
| 2294 | + $s13 -= $carry13 << 21; |
|
| 2295 | + $carry15 = ($s15 + (1 << 20)) >> 21; |
|
| 2296 | + $s16 += $carry15; |
|
| 2297 | + $s15 -= $carry15 << 21; |
|
| 2298 | + |
|
| 2299 | + $s5 += self::mul($s17, 666643, 20); |
|
| 2300 | + $s6 += self::mul($s17, 470296, 19); |
|
| 2301 | + $s7 += self::mul($s17, 654183, 20); |
|
| 2302 | + $s8 -= self::mul($s17, 997805, 20); |
|
| 2303 | + $s9 += self::mul($s17, 136657, 18); |
|
| 2304 | + $s10 -= self::mul($s17, 683901, 20); |
|
| 2305 | + |
|
| 2306 | + $s4 += self::mul($s16, 666643, 20); |
|
| 2307 | + $s5 += self::mul($s16, 470296, 19); |
|
| 2308 | + $s6 += self::mul($s16, 654183, 20); |
|
| 2309 | + $s7 -= self::mul($s16, 997805, 20); |
|
| 2310 | + $s8 += self::mul($s16, 136657, 18); |
|
| 2311 | + $s9 -= self::mul($s16, 683901, 20); |
|
| 2312 | + |
|
| 2313 | + $s3 += self::mul($s15, 666643, 20); |
|
| 2314 | + $s4 += self::mul($s15, 470296, 19); |
|
| 2315 | + $s5 += self::mul($s15, 654183, 20); |
|
| 2316 | + $s6 -= self::mul($s15, 997805, 20); |
|
| 2317 | + $s7 += self::mul($s15, 136657, 18); |
|
| 2318 | + $s8 -= self::mul($s15, 683901, 20); |
|
| 2319 | + |
|
| 2320 | + $s2 += self::mul($s14, 666643, 20); |
|
| 2321 | + $s3 += self::mul($s14, 470296, 19); |
|
| 2322 | + $s4 += self::mul($s14, 654183, 20); |
|
| 2323 | + $s5 -= self::mul($s14, 997805, 20); |
|
| 2324 | + $s6 += self::mul($s14, 136657, 18); |
|
| 2325 | + $s7 -= self::mul($s14, 683901, 20); |
|
| 2326 | + |
|
| 2327 | + $s1 += self::mul($s13, 666643, 20); |
|
| 2328 | + $s2 += self::mul($s13, 470296, 19); |
|
| 2329 | + $s3 += self::mul($s13, 654183, 20); |
|
| 2330 | + $s4 -= self::mul($s13, 997805, 20); |
|
| 2331 | + $s5 += self::mul($s13, 136657, 18); |
|
| 2332 | + $s6 -= self::mul($s13, 683901, 20); |
|
| 2333 | + |
|
| 2334 | + $s0 += self::mul($s12, 666643, 20); |
|
| 2335 | + $s1 += self::mul($s12, 470296, 19); |
|
| 2336 | + $s2 += self::mul($s12, 654183, 20); |
|
| 2337 | + $s3 -= self::mul($s12, 997805, 20); |
|
| 2338 | + $s4 += self::mul($s12, 136657, 18); |
|
| 2339 | + $s5 -= self::mul($s12, 683901, 20); |
|
| 2340 | + $s12 = 0; |
|
| 2341 | + |
|
| 2342 | + $carry0 = ($s0 + (1 << 20)) >> 21; |
|
| 2343 | + $s1 += $carry0; |
|
| 2344 | + $s0 -= $carry0 << 21; |
|
| 2345 | + $carry2 = ($s2 + (1 << 20)) >> 21; |
|
| 2346 | + $s3 += $carry2; |
|
| 2347 | + $s2 -= $carry2 << 21; |
|
| 2348 | + $carry4 = ($s4 + (1 << 20)) >> 21; |
|
| 2349 | + $s5 += $carry4; |
|
| 2350 | + $s4 -= $carry4 << 21; |
|
| 2351 | + $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 2352 | + $s7 += $carry6; |
|
| 2353 | + $s6 -= $carry6 << 21; |
|
| 2354 | + $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 2355 | + $s9 += $carry8; |
|
| 2356 | + $s8 -= $carry8 << 21; |
|
| 2357 | + $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 2358 | + $s11 += $carry10; |
|
| 2359 | + $s10 -= $carry10 << 21; |
|
| 2360 | + |
|
| 2361 | + $carry1 = ($s1 + (1 << 20)) >> 21; |
|
| 2362 | + $s2 += $carry1; |
|
| 2363 | + $s1 -= $carry1 << 21; |
|
| 2364 | + $carry3 = ($s3 + (1 << 20)) >> 21; |
|
| 2365 | + $s4 += $carry3; |
|
| 2366 | + $s3 -= $carry3 << 21; |
|
| 2367 | + $carry5 = ($s5 + (1 << 20)) >> 21; |
|
| 2368 | + $s6 += $carry5; |
|
| 2369 | + $s5 -= $carry5 << 21; |
|
| 2370 | + $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 2371 | + $s8 += $carry7; |
|
| 2372 | + $s7 -= $carry7 << 21; |
|
| 2373 | + $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 2374 | + $s10 += $carry9; |
|
| 2375 | + $s9 -= $carry9 << 21; |
|
| 2376 | + $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 2377 | + $s12 += $carry11; |
|
| 2378 | + $s11 -= $carry11 << 21; |
|
| 2379 | + |
|
| 2380 | + $s0 += self::mul($s12, 666643, 20); |
|
| 2381 | + $s1 += self::mul($s12, 470296, 19); |
|
| 2382 | + $s2 += self::mul($s12, 654183, 20); |
|
| 2383 | + $s3 -= self::mul($s12, 997805, 20); |
|
| 2384 | + $s4 += self::mul($s12, 136657, 18); |
|
| 2385 | + $s5 -= self::mul($s12, 683901, 20); |
|
| 2386 | + $s12 = 0; |
|
| 2387 | + |
|
| 2388 | + $carry0 = $s0 >> 21; |
|
| 2389 | + $s1 += $carry0; |
|
| 2390 | + $s0 -= $carry0 << 21; |
|
| 2391 | + $carry1 = $s1 >> 21; |
|
| 2392 | + $s2 += $carry1; |
|
| 2393 | + $s1 -= $carry1 << 21; |
|
| 2394 | + $carry2 = $s2 >> 21; |
|
| 2395 | + $s3 += $carry2; |
|
| 2396 | + $s2 -= $carry2 << 21; |
|
| 2397 | + $carry3 = $s3 >> 21; |
|
| 2398 | + $s4 += $carry3; |
|
| 2399 | + $s3 -= $carry3 << 21; |
|
| 2400 | + $carry4 = $s4 >> 21; |
|
| 2401 | + $s5 += $carry4; |
|
| 2402 | + $s4 -= $carry4 << 21; |
|
| 2403 | + $carry5 = $s5 >> 21; |
|
| 2404 | + $s6 += $carry5; |
|
| 2405 | + $s5 -= $carry5 << 21; |
|
| 2406 | + $carry6 = $s6 >> 21; |
|
| 2407 | + $s7 += $carry6; |
|
| 2408 | + $s6 -= $carry6 << 21; |
|
| 2409 | + $carry7 = $s7 >> 21; |
|
| 2410 | + $s8 += $carry7; |
|
| 2411 | + $s7 -= $carry7 << 21; |
|
| 2412 | + $carry8 = $s8 >> 21; |
|
| 2413 | + $s9 += $carry8; |
|
| 2414 | + $s8 -= $carry8 << 21; |
|
| 2415 | + $carry9 = $s9 >> 21; |
|
| 2416 | + $s10 += $carry9; |
|
| 2417 | + $s9 -= $carry9 << 21; |
|
| 2418 | + $carry10 = $s10 >> 21; |
|
| 2419 | + $s11 += $carry10; |
|
| 2420 | + $s10 -= $carry10 << 21; |
|
| 2421 | + $carry11 = $s11 >> 21; |
|
| 2422 | + $s12 += $carry11; |
|
| 2423 | + $s11 -= $carry11 << 21; |
|
| 2424 | + |
|
| 2425 | + $s0 += self::mul($s12, 666643, 20); |
|
| 2426 | + $s1 += self::mul($s12, 470296, 19); |
|
| 2427 | + $s2 += self::mul($s12, 654183, 20); |
|
| 2428 | + $s3 -= self::mul($s12, 997805, 20); |
|
| 2429 | + $s4 += self::mul($s12, 136657, 18); |
|
| 2430 | + $s5 -= self::mul($s12, 683901, 20); |
|
| 2431 | + |
|
| 2432 | + $carry0 = $s0 >> 21; |
|
| 2433 | + $s1 += $carry0; |
|
| 2434 | + $s0 -= $carry0 << 21; |
|
| 2435 | + $carry1 = $s1 >> 21; |
|
| 2436 | + $s2 += $carry1; |
|
| 2437 | + $s1 -= $carry1 << 21; |
|
| 2438 | + $carry2 = $s2 >> 21; |
|
| 2439 | + $s3 += $carry2; |
|
| 2440 | + $s2 -= $carry2 << 21; |
|
| 2441 | + $carry3 = $s3 >> 21; |
|
| 2442 | + $s4 += $carry3; |
|
| 2443 | + $s3 -= $carry3 << 21; |
|
| 2444 | + $carry4 = $s4 >> 21; |
|
| 2445 | + $s5 += $carry4; |
|
| 2446 | + $s4 -= $carry4 << 21; |
|
| 2447 | + $carry5 = $s5 >> 21; |
|
| 2448 | + $s6 += $carry5; |
|
| 2449 | + $s5 -= $carry5 << 21; |
|
| 2450 | + $carry6 = $s6 >> 21; |
|
| 2451 | + $s7 += $carry6; |
|
| 2452 | + $s6 -= $carry6 << 21; |
|
| 2453 | + $carry7 = $s7 >> 21; |
|
| 2454 | + $s8 += $carry7; |
|
| 2455 | + $s7 -= $carry7 << 21; |
|
| 2456 | + $carry8 = $s8 >> 21; |
|
| 2457 | + $s9 += $carry8; |
|
| 2458 | + $s8 -= $carry8 << 21; |
|
| 2459 | + $carry9 = $s9 >> 21; |
|
| 2460 | + $s10 += $carry9; |
|
| 2461 | + $s9 -= $carry9 << 21; |
|
| 2462 | + $carry10 = $s10 >> 21; |
|
| 2463 | + $s11 += $carry10; |
|
| 2464 | + $s10 -= $carry10 << 21; |
|
| 2465 | + |
|
| 2466 | + /** |
|
| 2467 | + * @var array<int, int> |
|
| 2468 | + */ |
|
| 2469 | + $arr = array( |
|
| 2470 | + (int) (0xff & ($s0 >> 0)), |
|
| 2471 | + (int) (0xff & ($s0 >> 8)), |
|
| 2472 | + (int) (0xff & (($s0 >> 16) | $s1 << 5)), |
|
| 2473 | + (int) (0xff & ($s1 >> 3)), |
|
| 2474 | + (int) (0xff & ($s1 >> 11)), |
|
| 2475 | + (int) (0xff & (($s1 >> 19) | $s2 << 2)), |
|
| 2476 | + (int) (0xff & ($s2 >> 6)), |
|
| 2477 | + (int) (0xff & (($s2 >> 14) | $s3 << 7)), |
|
| 2478 | + (int) (0xff & ($s3 >> 1)), |
|
| 2479 | + (int) (0xff & ($s3 >> 9)), |
|
| 2480 | + (int) (0xff & (($s3 >> 17) | $s4 << 4)), |
|
| 2481 | + (int) (0xff & ($s4 >> 4)), |
|
| 2482 | + (int) (0xff & ($s4 >> 12)), |
|
| 2483 | + (int) (0xff & (($s4 >> 20) | $s5 << 1)), |
|
| 2484 | + (int) (0xff & ($s5 >> 7)), |
|
| 2485 | + (int) (0xff & (($s5 >> 15) | $s6 << 6)), |
|
| 2486 | + (int) (0xff & ($s6 >> 2)), |
|
| 2487 | + (int) (0xff & ($s6 >> 10)), |
|
| 2488 | + (int) (0xff & (($s6 >> 18) | $s7 << 3)), |
|
| 2489 | + (int) (0xff & ($s7 >> 5)), |
|
| 2490 | + (int) (0xff & ($s7 >> 13)), |
|
| 2491 | + (int) (0xff & ($s8 >> 0)), |
|
| 2492 | + (int) (0xff & ($s8 >> 8)), |
|
| 2493 | + (int) (0xff & (($s8 >> 16) | $s9 << 5)), |
|
| 2494 | + (int) (0xff & ($s9 >> 3)), |
|
| 2495 | + (int) (0xff & ($s9 >> 11)), |
|
| 2496 | + (int) (0xff & (($s9 >> 19) | $s10 << 2)), |
|
| 2497 | + (int) (0xff & ($s10 >> 6)), |
|
| 2498 | + (int) (0xff & (($s10 >> 14) | $s11 << 7)), |
|
| 2499 | + (int) (0xff & ($s11 >> 1)), |
|
| 2500 | + (int) (0xff & ($s11 >> 9)), |
|
| 2501 | + 0xff & ($s11 >> 17) |
|
| 2502 | + ); |
|
| 2503 | + return self::intArrayToString($arr); |
|
| 2504 | + } |
|
| 2505 | + |
|
| 2506 | + /** |
|
| 2507 | + * @internal You should not use this directly from another application |
|
| 2508 | + * |
|
| 2509 | + * @param string $s |
|
| 2510 | + * @return string |
|
| 2511 | + * @throws TypeError |
|
| 2512 | + */ |
|
| 2513 | + public static function sc_reduce($s) |
|
| 2514 | + { |
|
| 2515 | + $s0 = 2097151 & self::load_3(self::substr($s, 0, 3)); |
|
| 2516 | + $s1 = 2097151 & (self::load_4(self::substr($s, 2, 4)) >> 5); |
|
| 2517 | + $s2 = 2097151 & (self::load_3(self::substr($s, 5, 3)) >> 2); |
|
| 2518 | + $s3 = 2097151 & (self::load_4(self::substr($s, 7, 4)) >> 7); |
|
| 2519 | + $s4 = 2097151 & (self::load_4(self::substr($s, 10, 4)) >> 4); |
|
| 2520 | + $s5 = 2097151 & (self::load_3(self::substr($s, 13, 3)) >> 1); |
|
| 2521 | + $s6 = 2097151 & (self::load_4(self::substr($s, 15, 4)) >> 6); |
|
| 2522 | + $s7 = 2097151 & (self::load_3(self::substr($s, 18, 4)) >> 3); |
|
| 2523 | + $s8 = 2097151 & self::load_3(self::substr($s, 21, 3)); |
|
| 2524 | + $s9 = 2097151 & (self::load_4(self::substr($s, 23, 4)) >> 5); |
|
| 2525 | + $s10 = 2097151 & (self::load_3(self::substr($s, 26, 3)) >> 2); |
|
| 2526 | + $s11 = 2097151 & (self::load_4(self::substr($s, 28, 4)) >> 7); |
|
| 2527 | + $s12 = 2097151 & (self::load_4(self::substr($s, 31, 4)) >> 4); |
|
| 2528 | + $s13 = 2097151 & (self::load_3(self::substr($s, 34, 3)) >> 1); |
|
| 2529 | + $s14 = 2097151 & (self::load_4(self::substr($s, 36, 4)) >> 6); |
|
| 2530 | + $s15 = 2097151 & (self::load_3(self::substr($s, 39, 4)) >> 3); |
|
| 2531 | + $s16 = 2097151 & self::load_3(self::substr($s, 42, 3)); |
|
| 2532 | + $s17 = 2097151 & (self::load_4(self::substr($s, 44, 4)) >> 5); |
|
| 2533 | + $s18 = 2097151 & (self::load_3(self::substr($s, 47, 3)) >> 2); |
|
| 2534 | + $s19 = 2097151 & (self::load_4(self::substr($s, 49, 4)) >> 7); |
|
| 2535 | + $s20 = 2097151 & (self::load_4(self::substr($s, 52, 4)) >> 4); |
|
| 2536 | + $s21 = 2097151 & (self::load_3(self::substr($s, 55, 3)) >> 1); |
|
| 2537 | + $s22 = 2097151 & (self::load_4(self::substr($s, 57, 4)) >> 6); |
|
| 2538 | + $s23 = (self::load_4(self::substr($s, 60, 4)) >> 3); |
|
| 2539 | + |
|
| 2540 | + $s11 += self::mul($s23, 666643, 20); |
|
| 2541 | + $s12 += self::mul($s23, 470296, 19); |
|
| 2542 | + $s13 += self::mul($s23, 654183, 20); |
|
| 2543 | + $s14 -= self::mul($s23, 997805, 20); |
|
| 2544 | + $s15 += self::mul($s23, 136657, 18); |
|
| 2545 | + $s16 -= self::mul($s23, 683901, 20); |
|
| 2546 | + |
|
| 2547 | + $s10 += self::mul($s22, 666643, 20); |
|
| 2548 | + $s11 += self::mul($s22, 470296, 19); |
|
| 2549 | + $s12 += self::mul($s22, 654183, 20); |
|
| 2550 | + $s13 -= self::mul($s22, 997805, 20); |
|
| 2551 | + $s14 += self::mul($s22, 136657, 18); |
|
| 2552 | + $s15 -= self::mul($s22, 683901, 20); |
|
| 2553 | + |
|
| 2554 | + $s9 += self::mul($s21, 666643, 20); |
|
| 2555 | + $s10 += self::mul($s21, 470296, 19); |
|
| 2556 | + $s11 += self::mul($s21, 654183, 20); |
|
| 2557 | + $s12 -= self::mul($s21, 997805, 20); |
|
| 2558 | + $s13 += self::mul($s21, 136657, 18); |
|
| 2559 | + $s14 -= self::mul($s21, 683901, 20); |
|
| 2560 | + |
|
| 2561 | + $s8 += self::mul($s20, 666643, 20); |
|
| 2562 | + $s9 += self::mul($s20, 470296, 19); |
|
| 2563 | + $s10 += self::mul($s20, 654183, 20); |
|
| 2564 | + $s11 -= self::mul($s20, 997805, 20); |
|
| 2565 | + $s12 += self::mul($s20, 136657, 18); |
|
| 2566 | + $s13 -= self::mul($s20, 683901, 20); |
|
| 2567 | + |
|
| 2568 | + $s7 += self::mul($s19, 666643, 20); |
|
| 2569 | + $s8 += self::mul($s19, 470296, 19); |
|
| 2570 | + $s9 += self::mul($s19, 654183, 20); |
|
| 2571 | + $s10 -= self::mul($s19, 997805, 20); |
|
| 2572 | + $s11 += self::mul($s19, 136657, 18); |
|
| 2573 | + $s12 -= self::mul($s19, 683901, 20); |
|
| 2574 | + |
|
| 2575 | + $s6 += self::mul($s18, 666643, 20); |
|
| 2576 | + $s7 += self::mul($s18, 470296, 19); |
|
| 2577 | + $s8 += self::mul($s18, 654183, 20); |
|
| 2578 | + $s9 -= self::mul($s18, 997805, 20); |
|
| 2579 | + $s10 += self::mul($s18, 136657, 18); |
|
| 2580 | + $s11 -= self::mul($s18, 683901, 20); |
|
| 2581 | + |
|
| 2582 | + $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 2583 | + $s7 += $carry6; |
|
| 2584 | + $s6 -= $carry6 << 21; |
|
| 2585 | + $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 2586 | + $s9 += $carry8; |
|
| 2587 | + $s8 -= $carry8 << 21; |
|
| 2588 | + $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 2589 | + $s11 += $carry10; |
|
| 2590 | + $s10 -= $carry10 << 21; |
|
| 2591 | + $carry12 = ($s12 + (1 << 20)) >> 21; |
|
| 2592 | + $s13 += $carry12; |
|
| 2593 | + $s12 -= $carry12 << 21; |
|
| 2594 | + $carry14 = ($s14 + (1 << 20)) >> 21; |
|
| 2595 | + $s15 += $carry14; |
|
| 2596 | + $s14 -= $carry14 << 21; |
|
| 2597 | + $carry16 = ($s16 + (1 << 20)) >> 21; |
|
| 2598 | + $s17 += $carry16; |
|
| 2599 | + $s16 -= $carry16 << 21; |
|
| 2600 | + |
|
| 2601 | + $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 2602 | + $s8 += $carry7; |
|
| 2603 | + $s7 -= $carry7 << 21; |
|
| 2604 | + $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 2605 | + $s10 += $carry9; |
|
| 2606 | + $s9 -= $carry9 << 21; |
|
| 2607 | + $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 2608 | + $s12 += $carry11; |
|
| 2609 | + $s11 -= $carry11 << 21; |
|
| 2610 | + $carry13 = ($s13 + (1 << 20)) >> 21; |
|
| 2611 | + $s14 += $carry13; |
|
| 2612 | + $s13 -= $carry13 << 21; |
|
| 2613 | + $carry15 = ($s15 + (1 << 20)) >> 21; |
|
| 2614 | + $s16 += $carry15; |
|
| 2615 | + $s15 -= $carry15 << 21; |
|
| 2616 | + |
|
| 2617 | + $s5 += self::mul($s17, 666643, 20); |
|
| 2618 | + $s6 += self::mul($s17, 470296, 19); |
|
| 2619 | + $s7 += self::mul($s17, 654183, 20); |
|
| 2620 | + $s8 -= self::mul($s17, 997805, 20); |
|
| 2621 | + $s9 += self::mul($s17, 136657, 18); |
|
| 2622 | + $s10 -= self::mul($s17, 683901, 20); |
|
| 2623 | + |
|
| 2624 | + $s4 += self::mul($s16, 666643, 20); |
|
| 2625 | + $s5 += self::mul($s16, 470296, 19); |
|
| 2626 | + $s6 += self::mul($s16, 654183, 20); |
|
| 2627 | + $s7 -= self::mul($s16, 997805, 20); |
|
| 2628 | + $s8 += self::mul($s16, 136657, 18); |
|
| 2629 | + $s9 -= self::mul($s16, 683901, 20); |
|
| 2630 | + |
|
| 2631 | + $s3 += self::mul($s15, 666643, 20); |
|
| 2632 | + $s4 += self::mul($s15, 470296, 19); |
|
| 2633 | + $s5 += self::mul($s15, 654183, 20); |
|
| 2634 | + $s6 -= self::mul($s15, 997805, 20); |
|
| 2635 | + $s7 += self::mul($s15, 136657, 18); |
|
| 2636 | + $s8 -= self::mul($s15, 683901, 20); |
|
| 2637 | + |
|
| 2638 | + $s2 += self::mul($s14, 666643, 20); |
|
| 2639 | + $s3 += self::mul($s14, 470296, 19); |
|
| 2640 | + $s4 += self::mul($s14, 654183, 20); |
|
| 2641 | + $s5 -= self::mul($s14, 997805, 20); |
|
| 2642 | + $s6 += self::mul($s14, 136657, 18); |
|
| 2643 | + $s7 -= self::mul($s14, 683901, 20); |
|
| 2644 | + |
|
| 2645 | + $s1 += self::mul($s13, 666643, 20); |
|
| 2646 | + $s2 += self::mul($s13, 470296, 19); |
|
| 2647 | + $s3 += self::mul($s13, 654183, 20); |
|
| 2648 | + $s4 -= self::mul($s13, 997805, 20); |
|
| 2649 | + $s5 += self::mul($s13, 136657, 18); |
|
| 2650 | + $s6 -= self::mul($s13, 683901, 20); |
|
| 2651 | + |
|
| 2652 | + $s0 += self::mul($s12, 666643, 20); |
|
| 2653 | + $s1 += self::mul($s12, 470296, 19); |
|
| 2654 | + $s2 += self::mul($s12, 654183, 20); |
|
| 2655 | + $s3 -= self::mul($s12, 997805, 20); |
|
| 2656 | + $s4 += self::mul($s12, 136657, 18); |
|
| 2657 | + $s5 -= self::mul($s12, 683901, 20); |
|
| 2658 | + $s12 = 0; |
|
| 2659 | + |
|
| 2660 | + $carry0 = ($s0 + (1 << 20)) >> 21; |
|
| 2661 | + $s1 += $carry0; |
|
| 2662 | + $s0 -= $carry0 << 21; |
|
| 2663 | + $carry2 = ($s2 + (1 << 20)) >> 21; |
|
| 2664 | + $s3 += $carry2; |
|
| 2665 | + $s2 -= $carry2 << 21; |
|
| 2666 | + $carry4 = ($s4 + (1 << 20)) >> 21; |
|
| 2667 | + $s5 += $carry4; |
|
| 2668 | + $s4 -= $carry4 << 21; |
|
| 2669 | + $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 2670 | + $s7 += $carry6; |
|
| 2671 | + $s6 -= $carry6 << 21; |
|
| 2672 | + $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 2673 | + $s9 += $carry8; |
|
| 2674 | + $s8 -= $carry8 << 21; |
|
| 2675 | + $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 2676 | + $s11 += $carry10; |
|
| 2677 | + $s10 -= $carry10 << 21; |
|
| 2678 | + |
|
| 2679 | + $carry1 = ($s1 + (1 << 20)) >> 21; |
|
| 2680 | + $s2 += $carry1; |
|
| 2681 | + $s1 -= $carry1 << 21; |
|
| 2682 | + $carry3 = ($s3 + (1 << 20)) >> 21; |
|
| 2683 | + $s4 += $carry3; |
|
| 2684 | + $s3 -= $carry3 << 21; |
|
| 2685 | + $carry5 = ($s5 + (1 << 20)) >> 21; |
|
| 2686 | + $s6 += $carry5; |
|
| 2687 | + $s5 -= $carry5 << 21; |
|
| 2688 | + $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 2689 | + $s8 += $carry7; |
|
| 2690 | + $s7 -= $carry7 << 21; |
|
| 2691 | + $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 2692 | + $s10 += $carry9; |
|
| 2693 | + $s9 -= $carry9 << 21; |
|
| 2694 | + $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 2695 | + $s12 += $carry11; |
|
| 2696 | + $s11 -= $carry11 << 21; |
|
| 2697 | + |
|
| 2698 | + $s0 += self::mul($s12, 666643, 20); |
|
| 2699 | + $s1 += self::mul($s12, 470296, 19); |
|
| 2700 | + $s2 += self::mul($s12, 654183, 20); |
|
| 2701 | + $s3 -= self::mul($s12, 997805, 20); |
|
| 2702 | + $s4 += self::mul($s12, 136657, 18); |
|
| 2703 | + $s5 -= self::mul($s12, 683901, 20); |
|
| 2704 | + $s12 = 0; |
|
| 2705 | + |
|
| 2706 | + $carry0 = $s0 >> 21; |
|
| 2707 | + $s1 += $carry0; |
|
| 2708 | + $s0 -= $carry0 << 21; |
|
| 2709 | + $carry1 = $s1 >> 21; |
|
| 2710 | + $s2 += $carry1; |
|
| 2711 | + $s1 -= $carry1 << 21; |
|
| 2712 | + $carry2 = $s2 >> 21; |
|
| 2713 | + $s3 += $carry2; |
|
| 2714 | + $s2 -= $carry2 << 21; |
|
| 2715 | + $carry3 = $s3 >> 21; |
|
| 2716 | + $s4 += $carry3; |
|
| 2717 | + $s3 -= $carry3 << 21; |
|
| 2718 | + $carry4 = $s4 >> 21; |
|
| 2719 | + $s5 += $carry4; |
|
| 2720 | + $s4 -= $carry4 << 21; |
|
| 2721 | + $carry5 = $s5 >> 21; |
|
| 2722 | + $s6 += $carry5; |
|
| 2723 | + $s5 -= $carry5 << 21; |
|
| 2724 | + $carry6 = $s6 >> 21; |
|
| 2725 | + $s7 += $carry6; |
|
| 2726 | + $s6 -= $carry6 << 21; |
|
| 2727 | + $carry7 = $s7 >> 21; |
|
| 2728 | + $s8 += $carry7; |
|
| 2729 | + $s7 -= $carry7 << 21; |
|
| 2730 | + $carry8 = $s8 >> 21; |
|
| 2731 | + $s9 += $carry8; |
|
| 2732 | + $s8 -= $carry8 << 21; |
|
| 2733 | + $carry9 = $s9 >> 21; |
|
| 2734 | + $s10 += $carry9; |
|
| 2735 | + $s9 -= $carry9 << 21; |
|
| 2736 | + $carry10 = $s10 >> 21; |
|
| 2737 | + $s11 += $carry10; |
|
| 2738 | + $s10 -= $carry10 << 21; |
|
| 2739 | + $carry11 = $s11 >> 21; |
|
| 2740 | + $s12 += $carry11; |
|
| 2741 | + $s11 -= $carry11 << 21; |
|
| 2742 | + |
|
| 2743 | + $s0 += self::mul($s12, 666643, 20); |
|
| 2744 | + $s1 += self::mul($s12, 470296, 19); |
|
| 2745 | + $s2 += self::mul($s12, 654183, 20); |
|
| 2746 | + $s3 -= self::mul($s12, 997805, 20); |
|
| 2747 | + $s4 += self::mul($s12, 136657, 18); |
|
| 2748 | + $s5 -= self::mul($s12, 683901, 20); |
|
| 2749 | + |
|
| 2750 | + $carry0 = $s0 >> 21; |
|
| 2751 | + $s1 += $carry0; |
|
| 2752 | + $s0 -= $carry0 << 21; |
|
| 2753 | + $carry1 = $s1 >> 21; |
|
| 2754 | + $s2 += $carry1; |
|
| 2755 | + $s1 -= $carry1 << 21; |
|
| 2756 | + $carry2 = $s2 >> 21; |
|
| 2757 | + $s3 += $carry2; |
|
| 2758 | + $s2 -= $carry2 << 21; |
|
| 2759 | + $carry3 = $s3 >> 21; |
|
| 2760 | + $s4 += $carry3; |
|
| 2761 | + $s3 -= $carry3 << 21; |
|
| 2762 | + $carry4 = $s4 >> 21; |
|
| 2763 | + $s5 += $carry4; |
|
| 2764 | + $s4 -= $carry4 << 21; |
|
| 2765 | + $carry5 = $s5 >> 21; |
|
| 2766 | + $s6 += $carry5; |
|
| 2767 | + $s5 -= $carry5 << 21; |
|
| 2768 | + $carry6 = $s6 >> 21; |
|
| 2769 | + $s7 += $carry6; |
|
| 2770 | + $s6 -= $carry6 << 21; |
|
| 2771 | + $carry7 = $s7 >> 21; |
|
| 2772 | + $s8 += $carry7; |
|
| 2773 | + $s7 -= $carry7 << 21; |
|
| 2774 | + $carry8 = $s8 >> 21; |
|
| 2775 | + $s9 += $carry8; |
|
| 2776 | + $s8 -= $carry8 << 21; |
|
| 2777 | + $carry9 = $s9 >> 21; |
|
| 2778 | + $s10 += $carry9; |
|
| 2779 | + $s9 -= $carry9 << 21; |
|
| 2780 | + $carry10 = $s10 >> 21; |
|
| 2781 | + $s11 += $carry10; |
|
| 2782 | + $s10 -= $carry10 << 21; |
|
| 2783 | + |
|
| 2784 | + /** |
|
| 2785 | + * @var array<int, int> |
|
| 2786 | + */ |
|
| 2787 | + $arr = array( |
|
| 2788 | + (int) ($s0 >> 0), |
|
| 2789 | + (int) ($s0 >> 8), |
|
| 2790 | + (int) (($s0 >> 16) | $s1 << 5), |
|
| 2791 | + (int) ($s1 >> 3), |
|
| 2792 | + (int) ($s1 >> 11), |
|
| 2793 | + (int) (($s1 >> 19) | $s2 << 2), |
|
| 2794 | + (int) ($s2 >> 6), |
|
| 2795 | + (int) (($s2 >> 14) | $s3 << 7), |
|
| 2796 | + (int) ($s3 >> 1), |
|
| 2797 | + (int) ($s3 >> 9), |
|
| 2798 | + (int) (($s3 >> 17) | $s4 << 4), |
|
| 2799 | + (int) ($s4 >> 4), |
|
| 2800 | + (int) ($s4 >> 12), |
|
| 2801 | + (int) (($s4 >> 20) | $s5 << 1), |
|
| 2802 | + (int) ($s5 >> 7), |
|
| 2803 | + (int) (($s5 >> 15) | $s6 << 6), |
|
| 2804 | + (int) ($s6 >> 2), |
|
| 2805 | + (int) ($s6 >> 10), |
|
| 2806 | + (int) (($s6 >> 18) | $s7 << 3), |
|
| 2807 | + (int) ($s7 >> 5), |
|
| 2808 | + (int) ($s7 >> 13), |
|
| 2809 | + (int) ($s8 >> 0), |
|
| 2810 | + (int) ($s8 >> 8), |
|
| 2811 | + (int) (($s8 >> 16) | $s9 << 5), |
|
| 2812 | + (int) ($s9 >> 3), |
|
| 2813 | + (int) ($s9 >> 11), |
|
| 2814 | + (int) (($s9 >> 19) | $s10 << 2), |
|
| 2815 | + (int) ($s10 >> 6), |
|
| 2816 | + (int) (($s10 >> 14) | $s11 << 7), |
|
| 2817 | + (int) ($s11 >> 1), |
|
| 2818 | + (int) ($s11 >> 9), |
|
| 2819 | + (int) $s11 >> 17 |
|
| 2820 | + ); |
|
| 2821 | + return self::intArrayToString($arr); |
|
| 2822 | + } |
|
| 2823 | + |
|
| 2824 | + /** |
|
| 2825 | + * multiply by the order of the main subgroup l = 2^252+27742317777372353535851937790883648493 |
|
| 2826 | + * |
|
| 2827 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A |
|
| 2828 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 2829 | + */ |
|
| 2830 | + public static function ge_mul_l(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A) |
|
| 2831 | + { |
|
| 2832 | + $aslide = array( |
|
| 2833 | + 13, 0, 0, 0, 0, -1, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, |
|
| 2834 | + 0, 0, 0, -3, 0, 0, 0, 0, -13, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 3, 0, |
|
| 2835 | + 0, 0, 0, -13, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, |
|
| 2836 | + 0, 0, 11, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, -1, |
|
| 2837 | + 0, 0, 0, 0, 3, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, |
|
| 2838 | + 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 5, 0, 0, 0, 0, |
|
| 2839 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 2840 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 2841 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 2842 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 2843 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 2844 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 |
|
| 2845 | + ); |
|
| 2846 | + |
|
| 2847 | + /** @var array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Cached> $Ai size 8 */ |
|
| 2848 | + $Ai = array(); |
|
| 2849 | + |
|
| 2850 | + # ge_p3_to_cached(&Ai[0], A); |
|
| 2851 | + $Ai[0] = self::ge_p3_to_cached($A); |
|
| 2852 | + # ge_p3_dbl(&t, A); |
|
| 2853 | + $t = self::ge_p3_dbl($A); |
|
| 2854 | + # ge_p1p1_to_p3(&A2, &t); |
|
| 2855 | + $A2 = self::ge_p1p1_to_p3($t); |
|
| 2856 | + |
|
| 2857 | + for ($i = 1; $i < 8; ++$i) { |
|
| 2858 | + # ge_add(&t, &A2, &Ai[0]); |
|
| 2859 | + $t = self::ge_add($A2, $Ai[$i - 1]); |
|
| 2860 | + # ge_p1p1_to_p3(&u, &t); |
|
| 2861 | + $u = self::ge_p1p1_to_p3($t); |
|
| 2862 | + # ge_p3_to_cached(&Ai[i], &u); |
|
| 2863 | + $Ai[$i] = self::ge_p3_to_cached($u); |
|
| 2864 | + } |
|
| 2865 | + |
|
| 2866 | + $r = self::ge_p3_0(); |
|
| 2867 | + for ($i = 252; $i >= 0; --$i) { |
|
| 2868 | + $t = self::ge_p3_dbl($r); |
|
| 2869 | + if ($aslide[$i] > 0) { |
|
| 2870 | + # ge_p1p1_to_p3(&u, &t); |
|
| 2871 | + $u = self::ge_p1p1_to_p3($t); |
|
| 2872 | + # ge_add(&t, &u, &Ai[aslide[i] / 2]); |
|
| 2873 | + $t = self::ge_add($u, $Ai[(int)($aslide[$i] / 2)]); |
|
| 2874 | + } elseif ($aslide[$i] < 0) { |
|
| 2875 | + # ge_p1p1_to_p3(&u, &t); |
|
| 2876 | + $u = self::ge_p1p1_to_p3($t); |
|
| 2877 | + # ge_sub(&t, &u, &Ai[(-aslide[i]) / 2]); |
|
| 2878 | + $t = self::ge_sub($u, $Ai[(int)(-$aslide[$i] / 2)]); |
|
| 2879 | + } |
|
| 2880 | + } |
|
| 2881 | + |
|
| 2882 | + # ge_p1p1_to_p3(r, &t); |
|
| 2883 | + return self::ge_p1p1_to_p3($t); |
|
| 2884 | + } |
|
| 2885 | + |
|
| 2886 | + /** |
|
| 2887 | + * @param string $a |
|
| 2888 | + * @param string $b |
|
| 2889 | + * @return string |
|
| 2890 | + */ |
|
| 2891 | + public static function sc25519_mul($a, $b) |
|
| 2892 | + { |
|
| 2893 | + // int64_t a0 = 2097151 & load_3(a); |
|
| 2894 | + // int64_t a1 = 2097151 & (load_4(a + 2) >> 5); |
|
| 2895 | + // int64_t a2 = 2097151 & (load_3(a + 5) >> 2); |
|
| 2896 | + // int64_t a3 = 2097151 & (load_4(a + 7) >> 7); |
|
| 2897 | + // int64_t a4 = 2097151 & (load_4(a + 10) >> 4); |
|
| 2898 | + // int64_t a5 = 2097151 & (load_3(a + 13) >> 1); |
|
| 2899 | + // int64_t a6 = 2097151 & (load_4(a + 15) >> 6); |
|
| 2900 | + // int64_t a7 = 2097151 & (load_3(a + 18) >> 3); |
|
| 2901 | + // int64_t a8 = 2097151 & load_3(a + 21); |
|
| 2902 | + // int64_t a9 = 2097151 & (load_4(a + 23) >> 5); |
|
| 2903 | + // int64_t a10 = 2097151 & (load_3(a + 26) >> 2); |
|
| 2904 | + // int64_t a11 = (load_4(a + 28) >> 7); |
|
| 2905 | + $a0 = 2097151 & self::load_3(self::substr($a, 0, 3)); |
|
| 2906 | + $a1 = 2097151 & (self::load_4(self::substr($a, 2, 4)) >> 5); |
|
| 2907 | + $a2 = 2097151 & (self::load_3(self::substr($a, 5, 3)) >> 2); |
|
| 2908 | + $a3 = 2097151 & (self::load_4(self::substr($a, 7, 4)) >> 7); |
|
| 2909 | + $a4 = 2097151 & (self::load_4(self::substr($a, 10, 4)) >> 4); |
|
| 2910 | + $a5 = 2097151 & (self::load_3(self::substr($a, 13, 3)) >> 1); |
|
| 2911 | + $a6 = 2097151 & (self::load_4(self::substr($a, 15, 4)) >> 6); |
|
| 2912 | + $a7 = 2097151 & (self::load_3(self::substr($a, 18, 3)) >> 3); |
|
| 2913 | + $a8 = 2097151 & self::load_3(self::substr($a, 21, 3)); |
|
| 2914 | + $a9 = 2097151 & (self::load_4(self::substr($a, 23, 4)) >> 5); |
|
| 2915 | + $a10 = 2097151 & (self::load_3(self::substr($a, 26, 3)) >> 2); |
|
| 2916 | + $a11 = (self::load_4(self::substr($a, 28, 4)) >> 7); |
|
| 2917 | + |
|
| 2918 | + // int64_t b0 = 2097151 & load_3(b); |
|
| 2919 | + // int64_t b1 = 2097151 & (load_4(b + 2) >> 5); |
|
| 2920 | + // int64_t b2 = 2097151 & (load_3(b + 5) >> 2); |
|
| 2921 | + // int64_t b3 = 2097151 & (load_4(b + 7) >> 7); |
|
| 2922 | + // int64_t b4 = 2097151 & (load_4(b + 10) >> 4); |
|
| 2923 | + // int64_t b5 = 2097151 & (load_3(b + 13) >> 1); |
|
| 2924 | + // int64_t b6 = 2097151 & (load_4(b + 15) >> 6); |
|
| 2925 | + // int64_t b7 = 2097151 & (load_3(b + 18) >> 3); |
|
| 2926 | + // int64_t b8 = 2097151 & load_3(b + 21); |
|
| 2927 | + // int64_t b9 = 2097151 & (load_4(b + 23) >> 5); |
|
| 2928 | + // int64_t b10 = 2097151 & (load_3(b + 26) >> 2); |
|
| 2929 | + // int64_t b11 = (load_4(b + 28) >> 7); |
|
| 2930 | + $b0 = 2097151 & self::load_3(self::substr($b, 0, 3)); |
|
| 2931 | + $b1 = 2097151 & (self::load_4(self::substr($b, 2, 4)) >> 5); |
|
| 2932 | + $b2 = 2097151 & (self::load_3(self::substr($b, 5, 3)) >> 2); |
|
| 2933 | + $b3 = 2097151 & (self::load_4(self::substr($b, 7, 4)) >> 7); |
|
| 2934 | + $b4 = 2097151 & (self::load_4(self::substr($b, 10, 4)) >> 4); |
|
| 2935 | + $b5 = 2097151 & (self::load_3(self::substr($b, 13, 3)) >> 1); |
|
| 2936 | + $b6 = 2097151 & (self::load_4(self::substr($b, 15, 4)) >> 6); |
|
| 2937 | + $b7 = 2097151 & (self::load_3(self::substr($b, 18, 3)) >> 3); |
|
| 2938 | + $b8 = 2097151 & self::load_3(self::substr($b, 21, 3)); |
|
| 2939 | + $b9 = 2097151 & (self::load_4(self::substr($b, 23, 4)) >> 5); |
|
| 2940 | + $b10 = 2097151 & (self::load_3(self::substr($b, 26, 3)) >> 2); |
|
| 2941 | + $b11 = (self::load_4(self::substr($b, 28, 4)) >> 7); |
|
| 2942 | + |
|
| 2943 | + // s0 = a0 * b0; |
|
| 2944 | + // s1 = a0 * b1 + a1 * b0; |
|
| 2945 | + // s2 = a0 * b2 + a1 * b1 + a2 * b0; |
|
| 2946 | + // s3 = a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0; |
|
| 2947 | + // s4 = a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0; |
|
| 2948 | + // s5 = a0 * b5 + a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0; |
|
| 2949 | + // s6 = a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0; |
|
| 2950 | + // s7 = a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + |
|
| 2951 | + // a6 * b1 + a7 * b0; |
|
| 2952 | + // s8 = a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + |
|
| 2953 | + // a6 * b2 + a7 * b1 + a8 * b0; |
|
| 2954 | + // s9 = a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + |
|
| 2955 | + // a6 * b3 + a7 * b2 + a8 * b1 + a9 * b0; |
|
| 2956 | + // s10 = a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 + |
|
| 2957 | + // a6 * b4 + a7 * b3 + a8 * b2 + a9 * b1 + a10 * b0; |
|
| 2958 | + // s11 = a0 * b11 + a1 * b10 + a2 * b9 + a3 * b8 + a4 * b7 + a5 * b6 + |
|
| 2959 | + // a6 * b5 + a7 * b4 + a8 * b3 + a9 * b2 + a10 * b1 + a11 * b0; |
|
| 2960 | + // s12 = a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + |
|
| 2961 | + // a7 * b5 + a8 * b4 + a9 * b3 + a10 * b2 + a11 * b1; |
|
| 2962 | + // s13 = a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + |
|
| 2963 | + // a8 * b5 + a9 * b4 + a10 * b3 + a11 * b2; |
|
| 2964 | + // s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + |
|
| 2965 | + // a9 * b5 + a10 * b4 + a11 * b3; |
|
| 2966 | + // s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + |
|
| 2967 | + // a10 * b5 + a11 * b4; |
|
| 2968 | + // s16 = |
|
| 2969 | + // a5 * b11 + a6 * b10 + a7 * b9 + a8 * b8 + a9 * b7 + a10 * b6 + a11 * b5; |
|
| 2970 | + // s17 = a6 * b11 + a7 * b10 + a8 * b9 + a9 * b8 + a10 * b7 + a11 * b6; |
|
| 2971 | + // s18 = a7 * b11 + a8 * b10 + a9 * b9 + a10 * b8 + a11 * b7; |
|
| 2972 | + // s19 = a8 * b11 + a9 * b10 + a10 * b9 + a11 * b8; |
|
| 2973 | + // s20 = a9 * b11 + a10 * b10 + a11 * b9; |
|
| 2974 | + // s21 = a10 * b11 + a11 * b10; |
|
| 2975 | + // s22 = a11 * b11; |
|
| 2976 | + // s23 = 0; |
|
| 2977 | + $s0 = self::mul($a0, $b0, 22); |
|
| 2978 | + $s1 = self::mul($a0, $b1, 22) + self::mul($a1, $b0, 22); |
|
| 2979 | + $s2 = self::mul($a0, $b2, 22) + self::mul($a1, $b1, 22) + self::mul($a2, $b0, 22); |
|
| 2980 | + $s3 = self::mul($a0, $b3, 22) + self::mul($a1, $b2, 22) + self::mul($a2, $b1, 22) + self::mul($a3, $b0, 22); |
|
| 2981 | + $s4 = self::mul($a0, $b4, 22) + self::mul($a1, $b3, 22) + self::mul($a2, $b2, 22) + self::mul($a3, $b1, 22) + |
|
| 2982 | + self::mul($a4, $b0, 22); |
|
| 2983 | + $s5 = self::mul($a0, $b5, 22) + self::mul($a1, $b4, 22) + self::mul($a2, $b3, 22) + self::mul($a3, $b2, 22) + |
|
| 2984 | + self::mul($a4, $b1, 22) + self::mul($a5, $b0, 22); |
|
| 2985 | + $s6 = self::mul($a0, $b6, 22) + self::mul($a1, $b5, 22) + self::mul($a2, $b4, 22) + self::mul($a3, $b3, 22) + |
|
| 2986 | + self::mul($a4, $b2, 22) + self::mul($a5, $b1, 22) + self::mul($a6, $b0, 22); |
|
| 2987 | + $s7 = self::mul($a0, $b7, 22) + self::mul($a1, $b6, 22) + self::mul($a2, $b5, 22) + self::mul($a3, $b4, 22) + |
|
| 2988 | + self::mul($a4, $b3, 22) + self::mul($a5, $b2, 22) + self::mul($a6, $b1, 22) + self::mul($a7, $b0, 22); |
|
| 2989 | + $s8 = self::mul($a0, $b8, 22) + self::mul($a1, $b7, 22) + self::mul($a2, $b6, 22) + self::mul($a3, $b5, 22) + |
|
| 2990 | + self::mul($a4, $b4, 22) + self::mul($a5, $b3, 22) + self::mul($a6, $b2, 22) + self::mul($a7, $b1, 22) + |
|
| 2991 | + self::mul($a8, $b0, 22); |
|
| 2992 | + $s9 = self::mul($a0, $b9, 22) + self::mul($a1, $b8, 22) + self::mul($a2, $b7, 22) + self::mul($a3, $b6, 22) + |
|
| 2993 | + self::mul($a4, $b5, 22) + self::mul($a5, $b4, 22) + self::mul($a6, $b3, 22) + self::mul($a7, $b2, 22) + |
|
| 2994 | + self::mul($a8, $b1, 22) + self::mul($a9, $b0, 22); |
|
| 2995 | + $s10 = self::mul($a0, $b10, 22) + self::mul($a1, $b9, 22) + self::mul($a2, $b8, 22) + self::mul($a3, $b7, 22) + |
|
| 2996 | + self::mul($a4, $b6, 22) + self::mul($a5, $b5, 22) + self::mul($a6, $b4, 22) + self::mul($a7, $b3, 22) + |
|
| 2997 | + self::mul($a8, $b2, 22) + self::mul($a9, $b1, 22) + self::mul($a10, $b0, 22); |
|
| 2998 | + $s11 = self::mul($a0, $b11, 22) + self::mul($a1, $b10, 22) + self::mul($a2, $b9, 22) + self::mul($a3, $b8, 22) + |
|
| 2999 | + self::mul($a4, $b7, 22) + self::mul($a5, $b6, 22) + self::mul($a6, $b5, 22) + self::mul($a7, $b4, 22) + |
|
| 3000 | + self::mul($a8, $b3, 22) + self::mul($a9, $b2, 22) + self::mul($a10, $b1, 22) + self::mul($a11, $b0, 22); |
|
| 3001 | + $s12 = self::mul($a1, $b11, 22) + self::mul($a2, $b10, 22) + self::mul($a3, $b9, 22) + self::mul($a4, $b8, 22) + |
|
| 3002 | + self::mul($a5, $b7, 22) + self::mul($a6, $b6, 22) + self::mul($a7, $b5, 22) + self::mul($a8, $b4, 22) + |
|
| 3003 | + self::mul($a9, $b3, 22) + self::mul($a10, $b2, 22) + self::mul($a11, $b1, 22); |
|
| 3004 | + $s13 = self::mul($a2, $b11, 22) + self::mul($a3, $b10, 22) + self::mul($a4, $b9, 22) + self::mul($a5, $b8, 22) + |
|
| 3005 | + self::mul($a6, $b7, 22) + self::mul($a7, $b6, 22) + self::mul($a8, $b5, 22) + self::mul($a9, $b4, 22) + |
|
| 3006 | + self::mul($a10, $b3, 22) + self::mul($a11, $b2, 22); |
|
| 3007 | + $s14 = self::mul($a3, $b11, 22) + self::mul($a4, $b10, 22) + self::mul($a5, $b9, 22) + self::mul($a6, $b8, 22) + |
|
| 3008 | + self::mul($a7, $b7, 22) + self::mul($a8, $b6, 22) + self::mul($a9, $b5, 22) + self::mul($a10, $b4, 22) + |
|
| 3009 | + self::mul($a11, $b3, 22); |
|
| 3010 | + $s15 = self::mul($a4, $b11, 22) + self::mul($a5, $b10, 22) + self::mul($a6, $b9, 22) + self::mul($a7, $b8, 22) + |
|
| 3011 | + self::mul($a8, $b7, 22) + self::mul($a9, $b6, 22) + self::mul($a10, $b5, 22) + self::mul($a11, $b4, 22); |
|
| 3012 | + $s16 = |
|
| 3013 | + self::mul($a5, $b11, 22) + self::mul($a6, $b10, 22) + self::mul($a7, $b9, 22) + self::mul($a8, $b8, 22) + |
|
| 3014 | + self::mul($a9, $b7, 22) + self::mul($a10, $b6, 22) + self::mul($a11, $b5, 22); |
|
| 3015 | + $s17 = self::mul($a6, $b11, 22) + self::mul($a7, $b10, 22) + self::mul($a8, $b9, 22) + self::mul($a9, $b8, 22) + |
|
| 3016 | + self::mul($a10, $b7, 22) + self::mul($a11, $b6, 22); |
|
| 3017 | + $s18 = self::mul($a7, $b11, 22) + self::mul($a8, $b10, 22) + self::mul($a9, $b9, 22) + self::mul($a10, $b8, 22) |
|
| 3018 | + + self::mul($a11, $b7, 22); |
|
| 3019 | + $s19 = self::mul($a8, $b11, 22) + self::mul($a9, $b10, 22) + self::mul($a10, $b9, 22) + |
|
| 3020 | + self::mul($a11, $b8, 22); |
|
| 3021 | + $s20 = self::mul($a9, $b11, 22) + self::mul($a10, $b10, 22) + self::mul($a11, $b9, 22); |
|
| 3022 | + $s21 = self::mul($a10, $b11, 22) + self::mul($a11, $b10, 22); |
|
| 3023 | + $s22 = self::mul($a11, $b11, 22); |
|
| 3024 | + $s23 = 0; |
|
| 3025 | + |
|
| 3026 | + // carry0 = (s0 + (int64_t) (1L << 20)) >> 21; |
|
| 3027 | + // s1 += carry0; |
|
| 3028 | + // s0 -= carry0 * ((uint64_t) 1L << 21); |
|
| 3029 | + $carry0 = ($s0 + (1 << 20)) >> 21; |
|
| 3030 | + $s1 += $carry0; |
|
| 3031 | + $s0 -= $carry0 << 21; |
|
| 3032 | + // carry2 = (s2 + (int64_t) (1L << 20)) >> 21; |
|
| 3033 | + // s3 += carry2; |
|
| 3034 | + // s2 -= carry2 * ((uint64_t) 1L << 21); |
|
| 3035 | + $carry2 = ($s2 + (1 << 20)) >> 21; |
|
| 3036 | + $s3 += $carry2; |
|
| 3037 | + $s2 -= $carry2 << 21; |
|
| 3038 | + // carry4 = (s4 + (int64_t) (1L << 20)) >> 21; |
|
| 3039 | + // s5 += carry4; |
|
| 3040 | + // s4 -= carry4 * ((uint64_t) 1L << 21); |
|
| 3041 | + $carry4 = ($s4 + (1 << 20)) >> 21; |
|
| 3042 | + $s5 += $carry4; |
|
| 3043 | + $s4 -= $carry4 << 21; |
|
| 3044 | + // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; |
|
| 3045 | + // s7 += carry6; |
|
| 3046 | + // s6 -= carry6 * ((uint64_t) 1L << 21); |
|
| 3047 | + $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 3048 | + $s7 += $carry6; |
|
| 3049 | + $s6 -= $carry6 << 21; |
|
| 3050 | + // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; |
|
| 3051 | + // s9 += carry8; |
|
| 3052 | + // s8 -= carry8 * ((uint64_t) 1L << 21); |
|
| 3053 | + $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 3054 | + $s9 += $carry8; |
|
| 3055 | + $s8 -= $carry8 << 21; |
|
| 3056 | + // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; |
|
| 3057 | + // s11 += carry10; |
|
| 3058 | + // s10 -= carry10 * ((uint64_t) 1L << 21); |
|
| 3059 | + $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 3060 | + $s11 += $carry10; |
|
| 3061 | + $s10 -= $carry10 << 21; |
|
| 3062 | + // carry12 = (s12 + (int64_t) (1L << 20)) >> 21; |
|
| 3063 | + // s13 += carry12; |
|
| 3064 | + // s12 -= carry12 * ((uint64_t) 1L << 21); |
|
| 3065 | + $carry12 = ($s12 + (1 << 20)) >> 21; |
|
| 3066 | + $s13 += $carry12; |
|
| 3067 | + $s12 -= $carry12 << 21; |
|
| 3068 | + // carry14 = (s14 + (int64_t) (1L << 20)) >> 21; |
|
| 3069 | + // s15 += carry14; |
|
| 3070 | + // s14 -= carry14 * ((uint64_t) 1L << 21); |
|
| 3071 | + $carry14 = ($s14 + (1 << 20)) >> 21; |
|
| 3072 | + $s15 += $carry14; |
|
| 3073 | + $s14 -= $carry14 << 21; |
|
| 3074 | + // carry16 = (s16 + (int64_t) (1L << 20)) >> 21; |
|
| 3075 | + // s17 += carry16; |
|
| 3076 | + // s16 -= carry16 * ((uint64_t) 1L << 21); |
|
| 3077 | + $carry16 = ($s16 + (1 << 20)) >> 21; |
|
| 3078 | + $s17 += $carry16; |
|
| 3079 | + $s16 -= $carry16 << 21; |
|
| 3080 | + // carry18 = (s18 + (int64_t) (1L << 20)) >> 21; |
|
| 3081 | + // s19 += carry18; |
|
| 3082 | + // s18 -= carry18 * ((uint64_t) 1L << 21); |
|
| 3083 | + $carry18 = ($s18 + (1 << 20)) >> 21; |
|
| 3084 | + $s19 += $carry18; |
|
| 3085 | + $s18 -= $carry18 << 21; |
|
| 3086 | + // carry20 = (s20 + (int64_t) (1L << 20)) >> 21; |
|
| 3087 | + // s21 += carry20; |
|
| 3088 | + // s20 -= carry20 * ((uint64_t) 1L << 21); |
|
| 3089 | + $carry20 = ($s20 + (1 << 20)) >> 21; |
|
| 3090 | + $s21 += $carry20; |
|
| 3091 | + $s20 -= $carry20 << 21; |
|
| 3092 | + // carry22 = (s22 + (int64_t) (1L << 20)) >> 21; |
|
| 3093 | + // s23 += carry22; |
|
| 3094 | + // s22 -= carry22 * ((uint64_t) 1L << 21); |
|
| 3095 | + $carry22 = ($s22 + (1 << 20)) >> 21; |
|
| 3096 | + $s23 += $carry22; |
|
| 3097 | + $s22 -= $carry22 << 21; |
|
| 3098 | + |
|
| 3099 | + // carry1 = (s1 + (int64_t) (1L << 20)) >> 21; |
|
| 3100 | + // s2 += carry1; |
|
| 3101 | + // s1 -= carry1 * ((uint64_t) 1L << 21); |
|
| 3102 | + $carry1 = ($s1 + (1 << 20)) >> 21; |
|
| 3103 | + $s2 += $carry1; |
|
| 3104 | + $s1 -= $carry1 << 21; |
|
| 3105 | + // carry3 = (s3 + (int64_t) (1L << 20)) >> 21; |
|
| 3106 | + // s4 += carry3; |
|
| 3107 | + // s3 -= carry3 * ((uint64_t) 1L << 21); |
|
| 3108 | + $carry3 = ($s3 + (1 << 20)) >> 21; |
|
| 3109 | + $s4 += $carry3; |
|
| 3110 | + $s3 -= $carry3 << 21; |
|
| 3111 | + // carry5 = (s5 + (int64_t) (1L << 20)) >> 21; |
|
| 3112 | + // s6 += carry5; |
|
| 3113 | + // s5 -= carry5 * ((uint64_t) 1L << 21); |
|
| 3114 | + $carry5 = ($s5 + (1 << 20)) >> 21; |
|
| 3115 | + $s6 += $carry5; |
|
| 3116 | + $s5 -= $carry5 << 21; |
|
| 3117 | + // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; |
|
| 3118 | + // s8 += carry7; |
|
| 3119 | + // s7 -= carry7 * ((uint64_t) 1L << 21); |
|
| 3120 | + $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 3121 | + $s8 += $carry7; |
|
| 3122 | + $s7 -= $carry7 << 21; |
|
| 3123 | + // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; |
|
| 3124 | + // s10 += carry9; |
|
| 3125 | + // s9 -= carry9 * ((uint64_t) 1L << 21); |
|
| 3126 | + $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 3127 | + $s10 += $carry9; |
|
| 3128 | + $s9 -= $carry9 << 21; |
|
| 3129 | + // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; |
|
| 3130 | + // s12 += carry11; |
|
| 3131 | + // s11 -= carry11 * ((uint64_t) 1L << 21); |
|
| 3132 | + $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 3133 | + $s12 += $carry11; |
|
| 3134 | + $s11 -= $carry11 << 21; |
|
| 3135 | + // carry13 = (s13 + (int64_t) (1L << 20)) >> 21; |
|
| 3136 | + // s14 += carry13; |
|
| 3137 | + // s13 -= carry13 * ((uint64_t) 1L << 21); |
|
| 3138 | + $carry13 = ($s13 + (1 << 20)) >> 21; |
|
| 3139 | + $s14 += $carry13; |
|
| 3140 | + $s13 -= $carry13 << 21; |
|
| 3141 | + // carry15 = (s15 + (int64_t) (1L << 20)) >> 21; |
|
| 3142 | + // s16 += carry15; |
|
| 3143 | + // s15 -= carry15 * ((uint64_t) 1L << 21); |
|
| 3144 | + $carry15 = ($s15 + (1 << 20)) >> 21; |
|
| 3145 | + $s16 += $carry15; |
|
| 3146 | + $s15 -= $carry15 << 21; |
|
| 3147 | + // carry17 = (s17 + (int64_t) (1L << 20)) >> 21; |
|
| 3148 | + // s18 += carry17; |
|
| 3149 | + // s17 -= carry17 * ((uint64_t) 1L << 21); |
|
| 3150 | + $carry17 = ($s17 + (1 << 20)) >> 21; |
|
| 3151 | + $s18 += $carry17; |
|
| 3152 | + $s17 -= $carry17 << 21; |
|
| 3153 | + // carry19 = (s19 + (int64_t) (1L << 20)) >> 21; |
|
| 3154 | + // s20 += carry19; |
|
| 3155 | + // s19 -= carry19 * ((uint64_t) 1L << 21); |
|
| 3156 | + $carry19 = ($s19 + (1 << 20)) >> 21; |
|
| 3157 | + $s20 += $carry19; |
|
| 3158 | + $s19 -= $carry19 << 21; |
|
| 3159 | + // carry21 = (s21 + (int64_t) (1L << 20)) >> 21; |
|
| 3160 | + // s22 += carry21; |
|
| 3161 | + // s21 -= carry21 * ((uint64_t) 1L << 21); |
|
| 3162 | + $carry21 = ($s21 + (1 << 20)) >> 21; |
|
| 3163 | + $s22 += $carry21; |
|
| 3164 | + $s21 -= $carry21 << 21; |
|
| 3165 | + |
|
| 3166 | + // s11 += s23 * 666643; |
|
| 3167 | + // s12 += s23 * 470296; |
|
| 3168 | + // s13 += s23 * 654183; |
|
| 3169 | + // s14 -= s23 * 997805; |
|
| 3170 | + // s15 += s23 * 136657; |
|
| 3171 | + // s16 -= s23 * 683901; |
|
| 3172 | + $s11 += self::mul($s23, 666643, 20); |
|
| 3173 | + $s12 += self::mul($s23, 470296, 19); |
|
| 3174 | + $s13 += self::mul($s23, 654183, 20); |
|
| 3175 | + $s14 -= self::mul($s23, 997805, 20); |
|
| 3176 | + $s15 += self::mul($s23, 136657, 18); |
|
| 3177 | + $s16 -= self::mul($s23, 683901, 20); |
|
| 3178 | + |
|
| 3179 | + // s10 += s22 * 666643; |
|
| 3180 | + // s11 += s22 * 470296; |
|
| 3181 | + // s12 += s22 * 654183; |
|
| 3182 | + // s13 -= s22 * 997805; |
|
| 3183 | + // s14 += s22 * 136657; |
|
| 3184 | + // s15 -= s22 * 683901; |
|
| 3185 | + $s10 += self::mul($s22, 666643, 20); |
|
| 3186 | + $s11 += self::mul($s22, 470296, 19); |
|
| 3187 | + $s12 += self::mul($s22, 654183, 20); |
|
| 3188 | + $s13 -= self::mul($s22, 997805, 20); |
|
| 3189 | + $s14 += self::mul($s22, 136657, 18); |
|
| 3190 | + $s15 -= self::mul($s22, 683901, 20); |
|
| 3191 | + |
|
| 3192 | + // s9 += s21 * 666643; |
|
| 3193 | + // s10 += s21 * 470296; |
|
| 3194 | + // s11 += s21 * 654183; |
|
| 3195 | + // s12 -= s21 * 997805; |
|
| 3196 | + // s13 += s21 * 136657; |
|
| 3197 | + // s14 -= s21 * 683901; |
|
| 3198 | + $s9 += self::mul($s21, 666643, 20); |
|
| 3199 | + $s10 += self::mul($s21, 470296, 19); |
|
| 3200 | + $s11 += self::mul($s21, 654183, 20); |
|
| 3201 | + $s12 -= self::mul($s21, 997805, 20); |
|
| 3202 | + $s13 += self::mul($s21, 136657, 18); |
|
| 3203 | + $s14 -= self::mul($s21, 683901, 20); |
|
| 3204 | + |
|
| 3205 | + // s8 += s20 * 666643; |
|
| 3206 | + // s9 += s20 * 470296; |
|
| 3207 | + // s10 += s20 * 654183; |
|
| 3208 | + // s11 -= s20 * 997805; |
|
| 3209 | + // s12 += s20 * 136657; |
|
| 3210 | + // s13 -= s20 * 683901; |
|
| 3211 | + $s8 += self::mul($s20, 666643, 20); |
|
| 3212 | + $s9 += self::mul($s20, 470296, 19); |
|
| 3213 | + $s10 += self::mul($s20, 654183, 20); |
|
| 3214 | + $s11 -= self::mul($s20, 997805, 20); |
|
| 3215 | + $s12 += self::mul($s20, 136657, 18); |
|
| 3216 | + $s13 -= self::mul($s20, 683901, 20); |
|
| 3217 | + |
|
| 3218 | + // s7 += s19 * 666643; |
|
| 3219 | + // s8 += s19 * 470296; |
|
| 3220 | + // s9 += s19 * 654183; |
|
| 3221 | + // s10 -= s19 * 997805; |
|
| 3222 | + // s11 += s19 * 136657; |
|
| 3223 | + // s12 -= s19 * 683901; |
|
| 3224 | + $s7 += self::mul($s19, 666643, 20); |
|
| 3225 | + $s8 += self::mul($s19, 470296, 19); |
|
| 3226 | + $s9 += self::mul($s19, 654183, 20); |
|
| 3227 | + $s10 -= self::mul($s19, 997805, 20); |
|
| 3228 | + $s11 += self::mul($s19, 136657, 18); |
|
| 3229 | + $s12 -= self::mul($s19, 683901, 20); |
|
| 3230 | + |
|
| 3231 | + // s6 += s18 * 666643; |
|
| 3232 | + // s7 += s18 * 470296; |
|
| 3233 | + // s8 += s18 * 654183; |
|
| 3234 | + // s9 -= s18 * 997805; |
|
| 3235 | + // s10 += s18 * 136657; |
|
| 3236 | + // s11 -= s18 * 683901; |
|
| 3237 | + $s6 += self::mul($s18, 666643, 20); |
|
| 3238 | + $s7 += self::mul($s18, 470296, 19); |
|
| 3239 | + $s8 += self::mul($s18, 654183, 20); |
|
| 3240 | + $s9 -= self::mul($s18, 997805, 20); |
|
| 3241 | + $s10 += self::mul($s18, 136657, 18); |
|
| 3242 | + $s11 -= self::mul($s18, 683901, 20); |
|
| 3243 | + |
|
| 3244 | + // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; |
|
| 3245 | + // s7 += carry6; |
|
| 3246 | + // s6 -= carry6 * ((uint64_t) 1L << 21); |
|
| 3247 | + $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 3248 | + $s7 += $carry6; |
|
| 3249 | + $s6 -= $carry6 << 21; |
|
| 3250 | + // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; |
|
| 3251 | + // s9 += carry8; |
|
| 3252 | + // s8 -= carry8 * ((uint64_t) 1L << 21); |
|
| 3253 | + $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 3254 | + $s9 += $carry8; |
|
| 3255 | + $s8 -= $carry8 << 21; |
|
| 3256 | + // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; |
|
| 3257 | + // s11 += carry10; |
|
| 3258 | + // s10 -= carry10 * ((uint64_t) 1L << 21); |
|
| 3259 | + $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 3260 | + $s11 += $carry10; |
|
| 3261 | + $s10 -= $carry10 << 21; |
|
| 3262 | + // carry12 = (s12 + (int64_t) (1L << 20)) >> 21; |
|
| 3263 | + // s13 += carry12; |
|
| 3264 | + // s12 -= carry12 * ((uint64_t) 1L << 21); |
|
| 3265 | + $carry12 = ($s12 + (1 << 20)) >> 21; |
|
| 3266 | + $s13 += $carry12; |
|
| 3267 | + $s12 -= $carry12 << 21; |
|
| 3268 | + // carry14 = (s14 + (int64_t) (1L << 20)) >> 21; |
|
| 3269 | + // s15 += carry14; |
|
| 3270 | + // s14 -= carry14 * ((uint64_t) 1L << 21); |
|
| 3271 | + $carry14 = ($s14 + (1 << 20)) >> 21; |
|
| 3272 | + $s15 += $carry14; |
|
| 3273 | + $s14 -= $carry14 << 21; |
|
| 3274 | + // carry16 = (s16 + (int64_t) (1L << 20)) >> 21; |
|
| 3275 | + // s17 += carry16; |
|
| 3276 | + // s16 -= carry16 * ((uint64_t) 1L << 21); |
|
| 3277 | + $carry16 = ($s16 + (1 << 20)) >> 21; |
|
| 3278 | + $s17 += $carry16; |
|
| 3279 | + $s16 -= $carry16 << 21; |
|
| 3280 | + |
|
| 3281 | + // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; |
|
| 3282 | + // s8 += carry7; |
|
| 3283 | + // s7 -= carry7 * ((uint64_t) 1L << 21); |
|
| 3284 | + $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 3285 | + $s8 += $carry7; |
|
| 3286 | + $s7 -= $carry7 << 21; |
|
| 3287 | + // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; |
|
| 3288 | + // s10 += carry9; |
|
| 3289 | + // s9 -= carry9 * ((uint64_t) 1L << 21); |
|
| 3290 | + $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 3291 | + $s10 += $carry9; |
|
| 3292 | + $s9 -= $carry9 << 21; |
|
| 3293 | + // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; |
|
| 3294 | + // s12 += carry11; |
|
| 3295 | + // s11 -= carry11 * ((uint64_t) 1L << 21); |
|
| 3296 | + $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 3297 | + $s12 += $carry11; |
|
| 3298 | + $s11 -= $carry11 << 21; |
|
| 3299 | + // carry13 = (s13 + (int64_t) (1L << 20)) >> 21; |
|
| 3300 | + // s14 += carry13; |
|
| 3301 | + // s13 -= carry13 * ((uint64_t) 1L << 21); |
|
| 3302 | + $carry13 = ($s13 + (1 << 20)) >> 21; |
|
| 3303 | + $s14 += $carry13; |
|
| 3304 | + $s13 -= $carry13 << 21; |
|
| 3305 | + // carry15 = (s15 + (int64_t) (1L << 20)) >> 21; |
|
| 3306 | + // s16 += carry15; |
|
| 3307 | + // s15 -= carry15 * ((uint64_t) 1L << 21); |
|
| 3308 | + $carry15 = ($s15 + (1 << 20)) >> 21; |
|
| 3309 | + $s16 += $carry15; |
|
| 3310 | + $s15 -= $carry15 << 21; |
|
| 3311 | + |
|
| 3312 | + // s5 += s17 * 666643; |
|
| 3313 | + // s6 += s17 * 470296; |
|
| 3314 | + // s7 += s17 * 654183; |
|
| 3315 | + // s8 -= s17 * 997805; |
|
| 3316 | + // s9 += s17 * 136657; |
|
| 3317 | + // s10 -= s17 * 683901; |
|
| 3318 | + $s5 += self::mul($s17, 666643, 20); |
|
| 3319 | + $s6 += self::mul($s17, 470296, 19); |
|
| 3320 | + $s7 += self::mul($s17, 654183, 20); |
|
| 3321 | + $s8 -= self::mul($s17, 997805, 20); |
|
| 3322 | + $s9 += self::mul($s17, 136657, 18); |
|
| 3323 | + $s10 -= self::mul($s17, 683901, 20); |
|
| 3324 | + |
|
| 3325 | + // s4 += s16 * 666643; |
|
| 3326 | + // s5 += s16 * 470296; |
|
| 3327 | + // s6 += s16 * 654183; |
|
| 3328 | + // s7 -= s16 * 997805; |
|
| 3329 | + // s8 += s16 * 136657; |
|
| 3330 | + // s9 -= s16 * 683901; |
|
| 3331 | + $s4 += self::mul($s16, 666643, 20); |
|
| 3332 | + $s5 += self::mul($s16, 470296, 19); |
|
| 3333 | + $s6 += self::mul($s16, 654183, 20); |
|
| 3334 | + $s7 -= self::mul($s16, 997805, 20); |
|
| 3335 | + $s8 += self::mul($s16, 136657, 18); |
|
| 3336 | + $s9 -= self::mul($s16, 683901, 20); |
|
| 3337 | + |
|
| 3338 | + // s3 += s15 * 666643; |
|
| 3339 | + // s4 += s15 * 470296; |
|
| 3340 | + // s5 += s15 * 654183; |
|
| 3341 | + // s6 -= s15 * 997805; |
|
| 3342 | + // s7 += s15 * 136657; |
|
| 3343 | + // s8 -= s15 * 683901; |
|
| 3344 | + $s3 += self::mul($s15, 666643, 20); |
|
| 3345 | + $s4 += self::mul($s15, 470296, 19); |
|
| 3346 | + $s5 += self::mul($s15, 654183, 20); |
|
| 3347 | + $s6 -= self::mul($s15, 997805, 20); |
|
| 3348 | + $s7 += self::mul($s15, 136657, 18); |
|
| 3349 | + $s8 -= self::mul($s15, 683901, 20); |
|
| 3350 | + |
|
| 3351 | + // s2 += s14 * 666643; |
|
| 3352 | + // s3 += s14 * 470296; |
|
| 3353 | + // s4 += s14 * 654183; |
|
| 3354 | + // s5 -= s14 * 997805; |
|
| 3355 | + // s6 += s14 * 136657; |
|
| 3356 | + // s7 -= s14 * 683901; |
|
| 3357 | + $s2 += self::mul($s14, 666643, 20); |
|
| 3358 | + $s3 += self::mul($s14, 470296, 19); |
|
| 3359 | + $s4 += self::mul($s14, 654183, 20); |
|
| 3360 | + $s5 -= self::mul($s14, 997805, 20); |
|
| 3361 | + $s6 += self::mul($s14, 136657, 18); |
|
| 3362 | + $s7 -= self::mul($s14, 683901, 20); |
|
| 3363 | + |
|
| 3364 | + // s1 += s13 * 666643; |
|
| 3365 | + // s2 += s13 * 470296; |
|
| 3366 | + // s3 += s13 * 654183; |
|
| 3367 | + // s4 -= s13 * 997805; |
|
| 3368 | + // s5 += s13 * 136657; |
|
| 3369 | + // s6 -= s13 * 683901; |
|
| 3370 | + $s1 += self::mul($s13, 666643, 20); |
|
| 3371 | + $s2 += self::mul($s13, 470296, 19); |
|
| 3372 | + $s3 += self::mul($s13, 654183, 20); |
|
| 3373 | + $s4 -= self::mul($s13, 997805, 20); |
|
| 3374 | + $s5 += self::mul($s13, 136657, 18); |
|
| 3375 | + $s6 -= self::mul($s13, 683901, 20); |
|
| 3376 | + |
|
| 3377 | + // s0 += s12 * 666643; |
|
| 3378 | + // s1 += s12 * 470296; |
|
| 3379 | + // s2 += s12 * 654183; |
|
| 3380 | + // s3 -= s12 * 997805; |
|
| 3381 | + // s4 += s12 * 136657; |
|
| 3382 | + // s5 -= s12 * 683901; |
|
| 3383 | + // s12 = 0; |
|
| 3384 | + $s0 += self::mul($s12, 666643, 20); |
|
| 3385 | + $s1 += self::mul($s12, 470296, 19); |
|
| 3386 | + $s2 += self::mul($s12, 654183, 20); |
|
| 3387 | + $s3 -= self::mul($s12, 997805, 20); |
|
| 3388 | + $s4 += self::mul($s12, 136657, 18); |
|
| 3389 | + $s5 -= self::mul($s12, 683901, 20); |
|
| 3390 | + $s12 = 0; |
|
| 3391 | + |
|
| 3392 | + // carry0 = (s0 + (int64_t) (1L << 20)) >> 21; |
|
| 3393 | + // s1 += carry0; |
|
| 3394 | + // s0 -= carry0 * ((uint64_t) 1L << 21); |
|
| 3395 | + $carry0 = ($s0 + (1 << 20)) >> 21; |
|
| 3396 | + $s1 += $carry0; |
|
| 3397 | + $s0 -= $carry0 << 21; |
|
| 3398 | + // carry2 = (s2 + (int64_t) (1L << 20)) >> 21; |
|
| 3399 | + // s3 += carry2; |
|
| 3400 | + // s2 -= carry2 * ((uint64_t) 1L << 21); |
|
| 3401 | + $carry2 = ($s2 + (1 << 20)) >> 21; |
|
| 3402 | + $s3 += $carry2; |
|
| 3403 | + $s2 -= $carry2 << 21; |
|
| 3404 | + // carry4 = (s4 + (int64_t) (1L << 20)) >> 21; |
|
| 3405 | + // s5 += carry4; |
|
| 3406 | + // s4 -= carry4 * ((uint64_t) 1L << 21); |
|
| 3407 | + $carry4 = ($s4 + (1 << 20)) >> 21; |
|
| 3408 | + $s5 += $carry4; |
|
| 3409 | + $s4 -= $carry4 << 21; |
|
| 3410 | + // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; |
|
| 3411 | + // s7 += carry6; |
|
| 3412 | + // s6 -= carry6 * ((uint64_t) 1L << 21); |
|
| 3413 | + $carry6 = ($s6 + (1 << 20)) >> 21; |
|
| 3414 | + $s7 += $carry6; |
|
| 3415 | + $s6 -= $carry6 << 21; |
|
| 3416 | + // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; |
|
| 3417 | + // s9 += carry8; |
|
| 3418 | + // s8 -= carry8 * ((uint64_t) 1L << 21); |
|
| 3419 | + $carry8 = ($s8 + (1 << 20)) >> 21; |
|
| 3420 | + $s9 += $carry8; |
|
| 3421 | + $s8 -= $carry8 << 21; |
|
| 3422 | + // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; |
|
| 3423 | + // s11 += carry10; |
|
| 3424 | + // s10 -= carry10 * ((uint64_t) 1L << 21); |
|
| 3425 | + $carry10 = ($s10 + (1 << 20)) >> 21; |
|
| 3426 | + $s11 += $carry10; |
|
| 3427 | + $s10 -= $carry10 << 21; |
|
| 3428 | + |
|
| 3429 | + // carry1 = (s1 + (int64_t) (1L << 20)) >> 21; |
|
| 3430 | + // s2 += carry1; |
|
| 3431 | + // s1 -= carry1 * ((uint64_t) 1L << 21); |
|
| 3432 | + $carry1 = ($s1 + (1 << 20)) >> 21; |
|
| 3433 | + $s2 += $carry1; |
|
| 3434 | + $s1 -= $carry1 << 21; |
|
| 3435 | + // carry3 = (s3 + (int64_t) (1L << 20)) >> 21; |
|
| 3436 | + // s4 += carry3; |
|
| 3437 | + // s3 -= carry3 * ((uint64_t) 1L << 21); |
|
| 3438 | + $carry3 = ($s3 + (1 << 20)) >> 21; |
|
| 3439 | + $s4 += $carry3; |
|
| 3440 | + $s3 -= $carry3 << 21; |
|
| 3441 | + // carry5 = (s5 + (int64_t) (1L << 20)) >> 21; |
|
| 3442 | + // s6 += carry5; |
|
| 3443 | + // s5 -= carry5 * ((uint64_t) 1L << 21); |
|
| 3444 | + $carry5 = ($s5 + (1 << 20)) >> 21; |
|
| 3445 | + $s6 += $carry5; |
|
| 3446 | + $s5 -= $carry5 << 21; |
|
| 3447 | + // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; |
|
| 3448 | + // s8 += carry7; |
|
| 3449 | + // s7 -= carry7 * ((uint64_t) 1L << 21); |
|
| 3450 | + $carry7 = ($s7 + (1 << 20)) >> 21; |
|
| 3451 | + $s8 += $carry7; |
|
| 3452 | + $s7 -= $carry7 << 21; |
|
| 3453 | + // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; |
|
| 3454 | + // s10 += carry9; |
|
| 3455 | + // s9 -= carry9 * ((uint64_t) 1L << 21); |
|
| 3456 | + $carry9 = ($s9 + (1 << 20)) >> 21; |
|
| 3457 | + $s10 += $carry9; |
|
| 3458 | + $s9 -= $carry9 << 21; |
|
| 3459 | + // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; |
|
| 3460 | + // s12 += carry11; |
|
| 3461 | + // s11 -= carry11 * ((uint64_t) 1L << 21); |
|
| 3462 | + $carry11 = ($s11 + (1 << 20)) >> 21; |
|
| 3463 | + $s12 += $carry11; |
|
| 3464 | + $s11 -= $carry11 << 21; |
|
| 3465 | + |
|
| 3466 | + // s0 += s12 * 666643; |
|
| 3467 | + // s1 += s12 * 470296; |
|
| 3468 | + // s2 += s12 * 654183; |
|
| 3469 | + // s3 -= s12 * 997805; |
|
| 3470 | + // s4 += s12 * 136657; |
|
| 3471 | + // s5 -= s12 * 683901; |
|
| 3472 | + // s12 = 0; |
|
| 3473 | + $s0 += self::mul($s12, 666643, 20); |
|
| 3474 | + $s1 += self::mul($s12, 470296, 19); |
|
| 3475 | + $s2 += self::mul($s12, 654183, 20); |
|
| 3476 | + $s3 -= self::mul($s12, 997805, 20); |
|
| 3477 | + $s4 += self::mul($s12, 136657, 18); |
|
| 3478 | + $s5 -= self::mul($s12, 683901, 20); |
|
| 3479 | + $s12 = 0; |
|
| 3480 | + |
|
| 3481 | + // carry0 = s0 >> 21; |
|
| 3482 | + // s1 += carry0; |
|
| 3483 | + // s0 -= carry0 * ((uint64_t) 1L << 21); |
|
| 3484 | + $carry0 = $s0 >> 21; |
|
| 3485 | + $s1 += $carry0; |
|
| 3486 | + $s0 -= $carry0 << 21; |
|
| 3487 | + // carry1 = s1 >> 21; |
|
| 3488 | + // s2 += carry1; |
|
| 3489 | + // s1 -= carry1 * ((uint64_t) 1L << 21); |
|
| 3490 | + $carry1 = $s1 >> 21; |
|
| 3491 | + $s2 += $carry1; |
|
| 3492 | + $s1 -= $carry1 << 21; |
|
| 3493 | + // carry2 = s2 >> 21; |
|
| 3494 | + // s3 += carry2; |
|
| 3495 | + // s2 -= carry2 * ((uint64_t) 1L << 21); |
|
| 3496 | + $carry2 = $s2 >> 21; |
|
| 3497 | + $s3 += $carry2; |
|
| 3498 | + $s2 -= $carry2 << 21; |
|
| 3499 | + // carry3 = s3 >> 21; |
|
| 3500 | + // s4 += carry3; |
|
| 3501 | + // s3 -= carry3 * ((uint64_t) 1L << 21); |
|
| 3502 | + $carry3 = $s3 >> 21; |
|
| 3503 | + $s4 += $carry3; |
|
| 3504 | + $s3 -= $carry3 << 21; |
|
| 3505 | + // carry4 = s4 >> 21; |
|
| 3506 | + // s5 += carry4; |
|
| 3507 | + // s4 -= carry4 * ((uint64_t) 1L << 21); |
|
| 3508 | + $carry4 = $s4 >> 21; |
|
| 3509 | + $s5 += $carry4; |
|
| 3510 | + $s4 -= $carry4 << 21; |
|
| 3511 | + // carry5 = s5 >> 21; |
|
| 3512 | + // s6 += carry5; |
|
| 3513 | + // s5 -= carry5 * ((uint64_t) 1L << 21); |
|
| 3514 | + $carry5 = $s5 >> 21; |
|
| 3515 | + $s6 += $carry5; |
|
| 3516 | + $s5 -= $carry5 << 21; |
|
| 3517 | + // carry6 = s6 >> 21; |
|
| 3518 | + // s7 += carry6; |
|
| 3519 | + // s6 -= carry6 * ((uint64_t) 1L << 21); |
|
| 3520 | + $carry6 = $s6 >> 21; |
|
| 3521 | + $s7 += $carry6; |
|
| 3522 | + $s6 -= $carry6 << 21; |
|
| 3523 | + // carry7 = s7 >> 21; |
|
| 3524 | + // s8 += carry7; |
|
| 3525 | + // s7 -= carry7 * ((uint64_t) 1L << 21); |
|
| 3526 | + $carry7 = $s7 >> 21; |
|
| 3527 | + $s8 += $carry7; |
|
| 3528 | + $s7 -= $carry7 << 21; |
|
| 3529 | + // carry8 = s8 >> 21; |
|
| 3530 | + // s9 += carry8; |
|
| 3531 | + // s8 -= carry8 * ((uint64_t) 1L << 21); |
|
| 3532 | + $carry8 = $s8 >> 21; |
|
| 3533 | + $s9 += $carry8; |
|
| 3534 | + $s8 -= $carry8 << 21; |
|
| 3535 | + // carry9 = s9 >> 21; |
|
| 3536 | + // s10 += carry9; |
|
| 3537 | + // s9 -= carry9 * ((uint64_t) 1L << 21); |
|
| 3538 | + $carry9 = $s9 >> 21; |
|
| 3539 | + $s10 += $carry9; |
|
| 3540 | + $s9 -= $carry9 << 21; |
|
| 3541 | + // carry10 = s10 >> 21; |
|
| 3542 | + // s11 += carry10; |
|
| 3543 | + // s10 -= carry10 * ((uint64_t) 1L << 21); |
|
| 3544 | + $carry10 = $s10 >> 21; |
|
| 3545 | + $s11 += $carry10; |
|
| 3546 | + $s10 -= $carry10 << 21; |
|
| 3547 | + // carry11 = s11 >> 21; |
|
| 3548 | + // s12 += carry11; |
|
| 3549 | + // s11 -= carry11 * ((uint64_t) 1L << 21); |
|
| 3550 | + $carry11 = $s11 >> 21; |
|
| 3551 | + $s12 += $carry11; |
|
| 3552 | + $s11 -= $carry11 << 21; |
|
| 3553 | + |
|
| 3554 | + // s0 += s12 * 666643; |
|
| 3555 | + // s1 += s12 * 470296; |
|
| 3556 | + // s2 += s12 * 654183; |
|
| 3557 | + // s3 -= s12 * 997805; |
|
| 3558 | + // s4 += s12 * 136657; |
|
| 3559 | + // s5 -= s12 * 683901; |
|
| 3560 | + $s0 += self::mul($s12, 666643, 20); |
|
| 3561 | + $s1 += self::mul($s12, 470296, 19); |
|
| 3562 | + $s2 += self::mul($s12, 654183, 20); |
|
| 3563 | + $s3 -= self::mul($s12, 997805, 20); |
|
| 3564 | + $s4 += self::mul($s12, 136657, 18); |
|
| 3565 | + $s5 -= self::mul($s12, 683901, 20); |
|
| 3566 | + |
|
| 3567 | + // carry0 = s0 >> 21; |
|
| 3568 | + // s1 += carry0; |
|
| 3569 | + // s0 -= carry0 * ((uint64_t) 1L << 21); |
|
| 3570 | + $carry0 = $s0 >> 21; |
|
| 3571 | + $s1 += $carry0; |
|
| 3572 | + $s0 -= $carry0 << 21; |
|
| 3573 | + // carry1 = s1 >> 21; |
|
| 3574 | + // s2 += carry1; |
|
| 3575 | + // s1 -= carry1 * ((uint64_t) 1L << 21); |
|
| 3576 | + $carry1 = $s1 >> 21; |
|
| 3577 | + $s2 += $carry1; |
|
| 3578 | + $s1 -= $carry1 << 21; |
|
| 3579 | + // carry2 = s2 >> 21; |
|
| 3580 | + // s3 += carry2; |
|
| 3581 | + // s2 -= carry2 * ((uint64_t) 1L << 21); |
|
| 3582 | + $carry2 = $s2 >> 21; |
|
| 3583 | + $s3 += $carry2; |
|
| 3584 | + $s2 -= $carry2 << 21; |
|
| 3585 | + // carry3 = s3 >> 21; |
|
| 3586 | + // s4 += carry3; |
|
| 3587 | + // s3 -= carry3 * ((uint64_t) 1L << 21); |
|
| 3588 | + $carry3 = $s3 >> 21; |
|
| 3589 | + $s4 += $carry3; |
|
| 3590 | + $s3 -= $carry3 << 21; |
|
| 3591 | + // carry4 = s4 >> 21; |
|
| 3592 | + // s5 += carry4; |
|
| 3593 | + // s4 -= carry4 * ((uint64_t) 1L << 21); |
|
| 3594 | + $carry4 = $s4 >> 21; |
|
| 3595 | + $s5 += $carry4; |
|
| 3596 | + $s4 -= $carry4 << 21; |
|
| 3597 | + // carry5 = s5 >> 21; |
|
| 3598 | + // s6 += carry5; |
|
| 3599 | + // s5 -= carry5 * ((uint64_t) 1L << 21); |
|
| 3600 | + $carry5 = $s5 >> 21; |
|
| 3601 | + $s6 += $carry5; |
|
| 3602 | + $s5 -= $carry5 << 21; |
|
| 3603 | + // carry6 = s6 >> 21; |
|
| 3604 | + // s7 += carry6; |
|
| 3605 | + // s6 -= carry6 * ((uint64_t) 1L << 21); |
|
| 3606 | + $carry6 = $s6 >> 21; |
|
| 3607 | + $s7 += $carry6; |
|
| 3608 | + $s6 -= $carry6 << 21; |
|
| 3609 | + // carry7 = s7 >> 21; |
|
| 3610 | + // s8 += carry7; |
|
| 3611 | + // s7 -= carry7 * ((uint64_t) 1L << 21); |
|
| 3612 | + $carry7 = $s7 >> 21; |
|
| 3613 | + $s8 += $carry7; |
|
| 3614 | + $s7 -= $carry7 << 21; |
|
| 3615 | + // carry8 = s8 >> 21; |
|
| 3616 | + // s9 += carry8; |
|
| 3617 | + // s8 -= carry8 * ((uint64_t) 1L << 21); |
|
| 3618 | + $carry8 = $s8 >> 21; |
|
| 3619 | + $s9 += $carry8; |
|
| 3620 | + $s8 -= $carry8 << 21; |
|
| 3621 | + // carry9 = s9 >> 21; |
|
| 3622 | + // s10 += carry9; |
|
| 3623 | + // s9 -= carry9 * ((uint64_t) 1L << 21); |
|
| 3624 | + $carry9 = $s9 >> 21; |
|
| 3625 | + $s10 += $carry9; |
|
| 3626 | + $s9 -= $carry9 << 21; |
|
| 3627 | + // carry10 = s10 >> 21; |
|
| 3628 | + // s11 += carry10; |
|
| 3629 | + // s10 -= carry10 * ((uint64_t) 1L << 21); |
|
| 3630 | + $carry10 = $s10 >> 21; |
|
| 3631 | + $s11 += $carry10; |
|
| 3632 | + $s10 -= $carry10 << 21; |
|
| 3633 | + |
|
| 3634 | + $s = array_fill(0, 32, 0); |
|
| 3635 | + // s[0] = s0 >> 0; |
|
| 3636 | + $s[0] = $s0 >> 0; |
|
| 3637 | + // s[1] = s0 >> 8; |
|
| 3638 | + $s[1] = $s0 >> 8; |
|
| 3639 | + // s[2] = (s0 >> 16) | (s1 * ((uint64_t) 1 << 5)); |
|
| 3640 | + $s[2] = ($s0 >> 16) | ($s1 << 5); |
|
| 3641 | + // s[3] = s1 >> 3; |
|
| 3642 | + $s[3] = $s1 >> 3; |
|
| 3643 | + // s[4] = s1 >> 11; |
|
| 3644 | + $s[4] = $s1 >> 11; |
|
| 3645 | + // s[5] = (s1 >> 19) | (s2 * ((uint64_t) 1 << 2)); |
|
| 3646 | + $s[5] = ($s1 >> 19) | ($s2 << 2); |
|
| 3647 | + // s[6] = s2 >> 6; |
|
| 3648 | + $s[6] = $s2 >> 6; |
|
| 3649 | + // s[7] = (s2 >> 14) | (s3 * ((uint64_t) 1 << 7)); |
|
| 3650 | + $s[7] = ($s2 >> 14) | ($s3 << 7); |
|
| 3651 | + // s[8] = s3 >> 1; |
|
| 3652 | + $s[8] = $s3 >> 1; |
|
| 3653 | + // s[9] = s3 >> 9; |
|
| 3654 | + $s[9] = $s3 >> 9; |
|
| 3655 | + // s[10] = (s3 >> 17) | (s4 * ((uint64_t) 1 << 4)); |
|
| 3656 | + $s[10] = ($s3 >> 17) | ($s4 << 4); |
|
| 3657 | + // s[11] = s4 >> 4; |
|
| 3658 | + $s[11] = $s4 >> 4; |
|
| 3659 | + // s[12] = s4 >> 12; |
|
| 3660 | + $s[12] = $s4 >> 12; |
|
| 3661 | + // s[13] = (s4 >> 20) | (s5 * ((uint64_t) 1 << 1)); |
|
| 3662 | + $s[13] = ($s4 >> 20) | ($s5 << 1); |
|
| 3663 | + // s[14] = s5 >> 7; |
|
| 3664 | + $s[14] = $s5 >> 7; |
|
| 3665 | + // s[15] = (s5 >> 15) | (s6 * ((uint64_t) 1 << 6)); |
|
| 3666 | + $s[15] = ($s5 >> 15) | ($s6 << 6); |
|
| 3667 | + // s[16] = s6 >> 2; |
|
| 3668 | + $s[16] = $s6 >> 2; |
|
| 3669 | + // s[17] = s6 >> 10; |
|
| 3670 | + $s[17] = $s6 >> 10; |
|
| 3671 | + // s[18] = (s6 >> 18) | (s7 * ((uint64_t) 1 << 3)); |
|
| 3672 | + $s[18] = ($s6 >> 18) | ($s7 << 3); |
|
| 3673 | + // s[19] = s7 >> 5; |
|
| 3674 | + $s[19] = $s7 >> 5; |
|
| 3675 | + // s[20] = s7 >> 13; |
|
| 3676 | + $s[20] = $s7 >> 13; |
|
| 3677 | + // s[21] = s8 >> 0; |
|
| 3678 | + $s[21] = $s8 >> 0; |
|
| 3679 | + // s[22] = s8 >> 8; |
|
| 3680 | + $s[22] = $s8 >> 8; |
|
| 3681 | + // s[23] = (s8 >> 16) | (s9 * ((uint64_t) 1 << 5)); |
|
| 3682 | + $s[23] = ($s8 >> 16) | ($s9 << 5); |
|
| 3683 | + // s[24] = s9 >> 3; |
|
| 3684 | + $s[24] = $s9 >> 3; |
|
| 3685 | + // s[25] = s9 >> 11; |
|
| 3686 | + $s[25] = $s9 >> 11; |
|
| 3687 | + // s[26] = (s9 >> 19) | (s10 * ((uint64_t) 1 << 2)); |
|
| 3688 | + $s[26] = ($s9 >> 19) | ($s10 << 2); |
|
| 3689 | + // s[27] = s10 >> 6; |
|
| 3690 | + $s[27] = $s10 >> 6; |
|
| 3691 | + // s[28] = (s10 >> 14) | (s11 * ((uint64_t) 1 << 7)); |
|
| 3692 | + $s[28] = ($s10 >> 14) | ($s11 << 7); |
|
| 3693 | + // s[29] = s11 >> 1; |
|
| 3694 | + $s[29] = $s11 >> 1; |
|
| 3695 | + // s[30] = s11 >> 9; |
|
| 3696 | + $s[30] = $s11 >> 9; |
|
| 3697 | + // s[31] = s11 >> 17; |
|
| 3698 | + $s[31] = $s11 >> 17; |
|
| 3699 | + return self::intArrayToString($s); |
|
| 3700 | + } |
|
| 3701 | + |
|
| 3702 | + /** |
|
| 3703 | + * @param string $s |
|
| 3704 | + * @return string |
|
| 3705 | + */ |
|
| 3706 | + public static function sc25519_sq($s) |
|
| 3707 | + { |
|
| 3708 | + return self::sc25519_mul($s, $s); |
|
| 3709 | + } |
|
| 3710 | + |
|
| 3711 | + /** |
|
| 3712 | + * @param string $s |
|
| 3713 | + * @param int $n |
|
| 3714 | + * @param string $a |
|
| 3715 | + * @return string |
|
| 3716 | + */ |
|
| 3717 | + public static function sc25519_sqmul($s, $n, $a) |
|
| 3718 | + { |
|
| 3719 | + for ($i = 0; $i < $n; ++$i) { |
|
| 3720 | + $s = self::sc25519_sq($s); |
|
| 3721 | + } |
|
| 3722 | + return self::sc25519_mul($s, $a); |
|
| 3723 | + } |
|
| 3724 | + |
|
| 3725 | + /** |
|
| 3726 | + * @param string $s |
|
| 3727 | + * @return string |
|
| 3728 | + */ |
|
| 3729 | + public static function sc25519_invert($s) |
|
| 3730 | + { |
|
| 3731 | + $_10 = self::sc25519_sq($s); |
|
| 3732 | + $_11 = self::sc25519_mul($s, $_10); |
|
| 3733 | + $_100 = self::sc25519_mul($s, $_11); |
|
| 3734 | + $_1000 = self::sc25519_sq($_100); |
|
| 3735 | + $_1010 = self::sc25519_mul($_10, $_1000); |
|
| 3736 | + $_1011 = self::sc25519_mul($s, $_1010); |
|
| 3737 | + $_10000 = self::sc25519_sq($_1000); |
|
| 3738 | + $_10110 = self::sc25519_sq($_1011); |
|
| 3739 | + $_100000 = self::sc25519_mul($_1010, $_10110); |
|
| 3740 | + $_100110 = self::sc25519_mul($_10000, $_10110); |
|
| 3741 | + $_1000000 = self::sc25519_sq($_100000); |
|
| 3742 | + $_1010000 = self::sc25519_mul($_10000, $_1000000); |
|
| 3743 | + $_1010011 = self::sc25519_mul($_11, $_1010000); |
|
| 3744 | + $_1100011 = self::sc25519_mul($_10000, $_1010011); |
|
| 3745 | + $_1100111 = self::sc25519_mul($_100, $_1100011); |
|
| 3746 | + $_1101011 = self::sc25519_mul($_100, $_1100111); |
|
| 3747 | + $_10010011 = self::sc25519_mul($_1000000, $_1010011); |
|
| 3748 | + $_10010111 = self::sc25519_mul($_100, $_10010011); |
|
| 3749 | + $_10111101 = self::sc25519_mul($_100110, $_10010111); |
|
| 3750 | + $_11010011 = self::sc25519_mul($_10110, $_10111101); |
|
| 3751 | + $_11100111 = self::sc25519_mul($_1010000, $_10010111); |
|
| 3752 | + $_11101011 = self::sc25519_mul($_100, $_11100111); |
|
| 3753 | + $_11110101 = self::sc25519_mul($_1010, $_11101011); |
|
| 3754 | + |
|
| 3755 | + $recip = self::sc25519_mul($_1011, $_11110101); |
|
| 3756 | + $recip = self::sc25519_sqmul($recip, 126, $_1010011); |
|
| 3757 | + $recip = self::sc25519_sqmul($recip, 9, $_10); |
|
| 3758 | + $recip = self::sc25519_mul($recip, $_11110101); |
|
| 3759 | + $recip = self::sc25519_sqmul($recip, 7, $_1100111); |
|
| 3760 | + $recip = self::sc25519_sqmul($recip, 9, $_11110101); |
|
| 3761 | + $recip = self::sc25519_sqmul($recip, 11, $_10111101); |
|
| 3762 | + $recip = self::sc25519_sqmul($recip, 8, $_11100111); |
|
| 3763 | + $recip = self::sc25519_sqmul($recip, 9, $_1101011); |
|
| 3764 | + $recip = self::sc25519_sqmul($recip, 6, $_1011); |
|
| 3765 | + $recip = self::sc25519_sqmul($recip, 14, $_10010011); |
|
| 3766 | + $recip = self::sc25519_sqmul($recip, 10, $_1100011); |
|
| 3767 | + $recip = self::sc25519_sqmul($recip, 9, $_10010111); |
|
| 3768 | + $recip = self::sc25519_sqmul($recip, 10, $_11110101); |
|
| 3769 | + $recip = self::sc25519_sqmul($recip, 8, $_11010011); |
|
| 3770 | + return self::sc25519_sqmul($recip, 8, $_11101011); |
|
| 3771 | + } |
|
| 3772 | + |
|
| 3773 | + /** |
|
| 3774 | + * @param string $s |
|
| 3775 | + * @return string |
|
| 3776 | + */ |
|
| 3777 | + public static function clamp($s) |
|
| 3778 | + { |
|
| 3779 | + $s_ = self::stringToIntArray($s); |
|
| 3780 | + $s_[0] &= 248; |
|
| 3781 | + $s_[31] |= 64; |
|
| 3782 | + $s_[31] &= 128; |
|
| 3783 | + return self::intArrayToString($s_); |
|
| 3784 | + } |
|
| 3785 | 3785 | } |
@@ -5,703 +5,703 @@ |
||
| 5 | 5 | */ |
| 6 | 6 | class ParagonIE_Sodium_Core_Ristretto255 extends ParagonIE_Sodium_Core_Ed25519 |
| 7 | 7 | { |
| 8 | - const crypto_core_ristretto255_HASHBYTES = 64; |
|
| 9 | - const HASH_SC_L = 48; |
|
| 10 | - const CORE_H2C_SHA256 = 1; |
|
| 11 | - const CORE_H2C_SHA512 = 2; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 15 | - * @param int $b |
|
| 16 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 17 | - */ |
|
| 18 | - public static function fe_cneg(ParagonIE_Sodium_Core_Curve25519_Fe $f, $b) |
|
| 19 | - { |
|
| 20 | - $negf = self::fe_neg($f); |
|
| 21 | - return self::fe_cmov($f, $negf, $b); |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 26 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 27 | - * @throws SodiumException |
|
| 28 | - */ |
|
| 29 | - public static function fe_abs(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 30 | - { |
|
| 31 | - return self::fe_cneg($f, self::fe_isnegative($f)); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Returns 0 if this field element results in all NUL bytes. |
|
| 36 | - * |
|
| 37 | - * @internal You should not use this directly from another application |
|
| 38 | - * |
|
| 39 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 40 | - * @return int |
|
| 41 | - * @throws SodiumException |
|
| 42 | - */ |
|
| 43 | - public static function fe_iszero(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 44 | - { |
|
| 45 | - static $zero; |
|
| 46 | - if ($zero === null) { |
|
| 47 | - $zero = str_repeat("\x00", 32); |
|
| 48 | - } |
|
| 49 | - /** @var string $zero */ |
|
| 50 | - $str = self::fe_tobytes($f); |
|
| 51 | - |
|
| 52 | - $d = 0; |
|
| 53 | - for ($i = 0; $i < 32; ++$i) { |
|
| 54 | - $d |= self::chrToInt($str[$i]); |
|
| 55 | - } |
|
| 56 | - return (($d - 1) >> 31) & 1; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $u |
|
| 62 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $v |
|
| 63 | - * @return array{x: ParagonIE_Sodium_Core_Curve25519_Fe, nonsquare: int} |
|
| 64 | - * |
|
| 65 | - * @throws SodiumException |
|
| 66 | - */ |
|
| 67 | - public static function ristretto255_sqrt_ratio_m1( |
|
| 68 | - ParagonIE_Sodium_Core_Curve25519_Fe $u, |
|
| 69 | - ParagonIE_Sodium_Core_Curve25519_Fe $v |
|
| 70 | - ) { |
|
| 71 | - $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); |
|
| 72 | - |
|
| 73 | - $v3 = self::fe_mul( |
|
| 74 | - self::fe_sq($v), |
|
| 75 | - $v |
|
| 76 | - ); /* v3 = v^3 */ |
|
| 77 | - $x = self::fe_mul( |
|
| 78 | - self::fe_mul( |
|
| 79 | - self::fe_sq($v3), |
|
| 80 | - $u |
|
| 81 | - ), |
|
| 82 | - $v |
|
| 83 | - ); /* x = uv^7 */ |
|
| 84 | - |
|
| 85 | - $x = self::fe_mul( |
|
| 86 | - self::fe_mul( |
|
| 87 | - self::fe_pow22523($x), /* x = (uv^7)^((q-5)/8) */ |
|
| 88 | - $v3 |
|
| 89 | - ), |
|
| 90 | - $u |
|
| 91 | - ); /* x = uv^3(uv^7)^((q-5)/8) */ |
|
| 92 | - |
|
| 93 | - $vxx = self::fe_mul( |
|
| 94 | - self::fe_sq($x), |
|
| 95 | - $v |
|
| 96 | - ); /* vx^2 */ |
|
| 97 | - |
|
| 98 | - $m_root_check = self::fe_sub($vxx, $u); /* vx^2-u */ |
|
| 99 | - $p_root_check = self::fe_add($vxx, $u); /* vx^2+u */ |
|
| 100 | - $f_root_check = self::fe_mul($u, $sqrtm1); /* u*sqrt(-1) */ |
|
| 101 | - $f_root_check = self::fe_add($vxx, $f_root_check); /* vx^2+u*sqrt(-1) */ |
|
| 102 | - |
|
| 103 | - $has_m_root = self::fe_iszero($m_root_check); |
|
| 104 | - $has_p_root = self::fe_iszero($p_root_check); |
|
| 105 | - $has_f_root = self::fe_iszero($f_root_check); |
|
| 106 | - |
|
| 107 | - $x_sqrtm1 = self::fe_mul($x, $sqrtm1); /* x*sqrt(-1) */ |
|
| 108 | - |
|
| 109 | - $x = self::fe_abs( |
|
| 110 | - self::fe_cmov($x, $x_sqrtm1, $has_p_root | $has_f_root) |
|
| 111 | - ); |
|
| 112 | - return array( |
|
| 113 | - 'x' => $x, |
|
| 114 | - 'nonsquare' => $has_m_root | $has_p_root |
|
| 115 | - ); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param string $s |
|
| 120 | - * @return int |
|
| 121 | - * @throws SodiumException |
|
| 122 | - */ |
|
| 123 | - public static function ristretto255_point_is_canonical($s) |
|
| 124 | - { |
|
| 125 | - $c = (self::chrToInt($s[31]) & 0x7f) ^ 0x7f; |
|
| 126 | - for ($i = 30; $i > 0; --$i) { |
|
| 127 | - $c |= self::chrToInt($s[$i]) ^ 0xff; |
|
| 128 | - } |
|
| 129 | - $c = ($c - 1) >> 8; |
|
| 130 | - $d = (0xed - 1 - self::chrToInt($s[0])) >> 8; |
|
| 131 | - $e = self::chrToInt($s[31]) >> 7; |
|
| 132 | - |
|
| 133 | - return 1 - ((($c & $d) | $e | self::chrToInt($s[0])) & 1); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * @param string $s |
|
| 138 | - * @param bool $skipCanonicalCheck |
|
| 139 | - * @return array{h: ParagonIE_Sodium_Core_Curve25519_Ge_P3, res: int} |
|
| 140 | - * @throws SodiumException |
|
| 141 | - */ |
|
| 142 | - public static function ristretto255_frombytes($s, $skipCanonicalCheck = false) |
|
| 143 | - { |
|
| 144 | - if (!$skipCanonicalCheck) { |
|
| 145 | - if (!self::ristretto255_point_is_canonical($s)) { |
|
| 146 | - throw new SodiumException('S is not canonical'); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $s_ = self::fe_frombytes($s); |
|
| 151 | - $ss = self::fe_sq($s_); /* ss = s^2 */ |
|
| 152 | - |
|
| 153 | - $u1 = self::fe_sub(self::fe_1(), $ss); /* u1 = 1-ss */ |
|
| 154 | - $u1u1 = self::fe_sq($u1); /* u1u1 = u1^2 */ |
|
| 155 | - |
|
| 156 | - $u2 = self::fe_add(self::fe_1(), $ss); /* u2 = 1+ss */ |
|
| 157 | - $u2u2 = self::fe_sq($u2); /* u2u2 = u2^2 */ |
|
| 158 | - |
|
| 159 | - $v = self::fe_mul( |
|
| 160 | - ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d), |
|
| 161 | - $u1u1 |
|
| 162 | - ); /* v = d*u1^2 */ |
|
| 163 | - $v = self::fe_neg($v); /* v = -d*u1^2 */ |
|
| 164 | - $v = self::fe_sub($v, $u2u2); /* v = -(d*u1^2)-u2^2 */ |
|
| 165 | - $v_u2u2 = self::fe_mul($v, $u2u2); /* v_u2u2 = v*u2^2 */ |
|
| 166 | - |
|
| 167 | - // fe25519_1(one); |
|
| 168 | - // notsquare = ristretto255_sqrt_ratio_m1(inv_sqrt, one, v_u2u2); |
|
| 169 | - $one = self::fe_1(); |
|
| 170 | - $result = self::ristretto255_sqrt_ratio_m1($one, $v_u2u2); |
|
| 171 | - $inv_sqrt = $result['x']; |
|
| 172 | - $notsquare = $result['nonsquare']; |
|
| 173 | - |
|
| 174 | - $h = new ParagonIE_Sodium_Core_Curve25519_Ge_P3(); |
|
| 175 | - |
|
| 176 | - $h->X = self::fe_mul($inv_sqrt, $u2); |
|
| 177 | - $h->Y = self::fe_mul(self::fe_mul($inv_sqrt, $h->X), $v); |
|
| 178 | - |
|
| 179 | - $h->X = self::fe_mul($h->X, $s_); |
|
| 180 | - $h->X = self::fe_abs( |
|
| 181 | - self::fe_add($h->X, $h->X) |
|
| 182 | - ); |
|
| 183 | - $h->Y = self::fe_mul($u1, $h->Y); |
|
| 184 | - $h->Z = self::fe_1(); |
|
| 185 | - $h->T = self::fe_mul($h->X, $h->Y); |
|
| 186 | - |
|
| 187 | - $res = - ((1 - $notsquare) | self::fe_isnegative($h->T) | self::fe_iszero($h->Y)); |
|
| 188 | - return array('h' => $h, 'res' => $res); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h |
|
| 193 | - * @return string |
|
| 194 | - * @throws SodiumException |
|
| 195 | - */ |
|
| 196 | - public static function ristretto255_p3_tobytes(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h) |
|
| 197 | - { |
|
| 198 | - $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); |
|
| 199 | - $invsqrtamd = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$invsqrtamd); |
|
| 200 | - |
|
| 201 | - $u1 = self::fe_add($h->Z, $h->Y); /* u1 = Z+Y */ |
|
| 202 | - $zmy = self::fe_sub($h->Z, $h->Y); /* zmy = Z-Y */ |
|
| 203 | - $u1 = self::fe_mul($u1, $zmy); /* u1 = (Z+Y)*(Z-Y) */ |
|
| 204 | - $u2 = self::fe_mul($h->X, $h->Y); /* u2 = X*Y */ |
|
| 205 | - |
|
| 206 | - $u1_u2u2 = self::fe_mul(self::fe_sq($u2), $u1); /* u1_u2u2 = u1*u2^2 */ |
|
| 207 | - $one = self::fe_1(); |
|
| 208 | - |
|
| 209 | - // fe25519_1(one); |
|
| 210 | - // (void) ristretto255_sqrt_ratio_m1(inv_sqrt, one, u1_u2u2); |
|
| 211 | - $result = self::ristretto255_sqrt_ratio_m1($one, $u1_u2u2); |
|
| 212 | - $inv_sqrt = $result['x']; |
|
| 213 | - |
|
| 214 | - $den1 = self::fe_mul($inv_sqrt, $u1); /* den1 = inv_sqrt*u1 */ |
|
| 215 | - $den2 = self::fe_mul($inv_sqrt, $u2); /* den2 = inv_sqrt*u2 */ |
|
| 216 | - $z_inv = self::fe_mul($h->T, self::fe_mul($den1, $den2)); /* z_inv = den1*den2*T */ |
|
| 217 | - |
|
| 218 | - $ix = self::fe_mul($h->X, $sqrtm1); /* ix = X*sqrt(-1) */ |
|
| 219 | - $iy = self::fe_mul($h->Y, $sqrtm1); /* iy = Y*sqrt(-1) */ |
|
| 220 | - $eden = self::fe_mul($den1, $invsqrtamd); |
|
| 221 | - |
|
| 222 | - $t_z_inv = self::fe_mul($h->T, $z_inv); /* t_z_inv = T*z_inv */ |
|
| 223 | - $rotate = self::fe_isnegative($t_z_inv); |
|
| 224 | - |
|
| 225 | - $x_ = self::fe_copy($h->X); |
|
| 226 | - $y_ = self::fe_copy($h->Y); |
|
| 227 | - $den_inv = self::fe_copy($den2); |
|
| 228 | - |
|
| 229 | - $x_ = self::fe_cmov($x_, $iy, $rotate); |
|
| 230 | - $y_ = self::fe_cmov($y_, $ix, $rotate); |
|
| 231 | - $den_inv = self::fe_cmov($den_inv, $eden, $rotate); |
|
| 232 | - |
|
| 233 | - $x_z_inv = self::fe_mul($x_, $z_inv); |
|
| 234 | - $y_ = self::fe_cneg($y_, self::fe_isnegative($x_z_inv)); |
|
| 235 | - |
|
| 236 | - |
|
| 237 | - // fe25519_sub(s_, h->Z, y_); |
|
| 238 | - // fe25519_mul(s_, den_inv, s_); |
|
| 239 | - // fe25519_abs(s_, s_); |
|
| 240 | - // fe25519_tobytes(s, s_); |
|
| 241 | - return self::fe_tobytes( |
|
| 242 | - self::fe_abs( |
|
| 243 | - self::fe_mul( |
|
| 244 | - $den_inv, |
|
| 245 | - self::fe_sub($h->Z, $y_) |
|
| 246 | - ) |
|
| 247 | - ) |
|
| 248 | - ); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $t |
|
| 253 | - * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 254 | - * |
|
| 255 | - * @throws SodiumException |
|
| 256 | - */ |
|
| 257 | - public static function ristretto255_elligator(ParagonIE_Sodium_Core_Curve25519_Fe $t) |
|
| 258 | - { |
|
| 259 | - $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); |
|
| 260 | - $onemsqd = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$onemsqd); |
|
| 261 | - $d = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d); |
|
| 262 | - $sqdmone = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqdmone); |
|
| 263 | - $sqrtadm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtadm1); |
|
| 264 | - |
|
| 265 | - $one = self::fe_1(); |
|
| 266 | - $r = self::fe_mul($sqrtm1, self::fe_sq($t)); /* r = sqrt(-1)*t^2 */ |
|
| 267 | - $u = self::fe_mul(self::fe_add($r, $one), $onemsqd); /* u = (r+1)*(1-d^2) */ |
|
| 268 | - $c = self::fe_neg(self::fe_1()); /* c = -1 */ |
|
| 269 | - $rpd = self::fe_add($r, $d); /* rpd = r+d */ |
|
| 270 | - |
|
| 271 | - $v = self::fe_mul( |
|
| 272 | - self::fe_sub( |
|
| 273 | - $c, |
|
| 274 | - self::fe_mul($r, $d) |
|
| 275 | - ), |
|
| 276 | - $rpd |
|
| 277 | - ); /* v = (c-r*d)*(r+d) */ |
|
| 278 | - |
|
| 279 | - $result = self::ristretto255_sqrt_ratio_m1($u, $v); |
|
| 280 | - $s = $result['x']; |
|
| 281 | - $wasnt_square = 1 - $result['nonsquare']; |
|
| 282 | - |
|
| 283 | - $s_prime = self::fe_neg( |
|
| 284 | - self::fe_abs( |
|
| 285 | - self::fe_mul($s, $t) |
|
| 286 | - ) |
|
| 287 | - ); /* s_prime = -|s*t| */ |
|
| 288 | - $s = self::fe_cmov($s, $s_prime, $wasnt_square); |
|
| 289 | - $c = self::fe_cmov($c, $r, $wasnt_square); |
|
| 290 | - |
|
| 291 | - // fe25519_sub(n, r, one); /* n = r-1 */ |
|
| 292 | - // fe25519_mul(n, n, c); /* n = c*(r-1) */ |
|
| 293 | - // fe25519_mul(n, n, ed25519_sqdmone); /* n = c*(r-1)*(d-1)^2 */ |
|
| 294 | - // fe25519_sub(n, n, v); /* n = c*(r-1)*(d-1)^2-v */ |
|
| 295 | - $n = self::fe_sub( |
|
| 296 | - self::fe_mul( |
|
| 297 | - self::fe_mul( |
|
| 298 | - self::fe_sub($r, $one), |
|
| 299 | - $c |
|
| 300 | - ), |
|
| 301 | - $sqdmone |
|
| 302 | - ), |
|
| 303 | - $v |
|
| 304 | - ); /* n = c*(r-1)*(d-1)^2-v */ |
|
| 305 | - |
|
| 306 | - $w0 = self::fe_mul( |
|
| 307 | - self::fe_add($s, $s), |
|
| 308 | - $v |
|
| 309 | - ); /* w0 = 2s*v */ |
|
| 310 | - |
|
| 311 | - $w1 = self::fe_mul($n, $sqrtadm1); /* w1 = n*sqrt(ad-1) */ |
|
| 312 | - $ss = self::fe_sq($s); /* ss = s^2 */ |
|
| 313 | - $w2 = self::fe_sub($one, $ss); /* w2 = 1-s^2 */ |
|
| 314 | - $w3 = self::fe_add($one, $ss); /* w3 = 1+s^2 */ |
|
| 315 | - |
|
| 316 | - return new ParagonIE_Sodium_Core_Curve25519_Ge_P3( |
|
| 317 | - self::fe_mul($w0, $w3), |
|
| 318 | - self::fe_mul($w2, $w1), |
|
| 319 | - self::fe_mul($w1, $w3), |
|
| 320 | - self::fe_mul($w0, $w2) |
|
| 321 | - ); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * @param string $h |
|
| 326 | - * @return string |
|
| 327 | - * @throws SodiumException |
|
| 328 | - */ |
|
| 329 | - public static function ristretto255_from_hash($h) |
|
| 330 | - { |
|
| 331 | - if (self::strlen($h) !== 64) { |
|
| 332 | - throw new SodiumException('Hash must be 64 bytes'); |
|
| 333 | - } |
|
| 334 | - //fe25519_frombytes(r0, h); |
|
| 335 | - //fe25519_frombytes(r1, h + 32); |
|
| 336 | - $r0 = self::fe_frombytes(self::substr($h, 0, 32)); |
|
| 337 | - $r1 = self::fe_frombytes(self::substr($h, 32, 32)); |
|
| 338 | - |
|
| 339 | - //ristretto255_elligator(&p0, r0); |
|
| 340 | - //ristretto255_elligator(&p1, r1); |
|
| 341 | - $p0 = self::ristretto255_elligator($r0); |
|
| 342 | - $p1 = self::ristretto255_elligator($r1); |
|
| 343 | - |
|
| 344 | - //ge25519_p3_to_cached(&p1_cached, &p1); |
|
| 345 | - //ge25519_add_cached(&p_p1p1, &p0, &p1_cached); |
|
| 346 | - $p_p1p1 = self::ge_add( |
|
| 347 | - $p0, |
|
| 348 | - self::ge_p3_to_cached($p1) |
|
| 349 | - ); |
|
| 350 | - |
|
| 351 | - //ge25519_p1p1_to_p3(&p, &p_p1p1); |
|
| 352 | - //ristretto255_p3_tobytes(s, &p); |
|
| 353 | - return self::ristretto255_p3_tobytes( |
|
| 354 | - self::ge_p1p1_to_p3($p_p1p1) |
|
| 355 | - ); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * @param string $p |
|
| 360 | - * @return int |
|
| 361 | - * @throws SodiumException |
|
| 362 | - */ |
|
| 363 | - public static function is_valid_point($p) |
|
| 364 | - { |
|
| 365 | - $result = self::ristretto255_frombytes($p); |
|
| 366 | - if ($result['res'] !== 0) { |
|
| 367 | - return 0; |
|
| 368 | - } |
|
| 369 | - return 1; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * @param string $p |
|
| 374 | - * @param string $q |
|
| 375 | - * @return string |
|
| 376 | - * @throws SodiumException |
|
| 377 | - */ |
|
| 378 | - public static function ristretto255_add($p, $q) |
|
| 379 | - { |
|
| 380 | - $p_res = self::ristretto255_frombytes($p); |
|
| 381 | - $q_res = self::ristretto255_frombytes($q); |
|
| 382 | - if ($p_res['res'] !== 0 || $q_res['res'] !== 0) { |
|
| 383 | - throw new SodiumException('Could not add points'); |
|
| 384 | - } |
|
| 385 | - $p_p3 = $p_res['h']; |
|
| 386 | - $q_p3 = $q_res['h']; |
|
| 387 | - $q_cached = self::ge_p3_to_cached($q_p3); |
|
| 388 | - $r_p1p1 = self::ge_add($p_p3, $q_cached); |
|
| 389 | - $r_p3 = self::ge_p1p1_to_p3($r_p1p1); |
|
| 390 | - return self::ristretto255_p3_tobytes($r_p3); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * @param string $p |
|
| 395 | - * @param string $q |
|
| 396 | - * @return string |
|
| 397 | - * @throws SodiumException |
|
| 398 | - */ |
|
| 399 | - public static function ristretto255_sub($p, $q) |
|
| 400 | - { |
|
| 401 | - $p_res = self::ristretto255_frombytes($p); |
|
| 402 | - $q_res = self::ristretto255_frombytes($q); |
|
| 403 | - if ($p_res['res'] !== 0 || $q_res['res'] !== 0) { |
|
| 404 | - throw new SodiumException('Could not add points'); |
|
| 405 | - } |
|
| 406 | - $p_p3 = $p_res['h']; |
|
| 407 | - $q_p3 = $q_res['h']; |
|
| 408 | - $q_cached = self::ge_p3_to_cached($q_p3); |
|
| 409 | - $r_p1p1 = self::ge_sub($p_p3, $q_cached); |
|
| 410 | - $r_p3 = self::ge_p1p1_to_p3($r_p1p1); |
|
| 411 | - return self::ristretto255_p3_tobytes($r_p3); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * @param int $hLen |
|
| 417 | - * @param ?string $ctx |
|
| 418 | - * @param string $msg |
|
| 419 | - * @return string |
|
| 420 | - * @throws SodiumException |
|
| 421 | - * @psalm-suppress PossiblyInvalidArgument hash API |
|
| 422 | - */ |
|
| 423 | - protected static function h2c_string_to_hash_sha256($hLen, $ctx, $msg) |
|
| 424 | - { |
|
| 425 | - $h = array_fill(0, $hLen, 0); |
|
| 426 | - $ctx_len = !is_null($ctx) ? self::strlen($ctx) : 0; |
|
| 427 | - if ($hLen > 0xff) { |
|
| 428 | - throw new SodiumException('Hash must be less than 256 bytes'); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - if ($ctx_len > 0xff) { |
|
| 432 | - $st = hash_init('sha256'); |
|
| 433 | - self::hash_update($st, "H2C-OVERSIZE-DST-"); |
|
| 434 | - self::hash_update($st, $ctx); |
|
| 435 | - $ctx = hash_final($st, true); |
|
| 436 | - $ctx_len = 32; |
|
| 437 | - } |
|
| 438 | - $t = array(0, $hLen, 0); |
|
| 439 | - $ux = str_repeat("\0", 64); |
|
| 440 | - $st = hash_init('sha256'); |
|
| 441 | - self::hash_update($st, $ux); |
|
| 442 | - self::hash_update($st, $msg); |
|
| 443 | - self::hash_update($st, self::intArrayToString($t)); |
|
| 444 | - self::hash_update($st, $ctx); |
|
| 445 | - self::hash_update($st, self::intToChr($ctx_len)); |
|
| 446 | - $u0 = hash_final($st, true); |
|
| 447 | - |
|
| 448 | - for ($i = 0; $i < $hLen; $i += 64) { |
|
| 449 | - $ux = self::xorStrings($ux, $u0); |
|
| 450 | - ++$t[2]; |
|
| 451 | - $st = hash_init('sha256'); |
|
| 452 | - self::hash_update($st, $ux); |
|
| 453 | - self::hash_update($st, self::intToChr($t[2])); |
|
| 454 | - self::hash_update($st, $ctx); |
|
| 455 | - self::hash_update($st, self::intToChr($ctx_len)); |
|
| 456 | - $ux = hash_final($st, true); |
|
| 457 | - $amount = min($hLen - $i, 64); |
|
| 458 | - for ($j = 0; $j < $amount; ++$j) { |
|
| 459 | - $h[$i + $j] = self::chrToInt($ux[$i]); |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - return self::intArrayToString(array_slice($h, 0, $hLen)); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * @param int $hLen |
|
| 467 | - * @param ?string $ctx |
|
| 468 | - * @param string $msg |
|
| 469 | - * @return string |
|
| 470 | - * @throws SodiumException |
|
| 471 | - * @psalm-suppress PossiblyInvalidArgument hash API |
|
| 472 | - */ |
|
| 473 | - protected static function h2c_string_to_hash_sha512($hLen, $ctx, $msg) |
|
| 474 | - { |
|
| 475 | - $h = array_fill(0, $hLen, 0); |
|
| 476 | - $ctx_len = !is_null($ctx) ? self::strlen($ctx) : 0; |
|
| 477 | - if ($hLen > 0xff) { |
|
| 478 | - throw new SodiumException('Hash must be less than 256 bytes'); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - if ($ctx_len > 0xff) { |
|
| 482 | - $st = hash_init('sha256'); |
|
| 483 | - self::hash_update($st, "H2C-OVERSIZE-DST-"); |
|
| 484 | - self::hash_update($st, $ctx); |
|
| 485 | - $ctx = hash_final($st, true); |
|
| 486 | - $ctx_len = 32; |
|
| 487 | - } |
|
| 488 | - $t = array(0, $hLen, 0); |
|
| 489 | - $ux = str_repeat("\0", 128); |
|
| 490 | - $st = hash_init('sha512'); |
|
| 491 | - self::hash_update($st, $ux); |
|
| 492 | - self::hash_update($st, $msg); |
|
| 493 | - self::hash_update($st, self::intArrayToString($t)); |
|
| 494 | - self::hash_update($st, $ctx); |
|
| 495 | - self::hash_update($st, self::intToChr($ctx_len)); |
|
| 496 | - $u0 = hash_final($st, true); |
|
| 497 | - |
|
| 498 | - for ($i = 0; $i < $hLen; $i += 128) { |
|
| 499 | - $ux = self::xorStrings($ux, $u0); |
|
| 500 | - ++$t[2]; |
|
| 501 | - $st = hash_init('sha512'); |
|
| 502 | - self::hash_update($st, $ux); |
|
| 503 | - self::hash_update($st, self::intToChr($t[2])); |
|
| 504 | - self::hash_update($st, $ctx); |
|
| 505 | - self::hash_update($st, self::intToChr($ctx_len)); |
|
| 506 | - $ux = hash_final($st, true); |
|
| 507 | - $amount = min($hLen - $i, 128); |
|
| 508 | - for ($j = 0; $j < $amount; ++$j) { |
|
| 509 | - $h[$i + $j] = self::chrToInt($ux[$i]); |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - return self::intArrayToString(array_slice($h, 0, $hLen)); |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * @param int $hLen |
|
| 517 | - * @param ?string $ctx |
|
| 518 | - * @param string $msg |
|
| 519 | - * @param int $hash_alg |
|
| 520 | - * @return string |
|
| 521 | - * @throws SodiumException |
|
| 522 | - */ |
|
| 523 | - public static function h2c_string_to_hash($hLen, $ctx, $msg, $hash_alg) |
|
| 524 | - { |
|
| 525 | - switch ($hash_alg) { |
|
| 526 | - case self::CORE_H2C_SHA256: |
|
| 527 | - return self::h2c_string_to_hash_sha256($hLen, $ctx, $msg); |
|
| 528 | - case self::CORE_H2C_SHA512: |
|
| 529 | - return self::h2c_string_to_hash_sha512($hLen, $ctx, $msg); |
|
| 530 | - default: |
|
| 531 | - throw new SodiumException('Invalid H2C hash algorithm'); |
|
| 532 | - } |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - /** |
|
| 536 | - * @param ?string $ctx |
|
| 537 | - * @param string $msg |
|
| 538 | - * @param int $hash_alg |
|
| 539 | - * @return string |
|
| 540 | - * @throws SodiumException |
|
| 541 | - */ |
|
| 542 | - protected static function _string_to_element($ctx, $msg, $hash_alg) |
|
| 543 | - { |
|
| 544 | - return self::ristretto255_from_hash( |
|
| 545 | - self::h2c_string_to_hash(self::crypto_core_ristretto255_HASHBYTES, $ctx, $msg, $hash_alg) |
|
| 546 | - ); |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - /** |
|
| 550 | - * @return string |
|
| 551 | - * @throws SodiumException |
|
| 552 | - * @throws Exception |
|
| 553 | - */ |
|
| 554 | - public static function ristretto255_random() |
|
| 555 | - { |
|
| 556 | - return self::ristretto255_from_hash( |
|
| 557 | - ParagonIE_Sodium_Compat::randombytes_buf(self::crypto_core_ristretto255_HASHBYTES) |
|
| 558 | - ); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * @return string |
|
| 563 | - * @throws SodiumException |
|
| 564 | - */ |
|
| 565 | - public static function ristretto255_scalar_random() |
|
| 566 | - { |
|
| 567 | - return self::scalar_random(); |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * @param string $s |
|
| 572 | - * @return string |
|
| 573 | - * @throws SodiumException |
|
| 574 | - */ |
|
| 575 | - public static function ristretto255_scalar_complement($s) |
|
| 576 | - { |
|
| 577 | - return self::scalar_complement($s); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * @param string $s |
|
| 583 | - * @return string |
|
| 584 | - */ |
|
| 585 | - public static function ristretto255_scalar_invert($s) |
|
| 586 | - { |
|
| 587 | - return self::sc25519_invert($s); |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * @param string $s |
|
| 592 | - * @return string |
|
| 593 | - * @throws SodiumException |
|
| 594 | - */ |
|
| 595 | - public static function ristretto255_scalar_negate($s) |
|
| 596 | - { |
|
| 597 | - return self::scalar_negate($s); |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - /** |
|
| 601 | - * @param string $x |
|
| 602 | - * @param string $y |
|
| 603 | - * @return string |
|
| 604 | - */ |
|
| 605 | - public static function ristretto255_scalar_add($x, $y) |
|
| 606 | - { |
|
| 607 | - return self::scalar_add($x, $y); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * @param string $x |
|
| 612 | - * @param string $y |
|
| 613 | - * @return string |
|
| 614 | - */ |
|
| 615 | - public static function ristretto255_scalar_sub($x, $y) |
|
| 616 | - { |
|
| 617 | - return self::scalar_sub($x, $y); |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * @param string $x |
|
| 622 | - * @param string $y |
|
| 623 | - * @return string |
|
| 624 | - */ |
|
| 625 | - public static function ristretto255_scalar_mul($x, $y) |
|
| 626 | - { |
|
| 627 | - return self::sc25519_mul($x, $y); |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - /** |
|
| 631 | - * @param string $ctx |
|
| 632 | - * @param string $msg |
|
| 633 | - * @param int $hash_alg |
|
| 634 | - * @return string |
|
| 635 | - * @throws SodiumException |
|
| 636 | - */ |
|
| 637 | - public static function ristretto255_scalar_from_string($ctx, $msg, $hash_alg) |
|
| 638 | - { |
|
| 639 | - $h = array_fill(0, 64, 0); |
|
| 640 | - $h_be = self::stringToIntArray( |
|
| 641 | - self::h2c_string_to_hash( |
|
| 642 | - self::HASH_SC_L, $ctx, $msg, $hash_alg |
|
| 643 | - ) |
|
| 644 | - ); |
|
| 645 | - |
|
| 646 | - for ($i = 0; $i < self::HASH_SC_L; ++$i) { |
|
| 647 | - $h[$i] = $h_be[self::HASH_SC_L - 1 - $i]; |
|
| 648 | - } |
|
| 649 | - return self::ristretto255_scalar_reduce(self::intArrayToString($h)); |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - /** |
|
| 653 | - * @param string $s |
|
| 654 | - * @return string |
|
| 655 | - */ |
|
| 656 | - public static function ristretto255_scalar_reduce($s) |
|
| 657 | - { |
|
| 658 | - return self::sc_reduce($s); |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - /** |
|
| 662 | - * @param string $n |
|
| 663 | - * @param string $p |
|
| 664 | - * @return string |
|
| 665 | - * @throws SodiumException |
|
| 666 | - */ |
|
| 667 | - public static function scalarmult_ristretto255($n, $p) |
|
| 668 | - { |
|
| 669 | - if (self::strlen($n) !== 32) { |
|
| 670 | - throw new SodiumException('Scalar must be 32 bytes, ' . self::strlen($p) . ' given.'); |
|
| 671 | - } |
|
| 672 | - if (self::strlen($p) !== 32) { |
|
| 673 | - throw new SodiumException('Point must be 32 bytes, ' . self::strlen($p) . ' given.'); |
|
| 674 | - } |
|
| 675 | - $result = self::ristretto255_frombytes($p); |
|
| 676 | - if ($result['res'] !== 0) { |
|
| 677 | - throw new SodiumException('Could not multiply points'); |
|
| 678 | - } |
|
| 679 | - $P = $result['h']; |
|
| 680 | - |
|
| 681 | - $t = self::stringToIntArray($n); |
|
| 682 | - $t[31] &= 0x7f; |
|
| 683 | - $Q = self::ge_scalarmult(self::intArrayToString($t), $P); |
|
| 684 | - $q = self::ristretto255_p3_tobytes($Q); |
|
| 685 | - if (ParagonIE_Sodium_Compat::is_zero($q)) { |
|
| 686 | - throw new SodiumException('An unknown error has occurred'); |
|
| 687 | - } |
|
| 688 | - return $q; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - /** |
|
| 692 | - * @param string $n |
|
| 693 | - * @return string |
|
| 694 | - * @throws SodiumException |
|
| 695 | - */ |
|
| 696 | - public static function scalarmult_ristretto255_base($n) |
|
| 697 | - { |
|
| 698 | - $t = self::stringToIntArray($n); |
|
| 699 | - $t[31] &= 0x7f; |
|
| 700 | - $Q = self::ge_scalarmult_base(self::intArrayToString($t)); |
|
| 701 | - $q = self::ristretto255_p3_tobytes($Q); |
|
| 702 | - if (ParagonIE_Sodium_Compat::is_zero($q)) { |
|
| 703 | - throw new SodiumException('An unknown error has occurred'); |
|
| 704 | - } |
|
| 705 | - return $q; |
|
| 706 | - } |
|
| 8 | + const crypto_core_ristretto255_HASHBYTES = 64; |
|
| 9 | + const HASH_SC_L = 48; |
|
| 10 | + const CORE_H2C_SHA256 = 1; |
|
| 11 | + const CORE_H2C_SHA512 = 2; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 15 | + * @param int $b |
|
| 16 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 17 | + */ |
|
| 18 | + public static function fe_cneg(ParagonIE_Sodium_Core_Curve25519_Fe $f, $b) |
|
| 19 | + { |
|
| 20 | + $negf = self::fe_neg($f); |
|
| 21 | + return self::fe_cmov($f, $negf, $b); |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 26 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 27 | + * @throws SodiumException |
|
| 28 | + */ |
|
| 29 | + public static function fe_abs(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 30 | + { |
|
| 31 | + return self::fe_cneg($f, self::fe_isnegative($f)); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Returns 0 if this field element results in all NUL bytes. |
|
| 36 | + * |
|
| 37 | + * @internal You should not use this directly from another application |
|
| 38 | + * |
|
| 39 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
| 40 | + * @return int |
|
| 41 | + * @throws SodiumException |
|
| 42 | + */ |
|
| 43 | + public static function fe_iszero(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 44 | + { |
|
| 45 | + static $zero; |
|
| 46 | + if ($zero === null) { |
|
| 47 | + $zero = str_repeat("\x00", 32); |
|
| 48 | + } |
|
| 49 | + /** @var string $zero */ |
|
| 50 | + $str = self::fe_tobytes($f); |
|
| 51 | + |
|
| 52 | + $d = 0; |
|
| 53 | + for ($i = 0; $i < 32; ++$i) { |
|
| 54 | + $d |= self::chrToInt($str[$i]); |
|
| 55 | + } |
|
| 56 | + return (($d - 1) >> 31) & 1; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $u |
|
| 62 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $v |
|
| 63 | + * @return array{x: ParagonIE_Sodium_Core_Curve25519_Fe, nonsquare: int} |
|
| 64 | + * |
|
| 65 | + * @throws SodiumException |
|
| 66 | + */ |
|
| 67 | + public static function ristretto255_sqrt_ratio_m1( |
|
| 68 | + ParagonIE_Sodium_Core_Curve25519_Fe $u, |
|
| 69 | + ParagonIE_Sodium_Core_Curve25519_Fe $v |
|
| 70 | + ) { |
|
| 71 | + $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); |
|
| 72 | + |
|
| 73 | + $v3 = self::fe_mul( |
|
| 74 | + self::fe_sq($v), |
|
| 75 | + $v |
|
| 76 | + ); /* v3 = v^3 */ |
|
| 77 | + $x = self::fe_mul( |
|
| 78 | + self::fe_mul( |
|
| 79 | + self::fe_sq($v3), |
|
| 80 | + $u |
|
| 81 | + ), |
|
| 82 | + $v |
|
| 83 | + ); /* x = uv^7 */ |
|
| 84 | + |
|
| 85 | + $x = self::fe_mul( |
|
| 86 | + self::fe_mul( |
|
| 87 | + self::fe_pow22523($x), /* x = (uv^7)^((q-5)/8) */ |
|
| 88 | + $v3 |
|
| 89 | + ), |
|
| 90 | + $u |
|
| 91 | + ); /* x = uv^3(uv^7)^((q-5)/8) */ |
|
| 92 | + |
|
| 93 | + $vxx = self::fe_mul( |
|
| 94 | + self::fe_sq($x), |
|
| 95 | + $v |
|
| 96 | + ); /* vx^2 */ |
|
| 97 | + |
|
| 98 | + $m_root_check = self::fe_sub($vxx, $u); /* vx^2-u */ |
|
| 99 | + $p_root_check = self::fe_add($vxx, $u); /* vx^2+u */ |
|
| 100 | + $f_root_check = self::fe_mul($u, $sqrtm1); /* u*sqrt(-1) */ |
|
| 101 | + $f_root_check = self::fe_add($vxx, $f_root_check); /* vx^2+u*sqrt(-1) */ |
|
| 102 | + |
|
| 103 | + $has_m_root = self::fe_iszero($m_root_check); |
|
| 104 | + $has_p_root = self::fe_iszero($p_root_check); |
|
| 105 | + $has_f_root = self::fe_iszero($f_root_check); |
|
| 106 | + |
|
| 107 | + $x_sqrtm1 = self::fe_mul($x, $sqrtm1); /* x*sqrt(-1) */ |
|
| 108 | + |
|
| 109 | + $x = self::fe_abs( |
|
| 110 | + self::fe_cmov($x, $x_sqrtm1, $has_p_root | $has_f_root) |
|
| 111 | + ); |
|
| 112 | + return array( |
|
| 113 | + 'x' => $x, |
|
| 114 | + 'nonsquare' => $has_m_root | $has_p_root |
|
| 115 | + ); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param string $s |
|
| 120 | + * @return int |
|
| 121 | + * @throws SodiumException |
|
| 122 | + */ |
|
| 123 | + public static function ristretto255_point_is_canonical($s) |
|
| 124 | + { |
|
| 125 | + $c = (self::chrToInt($s[31]) & 0x7f) ^ 0x7f; |
|
| 126 | + for ($i = 30; $i > 0; --$i) { |
|
| 127 | + $c |= self::chrToInt($s[$i]) ^ 0xff; |
|
| 128 | + } |
|
| 129 | + $c = ($c - 1) >> 8; |
|
| 130 | + $d = (0xed - 1 - self::chrToInt($s[0])) >> 8; |
|
| 131 | + $e = self::chrToInt($s[31]) >> 7; |
|
| 132 | + |
|
| 133 | + return 1 - ((($c & $d) | $e | self::chrToInt($s[0])) & 1); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * @param string $s |
|
| 138 | + * @param bool $skipCanonicalCheck |
|
| 139 | + * @return array{h: ParagonIE_Sodium_Core_Curve25519_Ge_P3, res: int} |
|
| 140 | + * @throws SodiumException |
|
| 141 | + */ |
|
| 142 | + public static function ristretto255_frombytes($s, $skipCanonicalCheck = false) |
|
| 143 | + { |
|
| 144 | + if (!$skipCanonicalCheck) { |
|
| 145 | + if (!self::ristretto255_point_is_canonical($s)) { |
|
| 146 | + throw new SodiumException('S is not canonical'); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $s_ = self::fe_frombytes($s); |
|
| 151 | + $ss = self::fe_sq($s_); /* ss = s^2 */ |
|
| 152 | + |
|
| 153 | + $u1 = self::fe_sub(self::fe_1(), $ss); /* u1 = 1-ss */ |
|
| 154 | + $u1u1 = self::fe_sq($u1); /* u1u1 = u1^2 */ |
|
| 155 | + |
|
| 156 | + $u2 = self::fe_add(self::fe_1(), $ss); /* u2 = 1+ss */ |
|
| 157 | + $u2u2 = self::fe_sq($u2); /* u2u2 = u2^2 */ |
|
| 158 | + |
|
| 159 | + $v = self::fe_mul( |
|
| 160 | + ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d), |
|
| 161 | + $u1u1 |
|
| 162 | + ); /* v = d*u1^2 */ |
|
| 163 | + $v = self::fe_neg($v); /* v = -d*u1^2 */ |
|
| 164 | + $v = self::fe_sub($v, $u2u2); /* v = -(d*u1^2)-u2^2 */ |
|
| 165 | + $v_u2u2 = self::fe_mul($v, $u2u2); /* v_u2u2 = v*u2^2 */ |
|
| 166 | + |
|
| 167 | + // fe25519_1(one); |
|
| 168 | + // notsquare = ristretto255_sqrt_ratio_m1(inv_sqrt, one, v_u2u2); |
|
| 169 | + $one = self::fe_1(); |
|
| 170 | + $result = self::ristretto255_sqrt_ratio_m1($one, $v_u2u2); |
|
| 171 | + $inv_sqrt = $result['x']; |
|
| 172 | + $notsquare = $result['nonsquare']; |
|
| 173 | + |
|
| 174 | + $h = new ParagonIE_Sodium_Core_Curve25519_Ge_P3(); |
|
| 175 | + |
|
| 176 | + $h->X = self::fe_mul($inv_sqrt, $u2); |
|
| 177 | + $h->Y = self::fe_mul(self::fe_mul($inv_sqrt, $h->X), $v); |
|
| 178 | + |
|
| 179 | + $h->X = self::fe_mul($h->X, $s_); |
|
| 180 | + $h->X = self::fe_abs( |
|
| 181 | + self::fe_add($h->X, $h->X) |
|
| 182 | + ); |
|
| 183 | + $h->Y = self::fe_mul($u1, $h->Y); |
|
| 184 | + $h->Z = self::fe_1(); |
|
| 185 | + $h->T = self::fe_mul($h->X, $h->Y); |
|
| 186 | + |
|
| 187 | + $res = - ((1 - $notsquare) | self::fe_isnegative($h->T) | self::fe_iszero($h->Y)); |
|
| 188 | + return array('h' => $h, 'res' => $res); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h |
|
| 193 | + * @return string |
|
| 194 | + * @throws SodiumException |
|
| 195 | + */ |
|
| 196 | + public static function ristretto255_p3_tobytes(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h) |
|
| 197 | + { |
|
| 198 | + $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); |
|
| 199 | + $invsqrtamd = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$invsqrtamd); |
|
| 200 | + |
|
| 201 | + $u1 = self::fe_add($h->Z, $h->Y); /* u1 = Z+Y */ |
|
| 202 | + $zmy = self::fe_sub($h->Z, $h->Y); /* zmy = Z-Y */ |
|
| 203 | + $u1 = self::fe_mul($u1, $zmy); /* u1 = (Z+Y)*(Z-Y) */ |
|
| 204 | + $u2 = self::fe_mul($h->X, $h->Y); /* u2 = X*Y */ |
|
| 205 | + |
|
| 206 | + $u1_u2u2 = self::fe_mul(self::fe_sq($u2), $u1); /* u1_u2u2 = u1*u2^2 */ |
|
| 207 | + $one = self::fe_1(); |
|
| 208 | + |
|
| 209 | + // fe25519_1(one); |
|
| 210 | + // (void) ristretto255_sqrt_ratio_m1(inv_sqrt, one, u1_u2u2); |
|
| 211 | + $result = self::ristretto255_sqrt_ratio_m1($one, $u1_u2u2); |
|
| 212 | + $inv_sqrt = $result['x']; |
|
| 213 | + |
|
| 214 | + $den1 = self::fe_mul($inv_sqrt, $u1); /* den1 = inv_sqrt*u1 */ |
|
| 215 | + $den2 = self::fe_mul($inv_sqrt, $u2); /* den2 = inv_sqrt*u2 */ |
|
| 216 | + $z_inv = self::fe_mul($h->T, self::fe_mul($den1, $den2)); /* z_inv = den1*den2*T */ |
|
| 217 | + |
|
| 218 | + $ix = self::fe_mul($h->X, $sqrtm1); /* ix = X*sqrt(-1) */ |
|
| 219 | + $iy = self::fe_mul($h->Y, $sqrtm1); /* iy = Y*sqrt(-1) */ |
|
| 220 | + $eden = self::fe_mul($den1, $invsqrtamd); |
|
| 221 | + |
|
| 222 | + $t_z_inv = self::fe_mul($h->T, $z_inv); /* t_z_inv = T*z_inv */ |
|
| 223 | + $rotate = self::fe_isnegative($t_z_inv); |
|
| 224 | + |
|
| 225 | + $x_ = self::fe_copy($h->X); |
|
| 226 | + $y_ = self::fe_copy($h->Y); |
|
| 227 | + $den_inv = self::fe_copy($den2); |
|
| 228 | + |
|
| 229 | + $x_ = self::fe_cmov($x_, $iy, $rotate); |
|
| 230 | + $y_ = self::fe_cmov($y_, $ix, $rotate); |
|
| 231 | + $den_inv = self::fe_cmov($den_inv, $eden, $rotate); |
|
| 232 | + |
|
| 233 | + $x_z_inv = self::fe_mul($x_, $z_inv); |
|
| 234 | + $y_ = self::fe_cneg($y_, self::fe_isnegative($x_z_inv)); |
|
| 235 | + |
|
| 236 | + |
|
| 237 | + // fe25519_sub(s_, h->Z, y_); |
|
| 238 | + // fe25519_mul(s_, den_inv, s_); |
|
| 239 | + // fe25519_abs(s_, s_); |
|
| 240 | + // fe25519_tobytes(s, s_); |
|
| 241 | + return self::fe_tobytes( |
|
| 242 | + self::fe_abs( |
|
| 243 | + self::fe_mul( |
|
| 244 | + $den_inv, |
|
| 245 | + self::fe_sub($h->Z, $y_) |
|
| 246 | + ) |
|
| 247 | + ) |
|
| 248 | + ); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $t |
|
| 253 | + * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 254 | + * |
|
| 255 | + * @throws SodiumException |
|
| 256 | + */ |
|
| 257 | + public static function ristretto255_elligator(ParagonIE_Sodium_Core_Curve25519_Fe $t) |
|
| 258 | + { |
|
| 259 | + $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); |
|
| 260 | + $onemsqd = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$onemsqd); |
|
| 261 | + $d = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d); |
|
| 262 | + $sqdmone = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqdmone); |
|
| 263 | + $sqrtadm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtadm1); |
|
| 264 | + |
|
| 265 | + $one = self::fe_1(); |
|
| 266 | + $r = self::fe_mul($sqrtm1, self::fe_sq($t)); /* r = sqrt(-1)*t^2 */ |
|
| 267 | + $u = self::fe_mul(self::fe_add($r, $one), $onemsqd); /* u = (r+1)*(1-d^2) */ |
|
| 268 | + $c = self::fe_neg(self::fe_1()); /* c = -1 */ |
|
| 269 | + $rpd = self::fe_add($r, $d); /* rpd = r+d */ |
|
| 270 | + |
|
| 271 | + $v = self::fe_mul( |
|
| 272 | + self::fe_sub( |
|
| 273 | + $c, |
|
| 274 | + self::fe_mul($r, $d) |
|
| 275 | + ), |
|
| 276 | + $rpd |
|
| 277 | + ); /* v = (c-r*d)*(r+d) */ |
|
| 278 | + |
|
| 279 | + $result = self::ristretto255_sqrt_ratio_m1($u, $v); |
|
| 280 | + $s = $result['x']; |
|
| 281 | + $wasnt_square = 1 - $result['nonsquare']; |
|
| 282 | + |
|
| 283 | + $s_prime = self::fe_neg( |
|
| 284 | + self::fe_abs( |
|
| 285 | + self::fe_mul($s, $t) |
|
| 286 | + ) |
|
| 287 | + ); /* s_prime = -|s*t| */ |
|
| 288 | + $s = self::fe_cmov($s, $s_prime, $wasnt_square); |
|
| 289 | + $c = self::fe_cmov($c, $r, $wasnt_square); |
|
| 290 | + |
|
| 291 | + // fe25519_sub(n, r, one); /* n = r-1 */ |
|
| 292 | + // fe25519_mul(n, n, c); /* n = c*(r-1) */ |
|
| 293 | + // fe25519_mul(n, n, ed25519_sqdmone); /* n = c*(r-1)*(d-1)^2 */ |
|
| 294 | + // fe25519_sub(n, n, v); /* n = c*(r-1)*(d-1)^2-v */ |
|
| 295 | + $n = self::fe_sub( |
|
| 296 | + self::fe_mul( |
|
| 297 | + self::fe_mul( |
|
| 298 | + self::fe_sub($r, $one), |
|
| 299 | + $c |
|
| 300 | + ), |
|
| 301 | + $sqdmone |
|
| 302 | + ), |
|
| 303 | + $v |
|
| 304 | + ); /* n = c*(r-1)*(d-1)^2-v */ |
|
| 305 | + |
|
| 306 | + $w0 = self::fe_mul( |
|
| 307 | + self::fe_add($s, $s), |
|
| 308 | + $v |
|
| 309 | + ); /* w0 = 2s*v */ |
|
| 310 | + |
|
| 311 | + $w1 = self::fe_mul($n, $sqrtadm1); /* w1 = n*sqrt(ad-1) */ |
|
| 312 | + $ss = self::fe_sq($s); /* ss = s^2 */ |
|
| 313 | + $w2 = self::fe_sub($one, $ss); /* w2 = 1-s^2 */ |
|
| 314 | + $w3 = self::fe_add($one, $ss); /* w3 = 1+s^2 */ |
|
| 315 | + |
|
| 316 | + return new ParagonIE_Sodium_Core_Curve25519_Ge_P3( |
|
| 317 | + self::fe_mul($w0, $w3), |
|
| 318 | + self::fe_mul($w2, $w1), |
|
| 319 | + self::fe_mul($w1, $w3), |
|
| 320 | + self::fe_mul($w0, $w2) |
|
| 321 | + ); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * @param string $h |
|
| 326 | + * @return string |
|
| 327 | + * @throws SodiumException |
|
| 328 | + */ |
|
| 329 | + public static function ristretto255_from_hash($h) |
|
| 330 | + { |
|
| 331 | + if (self::strlen($h) !== 64) { |
|
| 332 | + throw new SodiumException('Hash must be 64 bytes'); |
|
| 333 | + } |
|
| 334 | + //fe25519_frombytes(r0, h); |
|
| 335 | + //fe25519_frombytes(r1, h + 32); |
|
| 336 | + $r0 = self::fe_frombytes(self::substr($h, 0, 32)); |
|
| 337 | + $r1 = self::fe_frombytes(self::substr($h, 32, 32)); |
|
| 338 | + |
|
| 339 | + //ristretto255_elligator(&p0, r0); |
|
| 340 | + //ristretto255_elligator(&p1, r1); |
|
| 341 | + $p0 = self::ristretto255_elligator($r0); |
|
| 342 | + $p1 = self::ristretto255_elligator($r1); |
|
| 343 | + |
|
| 344 | + //ge25519_p3_to_cached(&p1_cached, &p1); |
|
| 345 | + //ge25519_add_cached(&p_p1p1, &p0, &p1_cached); |
|
| 346 | + $p_p1p1 = self::ge_add( |
|
| 347 | + $p0, |
|
| 348 | + self::ge_p3_to_cached($p1) |
|
| 349 | + ); |
|
| 350 | + |
|
| 351 | + //ge25519_p1p1_to_p3(&p, &p_p1p1); |
|
| 352 | + //ristretto255_p3_tobytes(s, &p); |
|
| 353 | + return self::ristretto255_p3_tobytes( |
|
| 354 | + self::ge_p1p1_to_p3($p_p1p1) |
|
| 355 | + ); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * @param string $p |
|
| 360 | + * @return int |
|
| 361 | + * @throws SodiumException |
|
| 362 | + */ |
|
| 363 | + public static function is_valid_point($p) |
|
| 364 | + { |
|
| 365 | + $result = self::ristretto255_frombytes($p); |
|
| 366 | + if ($result['res'] !== 0) { |
|
| 367 | + return 0; |
|
| 368 | + } |
|
| 369 | + return 1; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * @param string $p |
|
| 374 | + * @param string $q |
|
| 375 | + * @return string |
|
| 376 | + * @throws SodiumException |
|
| 377 | + */ |
|
| 378 | + public static function ristretto255_add($p, $q) |
|
| 379 | + { |
|
| 380 | + $p_res = self::ristretto255_frombytes($p); |
|
| 381 | + $q_res = self::ristretto255_frombytes($q); |
|
| 382 | + if ($p_res['res'] !== 0 || $q_res['res'] !== 0) { |
|
| 383 | + throw new SodiumException('Could not add points'); |
|
| 384 | + } |
|
| 385 | + $p_p3 = $p_res['h']; |
|
| 386 | + $q_p3 = $q_res['h']; |
|
| 387 | + $q_cached = self::ge_p3_to_cached($q_p3); |
|
| 388 | + $r_p1p1 = self::ge_add($p_p3, $q_cached); |
|
| 389 | + $r_p3 = self::ge_p1p1_to_p3($r_p1p1); |
|
| 390 | + return self::ristretto255_p3_tobytes($r_p3); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * @param string $p |
|
| 395 | + * @param string $q |
|
| 396 | + * @return string |
|
| 397 | + * @throws SodiumException |
|
| 398 | + */ |
|
| 399 | + public static function ristretto255_sub($p, $q) |
|
| 400 | + { |
|
| 401 | + $p_res = self::ristretto255_frombytes($p); |
|
| 402 | + $q_res = self::ristretto255_frombytes($q); |
|
| 403 | + if ($p_res['res'] !== 0 || $q_res['res'] !== 0) { |
|
| 404 | + throw new SodiumException('Could not add points'); |
|
| 405 | + } |
|
| 406 | + $p_p3 = $p_res['h']; |
|
| 407 | + $q_p3 = $q_res['h']; |
|
| 408 | + $q_cached = self::ge_p3_to_cached($q_p3); |
|
| 409 | + $r_p1p1 = self::ge_sub($p_p3, $q_cached); |
|
| 410 | + $r_p3 = self::ge_p1p1_to_p3($r_p1p1); |
|
| 411 | + return self::ristretto255_p3_tobytes($r_p3); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * @param int $hLen |
|
| 417 | + * @param ?string $ctx |
|
| 418 | + * @param string $msg |
|
| 419 | + * @return string |
|
| 420 | + * @throws SodiumException |
|
| 421 | + * @psalm-suppress PossiblyInvalidArgument hash API |
|
| 422 | + */ |
|
| 423 | + protected static function h2c_string_to_hash_sha256($hLen, $ctx, $msg) |
|
| 424 | + { |
|
| 425 | + $h = array_fill(0, $hLen, 0); |
|
| 426 | + $ctx_len = !is_null($ctx) ? self::strlen($ctx) : 0; |
|
| 427 | + if ($hLen > 0xff) { |
|
| 428 | + throw new SodiumException('Hash must be less than 256 bytes'); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + if ($ctx_len > 0xff) { |
|
| 432 | + $st = hash_init('sha256'); |
|
| 433 | + self::hash_update($st, "H2C-OVERSIZE-DST-"); |
|
| 434 | + self::hash_update($st, $ctx); |
|
| 435 | + $ctx = hash_final($st, true); |
|
| 436 | + $ctx_len = 32; |
|
| 437 | + } |
|
| 438 | + $t = array(0, $hLen, 0); |
|
| 439 | + $ux = str_repeat("\0", 64); |
|
| 440 | + $st = hash_init('sha256'); |
|
| 441 | + self::hash_update($st, $ux); |
|
| 442 | + self::hash_update($st, $msg); |
|
| 443 | + self::hash_update($st, self::intArrayToString($t)); |
|
| 444 | + self::hash_update($st, $ctx); |
|
| 445 | + self::hash_update($st, self::intToChr($ctx_len)); |
|
| 446 | + $u0 = hash_final($st, true); |
|
| 447 | + |
|
| 448 | + for ($i = 0; $i < $hLen; $i += 64) { |
|
| 449 | + $ux = self::xorStrings($ux, $u0); |
|
| 450 | + ++$t[2]; |
|
| 451 | + $st = hash_init('sha256'); |
|
| 452 | + self::hash_update($st, $ux); |
|
| 453 | + self::hash_update($st, self::intToChr($t[2])); |
|
| 454 | + self::hash_update($st, $ctx); |
|
| 455 | + self::hash_update($st, self::intToChr($ctx_len)); |
|
| 456 | + $ux = hash_final($st, true); |
|
| 457 | + $amount = min($hLen - $i, 64); |
|
| 458 | + for ($j = 0; $j < $amount; ++$j) { |
|
| 459 | + $h[$i + $j] = self::chrToInt($ux[$i]); |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + return self::intArrayToString(array_slice($h, 0, $hLen)); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * @param int $hLen |
|
| 467 | + * @param ?string $ctx |
|
| 468 | + * @param string $msg |
|
| 469 | + * @return string |
|
| 470 | + * @throws SodiumException |
|
| 471 | + * @psalm-suppress PossiblyInvalidArgument hash API |
|
| 472 | + */ |
|
| 473 | + protected static function h2c_string_to_hash_sha512($hLen, $ctx, $msg) |
|
| 474 | + { |
|
| 475 | + $h = array_fill(0, $hLen, 0); |
|
| 476 | + $ctx_len = !is_null($ctx) ? self::strlen($ctx) : 0; |
|
| 477 | + if ($hLen > 0xff) { |
|
| 478 | + throw new SodiumException('Hash must be less than 256 bytes'); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + if ($ctx_len > 0xff) { |
|
| 482 | + $st = hash_init('sha256'); |
|
| 483 | + self::hash_update($st, "H2C-OVERSIZE-DST-"); |
|
| 484 | + self::hash_update($st, $ctx); |
|
| 485 | + $ctx = hash_final($st, true); |
|
| 486 | + $ctx_len = 32; |
|
| 487 | + } |
|
| 488 | + $t = array(0, $hLen, 0); |
|
| 489 | + $ux = str_repeat("\0", 128); |
|
| 490 | + $st = hash_init('sha512'); |
|
| 491 | + self::hash_update($st, $ux); |
|
| 492 | + self::hash_update($st, $msg); |
|
| 493 | + self::hash_update($st, self::intArrayToString($t)); |
|
| 494 | + self::hash_update($st, $ctx); |
|
| 495 | + self::hash_update($st, self::intToChr($ctx_len)); |
|
| 496 | + $u0 = hash_final($st, true); |
|
| 497 | + |
|
| 498 | + for ($i = 0; $i < $hLen; $i += 128) { |
|
| 499 | + $ux = self::xorStrings($ux, $u0); |
|
| 500 | + ++$t[2]; |
|
| 501 | + $st = hash_init('sha512'); |
|
| 502 | + self::hash_update($st, $ux); |
|
| 503 | + self::hash_update($st, self::intToChr($t[2])); |
|
| 504 | + self::hash_update($st, $ctx); |
|
| 505 | + self::hash_update($st, self::intToChr($ctx_len)); |
|
| 506 | + $ux = hash_final($st, true); |
|
| 507 | + $amount = min($hLen - $i, 128); |
|
| 508 | + for ($j = 0; $j < $amount; ++$j) { |
|
| 509 | + $h[$i + $j] = self::chrToInt($ux[$i]); |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + return self::intArrayToString(array_slice($h, 0, $hLen)); |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * @param int $hLen |
|
| 517 | + * @param ?string $ctx |
|
| 518 | + * @param string $msg |
|
| 519 | + * @param int $hash_alg |
|
| 520 | + * @return string |
|
| 521 | + * @throws SodiumException |
|
| 522 | + */ |
|
| 523 | + public static function h2c_string_to_hash($hLen, $ctx, $msg, $hash_alg) |
|
| 524 | + { |
|
| 525 | + switch ($hash_alg) { |
|
| 526 | + case self::CORE_H2C_SHA256: |
|
| 527 | + return self::h2c_string_to_hash_sha256($hLen, $ctx, $msg); |
|
| 528 | + case self::CORE_H2C_SHA512: |
|
| 529 | + return self::h2c_string_to_hash_sha512($hLen, $ctx, $msg); |
|
| 530 | + default: |
|
| 531 | + throw new SodiumException('Invalid H2C hash algorithm'); |
|
| 532 | + } |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + /** |
|
| 536 | + * @param ?string $ctx |
|
| 537 | + * @param string $msg |
|
| 538 | + * @param int $hash_alg |
|
| 539 | + * @return string |
|
| 540 | + * @throws SodiumException |
|
| 541 | + */ |
|
| 542 | + protected static function _string_to_element($ctx, $msg, $hash_alg) |
|
| 543 | + { |
|
| 544 | + return self::ristretto255_from_hash( |
|
| 545 | + self::h2c_string_to_hash(self::crypto_core_ristretto255_HASHBYTES, $ctx, $msg, $hash_alg) |
|
| 546 | + ); |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + /** |
|
| 550 | + * @return string |
|
| 551 | + * @throws SodiumException |
|
| 552 | + * @throws Exception |
|
| 553 | + */ |
|
| 554 | + public static function ristretto255_random() |
|
| 555 | + { |
|
| 556 | + return self::ristretto255_from_hash( |
|
| 557 | + ParagonIE_Sodium_Compat::randombytes_buf(self::crypto_core_ristretto255_HASHBYTES) |
|
| 558 | + ); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * @return string |
|
| 563 | + * @throws SodiumException |
|
| 564 | + */ |
|
| 565 | + public static function ristretto255_scalar_random() |
|
| 566 | + { |
|
| 567 | + return self::scalar_random(); |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * @param string $s |
|
| 572 | + * @return string |
|
| 573 | + * @throws SodiumException |
|
| 574 | + */ |
|
| 575 | + public static function ristretto255_scalar_complement($s) |
|
| 576 | + { |
|
| 577 | + return self::scalar_complement($s); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * @param string $s |
|
| 583 | + * @return string |
|
| 584 | + */ |
|
| 585 | + public static function ristretto255_scalar_invert($s) |
|
| 586 | + { |
|
| 587 | + return self::sc25519_invert($s); |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * @param string $s |
|
| 592 | + * @return string |
|
| 593 | + * @throws SodiumException |
|
| 594 | + */ |
|
| 595 | + public static function ristretto255_scalar_negate($s) |
|
| 596 | + { |
|
| 597 | + return self::scalar_negate($s); |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + /** |
|
| 601 | + * @param string $x |
|
| 602 | + * @param string $y |
|
| 603 | + * @return string |
|
| 604 | + */ |
|
| 605 | + public static function ristretto255_scalar_add($x, $y) |
|
| 606 | + { |
|
| 607 | + return self::scalar_add($x, $y); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * @param string $x |
|
| 612 | + * @param string $y |
|
| 613 | + * @return string |
|
| 614 | + */ |
|
| 615 | + public static function ristretto255_scalar_sub($x, $y) |
|
| 616 | + { |
|
| 617 | + return self::scalar_sub($x, $y); |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * @param string $x |
|
| 622 | + * @param string $y |
|
| 623 | + * @return string |
|
| 624 | + */ |
|
| 625 | + public static function ristretto255_scalar_mul($x, $y) |
|
| 626 | + { |
|
| 627 | + return self::sc25519_mul($x, $y); |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + /** |
|
| 631 | + * @param string $ctx |
|
| 632 | + * @param string $msg |
|
| 633 | + * @param int $hash_alg |
|
| 634 | + * @return string |
|
| 635 | + * @throws SodiumException |
|
| 636 | + */ |
|
| 637 | + public static function ristretto255_scalar_from_string($ctx, $msg, $hash_alg) |
|
| 638 | + { |
|
| 639 | + $h = array_fill(0, 64, 0); |
|
| 640 | + $h_be = self::stringToIntArray( |
|
| 641 | + self::h2c_string_to_hash( |
|
| 642 | + self::HASH_SC_L, $ctx, $msg, $hash_alg |
|
| 643 | + ) |
|
| 644 | + ); |
|
| 645 | + |
|
| 646 | + for ($i = 0; $i < self::HASH_SC_L; ++$i) { |
|
| 647 | + $h[$i] = $h_be[self::HASH_SC_L - 1 - $i]; |
|
| 648 | + } |
|
| 649 | + return self::ristretto255_scalar_reduce(self::intArrayToString($h)); |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + /** |
|
| 653 | + * @param string $s |
|
| 654 | + * @return string |
|
| 655 | + */ |
|
| 656 | + public static function ristretto255_scalar_reduce($s) |
|
| 657 | + { |
|
| 658 | + return self::sc_reduce($s); |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + /** |
|
| 662 | + * @param string $n |
|
| 663 | + * @param string $p |
|
| 664 | + * @return string |
|
| 665 | + * @throws SodiumException |
|
| 666 | + */ |
|
| 667 | + public static function scalarmult_ristretto255($n, $p) |
|
| 668 | + { |
|
| 669 | + if (self::strlen($n) !== 32) { |
|
| 670 | + throw new SodiumException('Scalar must be 32 bytes, ' . self::strlen($p) . ' given.'); |
|
| 671 | + } |
|
| 672 | + if (self::strlen($p) !== 32) { |
|
| 673 | + throw new SodiumException('Point must be 32 bytes, ' . self::strlen($p) . ' given.'); |
|
| 674 | + } |
|
| 675 | + $result = self::ristretto255_frombytes($p); |
|
| 676 | + if ($result['res'] !== 0) { |
|
| 677 | + throw new SodiumException('Could not multiply points'); |
|
| 678 | + } |
|
| 679 | + $P = $result['h']; |
|
| 680 | + |
|
| 681 | + $t = self::stringToIntArray($n); |
|
| 682 | + $t[31] &= 0x7f; |
|
| 683 | + $Q = self::ge_scalarmult(self::intArrayToString($t), $P); |
|
| 684 | + $q = self::ristretto255_p3_tobytes($Q); |
|
| 685 | + if (ParagonIE_Sodium_Compat::is_zero($q)) { |
|
| 686 | + throw new SodiumException('An unknown error has occurred'); |
|
| 687 | + } |
|
| 688 | + return $q; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + /** |
|
| 692 | + * @param string $n |
|
| 693 | + * @return string |
|
| 694 | + * @throws SodiumException |
|
| 695 | + */ |
|
| 696 | + public static function scalarmult_ristretto255_base($n) |
|
| 697 | + { |
|
| 698 | + $t = self::stringToIntArray($n); |
|
| 699 | + $t[31] &= 0x7f; |
|
| 700 | + $Q = self::ge_scalarmult_base(self::intArrayToString($t)); |
|
| 701 | + $q = self::ristretto255_p3_tobytes($Q); |
|
| 702 | + if (ParagonIE_Sodium_Compat::is_zero($q)) { |
|
| 703 | + throw new SodiumException('An unknown error has occurred'); |
|
| 704 | + } |
|
| 705 | + return $q; |
|
| 706 | + } |
|
| 707 | 707 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core_ChaCha20_Ctx', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,115 +9,115 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | class ParagonIE_Sodium_Core_ChaCha20_Ctx extends ParagonIE_Sodium_Core_Util implements ArrayAccess |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var SplFixedArray internally, <int, int> |
|
| 14 | - */ |
|
| 15 | - protected $container; |
|
| 12 | + /** |
|
| 13 | + * @var SplFixedArray internally, <int, int> |
|
| 14 | + */ |
|
| 15 | + protected $container; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * ParagonIE_Sodium_Core_ChaCha20_Ctx constructor. |
|
| 19 | - * |
|
| 20 | - * @internal You should not use this directly from another application |
|
| 21 | - * |
|
| 22 | - * @param string $key ChaCha20 key. |
|
| 23 | - * @param string $iv Initialization Vector (a.k.a. nonce). |
|
| 24 | - * @param string $counter The initial counter value. |
|
| 25 | - * Defaults to 8 0x00 bytes. |
|
| 26 | - * @throws InvalidArgumentException |
|
| 27 | - * @throws TypeError |
|
| 28 | - */ |
|
| 29 | - public function __construct($key = '', $iv = '', $counter = '') |
|
| 30 | - { |
|
| 31 | - if (self::strlen($key) !== 32) { |
|
| 32 | - throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.'); |
|
| 33 | - } |
|
| 34 | - if (self::strlen($iv) !== 8) { |
|
| 35 | - throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.'); |
|
| 36 | - } |
|
| 37 | - $this->container = new SplFixedArray(16); |
|
| 17 | + /** |
|
| 18 | + * ParagonIE_Sodium_Core_ChaCha20_Ctx constructor. |
|
| 19 | + * |
|
| 20 | + * @internal You should not use this directly from another application |
|
| 21 | + * |
|
| 22 | + * @param string $key ChaCha20 key. |
|
| 23 | + * @param string $iv Initialization Vector (a.k.a. nonce). |
|
| 24 | + * @param string $counter The initial counter value. |
|
| 25 | + * Defaults to 8 0x00 bytes. |
|
| 26 | + * @throws InvalidArgumentException |
|
| 27 | + * @throws TypeError |
|
| 28 | + */ |
|
| 29 | + public function __construct($key = '', $iv = '', $counter = '') |
|
| 30 | + { |
|
| 31 | + if (self::strlen($key) !== 32) { |
|
| 32 | + throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.'); |
|
| 33 | + } |
|
| 34 | + if (self::strlen($iv) !== 8) { |
|
| 35 | + throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.'); |
|
| 36 | + } |
|
| 37 | + $this->container = new SplFixedArray(16); |
|
| 38 | 38 | |
| 39 | - /* "expand 32-byte k" as per ChaCha20 spec */ |
|
| 40 | - $this->container[0] = 0x61707865; |
|
| 41 | - $this->container[1] = 0x3320646e; |
|
| 42 | - $this->container[2] = 0x79622d32; |
|
| 43 | - $this->container[3] = 0x6b206574; |
|
| 44 | - $this->container[4] = self::load_4(self::substr($key, 0, 4)); |
|
| 45 | - $this->container[5] = self::load_4(self::substr($key, 4, 4)); |
|
| 46 | - $this->container[6] = self::load_4(self::substr($key, 8, 4)); |
|
| 47 | - $this->container[7] = self::load_4(self::substr($key, 12, 4)); |
|
| 48 | - $this->container[8] = self::load_4(self::substr($key, 16, 4)); |
|
| 49 | - $this->container[9] = self::load_4(self::substr($key, 20, 4)); |
|
| 50 | - $this->container[10] = self::load_4(self::substr($key, 24, 4)); |
|
| 51 | - $this->container[11] = self::load_4(self::substr($key, 28, 4)); |
|
| 39 | + /* "expand 32-byte k" as per ChaCha20 spec */ |
|
| 40 | + $this->container[0] = 0x61707865; |
|
| 41 | + $this->container[1] = 0x3320646e; |
|
| 42 | + $this->container[2] = 0x79622d32; |
|
| 43 | + $this->container[3] = 0x6b206574; |
|
| 44 | + $this->container[4] = self::load_4(self::substr($key, 0, 4)); |
|
| 45 | + $this->container[5] = self::load_4(self::substr($key, 4, 4)); |
|
| 46 | + $this->container[6] = self::load_4(self::substr($key, 8, 4)); |
|
| 47 | + $this->container[7] = self::load_4(self::substr($key, 12, 4)); |
|
| 48 | + $this->container[8] = self::load_4(self::substr($key, 16, 4)); |
|
| 49 | + $this->container[9] = self::load_4(self::substr($key, 20, 4)); |
|
| 50 | + $this->container[10] = self::load_4(self::substr($key, 24, 4)); |
|
| 51 | + $this->container[11] = self::load_4(self::substr($key, 28, 4)); |
|
| 52 | 52 | |
| 53 | - if (empty($counter)) { |
|
| 54 | - $this->container[12] = 0; |
|
| 55 | - $this->container[13] = 0; |
|
| 56 | - } else { |
|
| 57 | - $this->container[12] = self::load_4(self::substr($counter, 0, 4)); |
|
| 58 | - $this->container[13] = self::load_4(self::substr($counter, 4, 4)); |
|
| 59 | - } |
|
| 60 | - $this->container[14] = self::load_4(self::substr($iv, 0, 4)); |
|
| 61 | - $this->container[15] = self::load_4(self::substr($iv, 4, 4)); |
|
| 62 | - } |
|
| 53 | + if (empty($counter)) { |
|
| 54 | + $this->container[12] = 0; |
|
| 55 | + $this->container[13] = 0; |
|
| 56 | + } else { |
|
| 57 | + $this->container[12] = self::load_4(self::substr($counter, 0, 4)); |
|
| 58 | + $this->container[13] = self::load_4(self::substr($counter, 4, 4)); |
|
| 59 | + } |
|
| 60 | + $this->container[14] = self::load_4(self::substr($iv, 0, 4)); |
|
| 61 | + $this->container[15] = self::load_4(self::substr($iv, 4, 4)); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * @internal You should not use this directly from another application |
|
| 66 | - * |
|
| 67 | - * @param int $offset |
|
| 68 | - * @param int $value |
|
| 69 | - * @return void |
|
| 70 | - * @psalm-suppress MixedArrayOffset |
|
| 71 | - */ |
|
| 72 | - #[ReturnTypeWillChange] |
|
| 73 | - public function offsetSet($offset, $value) |
|
| 74 | - { |
|
| 75 | - if (!is_int($offset)) { |
|
| 76 | - throw new InvalidArgumentException('Expected an integer'); |
|
| 77 | - } |
|
| 78 | - if (!is_int($value)) { |
|
| 79 | - throw new InvalidArgumentException('Expected an integer'); |
|
| 80 | - } |
|
| 81 | - $this->container[$offset] = $value; |
|
| 82 | - } |
|
| 64 | + /** |
|
| 65 | + * @internal You should not use this directly from another application |
|
| 66 | + * |
|
| 67 | + * @param int $offset |
|
| 68 | + * @param int $value |
|
| 69 | + * @return void |
|
| 70 | + * @psalm-suppress MixedArrayOffset |
|
| 71 | + */ |
|
| 72 | + #[ReturnTypeWillChange] |
|
| 73 | + public function offsetSet($offset, $value) |
|
| 74 | + { |
|
| 75 | + if (!is_int($offset)) { |
|
| 76 | + throw new InvalidArgumentException('Expected an integer'); |
|
| 77 | + } |
|
| 78 | + if (!is_int($value)) { |
|
| 79 | + throw new InvalidArgumentException('Expected an integer'); |
|
| 80 | + } |
|
| 81 | + $this->container[$offset] = $value; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * @internal You should not use this directly from another application |
|
| 86 | - * |
|
| 87 | - * @param int $offset |
|
| 88 | - * @return bool |
|
| 89 | - */ |
|
| 90 | - #[ReturnTypeWillChange] |
|
| 91 | - public function offsetExists($offset) |
|
| 92 | - { |
|
| 93 | - return isset($this->container[$offset]); |
|
| 94 | - } |
|
| 84 | + /** |
|
| 85 | + * @internal You should not use this directly from another application |
|
| 86 | + * |
|
| 87 | + * @param int $offset |
|
| 88 | + * @return bool |
|
| 89 | + */ |
|
| 90 | + #[ReturnTypeWillChange] |
|
| 91 | + public function offsetExists($offset) |
|
| 92 | + { |
|
| 93 | + return isset($this->container[$offset]); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * @internal You should not use this directly from another application |
|
| 98 | - * |
|
| 99 | - * @param int $offset |
|
| 100 | - * @return void |
|
| 101 | - * @psalm-suppress MixedArrayOffset |
|
| 102 | - */ |
|
| 103 | - #[ReturnTypeWillChange] |
|
| 104 | - public function offsetUnset($offset) |
|
| 105 | - { |
|
| 106 | - unset($this->container[$offset]); |
|
| 107 | - } |
|
| 96 | + /** |
|
| 97 | + * @internal You should not use this directly from another application |
|
| 98 | + * |
|
| 99 | + * @param int $offset |
|
| 100 | + * @return void |
|
| 101 | + * @psalm-suppress MixedArrayOffset |
|
| 102 | + */ |
|
| 103 | + #[ReturnTypeWillChange] |
|
| 104 | + public function offsetUnset($offset) |
|
| 105 | + { |
|
| 106 | + unset($this->container[$offset]); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * @internal You should not use this directly from another application |
|
| 111 | - * |
|
| 112 | - * @param int $offset |
|
| 113 | - * @return mixed|null |
|
| 114 | - * @psalm-suppress MixedArrayOffset |
|
| 115 | - */ |
|
| 116 | - #[ReturnTypeWillChange] |
|
| 117 | - public function offsetGet($offset) |
|
| 118 | - { |
|
| 119 | - return isset($this->container[$offset]) |
|
| 120 | - ? $this->container[$offset] |
|
| 121 | - : null; |
|
| 122 | - } |
|
| 109 | + /** |
|
| 110 | + * @internal You should not use this directly from another application |
|
| 111 | + * |
|
| 112 | + * @param int $offset |
|
| 113 | + * @return mixed|null |
|
| 114 | + * @psalm-suppress MixedArrayOffset |
|
| 115 | + */ |
|
| 116 | + #[ReturnTypeWillChange] |
|
| 117 | + public function offsetGet($offset) |
|
| 118 | + { |
|
| 119 | + return isset($this->container[$offset]) |
|
| 120 | + ? $this->container[$offset] |
|
| 121 | + : null; |
|
| 122 | + } |
|
| 123 | 123 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,30 +9,30 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | class ParagonIE_Sodium_Core_ChaCha20_IetfCtx extends ParagonIE_Sodium_Core_ChaCha20_Ctx |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * ParagonIE_Sodium_Core_ChaCha20_IetfCtx constructor. |
|
| 14 | - * |
|
| 15 | - * @internal You should not use this directly from another application |
|
| 16 | - * |
|
| 17 | - * @param string $key ChaCha20 key. |
|
| 18 | - * @param string $iv Initialization Vector (a.k.a. nonce). |
|
| 19 | - * @param string $counter The initial counter value. |
|
| 20 | - * Defaults to 4 0x00 bytes. |
|
| 21 | - * @throws InvalidArgumentException |
|
| 22 | - * @throws TypeError |
|
| 23 | - */ |
|
| 24 | - public function __construct($key = '', $iv = '', $counter = '') |
|
| 25 | - { |
|
| 26 | - if (self::strlen($iv) !== 12) { |
|
| 27 | - throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.'); |
|
| 28 | - } |
|
| 29 | - parent::__construct($key, self::substr($iv, 0, 8), $counter); |
|
| 12 | + /** |
|
| 13 | + * ParagonIE_Sodium_Core_ChaCha20_IetfCtx constructor. |
|
| 14 | + * |
|
| 15 | + * @internal You should not use this directly from another application |
|
| 16 | + * |
|
| 17 | + * @param string $key ChaCha20 key. |
|
| 18 | + * @param string $iv Initialization Vector (a.k.a. nonce). |
|
| 19 | + * @param string $counter The initial counter value. |
|
| 20 | + * Defaults to 4 0x00 bytes. |
|
| 21 | + * @throws InvalidArgumentException |
|
| 22 | + * @throws TypeError |
|
| 23 | + */ |
|
| 24 | + public function __construct($key = '', $iv = '', $counter = '') |
|
| 25 | + { |
|
| 26 | + if (self::strlen($iv) !== 12) { |
|
| 27 | + throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.'); |
|
| 28 | + } |
|
| 29 | + parent::__construct($key, self::substr($iv, 0, 8), $counter); |
|
| 30 | 30 | |
| 31 | - if (!empty($counter)) { |
|
| 32 | - $this->container[12] = self::load_4(self::substr($counter, 0, 4)); |
|
| 33 | - } |
|
| 34 | - $this->container[13] = self::load_4(self::substr($iv, 0, 4)); |
|
| 35 | - $this->container[14] = self::load_4(self::substr($iv, 4, 4)); |
|
| 36 | - $this->container[15] = self::load_4(self::substr($iv, 8, 4)); |
|
| 37 | - } |
|
| 31 | + if (!empty($counter)) { |
|
| 32 | + $this->container[12] = self::load_4(self::substr($counter, 0, 4)); |
|
| 33 | + } |
|
| 34 | + $this->container[13] = self::load_4(self::substr($iv, 0, 4)); |
|
| 35 | + $this->container[14] = self::load_4(self::substr($iv, 4, 4)); |
|
| 36 | + $this->container[15] = self::load_4(self::substr($iv, 8, 4)); |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -1,11 +1,11 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (!class_exists('SodiumException', false)) { |
| 4 | - /** |
|
| 5 | - * Class SodiumException |
|
| 6 | - */ |
|
| 7 | - class SodiumException extends Exception |
|
| 8 | - { |
|
| 4 | + /** |
|
| 5 | + * Class SodiumException |
|
| 6 | + */ |
|
| 7 | + class SodiumException extends Exception |
|
| 8 | + { |
|
| 9 | 9 | |
| 10 | - } |
|
| 10 | + } |
|
| 11 | 11 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -11,709 +11,709 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class ParagonIE_Sodium_Core32_BLAKE2b extends ParagonIE_Sodium_Core_Util |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var SplFixedArray |
|
| 16 | - */ |
|
| 17 | - public static $iv; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var array<int, array<int, int>> |
|
| 21 | - */ |
|
| 22 | - public static $sigma = array( |
|
| 23 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
| 24 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), |
|
| 25 | - array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), |
|
| 26 | - array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), |
|
| 27 | - array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), |
|
| 28 | - array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), |
|
| 29 | - array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), |
|
| 30 | - array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), |
|
| 31 | - array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), |
|
| 32 | - array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), |
|
| 33 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
| 34 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) |
|
| 35 | - ); |
|
| 36 | - |
|
| 37 | - const BLOCKBYTES = 128; |
|
| 38 | - const OUTBYTES = 64; |
|
| 39 | - const KEYBYTES = 64; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Turn two 32-bit integers into a fixed array representing a 64-bit integer. |
|
| 43 | - * |
|
| 44 | - * @internal You should not use this directly from another application |
|
| 45 | - * |
|
| 46 | - * @param int $high |
|
| 47 | - * @param int $low |
|
| 48 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 49 | - * @throws SodiumException |
|
| 50 | - * @throws TypeError |
|
| 51 | - */ |
|
| 52 | - public static function new64($high, $low) |
|
| 53 | - { |
|
| 54 | - return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Convert an arbitrary number into an SplFixedArray of two 32-bit integers |
|
| 59 | - * that represents a 64-bit integer. |
|
| 60 | - * |
|
| 61 | - * @internal You should not use this directly from another application |
|
| 62 | - * |
|
| 63 | - * @param int $num |
|
| 64 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 65 | - * @throws SodiumException |
|
| 66 | - * @throws TypeError |
|
| 67 | - */ |
|
| 68 | - protected static function to64($num) |
|
| 69 | - { |
|
| 70 | - list($hi, $lo) = self::numericTo64BitInteger($num); |
|
| 71 | - return self::new64($hi, $lo); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Adds two 64-bit integers together, returning their sum as a SplFixedArray |
|
| 76 | - * containing two 32-bit integers (representing a 64-bit integer). |
|
| 77 | - * |
|
| 78 | - * @internal You should not use this directly from another application |
|
| 79 | - * |
|
| 80 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
| 81 | - * @param ParagonIE_Sodium_Core32_Int64 $y |
|
| 82 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 83 | - */ |
|
| 84 | - protected static function add64($x, $y) |
|
| 85 | - { |
|
| 86 | - return $x->addInt64($y); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @internal You should not use this directly from another application |
|
| 91 | - * |
|
| 92 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
| 93 | - * @param ParagonIE_Sodium_Core32_Int64 $y |
|
| 94 | - * @param ParagonIE_Sodium_Core32_Int64 $z |
|
| 95 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 96 | - */ |
|
| 97 | - public static function add364($x, $y, $z) |
|
| 98 | - { |
|
| 99 | - return $x->addInt64($y)->addInt64($z); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @internal You should not use this directly from another application |
|
| 104 | - * |
|
| 105 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
| 106 | - * @param ParagonIE_Sodium_Core32_Int64 $y |
|
| 107 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 108 | - * @throws TypeError |
|
| 109 | - */ |
|
| 110 | - public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) |
|
| 111 | - { |
|
| 112 | - return $x->xorInt64($y); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @internal You should not use this directly from another application |
|
| 117 | - * |
|
| 118 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
| 119 | - * @param int $c |
|
| 120 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 121 | - * @throws SodiumException |
|
| 122 | - * @throws TypeError |
|
| 123 | - */ |
|
| 124 | - public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) |
|
| 125 | - { |
|
| 126 | - return $x->rotateRight($c); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @internal You should not use this directly from another application |
|
| 131 | - * |
|
| 132 | - * @param SplFixedArray $x |
|
| 133 | - * @param int $i |
|
| 134 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 135 | - * @throws SodiumException |
|
| 136 | - * @throws TypeError |
|
| 137 | - */ |
|
| 138 | - public static function load64($x, $i) |
|
| 139 | - { |
|
| 140 | - /** @var int $l */ |
|
| 141 | - $l = (int) ($x[$i]) |
|
| 142 | - | ((int) ($x[$i+1]) << 8) |
|
| 143 | - | ((int) ($x[$i+2]) << 16) |
|
| 144 | - | ((int) ($x[$i+3]) << 24); |
|
| 145 | - /** @var int $h */ |
|
| 146 | - $h = (int) ($x[$i+4]) |
|
| 147 | - | ((int) ($x[$i+5]) << 8) |
|
| 148 | - | ((int) ($x[$i+6]) << 16) |
|
| 149 | - | ((int) ($x[$i+7]) << 24); |
|
| 150 | - return self::new64($h, $l); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @internal You should not use this directly from another application |
|
| 155 | - * |
|
| 156 | - * @param SplFixedArray $x |
|
| 157 | - * @param int $i |
|
| 158 | - * @param ParagonIE_Sodium_Core32_Int64 $u |
|
| 159 | - * @return void |
|
| 160 | - * @throws TypeError |
|
| 161 | - * @psalm-suppress MixedArgument |
|
| 162 | - * @psalm-suppress MixedAssignment |
|
| 163 | - * @psalm-suppress MixedArrayAccess |
|
| 164 | - * @psalm-suppress MixedArrayAssignment |
|
| 165 | - * @psalm-suppress MixedArrayOffset |
|
| 166 | - */ |
|
| 167 | - public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) |
|
| 168 | - { |
|
| 169 | - $v = clone $u; |
|
| 170 | - $maxLength = $x->getSize() - 1; |
|
| 171 | - for ($j = 0; $j < 8; ++$j) { |
|
| 172 | - $k = 3 - ($j >> 1); |
|
| 173 | - $x[$i] = $v->limbs[$k] & 0xff; |
|
| 174 | - if (++$i > $maxLength) { |
|
| 175 | - return; |
|
| 176 | - } |
|
| 177 | - $v->limbs[$k] >>= 8; |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * This just sets the $iv static variable. |
|
| 183 | - * |
|
| 184 | - * @internal You should not use this directly from another application |
|
| 185 | - * |
|
| 186 | - * @return void |
|
| 187 | - * @throws SodiumException |
|
| 188 | - * @throws TypeError |
|
| 189 | - */ |
|
| 190 | - public static function pseudoConstructor() |
|
| 191 | - { |
|
| 192 | - static $called = false; |
|
| 193 | - if ($called) { |
|
| 194 | - return; |
|
| 195 | - } |
|
| 196 | - self::$iv = new SplFixedArray(8); |
|
| 197 | - self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
|
| 198 | - self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
|
| 199 | - self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
|
| 200 | - self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
|
| 201 | - self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
|
| 202 | - self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
|
| 203 | - self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
|
| 204 | - self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
|
| 205 | - |
|
| 206 | - $called = true; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Returns a fresh BLAKE2 context. |
|
| 211 | - * |
|
| 212 | - * @internal You should not use this directly from another application |
|
| 213 | - * |
|
| 214 | - * @return SplFixedArray |
|
| 215 | - * @throws TypeError |
|
| 216 | - * @psalm-suppress MixedArgument |
|
| 217 | - * @psalm-suppress MixedAssignment |
|
| 218 | - * @psalm-suppress MixedArrayAccess |
|
| 219 | - * @psalm-suppress MixedArrayAssignment |
|
| 220 | - * @psalm-suppress MixedArrayOffset |
|
| 221 | - * @throws SodiumException |
|
| 222 | - * @throws TypeError |
|
| 223 | - */ |
|
| 224 | - protected static function context() |
|
| 225 | - { |
|
| 226 | - $ctx = new SplFixedArray(6); |
|
| 227 | - $ctx[0] = new SplFixedArray(8); // h |
|
| 228 | - $ctx[1] = new SplFixedArray(2); // t |
|
| 229 | - $ctx[2] = new SplFixedArray(2); // f |
|
| 230 | - $ctx[3] = new SplFixedArray(256); // buf |
|
| 231 | - $ctx[4] = 0; // buflen |
|
| 232 | - $ctx[5] = 0; // last_node (uint8_t) |
|
| 233 | - |
|
| 234 | - for ($i = 8; $i--;) { |
|
| 235 | - $ctx[0][$i] = self::$iv[$i]; |
|
| 236 | - } |
|
| 237 | - for ($i = 256; $i--;) { |
|
| 238 | - $ctx[3][$i] = 0; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $zero = self::new64(0, 0); |
|
| 242 | - $ctx[1][0] = $zero; |
|
| 243 | - $ctx[1][1] = $zero; |
|
| 244 | - $ctx[2][0] = $zero; |
|
| 245 | - $ctx[2][1] = $zero; |
|
| 246 | - |
|
| 247 | - return $ctx; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @internal You should not use this directly from another application |
|
| 252 | - * |
|
| 253 | - * @param SplFixedArray $ctx |
|
| 254 | - * @param SplFixedArray $buf |
|
| 255 | - * @return void |
|
| 256 | - * @throws SodiumException |
|
| 257 | - * @throws TypeError |
|
| 258 | - * @psalm-suppress MixedArgument |
|
| 259 | - * @psalm-suppress MixedArrayAccess |
|
| 260 | - * @psalm-suppress MixedArrayAssignment |
|
| 261 | - * @psalm-suppress MixedAssignment |
|
| 262 | - */ |
|
| 263 | - protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
| 264 | - { |
|
| 265 | - $m = new SplFixedArray(16); |
|
| 266 | - $v = new SplFixedArray(16); |
|
| 267 | - |
|
| 268 | - for ($i = 16; $i--;) { |
|
| 269 | - $m[$i] = self::load64($buf, $i << 3); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - for ($i = 8; $i--;) { |
|
| 273 | - $v[$i] = $ctx[0][$i]; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - $v[ 8] = self::$iv[0]; |
|
| 277 | - $v[ 9] = self::$iv[1]; |
|
| 278 | - $v[10] = self::$iv[2]; |
|
| 279 | - $v[11] = self::$iv[3]; |
|
| 280 | - |
|
| 281 | - $v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
|
| 282 | - $v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
|
| 283 | - $v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
|
| 284 | - $v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
|
| 285 | - |
|
| 286 | - for ($r = 0; $r < 12; ++$r) { |
|
| 287 | - $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
|
| 288 | - $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
|
| 289 | - $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
|
| 290 | - $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
|
| 291 | - $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
|
| 292 | - $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
|
| 293 | - $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
|
| 294 | - $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - for ($i = 8; $i--;) { |
|
| 298 | - $ctx[0][$i] = self::xor64( |
|
| 299 | - $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
| 300 | - ); |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * @internal You should not use this directly from another application |
|
| 306 | - * |
|
| 307 | - * @param int $r |
|
| 308 | - * @param int $i |
|
| 309 | - * @param int $a |
|
| 310 | - * @param int $b |
|
| 311 | - * @param int $c |
|
| 312 | - * @param int $d |
|
| 313 | - * @param SplFixedArray $v |
|
| 314 | - * @param SplFixedArray $m |
|
| 315 | - * @return SplFixedArray |
|
| 316 | - * @throws SodiumException |
|
| 317 | - * @throws TypeError |
|
| 318 | - * @psalm-suppress MixedArgument |
|
| 319 | - * @psalm-suppress MixedArrayOffset |
|
| 320 | - */ |
|
| 321 | - public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
| 322 | - { |
|
| 323 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
|
| 324 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
|
| 325 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
| 326 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
|
| 327 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); |
|
| 328 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
|
| 329 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
| 330 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
|
| 331 | - return $v; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * @internal You should not use this directly from another application |
|
| 336 | - * |
|
| 337 | - * @param SplFixedArray $ctx |
|
| 338 | - * @param int $inc |
|
| 339 | - * @return void |
|
| 340 | - * @throws SodiumException |
|
| 341 | - * @throws TypeError |
|
| 342 | - * @psalm-suppress MixedArgument |
|
| 343 | - * @psalm-suppress MixedArrayAccess |
|
| 344 | - * @psalm-suppress MixedArrayAssignment |
|
| 345 | - */ |
|
| 346 | - public static function increment_counter($ctx, $inc) |
|
| 347 | - { |
|
| 348 | - if ($inc < 0) { |
|
| 349 | - throw new SodiumException('Increasing by a negative number makes no sense.'); |
|
| 350 | - } |
|
| 351 | - $t = self::to64($inc); |
|
| 352 | - # S->t is $ctx[1] in our implementation |
|
| 353 | - |
|
| 354 | - # S->t[0] = ( uint64_t )( t >> 0 ); |
|
| 355 | - $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
| 356 | - |
|
| 357 | - # S->t[1] += ( S->t[0] < inc ); |
|
| 358 | - if (!($ctx[1][0] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
| 359 | - throw new TypeError('Not an int64'); |
|
| 360 | - } |
|
| 361 | - /** @var ParagonIE_Sodium_Core32_Int64 $c*/ |
|
| 362 | - $c = $ctx[1][0]; |
|
| 363 | - if ($c->isLessThanInt($inc)) { |
|
| 364 | - $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * @internal You should not use this directly from another application |
|
| 370 | - * |
|
| 371 | - * @param SplFixedArray $ctx |
|
| 372 | - * @param SplFixedArray $p |
|
| 373 | - * @param int $plen |
|
| 374 | - * @return void |
|
| 375 | - * @throws SodiumException |
|
| 376 | - * @throws TypeError |
|
| 377 | - * @psalm-suppress MixedArgument |
|
| 378 | - * @psalm-suppress MixedAssignment |
|
| 379 | - * @psalm-suppress MixedArrayAccess |
|
| 380 | - * @psalm-suppress MixedArrayAssignment |
|
| 381 | - * @psalm-suppress MixedArrayOffset |
|
| 382 | - * @psalm-suppress MixedMethodCall |
|
| 383 | - * @psalm-suppress MixedOperand |
|
| 384 | - */ |
|
| 385 | - public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
| 386 | - { |
|
| 387 | - self::pseudoConstructor(); |
|
| 388 | - |
|
| 389 | - $offset = 0; |
|
| 390 | - while ($plen > 0) { |
|
| 391 | - $left = $ctx[4]; |
|
| 392 | - $fill = 256 - $left; |
|
| 393 | - |
|
| 394 | - if ($plen > $fill) { |
|
| 395 | - # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
|
| 396 | - for ($i = $fill; $i--;) { |
|
| 397 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - # S->buflen += fill; |
|
| 401 | - $ctx[4] += $fill; |
|
| 402 | - |
|
| 403 | - # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
|
| 404 | - self::increment_counter($ctx, 128); |
|
| 405 | - |
|
| 406 | - # blake2b_compress( S, S->buf ); /* Compress */ |
|
| 407 | - self::compress($ctx, $ctx[3]); |
|
| 408 | - |
|
| 409 | - # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
|
| 410 | - for ($i = 128; $i--;) { |
|
| 411 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - # S->buflen -= BLAKE2B_BLOCKBYTES; |
|
| 415 | - $ctx[4] -= 128; |
|
| 416 | - |
|
| 417 | - # in += fill; |
|
| 418 | - $offset += $fill; |
|
| 419 | - |
|
| 420 | - # inlen -= fill; |
|
| 421 | - $plen -= $fill; |
|
| 422 | - } else { |
|
| 423 | - for ($i = $plen; $i--;) { |
|
| 424 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
| 425 | - } |
|
| 426 | - $ctx[4] += $plen; |
|
| 427 | - $offset += $plen; |
|
| 428 | - $plen -= $plen; |
|
| 429 | - } |
|
| 430 | - } |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * @internal You should not use this directly from another application |
|
| 435 | - * |
|
| 436 | - * @param SplFixedArray $ctx |
|
| 437 | - * @param SplFixedArray $out |
|
| 438 | - * @return SplFixedArray |
|
| 439 | - * @throws SodiumException |
|
| 440 | - * @throws TypeError |
|
| 441 | - * @psalm-suppress MixedArgument |
|
| 442 | - * @psalm-suppress MixedAssignment |
|
| 443 | - * @psalm-suppress MixedArrayAccess |
|
| 444 | - * @psalm-suppress MixedArrayAssignment |
|
| 445 | - * @psalm-suppress MixedArrayOffset |
|
| 446 | - * @psalm-suppress MixedMethodCall |
|
| 447 | - * @psalm-suppress MixedOperand |
|
| 448 | - */ |
|
| 449 | - public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
| 450 | - { |
|
| 451 | - self::pseudoConstructor(); |
|
| 452 | - if ($ctx[4] > 128) { |
|
| 453 | - self::increment_counter($ctx, 128); |
|
| 454 | - self::compress($ctx, $ctx[3]); |
|
| 455 | - $ctx[4] -= 128; |
|
| 456 | - if ($ctx[4] > 128) { |
|
| 457 | - throw new SodiumException('Failed to assert that buflen <= 128 bytes'); |
|
| 458 | - } |
|
| 459 | - for ($i = $ctx[4]; $i--;) { |
|
| 460 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - self::increment_counter($ctx, $ctx[4]); |
|
| 465 | - $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
| 466 | - |
|
| 467 | - for ($i = 256 - $ctx[4]; $i--;) { |
|
| 468 | - /** @var int $i */ |
|
| 469 | - $ctx[3][$i + $ctx[4]] = 0; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - self::compress($ctx, $ctx[3]); |
|
| 473 | - |
|
| 474 | - $i = (int) (($out->getSize() - 1) / 8); |
|
| 475 | - for (; $i >= 0; --$i) { |
|
| 476 | - self::store64($out, $i << 3, $ctx[0][$i]); |
|
| 477 | - } |
|
| 478 | - return $out; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * @internal You should not use this directly from another application |
|
| 483 | - * |
|
| 484 | - * @param SplFixedArray|null $key |
|
| 485 | - * @param int $outlen |
|
| 486 | - * @param SplFixedArray|null $salt |
|
| 487 | - * @param SplFixedArray|null $personal |
|
| 488 | - * @return SplFixedArray |
|
| 489 | - * @throws SodiumException |
|
| 490 | - * @throws TypeError |
|
| 491 | - * @psalm-suppress MixedArgument |
|
| 492 | - * @psalm-suppress MixedAssignment |
|
| 493 | - * @psalm-suppress MixedArrayAccess |
|
| 494 | - * @psalm-suppress MixedArrayAssignment |
|
| 495 | - * @psalm-suppress MixedMethodCall |
|
| 496 | - */ |
|
| 497 | - public static function init( |
|
| 498 | - $key = null, |
|
| 499 | - $outlen = 64, |
|
| 500 | - $salt = null, |
|
| 501 | - $personal = null |
|
| 502 | - ) { |
|
| 503 | - self::pseudoConstructor(); |
|
| 504 | - $klen = 0; |
|
| 505 | - |
|
| 506 | - if ($key !== null) { |
|
| 507 | - if (count($key) > 64) { |
|
| 508 | - throw new SodiumException('Invalid key size'); |
|
| 509 | - } |
|
| 510 | - $klen = count($key); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - if ($outlen > 64) { |
|
| 514 | - throw new SodiumException('Invalid output size'); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - $ctx = self::context(); |
|
| 518 | - |
|
| 519 | - $p = new SplFixedArray(64); |
|
| 520 | - // Zero our param buffer... |
|
| 521 | - for ($i = 64; --$i;) { |
|
| 522 | - $p[$i] = 0; |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - $p[0] = $outlen; // digest_length |
|
| 526 | - $p[1] = $klen; // key_length |
|
| 527 | - $p[2] = 1; // fanout |
|
| 528 | - $p[3] = 1; // depth |
|
| 529 | - |
|
| 530 | - if ($salt instanceof SplFixedArray) { |
|
| 531 | - // salt: [32] through [47] |
|
| 532 | - for ($i = 0; $i < 16; ++$i) { |
|
| 533 | - $p[32 + $i] = (int) $salt[$i]; |
|
| 534 | - } |
|
| 535 | - } |
|
| 536 | - if ($personal instanceof SplFixedArray) { |
|
| 537 | - // personal: [48] through [63] |
|
| 538 | - for ($i = 0; $i < 16; ++$i) { |
|
| 539 | - $p[48 + $i] = (int) $personal[$i]; |
|
| 540 | - } |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - $ctx[0][0] = self::xor64( |
|
| 544 | - $ctx[0][0], |
|
| 545 | - self::load64($p, 0) |
|
| 546 | - ); |
|
| 547 | - |
|
| 548 | - if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { |
|
| 549 | - // We need to do what blake2b_init_param() does: |
|
| 550 | - for ($i = 1; $i < 8; ++$i) { |
|
| 551 | - $ctx[0][$i] = self::xor64( |
|
| 552 | - $ctx[0][$i], |
|
| 553 | - self::load64($p, $i << 3) |
|
| 554 | - ); |
|
| 555 | - } |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - if ($klen > 0 && $key instanceof SplFixedArray) { |
|
| 559 | - $block = new SplFixedArray(128); |
|
| 560 | - for ($i = 128; $i--;) { |
|
| 561 | - $block[$i] = 0; |
|
| 562 | - } |
|
| 563 | - for ($i = $klen; $i--;) { |
|
| 564 | - $block[$i] = $key[$i]; |
|
| 565 | - } |
|
| 566 | - self::update($ctx, $block, 128); |
|
| 567 | - $ctx[4] = 128; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - return $ctx; |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - /** |
|
| 574 | - * Convert a string into an SplFixedArray of integers |
|
| 575 | - * |
|
| 576 | - * @internal You should not use this directly from another application |
|
| 577 | - * |
|
| 578 | - * @param string $str |
|
| 579 | - * @return SplFixedArray |
|
| 580 | - * @psalm-suppress MixedArgumentTypeCoercion |
|
| 581 | - */ |
|
| 582 | - public static function stringToSplFixedArray($str = '') |
|
| 583 | - { |
|
| 584 | - $values = unpack('C*', $str); |
|
| 585 | - return SplFixedArray::fromArray(array_values($values)); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - /** |
|
| 589 | - * Convert an SplFixedArray of integers into a string |
|
| 590 | - * |
|
| 591 | - * @internal You should not use this directly from another application |
|
| 592 | - * |
|
| 593 | - * @param SplFixedArray $a |
|
| 594 | - * @return string |
|
| 595 | - */ |
|
| 596 | - public static function SplFixedArrayToString(SplFixedArray $a) |
|
| 597 | - { |
|
| 598 | - /** |
|
| 599 | - * @var array<int, string|int> |
|
| 600 | - */ |
|
| 601 | - $arr = $a->toArray(); |
|
| 602 | - $c = $a->count(); |
|
| 603 | - array_unshift($arr, str_repeat('C', $c)); |
|
| 604 | - return (string) (call_user_func_array('pack', $arr)); |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * @internal You should not use this directly from another application |
|
| 609 | - * |
|
| 610 | - * @param SplFixedArray $ctx |
|
| 611 | - * @return string |
|
| 612 | - * @throws TypeError |
|
| 613 | - * @psalm-suppress MixedArgument |
|
| 614 | - * @psalm-suppress MixedArrayAccess |
|
| 615 | - * @psalm-suppress MixedArrayAssignment |
|
| 616 | - * @psalm-suppress MixedMethodCall |
|
| 617 | - */ |
|
| 618 | - public static function contextToString(SplFixedArray $ctx) |
|
| 619 | - { |
|
| 620 | - $str = ''; |
|
| 621 | - /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ |
|
| 622 | - $ctxA = $ctx[0]->toArray(); |
|
| 623 | - |
|
| 624 | - # uint64_t h[8]; |
|
| 625 | - for ($i = 0; $i < 8; ++$i) { |
|
| 626 | - if (!($ctxA[$i] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
| 627 | - throw new TypeError('Not an instance of Int64'); |
|
| 628 | - } |
|
| 629 | - /** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */ |
|
| 630 | - $ctxAi = $ctxA[$i]; |
|
| 631 | - $str .= $ctxAi->toReverseString(); |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - # uint64_t t[2]; |
|
| 635 | - # uint64_t f[2]; |
|
| 636 | - for ($i = 1; $i < 3; ++$i) { |
|
| 637 | - /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ |
|
| 638 | - $ctxA = $ctx[$i]->toArray(); |
|
| 639 | - /** @var ParagonIE_Sodium_Core32_Int64 $ctxA1 */ |
|
| 640 | - $ctxA1 = $ctxA[0]; |
|
| 641 | - /** @var ParagonIE_Sodium_Core32_Int64 $ctxA2 */ |
|
| 642 | - $ctxA2 = $ctxA[1]; |
|
| 643 | - |
|
| 644 | - $str .= $ctxA1->toReverseString(); |
|
| 645 | - $str .= $ctxA2->toReverseString(); |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - # uint8_t buf[2 * 128]; |
|
| 649 | - $str .= self::SplFixedArrayToString($ctx[3]); |
|
| 650 | - |
|
| 651 | - /** @var int $ctx4 */ |
|
| 652 | - $ctx4 = $ctx[4]; |
|
| 653 | - |
|
| 654 | - # size_t buflen; |
|
| 655 | - $str .= implode('', array( |
|
| 656 | - self::intToChr($ctx4 & 0xff), |
|
| 657 | - self::intToChr(($ctx4 >> 8) & 0xff), |
|
| 658 | - self::intToChr(($ctx4 >> 16) & 0xff), |
|
| 659 | - self::intToChr(($ctx4 >> 24) & 0xff), |
|
| 660 | - "\x00\x00\x00\x00" |
|
| 661 | - /* |
|
| 14 | + /** |
|
| 15 | + * @var SplFixedArray |
|
| 16 | + */ |
|
| 17 | + public static $iv; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var array<int, array<int, int>> |
|
| 21 | + */ |
|
| 22 | + public static $sigma = array( |
|
| 23 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
| 24 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), |
|
| 25 | + array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), |
|
| 26 | + array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), |
|
| 27 | + array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), |
|
| 28 | + array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), |
|
| 29 | + array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), |
|
| 30 | + array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), |
|
| 31 | + array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), |
|
| 32 | + array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), |
|
| 33 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
| 34 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) |
|
| 35 | + ); |
|
| 36 | + |
|
| 37 | + const BLOCKBYTES = 128; |
|
| 38 | + const OUTBYTES = 64; |
|
| 39 | + const KEYBYTES = 64; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Turn two 32-bit integers into a fixed array representing a 64-bit integer. |
|
| 43 | + * |
|
| 44 | + * @internal You should not use this directly from another application |
|
| 45 | + * |
|
| 46 | + * @param int $high |
|
| 47 | + * @param int $low |
|
| 48 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 49 | + * @throws SodiumException |
|
| 50 | + * @throws TypeError |
|
| 51 | + */ |
|
| 52 | + public static function new64($high, $low) |
|
| 53 | + { |
|
| 54 | + return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Convert an arbitrary number into an SplFixedArray of two 32-bit integers |
|
| 59 | + * that represents a 64-bit integer. |
|
| 60 | + * |
|
| 61 | + * @internal You should not use this directly from another application |
|
| 62 | + * |
|
| 63 | + * @param int $num |
|
| 64 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 65 | + * @throws SodiumException |
|
| 66 | + * @throws TypeError |
|
| 67 | + */ |
|
| 68 | + protected static function to64($num) |
|
| 69 | + { |
|
| 70 | + list($hi, $lo) = self::numericTo64BitInteger($num); |
|
| 71 | + return self::new64($hi, $lo); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Adds two 64-bit integers together, returning their sum as a SplFixedArray |
|
| 76 | + * containing two 32-bit integers (representing a 64-bit integer). |
|
| 77 | + * |
|
| 78 | + * @internal You should not use this directly from another application |
|
| 79 | + * |
|
| 80 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
| 81 | + * @param ParagonIE_Sodium_Core32_Int64 $y |
|
| 82 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 83 | + */ |
|
| 84 | + protected static function add64($x, $y) |
|
| 85 | + { |
|
| 86 | + return $x->addInt64($y); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @internal You should not use this directly from another application |
|
| 91 | + * |
|
| 92 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
| 93 | + * @param ParagonIE_Sodium_Core32_Int64 $y |
|
| 94 | + * @param ParagonIE_Sodium_Core32_Int64 $z |
|
| 95 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 96 | + */ |
|
| 97 | + public static function add364($x, $y, $z) |
|
| 98 | + { |
|
| 99 | + return $x->addInt64($y)->addInt64($z); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @internal You should not use this directly from another application |
|
| 104 | + * |
|
| 105 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
| 106 | + * @param ParagonIE_Sodium_Core32_Int64 $y |
|
| 107 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 108 | + * @throws TypeError |
|
| 109 | + */ |
|
| 110 | + public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) |
|
| 111 | + { |
|
| 112 | + return $x->xorInt64($y); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @internal You should not use this directly from another application |
|
| 117 | + * |
|
| 118 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
| 119 | + * @param int $c |
|
| 120 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 121 | + * @throws SodiumException |
|
| 122 | + * @throws TypeError |
|
| 123 | + */ |
|
| 124 | + public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) |
|
| 125 | + { |
|
| 126 | + return $x->rotateRight($c); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @internal You should not use this directly from another application |
|
| 131 | + * |
|
| 132 | + * @param SplFixedArray $x |
|
| 133 | + * @param int $i |
|
| 134 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 135 | + * @throws SodiumException |
|
| 136 | + * @throws TypeError |
|
| 137 | + */ |
|
| 138 | + public static function load64($x, $i) |
|
| 139 | + { |
|
| 140 | + /** @var int $l */ |
|
| 141 | + $l = (int) ($x[$i]) |
|
| 142 | + | ((int) ($x[$i+1]) << 8) |
|
| 143 | + | ((int) ($x[$i+2]) << 16) |
|
| 144 | + | ((int) ($x[$i+3]) << 24); |
|
| 145 | + /** @var int $h */ |
|
| 146 | + $h = (int) ($x[$i+4]) |
|
| 147 | + | ((int) ($x[$i+5]) << 8) |
|
| 148 | + | ((int) ($x[$i+6]) << 16) |
|
| 149 | + | ((int) ($x[$i+7]) << 24); |
|
| 150 | + return self::new64($h, $l); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @internal You should not use this directly from another application |
|
| 155 | + * |
|
| 156 | + * @param SplFixedArray $x |
|
| 157 | + * @param int $i |
|
| 158 | + * @param ParagonIE_Sodium_Core32_Int64 $u |
|
| 159 | + * @return void |
|
| 160 | + * @throws TypeError |
|
| 161 | + * @psalm-suppress MixedArgument |
|
| 162 | + * @psalm-suppress MixedAssignment |
|
| 163 | + * @psalm-suppress MixedArrayAccess |
|
| 164 | + * @psalm-suppress MixedArrayAssignment |
|
| 165 | + * @psalm-suppress MixedArrayOffset |
|
| 166 | + */ |
|
| 167 | + public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) |
|
| 168 | + { |
|
| 169 | + $v = clone $u; |
|
| 170 | + $maxLength = $x->getSize() - 1; |
|
| 171 | + for ($j = 0; $j < 8; ++$j) { |
|
| 172 | + $k = 3 - ($j >> 1); |
|
| 173 | + $x[$i] = $v->limbs[$k] & 0xff; |
|
| 174 | + if (++$i > $maxLength) { |
|
| 175 | + return; |
|
| 176 | + } |
|
| 177 | + $v->limbs[$k] >>= 8; |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * This just sets the $iv static variable. |
|
| 183 | + * |
|
| 184 | + * @internal You should not use this directly from another application |
|
| 185 | + * |
|
| 186 | + * @return void |
|
| 187 | + * @throws SodiumException |
|
| 188 | + * @throws TypeError |
|
| 189 | + */ |
|
| 190 | + public static function pseudoConstructor() |
|
| 191 | + { |
|
| 192 | + static $called = false; |
|
| 193 | + if ($called) { |
|
| 194 | + return; |
|
| 195 | + } |
|
| 196 | + self::$iv = new SplFixedArray(8); |
|
| 197 | + self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
|
| 198 | + self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
|
| 199 | + self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
|
| 200 | + self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
|
| 201 | + self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
|
| 202 | + self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
|
| 203 | + self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
|
| 204 | + self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
|
| 205 | + |
|
| 206 | + $called = true; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Returns a fresh BLAKE2 context. |
|
| 211 | + * |
|
| 212 | + * @internal You should not use this directly from another application |
|
| 213 | + * |
|
| 214 | + * @return SplFixedArray |
|
| 215 | + * @throws TypeError |
|
| 216 | + * @psalm-suppress MixedArgument |
|
| 217 | + * @psalm-suppress MixedAssignment |
|
| 218 | + * @psalm-suppress MixedArrayAccess |
|
| 219 | + * @psalm-suppress MixedArrayAssignment |
|
| 220 | + * @psalm-suppress MixedArrayOffset |
|
| 221 | + * @throws SodiumException |
|
| 222 | + * @throws TypeError |
|
| 223 | + */ |
|
| 224 | + protected static function context() |
|
| 225 | + { |
|
| 226 | + $ctx = new SplFixedArray(6); |
|
| 227 | + $ctx[0] = new SplFixedArray(8); // h |
|
| 228 | + $ctx[1] = new SplFixedArray(2); // t |
|
| 229 | + $ctx[2] = new SplFixedArray(2); // f |
|
| 230 | + $ctx[3] = new SplFixedArray(256); // buf |
|
| 231 | + $ctx[4] = 0; // buflen |
|
| 232 | + $ctx[5] = 0; // last_node (uint8_t) |
|
| 233 | + |
|
| 234 | + for ($i = 8; $i--;) { |
|
| 235 | + $ctx[0][$i] = self::$iv[$i]; |
|
| 236 | + } |
|
| 237 | + for ($i = 256; $i--;) { |
|
| 238 | + $ctx[3][$i] = 0; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $zero = self::new64(0, 0); |
|
| 242 | + $ctx[1][0] = $zero; |
|
| 243 | + $ctx[1][1] = $zero; |
|
| 244 | + $ctx[2][0] = $zero; |
|
| 245 | + $ctx[2][1] = $zero; |
|
| 246 | + |
|
| 247 | + return $ctx; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @internal You should not use this directly from another application |
|
| 252 | + * |
|
| 253 | + * @param SplFixedArray $ctx |
|
| 254 | + * @param SplFixedArray $buf |
|
| 255 | + * @return void |
|
| 256 | + * @throws SodiumException |
|
| 257 | + * @throws TypeError |
|
| 258 | + * @psalm-suppress MixedArgument |
|
| 259 | + * @psalm-suppress MixedArrayAccess |
|
| 260 | + * @psalm-suppress MixedArrayAssignment |
|
| 261 | + * @psalm-suppress MixedAssignment |
|
| 262 | + */ |
|
| 263 | + protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
| 264 | + { |
|
| 265 | + $m = new SplFixedArray(16); |
|
| 266 | + $v = new SplFixedArray(16); |
|
| 267 | + |
|
| 268 | + for ($i = 16; $i--;) { |
|
| 269 | + $m[$i] = self::load64($buf, $i << 3); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + for ($i = 8; $i--;) { |
|
| 273 | + $v[$i] = $ctx[0][$i]; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + $v[ 8] = self::$iv[0]; |
|
| 277 | + $v[ 9] = self::$iv[1]; |
|
| 278 | + $v[10] = self::$iv[2]; |
|
| 279 | + $v[11] = self::$iv[3]; |
|
| 280 | + |
|
| 281 | + $v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
|
| 282 | + $v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
|
| 283 | + $v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
|
| 284 | + $v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
|
| 285 | + |
|
| 286 | + for ($r = 0; $r < 12; ++$r) { |
|
| 287 | + $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
|
| 288 | + $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
|
| 289 | + $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
|
| 290 | + $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
|
| 291 | + $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
|
| 292 | + $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
|
| 293 | + $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
|
| 294 | + $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + for ($i = 8; $i--;) { |
|
| 298 | + $ctx[0][$i] = self::xor64( |
|
| 299 | + $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
| 300 | + ); |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * @internal You should not use this directly from another application |
|
| 306 | + * |
|
| 307 | + * @param int $r |
|
| 308 | + * @param int $i |
|
| 309 | + * @param int $a |
|
| 310 | + * @param int $b |
|
| 311 | + * @param int $c |
|
| 312 | + * @param int $d |
|
| 313 | + * @param SplFixedArray $v |
|
| 314 | + * @param SplFixedArray $m |
|
| 315 | + * @return SplFixedArray |
|
| 316 | + * @throws SodiumException |
|
| 317 | + * @throws TypeError |
|
| 318 | + * @psalm-suppress MixedArgument |
|
| 319 | + * @psalm-suppress MixedArrayOffset |
|
| 320 | + */ |
|
| 321 | + public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
| 322 | + { |
|
| 323 | + $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
|
| 324 | + $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
|
| 325 | + $v[$c] = self::add64($v[$c], $v[$d]); |
|
| 326 | + $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
|
| 327 | + $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); |
|
| 328 | + $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
|
| 329 | + $v[$c] = self::add64($v[$c], $v[$d]); |
|
| 330 | + $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
|
| 331 | + return $v; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * @internal You should not use this directly from another application |
|
| 336 | + * |
|
| 337 | + * @param SplFixedArray $ctx |
|
| 338 | + * @param int $inc |
|
| 339 | + * @return void |
|
| 340 | + * @throws SodiumException |
|
| 341 | + * @throws TypeError |
|
| 342 | + * @psalm-suppress MixedArgument |
|
| 343 | + * @psalm-suppress MixedArrayAccess |
|
| 344 | + * @psalm-suppress MixedArrayAssignment |
|
| 345 | + */ |
|
| 346 | + public static function increment_counter($ctx, $inc) |
|
| 347 | + { |
|
| 348 | + if ($inc < 0) { |
|
| 349 | + throw new SodiumException('Increasing by a negative number makes no sense.'); |
|
| 350 | + } |
|
| 351 | + $t = self::to64($inc); |
|
| 352 | + # S->t is $ctx[1] in our implementation |
|
| 353 | + |
|
| 354 | + # S->t[0] = ( uint64_t )( t >> 0 ); |
|
| 355 | + $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
| 356 | + |
|
| 357 | + # S->t[1] += ( S->t[0] < inc ); |
|
| 358 | + if (!($ctx[1][0] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
| 359 | + throw new TypeError('Not an int64'); |
|
| 360 | + } |
|
| 361 | + /** @var ParagonIE_Sodium_Core32_Int64 $c*/ |
|
| 362 | + $c = $ctx[1][0]; |
|
| 363 | + if ($c->isLessThanInt($inc)) { |
|
| 364 | + $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * @internal You should not use this directly from another application |
|
| 370 | + * |
|
| 371 | + * @param SplFixedArray $ctx |
|
| 372 | + * @param SplFixedArray $p |
|
| 373 | + * @param int $plen |
|
| 374 | + * @return void |
|
| 375 | + * @throws SodiumException |
|
| 376 | + * @throws TypeError |
|
| 377 | + * @psalm-suppress MixedArgument |
|
| 378 | + * @psalm-suppress MixedAssignment |
|
| 379 | + * @psalm-suppress MixedArrayAccess |
|
| 380 | + * @psalm-suppress MixedArrayAssignment |
|
| 381 | + * @psalm-suppress MixedArrayOffset |
|
| 382 | + * @psalm-suppress MixedMethodCall |
|
| 383 | + * @psalm-suppress MixedOperand |
|
| 384 | + */ |
|
| 385 | + public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
| 386 | + { |
|
| 387 | + self::pseudoConstructor(); |
|
| 388 | + |
|
| 389 | + $offset = 0; |
|
| 390 | + while ($plen > 0) { |
|
| 391 | + $left = $ctx[4]; |
|
| 392 | + $fill = 256 - $left; |
|
| 393 | + |
|
| 394 | + if ($plen > $fill) { |
|
| 395 | + # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
|
| 396 | + for ($i = $fill; $i--;) { |
|
| 397 | + $ctx[3][$i + $left] = $p[$i + $offset]; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + # S->buflen += fill; |
|
| 401 | + $ctx[4] += $fill; |
|
| 402 | + |
|
| 403 | + # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
|
| 404 | + self::increment_counter($ctx, 128); |
|
| 405 | + |
|
| 406 | + # blake2b_compress( S, S->buf ); /* Compress */ |
|
| 407 | + self::compress($ctx, $ctx[3]); |
|
| 408 | + |
|
| 409 | + # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
|
| 410 | + for ($i = 128; $i--;) { |
|
| 411 | + $ctx[3][$i] = $ctx[3][$i + 128]; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + # S->buflen -= BLAKE2B_BLOCKBYTES; |
|
| 415 | + $ctx[4] -= 128; |
|
| 416 | + |
|
| 417 | + # in += fill; |
|
| 418 | + $offset += $fill; |
|
| 419 | + |
|
| 420 | + # inlen -= fill; |
|
| 421 | + $plen -= $fill; |
|
| 422 | + } else { |
|
| 423 | + for ($i = $plen; $i--;) { |
|
| 424 | + $ctx[3][$i + $left] = $p[$i + $offset]; |
|
| 425 | + } |
|
| 426 | + $ctx[4] += $plen; |
|
| 427 | + $offset += $plen; |
|
| 428 | + $plen -= $plen; |
|
| 429 | + } |
|
| 430 | + } |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * @internal You should not use this directly from another application |
|
| 435 | + * |
|
| 436 | + * @param SplFixedArray $ctx |
|
| 437 | + * @param SplFixedArray $out |
|
| 438 | + * @return SplFixedArray |
|
| 439 | + * @throws SodiumException |
|
| 440 | + * @throws TypeError |
|
| 441 | + * @psalm-suppress MixedArgument |
|
| 442 | + * @psalm-suppress MixedAssignment |
|
| 443 | + * @psalm-suppress MixedArrayAccess |
|
| 444 | + * @psalm-suppress MixedArrayAssignment |
|
| 445 | + * @psalm-suppress MixedArrayOffset |
|
| 446 | + * @psalm-suppress MixedMethodCall |
|
| 447 | + * @psalm-suppress MixedOperand |
|
| 448 | + */ |
|
| 449 | + public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
| 450 | + { |
|
| 451 | + self::pseudoConstructor(); |
|
| 452 | + if ($ctx[4] > 128) { |
|
| 453 | + self::increment_counter($ctx, 128); |
|
| 454 | + self::compress($ctx, $ctx[3]); |
|
| 455 | + $ctx[4] -= 128; |
|
| 456 | + if ($ctx[4] > 128) { |
|
| 457 | + throw new SodiumException('Failed to assert that buflen <= 128 bytes'); |
|
| 458 | + } |
|
| 459 | + for ($i = $ctx[4]; $i--;) { |
|
| 460 | + $ctx[3][$i] = $ctx[3][$i + 128]; |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + self::increment_counter($ctx, $ctx[4]); |
|
| 465 | + $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
| 466 | + |
|
| 467 | + for ($i = 256 - $ctx[4]; $i--;) { |
|
| 468 | + /** @var int $i */ |
|
| 469 | + $ctx[3][$i + $ctx[4]] = 0; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + self::compress($ctx, $ctx[3]); |
|
| 473 | + |
|
| 474 | + $i = (int) (($out->getSize() - 1) / 8); |
|
| 475 | + for (; $i >= 0; --$i) { |
|
| 476 | + self::store64($out, $i << 3, $ctx[0][$i]); |
|
| 477 | + } |
|
| 478 | + return $out; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * @internal You should not use this directly from another application |
|
| 483 | + * |
|
| 484 | + * @param SplFixedArray|null $key |
|
| 485 | + * @param int $outlen |
|
| 486 | + * @param SplFixedArray|null $salt |
|
| 487 | + * @param SplFixedArray|null $personal |
|
| 488 | + * @return SplFixedArray |
|
| 489 | + * @throws SodiumException |
|
| 490 | + * @throws TypeError |
|
| 491 | + * @psalm-suppress MixedArgument |
|
| 492 | + * @psalm-suppress MixedAssignment |
|
| 493 | + * @psalm-suppress MixedArrayAccess |
|
| 494 | + * @psalm-suppress MixedArrayAssignment |
|
| 495 | + * @psalm-suppress MixedMethodCall |
|
| 496 | + */ |
|
| 497 | + public static function init( |
|
| 498 | + $key = null, |
|
| 499 | + $outlen = 64, |
|
| 500 | + $salt = null, |
|
| 501 | + $personal = null |
|
| 502 | + ) { |
|
| 503 | + self::pseudoConstructor(); |
|
| 504 | + $klen = 0; |
|
| 505 | + |
|
| 506 | + if ($key !== null) { |
|
| 507 | + if (count($key) > 64) { |
|
| 508 | + throw new SodiumException('Invalid key size'); |
|
| 509 | + } |
|
| 510 | + $klen = count($key); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + if ($outlen > 64) { |
|
| 514 | + throw new SodiumException('Invalid output size'); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + $ctx = self::context(); |
|
| 518 | + |
|
| 519 | + $p = new SplFixedArray(64); |
|
| 520 | + // Zero our param buffer... |
|
| 521 | + for ($i = 64; --$i;) { |
|
| 522 | + $p[$i] = 0; |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + $p[0] = $outlen; // digest_length |
|
| 526 | + $p[1] = $klen; // key_length |
|
| 527 | + $p[2] = 1; // fanout |
|
| 528 | + $p[3] = 1; // depth |
|
| 529 | + |
|
| 530 | + if ($salt instanceof SplFixedArray) { |
|
| 531 | + // salt: [32] through [47] |
|
| 532 | + for ($i = 0; $i < 16; ++$i) { |
|
| 533 | + $p[32 + $i] = (int) $salt[$i]; |
|
| 534 | + } |
|
| 535 | + } |
|
| 536 | + if ($personal instanceof SplFixedArray) { |
|
| 537 | + // personal: [48] through [63] |
|
| 538 | + for ($i = 0; $i < 16; ++$i) { |
|
| 539 | + $p[48 + $i] = (int) $personal[$i]; |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + $ctx[0][0] = self::xor64( |
|
| 544 | + $ctx[0][0], |
|
| 545 | + self::load64($p, 0) |
|
| 546 | + ); |
|
| 547 | + |
|
| 548 | + if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { |
|
| 549 | + // We need to do what blake2b_init_param() does: |
|
| 550 | + for ($i = 1; $i < 8; ++$i) { |
|
| 551 | + $ctx[0][$i] = self::xor64( |
|
| 552 | + $ctx[0][$i], |
|
| 553 | + self::load64($p, $i << 3) |
|
| 554 | + ); |
|
| 555 | + } |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + if ($klen > 0 && $key instanceof SplFixedArray) { |
|
| 559 | + $block = new SplFixedArray(128); |
|
| 560 | + for ($i = 128; $i--;) { |
|
| 561 | + $block[$i] = 0; |
|
| 562 | + } |
|
| 563 | + for ($i = $klen; $i--;) { |
|
| 564 | + $block[$i] = $key[$i]; |
|
| 565 | + } |
|
| 566 | + self::update($ctx, $block, 128); |
|
| 567 | + $ctx[4] = 128; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + return $ctx; |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + /** |
|
| 574 | + * Convert a string into an SplFixedArray of integers |
|
| 575 | + * |
|
| 576 | + * @internal You should not use this directly from another application |
|
| 577 | + * |
|
| 578 | + * @param string $str |
|
| 579 | + * @return SplFixedArray |
|
| 580 | + * @psalm-suppress MixedArgumentTypeCoercion |
|
| 581 | + */ |
|
| 582 | + public static function stringToSplFixedArray($str = '') |
|
| 583 | + { |
|
| 584 | + $values = unpack('C*', $str); |
|
| 585 | + return SplFixedArray::fromArray(array_values($values)); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + /** |
|
| 589 | + * Convert an SplFixedArray of integers into a string |
|
| 590 | + * |
|
| 591 | + * @internal You should not use this directly from another application |
|
| 592 | + * |
|
| 593 | + * @param SplFixedArray $a |
|
| 594 | + * @return string |
|
| 595 | + */ |
|
| 596 | + public static function SplFixedArrayToString(SplFixedArray $a) |
|
| 597 | + { |
|
| 598 | + /** |
|
| 599 | + * @var array<int, string|int> |
|
| 600 | + */ |
|
| 601 | + $arr = $a->toArray(); |
|
| 602 | + $c = $a->count(); |
|
| 603 | + array_unshift($arr, str_repeat('C', $c)); |
|
| 604 | + return (string) (call_user_func_array('pack', $arr)); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * @internal You should not use this directly from another application |
|
| 609 | + * |
|
| 610 | + * @param SplFixedArray $ctx |
|
| 611 | + * @return string |
|
| 612 | + * @throws TypeError |
|
| 613 | + * @psalm-suppress MixedArgument |
|
| 614 | + * @psalm-suppress MixedArrayAccess |
|
| 615 | + * @psalm-suppress MixedArrayAssignment |
|
| 616 | + * @psalm-suppress MixedMethodCall |
|
| 617 | + */ |
|
| 618 | + public static function contextToString(SplFixedArray $ctx) |
|
| 619 | + { |
|
| 620 | + $str = ''; |
|
| 621 | + /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ |
|
| 622 | + $ctxA = $ctx[0]->toArray(); |
|
| 623 | + |
|
| 624 | + # uint64_t h[8]; |
|
| 625 | + for ($i = 0; $i < 8; ++$i) { |
|
| 626 | + if (!($ctxA[$i] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
| 627 | + throw new TypeError('Not an instance of Int64'); |
|
| 628 | + } |
|
| 629 | + /** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */ |
|
| 630 | + $ctxAi = $ctxA[$i]; |
|
| 631 | + $str .= $ctxAi->toReverseString(); |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + # uint64_t t[2]; |
|
| 635 | + # uint64_t f[2]; |
|
| 636 | + for ($i = 1; $i < 3; ++$i) { |
|
| 637 | + /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ |
|
| 638 | + $ctxA = $ctx[$i]->toArray(); |
|
| 639 | + /** @var ParagonIE_Sodium_Core32_Int64 $ctxA1 */ |
|
| 640 | + $ctxA1 = $ctxA[0]; |
|
| 641 | + /** @var ParagonIE_Sodium_Core32_Int64 $ctxA2 */ |
|
| 642 | + $ctxA2 = $ctxA[1]; |
|
| 643 | + |
|
| 644 | + $str .= $ctxA1->toReverseString(); |
|
| 645 | + $str .= $ctxA2->toReverseString(); |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + # uint8_t buf[2 * 128]; |
|
| 649 | + $str .= self::SplFixedArrayToString($ctx[3]); |
|
| 650 | + |
|
| 651 | + /** @var int $ctx4 */ |
|
| 652 | + $ctx4 = $ctx[4]; |
|
| 653 | + |
|
| 654 | + # size_t buflen; |
|
| 655 | + $str .= implode('', array( |
|
| 656 | + self::intToChr($ctx4 & 0xff), |
|
| 657 | + self::intToChr(($ctx4 >> 8) & 0xff), |
|
| 658 | + self::intToChr(($ctx4 >> 16) & 0xff), |
|
| 659 | + self::intToChr(($ctx4 >> 24) & 0xff), |
|
| 660 | + "\x00\x00\x00\x00" |
|
| 661 | + /* |
|
| 662 | 662 | self::intToChr(($ctx4 >> 32) & 0xff), |
| 663 | 663 | self::intToChr(($ctx4 >> 40) & 0xff), |
| 664 | 664 | self::intToChr(($ctx4 >> 48) & 0xff), |
| 665 | 665 | self::intToChr(($ctx4 >> 56) & 0xff) |
| 666 | 666 | */ |
| 667 | - )); |
|
| 668 | - # uint8_t last_node; |
|
| 669 | - return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - /** |
|
| 673 | - * Creates an SplFixedArray containing other SplFixedArray elements, from |
|
| 674 | - * a string (compatible with \Sodium\crypto_generichash_{init, update, final}) |
|
| 675 | - * |
|
| 676 | - * @internal You should not use this directly from another application |
|
| 677 | - * |
|
| 678 | - * @param string $string |
|
| 679 | - * @return SplFixedArray |
|
| 680 | - * @throws SodiumException |
|
| 681 | - * @throws TypeError |
|
| 682 | - * @psalm-suppress MixedArrayAccess |
|
| 683 | - * @psalm-suppress MixedArrayAssignment |
|
| 684 | - */ |
|
| 685 | - public static function stringToContext($string) |
|
| 686 | - { |
|
| 687 | - $ctx = self::context(); |
|
| 688 | - |
|
| 689 | - # uint64_t h[8]; |
|
| 690 | - for ($i = 0; $i < 8; ++$i) { |
|
| 691 | - $ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 692 | - self::substr($string, (($i << 3) + 0), 8) |
|
| 693 | - ); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - # uint64_t t[2]; |
|
| 697 | - # uint64_t f[2]; |
|
| 698 | - for ($i = 1; $i < 3; ++$i) { |
|
| 699 | - $ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 700 | - self::substr($string, 72 + (($i - 1) << 4), 8) |
|
| 701 | - ); |
|
| 702 | - $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 703 | - self::substr($string, 64 + (($i - 1) << 4), 8) |
|
| 704 | - ); |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - # uint8_t buf[2 * 128]; |
|
| 708 | - $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
| 709 | - |
|
| 710 | - # uint8_t buf[2 * 128]; |
|
| 711 | - $int = 0; |
|
| 712 | - for ($i = 0; $i < 8; ++$i) { |
|
| 713 | - $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
| 714 | - } |
|
| 715 | - $ctx[4] = $int; |
|
| 716 | - |
|
| 717 | - return $ctx; |
|
| 718 | - } |
|
| 667 | + )); |
|
| 668 | + # uint8_t last_node; |
|
| 669 | + return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + /** |
|
| 673 | + * Creates an SplFixedArray containing other SplFixedArray elements, from |
|
| 674 | + * a string (compatible with \Sodium\crypto_generichash_{init, update, final}) |
|
| 675 | + * |
|
| 676 | + * @internal You should not use this directly from another application |
|
| 677 | + * |
|
| 678 | + * @param string $string |
|
| 679 | + * @return SplFixedArray |
|
| 680 | + * @throws SodiumException |
|
| 681 | + * @throws TypeError |
|
| 682 | + * @psalm-suppress MixedArrayAccess |
|
| 683 | + * @psalm-suppress MixedArrayAssignment |
|
| 684 | + */ |
|
| 685 | + public static function stringToContext($string) |
|
| 686 | + { |
|
| 687 | + $ctx = self::context(); |
|
| 688 | + |
|
| 689 | + # uint64_t h[8]; |
|
| 690 | + for ($i = 0; $i < 8; ++$i) { |
|
| 691 | + $ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 692 | + self::substr($string, (($i << 3) + 0), 8) |
|
| 693 | + ); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + # uint64_t t[2]; |
|
| 697 | + # uint64_t f[2]; |
|
| 698 | + for ($i = 1; $i < 3; ++$i) { |
|
| 699 | + $ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 700 | + self::substr($string, 72 + (($i - 1) << 4), 8) |
|
| 701 | + ); |
|
| 702 | + $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 703 | + self::substr($string, 64 + (($i - 1) << 4), 8) |
|
| 704 | + ); |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + # uint8_t buf[2 * 128]; |
|
| 708 | + $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
| 709 | + |
|
| 710 | + # uint8_t buf[2 * 128]; |
|
| 711 | + $int = 0; |
|
| 712 | + for ($i = 0; $i < 8; ++$i) { |
|
| 713 | + $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
| 714 | + } |
|
| 715 | + $ctx[4] = $int; |
|
| 716 | + |
|
| 717 | + return $ctx; |
|
| 718 | + } |
|
| 719 | 719 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Ed25519', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,474 +9,474 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | abstract class ParagonIE_Sodium_Core32_Ed25519 extends ParagonIE_Sodium_Core32_Curve25519 |
| 11 | 11 | { |
| 12 | - const KEYPAIR_BYTES = 96; |
|
| 13 | - const SEED_BYTES = 32; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * @internal You should not use this directly from another application |
|
| 17 | - * |
|
| 18 | - * @return string (96 bytes) |
|
| 19 | - * @throws Exception |
|
| 20 | - * @throws SodiumException |
|
| 21 | - * @throws TypeError |
|
| 22 | - */ |
|
| 23 | - public static function keypair() |
|
| 24 | - { |
|
| 25 | - $seed = random_bytes(self::SEED_BYTES); |
|
| 26 | - $pk = ''; |
|
| 27 | - $sk = ''; |
|
| 28 | - self::seed_keypair($pk, $sk, $seed); |
|
| 29 | - return $sk . $pk; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @internal You should not use this directly from another application |
|
| 34 | - * |
|
| 35 | - * @param string $pk |
|
| 36 | - * @param string $sk |
|
| 37 | - * @param string $seed |
|
| 38 | - * @return string |
|
| 39 | - * @throws SodiumException |
|
| 40 | - * @throws TypeError |
|
| 41 | - */ |
|
| 42 | - public static function seed_keypair(&$pk, &$sk, $seed) |
|
| 43 | - { |
|
| 44 | - if (self::strlen($seed) !== self::SEED_BYTES) { |
|
| 45 | - throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** @var string $pk */ |
|
| 49 | - $pk = self::publickey_from_secretkey($seed); |
|
| 50 | - $sk = $seed . $pk; |
|
| 51 | - return $sk; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @internal You should not use this directly from another application |
|
| 56 | - * |
|
| 57 | - * @param string $keypair |
|
| 58 | - * @return string |
|
| 59 | - * @throws TypeError |
|
| 60 | - */ |
|
| 61 | - public static function secretkey($keypair) |
|
| 62 | - { |
|
| 63 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
| 64 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
| 65 | - } |
|
| 66 | - return self::substr($keypair, 0, 64); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @internal You should not use this directly from another application |
|
| 71 | - * |
|
| 72 | - * @param string $keypair |
|
| 73 | - * @return string |
|
| 74 | - * @throws RangeException |
|
| 75 | - * @throws TypeError |
|
| 76 | - */ |
|
| 77 | - public static function publickey($keypair) |
|
| 78 | - { |
|
| 79 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
| 80 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
| 81 | - } |
|
| 82 | - return self::substr($keypair, 64, 32); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @internal You should not use this directly from another application |
|
| 87 | - * |
|
| 88 | - * @param string $sk |
|
| 89 | - * @return string |
|
| 90 | - * @throws SodiumException |
|
| 91 | - * @throws TypeError |
|
| 92 | - */ |
|
| 93 | - public static function publickey_from_secretkey($sk) |
|
| 94 | - { |
|
| 95 | - /** @var string $sk */ |
|
| 96 | - $sk = hash('sha512', self::substr($sk, 0, 32), true); |
|
| 97 | - $sk[0] = self::intToChr( |
|
| 98 | - self::chrToInt($sk[0]) & 248 |
|
| 99 | - ); |
|
| 100 | - $sk[31] = self::intToChr( |
|
| 101 | - (self::chrToInt($sk[31]) & 63) | 64 |
|
| 102 | - ); |
|
| 103 | - return self::sk_to_pk($sk); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @param string $pk |
|
| 108 | - * @return string |
|
| 109 | - * @throws SodiumException |
|
| 110 | - * @throws TypeError |
|
| 111 | - */ |
|
| 112 | - public static function pk_to_curve25519($pk) |
|
| 113 | - { |
|
| 114 | - if (self::small_order($pk)) { |
|
| 115 | - throw new SodiumException('Public key is on a small order'); |
|
| 116 | - } |
|
| 117 | - $A = self::ge_frombytes_negate_vartime($pk); |
|
| 118 | - $p1 = self::ge_mul_l($A); |
|
| 119 | - if (!self::fe_isnonzero($p1->X)) { |
|
| 120 | - throw new SodiumException('Unexpected zero result'); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - # fe_1(one_minus_y); |
|
| 124 | - # fe_sub(one_minus_y, one_minus_y, A.Y); |
|
| 125 | - # fe_invert(one_minus_y, one_minus_y); |
|
| 126 | - $one_minux_y = self::fe_invert( |
|
| 127 | - self::fe_sub( |
|
| 128 | - self::fe_1(), |
|
| 129 | - $A->Y |
|
| 130 | - ) |
|
| 131 | - ); |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - # fe_1(x); |
|
| 135 | - # fe_add(x, x, A.Y); |
|
| 136 | - # fe_mul(x, x, one_minus_y); |
|
| 137 | - $x = self::fe_mul( |
|
| 138 | - self::fe_add(self::fe_1(), $A->Y), |
|
| 139 | - $one_minux_y |
|
| 140 | - ); |
|
| 141 | - |
|
| 142 | - # fe_tobytes(curve25519_pk, x); |
|
| 143 | - return self::fe_tobytes($x); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @internal You should not use this directly from another application |
|
| 148 | - * |
|
| 149 | - * @param string $sk |
|
| 150 | - * @return string |
|
| 151 | - * @throws SodiumException |
|
| 152 | - * @throws TypeError |
|
| 153 | - */ |
|
| 154 | - public static function sk_to_pk($sk) |
|
| 155 | - { |
|
| 156 | - return self::ge_p3_tobytes( |
|
| 157 | - self::ge_scalarmult_base( |
|
| 158 | - self::substr($sk, 0, 32) |
|
| 159 | - ) |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @internal You should not use this directly from another application |
|
| 165 | - * |
|
| 166 | - * @param string $message |
|
| 167 | - * @param string $sk |
|
| 168 | - * @return string |
|
| 169 | - * @throws SodiumException |
|
| 170 | - * @throws TypeError |
|
| 171 | - */ |
|
| 172 | - public static function sign($message, $sk) |
|
| 173 | - { |
|
| 174 | - /** @var string $signature */ |
|
| 175 | - $signature = self::sign_detached($message, $sk); |
|
| 176 | - return $signature . $message; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @internal You should not use this directly from another application |
|
| 181 | - * |
|
| 182 | - * @param string $message A signed message |
|
| 183 | - * @param string $pk Public key |
|
| 184 | - * @return string Message (without signature) |
|
| 185 | - * @throws SodiumException |
|
| 186 | - * @throws TypeError |
|
| 187 | - */ |
|
| 188 | - public static function sign_open($message, $pk) |
|
| 189 | - { |
|
| 190 | - /** @var string $signature */ |
|
| 191 | - $signature = self::substr($message, 0, 64); |
|
| 192 | - |
|
| 193 | - /** @var string $message */ |
|
| 194 | - $message = self::substr($message, 64); |
|
| 195 | - |
|
| 196 | - if (self::verify_detached($signature, $message, $pk)) { |
|
| 197 | - return $message; |
|
| 198 | - } |
|
| 199 | - throw new SodiumException('Invalid signature'); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * @internal You should not use this directly from another application |
|
| 204 | - * |
|
| 205 | - * @param string $message |
|
| 206 | - * @param string $sk |
|
| 207 | - * @return string |
|
| 208 | - * @throws SodiumException |
|
| 209 | - * @throws TypeError |
|
| 210 | - * @psalm-suppress PossiblyInvalidArgument |
|
| 211 | - */ |
|
| 212 | - public static function sign_detached($message, $sk) |
|
| 213 | - { |
|
| 214 | - # crypto_hash_sha512(az, sk, 32); |
|
| 215 | - $az = hash('sha512', self::substr($sk, 0, 32), true); |
|
| 216 | - |
|
| 217 | - # az[0] &= 248; |
|
| 218 | - # az[31] &= 63; |
|
| 219 | - # az[31] |= 64; |
|
| 220 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
| 221 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
| 222 | - |
|
| 223 | - # crypto_hash_sha512_init(&hs); |
|
| 224 | - # crypto_hash_sha512_update(&hs, az + 32, 32); |
|
| 225 | - # crypto_hash_sha512_update(&hs, m, mlen); |
|
| 226 | - # crypto_hash_sha512_final(&hs, nonce); |
|
| 227 | - $hs = hash_init('sha512'); |
|
| 228 | - self::hash_update($hs, self::substr($az, 32, 32)); |
|
| 229 | - self::hash_update($hs, $message); |
|
| 230 | - $nonceHash = hash_final($hs, true); |
|
| 231 | - |
|
| 232 | - # memmove(sig + 32, sk + 32, 32); |
|
| 233 | - $pk = self::substr($sk, 32, 32); |
|
| 234 | - |
|
| 235 | - # sc_reduce(nonce); |
|
| 236 | - # ge_scalarmult_base(&R, nonce); |
|
| 237 | - # ge_p3_tobytes(sig, &R); |
|
| 238 | - $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
| 239 | - $sig = self::ge_p3_tobytes( |
|
| 240 | - self::ge_scalarmult_base($nonce) |
|
| 241 | - ); |
|
| 242 | - |
|
| 243 | - # crypto_hash_sha512_init(&hs); |
|
| 244 | - # crypto_hash_sha512_update(&hs, sig, 64); |
|
| 245 | - # crypto_hash_sha512_update(&hs, m, mlen); |
|
| 246 | - # crypto_hash_sha512_final(&hs, hram); |
|
| 247 | - $hs = hash_init('sha512'); |
|
| 248 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
| 249 | - self::hash_update($hs, self::substr($pk, 0, 32)); |
|
| 250 | - self::hash_update($hs, $message); |
|
| 251 | - $hramHash = hash_final($hs, true); |
|
| 252 | - |
|
| 253 | - # sc_reduce(hram); |
|
| 254 | - # sc_muladd(sig + 32, hram, az, nonce); |
|
| 255 | - $hram = self::sc_reduce($hramHash); |
|
| 256 | - $sigAfter = self::sc_muladd($hram, $az, $nonce); |
|
| 257 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
| 258 | - |
|
| 259 | - try { |
|
| 260 | - ParagonIE_Sodium_Compat::memzero($az); |
|
| 261 | - } catch (SodiumException $ex) { |
|
| 262 | - $az = null; |
|
| 263 | - } |
|
| 264 | - return $sig; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * @internal You should not use this directly from another application |
|
| 269 | - * |
|
| 270 | - * @param string $sig |
|
| 271 | - * @param string $message |
|
| 272 | - * @param string $pk |
|
| 273 | - * @return bool |
|
| 274 | - * @throws SodiumException |
|
| 275 | - * @throws TypeError |
|
| 276 | - */ |
|
| 277 | - public static function verify_detached($sig, $message, $pk) |
|
| 278 | - { |
|
| 279 | - if (self::strlen($sig) < 64) { |
|
| 280 | - throw new SodiumException('Signature is too short'); |
|
| 281 | - } |
|
| 282 | - if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
| 283 | - throw new SodiumException('S < L - Invalid signature'); |
|
| 284 | - } |
|
| 285 | - if (self::small_order($sig)) { |
|
| 286 | - throw new SodiumException('Signature is on too small of an order'); |
|
| 287 | - } |
|
| 288 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
| 289 | - throw new SodiumException('Invalid signature'); |
|
| 290 | - } |
|
| 291 | - $d = 0; |
|
| 292 | - for ($i = 0; $i < 32; ++$i) { |
|
| 293 | - $d |= self::chrToInt($pk[$i]); |
|
| 294 | - } |
|
| 295 | - if ($d === 0) { |
|
| 296 | - throw new SodiumException('All zero public key'); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
| 300 | - $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
| 301 | - |
|
| 302 | - // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
| 303 | - ParagonIE_Sodium_Compat::$fastMult = true; |
|
| 304 | - |
|
| 305 | - /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
|
| 306 | - $A = self::ge_frombytes_negate_vartime($pk); |
|
| 307 | - |
|
| 308 | - /** @var string $hDigest */ |
|
| 309 | - $hDigest = hash( |
|
| 310 | - 'sha512', |
|
| 311 | - self::substr($sig, 0, 32) . |
|
| 312 | - self::substr($pk, 0, 32) . |
|
| 313 | - $message, |
|
| 314 | - true |
|
| 315 | - ); |
|
| 316 | - |
|
| 317 | - /** @var string $h */ |
|
| 318 | - $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
| 319 | - |
|
| 320 | - /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ |
|
| 321 | - $R = self::ge_double_scalarmult_vartime( |
|
| 322 | - $h, |
|
| 323 | - $A, |
|
| 324 | - self::substr($sig, 32) |
|
| 325 | - ); |
|
| 326 | - |
|
| 327 | - /** @var string $rcheck */ |
|
| 328 | - $rcheck = self::ge_tobytes($R); |
|
| 329 | - |
|
| 330 | - // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
| 331 | - ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
| 332 | - |
|
| 333 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * @internal You should not use this directly from another application |
|
| 338 | - * |
|
| 339 | - * @param string $S |
|
| 340 | - * @return bool |
|
| 341 | - * @throws SodiumException |
|
| 342 | - * @throws TypeError |
|
| 343 | - */ |
|
| 344 | - public static function check_S_lt_L($S) |
|
| 345 | - { |
|
| 346 | - if (self::strlen($S) < 32) { |
|
| 347 | - throw new SodiumException('Signature must be 32 bytes'); |
|
| 348 | - } |
|
| 349 | - static $L = array( |
|
| 350 | - 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
|
| 351 | - 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, |
|
| 352 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 353 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 |
|
| 354 | - ); |
|
| 355 | - /** @var array<int, int> $L */ |
|
| 356 | - $c = 0; |
|
| 357 | - $n = 1; |
|
| 358 | - $i = 32; |
|
| 359 | - |
|
| 360 | - do { |
|
| 361 | - --$i; |
|
| 362 | - $x = self::chrToInt($S[$i]); |
|
| 363 | - $c |= ( |
|
| 364 | - (($x - $L[$i]) >> 8) & $n |
|
| 365 | - ); |
|
| 366 | - $n &= ( |
|
| 367 | - (($x ^ $L[$i]) - 1) >> 8 |
|
| 368 | - ); |
|
| 369 | - } while ($i !== 0); |
|
| 370 | - |
|
| 371 | - return $c === 0; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * @param string $R |
|
| 376 | - * @return bool |
|
| 377 | - * @throws SodiumException |
|
| 378 | - * @throws TypeError |
|
| 379 | - */ |
|
| 380 | - public static function small_order($R) |
|
| 381 | - { |
|
| 382 | - static $blocklist = array( |
|
| 383 | - /* 0 (order 4) */ |
|
| 384 | - array( |
|
| 385 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 386 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 387 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 388 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
| 389 | - ), |
|
| 390 | - /* 1 (order 1) */ |
|
| 391 | - array( |
|
| 392 | - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 393 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 394 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 395 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
| 396 | - ), |
|
| 397 | - /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
| 398 | - array( |
|
| 399 | - 0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
| 400 | - 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
| 401 | - 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
| 402 | - 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05 |
|
| 403 | - ), |
|
| 404 | - /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
| 405 | - array( |
|
| 406 | - 0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
| 407 | - 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
| 408 | - 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
| 409 | - 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a |
|
| 410 | - ), |
|
| 411 | - /* p-1 (order 2) */ |
|
| 412 | - array( |
|
| 413 | - 0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
| 414 | - 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
| 415 | - 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
| 416 | - 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85 |
|
| 417 | - ), |
|
| 418 | - /* p (order 4) */ |
|
| 419 | - array( |
|
| 420 | - 0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
| 421 | - 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
| 422 | - 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
| 423 | - 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa |
|
| 424 | - ), |
|
| 425 | - /* p+1 (order 1) */ |
|
| 426 | - array( |
|
| 427 | - 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 428 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 429 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 430 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
| 431 | - ), |
|
| 432 | - /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
| 433 | - array( |
|
| 434 | - 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 435 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 436 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 437 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
| 438 | - ), |
|
| 439 | - /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
| 440 | - array( |
|
| 441 | - 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 442 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 443 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 444 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
| 445 | - ), |
|
| 446 | - /* 2p-1 (order 2) */ |
|
| 447 | - array( |
|
| 448 | - 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 449 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 450 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 451 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
| 452 | - ), |
|
| 453 | - /* 2p (order 4) */ |
|
| 454 | - array( |
|
| 455 | - 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 456 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 457 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 458 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
| 459 | - ), |
|
| 460 | - /* 2p+1 (order 1) */ |
|
| 461 | - array( |
|
| 462 | - 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 463 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 464 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 465 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
| 466 | - ) |
|
| 467 | - ); |
|
| 468 | - /** @var array<int, array<int, int>> $blocklist */ |
|
| 469 | - $countBlocklist = count($blocklist); |
|
| 470 | - |
|
| 471 | - for ($i = 0; $i < $countBlocklist; ++$i) { |
|
| 472 | - $c = 0; |
|
| 473 | - for ($j = 0; $j < 32; ++$j) { |
|
| 474 | - $c |= self::chrToInt($R[$j]) ^ $blocklist[$i][$j]; |
|
| 475 | - } |
|
| 476 | - if ($c === 0) { |
|
| 477 | - return true; |
|
| 478 | - } |
|
| 479 | - } |
|
| 480 | - return false; |
|
| 481 | - } |
|
| 12 | + const KEYPAIR_BYTES = 96; |
|
| 13 | + const SEED_BYTES = 32; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * @internal You should not use this directly from another application |
|
| 17 | + * |
|
| 18 | + * @return string (96 bytes) |
|
| 19 | + * @throws Exception |
|
| 20 | + * @throws SodiumException |
|
| 21 | + * @throws TypeError |
|
| 22 | + */ |
|
| 23 | + public static function keypair() |
|
| 24 | + { |
|
| 25 | + $seed = random_bytes(self::SEED_BYTES); |
|
| 26 | + $pk = ''; |
|
| 27 | + $sk = ''; |
|
| 28 | + self::seed_keypair($pk, $sk, $seed); |
|
| 29 | + return $sk . $pk; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @internal You should not use this directly from another application |
|
| 34 | + * |
|
| 35 | + * @param string $pk |
|
| 36 | + * @param string $sk |
|
| 37 | + * @param string $seed |
|
| 38 | + * @return string |
|
| 39 | + * @throws SodiumException |
|
| 40 | + * @throws TypeError |
|
| 41 | + */ |
|
| 42 | + public static function seed_keypair(&$pk, &$sk, $seed) |
|
| 43 | + { |
|
| 44 | + if (self::strlen($seed) !== self::SEED_BYTES) { |
|
| 45 | + throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** @var string $pk */ |
|
| 49 | + $pk = self::publickey_from_secretkey($seed); |
|
| 50 | + $sk = $seed . $pk; |
|
| 51 | + return $sk; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @internal You should not use this directly from another application |
|
| 56 | + * |
|
| 57 | + * @param string $keypair |
|
| 58 | + * @return string |
|
| 59 | + * @throws TypeError |
|
| 60 | + */ |
|
| 61 | + public static function secretkey($keypair) |
|
| 62 | + { |
|
| 63 | + if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
| 64 | + throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
| 65 | + } |
|
| 66 | + return self::substr($keypair, 0, 64); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @internal You should not use this directly from another application |
|
| 71 | + * |
|
| 72 | + * @param string $keypair |
|
| 73 | + * @return string |
|
| 74 | + * @throws RangeException |
|
| 75 | + * @throws TypeError |
|
| 76 | + */ |
|
| 77 | + public static function publickey($keypair) |
|
| 78 | + { |
|
| 79 | + if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
| 80 | + throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
| 81 | + } |
|
| 82 | + return self::substr($keypair, 64, 32); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @internal You should not use this directly from another application |
|
| 87 | + * |
|
| 88 | + * @param string $sk |
|
| 89 | + * @return string |
|
| 90 | + * @throws SodiumException |
|
| 91 | + * @throws TypeError |
|
| 92 | + */ |
|
| 93 | + public static function publickey_from_secretkey($sk) |
|
| 94 | + { |
|
| 95 | + /** @var string $sk */ |
|
| 96 | + $sk = hash('sha512', self::substr($sk, 0, 32), true); |
|
| 97 | + $sk[0] = self::intToChr( |
|
| 98 | + self::chrToInt($sk[0]) & 248 |
|
| 99 | + ); |
|
| 100 | + $sk[31] = self::intToChr( |
|
| 101 | + (self::chrToInt($sk[31]) & 63) | 64 |
|
| 102 | + ); |
|
| 103 | + return self::sk_to_pk($sk); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @param string $pk |
|
| 108 | + * @return string |
|
| 109 | + * @throws SodiumException |
|
| 110 | + * @throws TypeError |
|
| 111 | + */ |
|
| 112 | + public static function pk_to_curve25519($pk) |
|
| 113 | + { |
|
| 114 | + if (self::small_order($pk)) { |
|
| 115 | + throw new SodiumException('Public key is on a small order'); |
|
| 116 | + } |
|
| 117 | + $A = self::ge_frombytes_negate_vartime($pk); |
|
| 118 | + $p1 = self::ge_mul_l($A); |
|
| 119 | + if (!self::fe_isnonzero($p1->X)) { |
|
| 120 | + throw new SodiumException('Unexpected zero result'); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + # fe_1(one_minus_y); |
|
| 124 | + # fe_sub(one_minus_y, one_minus_y, A.Y); |
|
| 125 | + # fe_invert(one_minus_y, one_minus_y); |
|
| 126 | + $one_minux_y = self::fe_invert( |
|
| 127 | + self::fe_sub( |
|
| 128 | + self::fe_1(), |
|
| 129 | + $A->Y |
|
| 130 | + ) |
|
| 131 | + ); |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + # fe_1(x); |
|
| 135 | + # fe_add(x, x, A.Y); |
|
| 136 | + # fe_mul(x, x, one_minus_y); |
|
| 137 | + $x = self::fe_mul( |
|
| 138 | + self::fe_add(self::fe_1(), $A->Y), |
|
| 139 | + $one_minux_y |
|
| 140 | + ); |
|
| 141 | + |
|
| 142 | + # fe_tobytes(curve25519_pk, x); |
|
| 143 | + return self::fe_tobytes($x); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @internal You should not use this directly from another application |
|
| 148 | + * |
|
| 149 | + * @param string $sk |
|
| 150 | + * @return string |
|
| 151 | + * @throws SodiumException |
|
| 152 | + * @throws TypeError |
|
| 153 | + */ |
|
| 154 | + public static function sk_to_pk($sk) |
|
| 155 | + { |
|
| 156 | + return self::ge_p3_tobytes( |
|
| 157 | + self::ge_scalarmult_base( |
|
| 158 | + self::substr($sk, 0, 32) |
|
| 159 | + ) |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @internal You should not use this directly from another application |
|
| 165 | + * |
|
| 166 | + * @param string $message |
|
| 167 | + * @param string $sk |
|
| 168 | + * @return string |
|
| 169 | + * @throws SodiumException |
|
| 170 | + * @throws TypeError |
|
| 171 | + */ |
|
| 172 | + public static function sign($message, $sk) |
|
| 173 | + { |
|
| 174 | + /** @var string $signature */ |
|
| 175 | + $signature = self::sign_detached($message, $sk); |
|
| 176 | + return $signature . $message; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @internal You should not use this directly from another application |
|
| 181 | + * |
|
| 182 | + * @param string $message A signed message |
|
| 183 | + * @param string $pk Public key |
|
| 184 | + * @return string Message (without signature) |
|
| 185 | + * @throws SodiumException |
|
| 186 | + * @throws TypeError |
|
| 187 | + */ |
|
| 188 | + public static function sign_open($message, $pk) |
|
| 189 | + { |
|
| 190 | + /** @var string $signature */ |
|
| 191 | + $signature = self::substr($message, 0, 64); |
|
| 192 | + |
|
| 193 | + /** @var string $message */ |
|
| 194 | + $message = self::substr($message, 64); |
|
| 195 | + |
|
| 196 | + if (self::verify_detached($signature, $message, $pk)) { |
|
| 197 | + return $message; |
|
| 198 | + } |
|
| 199 | + throw new SodiumException('Invalid signature'); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * @internal You should not use this directly from another application |
|
| 204 | + * |
|
| 205 | + * @param string $message |
|
| 206 | + * @param string $sk |
|
| 207 | + * @return string |
|
| 208 | + * @throws SodiumException |
|
| 209 | + * @throws TypeError |
|
| 210 | + * @psalm-suppress PossiblyInvalidArgument |
|
| 211 | + */ |
|
| 212 | + public static function sign_detached($message, $sk) |
|
| 213 | + { |
|
| 214 | + # crypto_hash_sha512(az, sk, 32); |
|
| 215 | + $az = hash('sha512', self::substr($sk, 0, 32), true); |
|
| 216 | + |
|
| 217 | + # az[0] &= 248; |
|
| 218 | + # az[31] &= 63; |
|
| 219 | + # az[31] |= 64; |
|
| 220 | + $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
| 221 | + $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
| 222 | + |
|
| 223 | + # crypto_hash_sha512_init(&hs); |
|
| 224 | + # crypto_hash_sha512_update(&hs, az + 32, 32); |
|
| 225 | + # crypto_hash_sha512_update(&hs, m, mlen); |
|
| 226 | + # crypto_hash_sha512_final(&hs, nonce); |
|
| 227 | + $hs = hash_init('sha512'); |
|
| 228 | + self::hash_update($hs, self::substr($az, 32, 32)); |
|
| 229 | + self::hash_update($hs, $message); |
|
| 230 | + $nonceHash = hash_final($hs, true); |
|
| 231 | + |
|
| 232 | + # memmove(sig + 32, sk + 32, 32); |
|
| 233 | + $pk = self::substr($sk, 32, 32); |
|
| 234 | + |
|
| 235 | + # sc_reduce(nonce); |
|
| 236 | + # ge_scalarmult_base(&R, nonce); |
|
| 237 | + # ge_p3_tobytes(sig, &R); |
|
| 238 | + $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
| 239 | + $sig = self::ge_p3_tobytes( |
|
| 240 | + self::ge_scalarmult_base($nonce) |
|
| 241 | + ); |
|
| 242 | + |
|
| 243 | + # crypto_hash_sha512_init(&hs); |
|
| 244 | + # crypto_hash_sha512_update(&hs, sig, 64); |
|
| 245 | + # crypto_hash_sha512_update(&hs, m, mlen); |
|
| 246 | + # crypto_hash_sha512_final(&hs, hram); |
|
| 247 | + $hs = hash_init('sha512'); |
|
| 248 | + self::hash_update($hs, self::substr($sig, 0, 32)); |
|
| 249 | + self::hash_update($hs, self::substr($pk, 0, 32)); |
|
| 250 | + self::hash_update($hs, $message); |
|
| 251 | + $hramHash = hash_final($hs, true); |
|
| 252 | + |
|
| 253 | + # sc_reduce(hram); |
|
| 254 | + # sc_muladd(sig + 32, hram, az, nonce); |
|
| 255 | + $hram = self::sc_reduce($hramHash); |
|
| 256 | + $sigAfter = self::sc_muladd($hram, $az, $nonce); |
|
| 257 | + $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
| 258 | + |
|
| 259 | + try { |
|
| 260 | + ParagonIE_Sodium_Compat::memzero($az); |
|
| 261 | + } catch (SodiumException $ex) { |
|
| 262 | + $az = null; |
|
| 263 | + } |
|
| 264 | + return $sig; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * @internal You should not use this directly from another application |
|
| 269 | + * |
|
| 270 | + * @param string $sig |
|
| 271 | + * @param string $message |
|
| 272 | + * @param string $pk |
|
| 273 | + * @return bool |
|
| 274 | + * @throws SodiumException |
|
| 275 | + * @throws TypeError |
|
| 276 | + */ |
|
| 277 | + public static function verify_detached($sig, $message, $pk) |
|
| 278 | + { |
|
| 279 | + if (self::strlen($sig) < 64) { |
|
| 280 | + throw new SodiumException('Signature is too short'); |
|
| 281 | + } |
|
| 282 | + if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
| 283 | + throw new SodiumException('S < L - Invalid signature'); |
|
| 284 | + } |
|
| 285 | + if (self::small_order($sig)) { |
|
| 286 | + throw new SodiumException('Signature is on too small of an order'); |
|
| 287 | + } |
|
| 288 | + if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
| 289 | + throw new SodiumException('Invalid signature'); |
|
| 290 | + } |
|
| 291 | + $d = 0; |
|
| 292 | + for ($i = 0; $i < 32; ++$i) { |
|
| 293 | + $d |= self::chrToInt($pk[$i]); |
|
| 294 | + } |
|
| 295 | + if ($d === 0) { |
|
| 296 | + throw new SodiumException('All zero public key'); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
| 300 | + $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
| 301 | + |
|
| 302 | + // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
| 303 | + ParagonIE_Sodium_Compat::$fastMult = true; |
|
| 304 | + |
|
| 305 | + /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
|
| 306 | + $A = self::ge_frombytes_negate_vartime($pk); |
|
| 307 | + |
|
| 308 | + /** @var string $hDigest */ |
|
| 309 | + $hDigest = hash( |
|
| 310 | + 'sha512', |
|
| 311 | + self::substr($sig, 0, 32) . |
|
| 312 | + self::substr($pk, 0, 32) . |
|
| 313 | + $message, |
|
| 314 | + true |
|
| 315 | + ); |
|
| 316 | + |
|
| 317 | + /** @var string $h */ |
|
| 318 | + $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
| 319 | + |
|
| 320 | + /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ |
|
| 321 | + $R = self::ge_double_scalarmult_vartime( |
|
| 322 | + $h, |
|
| 323 | + $A, |
|
| 324 | + self::substr($sig, 32) |
|
| 325 | + ); |
|
| 326 | + |
|
| 327 | + /** @var string $rcheck */ |
|
| 328 | + $rcheck = self::ge_tobytes($R); |
|
| 329 | + |
|
| 330 | + // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
| 331 | + ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
| 332 | + |
|
| 333 | + return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * @internal You should not use this directly from another application |
|
| 338 | + * |
|
| 339 | + * @param string $S |
|
| 340 | + * @return bool |
|
| 341 | + * @throws SodiumException |
|
| 342 | + * @throws TypeError |
|
| 343 | + */ |
|
| 344 | + public static function check_S_lt_L($S) |
|
| 345 | + { |
|
| 346 | + if (self::strlen($S) < 32) { |
|
| 347 | + throw new SodiumException('Signature must be 32 bytes'); |
|
| 348 | + } |
|
| 349 | + static $L = array( |
|
| 350 | + 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
|
| 351 | + 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, |
|
| 352 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 353 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 |
|
| 354 | + ); |
|
| 355 | + /** @var array<int, int> $L */ |
|
| 356 | + $c = 0; |
|
| 357 | + $n = 1; |
|
| 358 | + $i = 32; |
|
| 359 | + |
|
| 360 | + do { |
|
| 361 | + --$i; |
|
| 362 | + $x = self::chrToInt($S[$i]); |
|
| 363 | + $c |= ( |
|
| 364 | + (($x - $L[$i]) >> 8) & $n |
|
| 365 | + ); |
|
| 366 | + $n &= ( |
|
| 367 | + (($x ^ $L[$i]) - 1) >> 8 |
|
| 368 | + ); |
|
| 369 | + } while ($i !== 0); |
|
| 370 | + |
|
| 371 | + return $c === 0; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * @param string $R |
|
| 376 | + * @return bool |
|
| 377 | + * @throws SodiumException |
|
| 378 | + * @throws TypeError |
|
| 379 | + */ |
|
| 380 | + public static function small_order($R) |
|
| 381 | + { |
|
| 382 | + static $blocklist = array( |
|
| 383 | + /* 0 (order 4) */ |
|
| 384 | + array( |
|
| 385 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 386 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 387 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 388 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
| 389 | + ), |
|
| 390 | + /* 1 (order 1) */ |
|
| 391 | + array( |
|
| 392 | + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 393 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 394 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
| 395 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
| 396 | + ), |
|
| 397 | + /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
| 398 | + array( |
|
| 399 | + 0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
| 400 | + 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
| 401 | + 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
| 402 | + 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05 |
|
| 403 | + ), |
|
| 404 | + /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
| 405 | + array( |
|
| 406 | + 0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
| 407 | + 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
| 408 | + 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
| 409 | + 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a |
|
| 410 | + ), |
|
| 411 | + /* p-1 (order 2) */ |
|
| 412 | + array( |
|
| 413 | + 0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
| 414 | + 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
| 415 | + 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
| 416 | + 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85 |
|
| 417 | + ), |
|
| 418 | + /* p (order 4) */ |
|
| 419 | + array( |
|
| 420 | + 0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
| 421 | + 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
| 422 | + 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
| 423 | + 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa |
|
| 424 | + ), |
|
| 425 | + /* p+1 (order 1) */ |
|
| 426 | + array( |
|
| 427 | + 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 428 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 429 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 430 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
| 431 | + ), |
|
| 432 | + /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
| 433 | + array( |
|
| 434 | + 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 435 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 436 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 437 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
| 438 | + ), |
|
| 439 | + /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
| 440 | + array( |
|
| 441 | + 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 442 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 443 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 444 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
| 445 | + ), |
|
| 446 | + /* 2p-1 (order 2) */ |
|
| 447 | + array( |
|
| 448 | + 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 449 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 450 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 451 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
| 452 | + ), |
|
| 453 | + /* 2p (order 4) */ |
|
| 454 | + array( |
|
| 455 | + 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 456 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 457 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 458 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
| 459 | + ), |
|
| 460 | + /* 2p+1 (order 1) */ |
|
| 461 | + array( |
|
| 462 | + 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 463 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 464 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
| 465 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
| 466 | + ) |
|
| 467 | + ); |
|
| 468 | + /** @var array<int, array<int, int>> $blocklist */ |
|
| 469 | + $countBlocklist = count($blocklist); |
|
| 470 | + |
|
| 471 | + for ($i = 0; $i < $countBlocklist; ++$i) { |
|
| 472 | + $c = 0; |
|
| 473 | + for ($j = 0; $j < 32; ++$j) { |
|
| 474 | + $c |= self::chrToInt($R[$j]) ^ $blocklist[$i][$j]; |
|
| 475 | + } |
|
| 476 | + if ($c === 0) { |
|
| 477 | + return true; |
|
| 478 | + } |
|
| 479 | + } |
|
| 480 | + return false; |
|
| 481 | + } |
|
| 482 | 482 | } |
@@ -1,7 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Util', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_SipHash', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -11,228 +11,228 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | class ParagonIE_Sodium_Core32_SipHash extends ParagonIE_Sodium_Core32_Util |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @internal You should not use this directly from another application |
|
| 16 | - * |
|
| 17 | - * @param array<int, ParagonIE_Sodium_Core32_Int64> $v |
|
| 18 | - * @return array<int, ParagonIE_Sodium_Core32_Int64> |
|
| 19 | - */ |
|
| 20 | - public static function sipRound(array $v) |
|
| 21 | - { |
|
| 22 | - # v0 += v1; |
|
| 23 | - $v[0] = $v[0]->addInt64($v[1]); |
|
| 24 | - |
|
| 25 | - # v1 = ROTL(v1, 13); |
|
| 26 | - $v[1] = $v[1]->rotateLeft(13); |
|
| 27 | - |
|
| 28 | - # v1 ^= v0; |
|
| 29 | - $v[1] = $v[1]->xorInt64($v[0]); |
|
| 30 | - |
|
| 31 | - # v0=ROTL(v0,32); |
|
| 32 | - $v[0] = $v[0]->rotateLeft(32); |
|
| 33 | - |
|
| 34 | - # v2 += v3; |
|
| 35 | - $v[2] = $v[2]->addInt64($v[3]); |
|
| 36 | - |
|
| 37 | - # v3=ROTL(v3,16); |
|
| 38 | - $v[3] = $v[3]->rotateLeft(16); |
|
| 39 | - |
|
| 40 | - # v3 ^= v2; |
|
| 41 | - $v[3] = $v[3]->xorInt64($v[2]); |
|
| 42 | - |
|
| 43 | - # v0 += v3; |
|
| 44 | - $v[0] = $v[0]->addInt64($v[3]); |
|
| 45 | - |
|
| 46 | - # v3=ROTL(v3,21); |
|
| 47 | - $v[3] = $v[3]->rotateLeft(21); |
|
| 48 | - |
|
| 49 | - # v3 ^= v0; |
|
| 50 | - $v[3] = $v[3]->xorInt64($v[0]); |
|
| 51 | - |
|
| 52 | - # v2 += v1; |
|
| 53 | - $v[2] = $v[2]->addInt64($v[1]); |
|
| 54 | - |
|
| 55 | - # v1=ROTL(v1,17); |
|
| 56 | - $v[1] = $v[1]->rotateLeft(17); |
|
| 57 | - |
|
| 58 | - # v1 ^= v2; |
|
| 59 | - $v[1] = $v[1]->xorInt64($v[2]); |
|
| 60 | - |
|
| 61 | - # v2=ROTL(v2,32) |
|
| 62 | - $v[2] = $v[2]->rotateLeft(32); |
|
| 63 | - |
|
| 64 | - return $v; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @internal You should not use this directly from another application |
|
| 69 | - * |
|
| 70 | - * @param string $in |
|
| 71 | - * @param string $key |
|
| 72 | - * @return string |
|
| 73 | - * @throws SodiumException |
|
| 74 | - * @throws TypeError |
|
| 75 | - */ |
|
| 76 | - public static function sipHash24($in, $key) |
|
| 77 | - { |
|
| 78 | - $inlen = self::strlen($in); |
|
| 79 | - |
|
| 80 | - # /* "somepseudorandomlygeneratedbytes" */ |
|
| 81 | - # u64 v0 = 0x736f6d6570736575ULL; |
|
| 82 | - # u64 v1 = 0x646f72616e646f6dULL; |
|
| 83 | - # u64 v2 = 0x6c7967656e657261ULL; |
|
| 84 | - # u64 v3 = 0x7465646279746573ULL; |
|
| 85 | - $v = array( |
|
| 86 | - new ParagonIE_Sodium_Core32_Int64( |
|
| 87 | - array(0x736f, 0x6d65, 0x7073, 0x6575) |
|
| 88 | - ), |
|
| 89 | - new ParagonIE_Sodium_Core32_Int64( |
|
| 90 | - array(0x646f, 0x7261, 0x6e64, 0x6f6d) |
|
| 91 | - ), |
|
| 92 | - new ParagonIE_Sodium_Core32_Int64( |
|
| 93 | - array(0x6c79, 0x6765, 0x6e65, 0x7261) |
|
| 94 | - ), |
|
| 95 | - new ParagonIE_Sodium_Core32_Int64( |
|
| 96 | - array(0x7465, 0x6462, 0x7974, 0x6573) |
|
| 97 | - ) |
|
| 98 | - ); |
|
| 99 | - |
|
| 100 | - # u64 k0 = LOAD64_LE( k ); |
|
| 101 | - # u64 k1 = LOAD64_LE( k + 8 ); |
|
| 102 | - $k = array( |
|
| 103 | - ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 104 | - self::substr($key, 0, 8) |
|
| 105 | - ), |
|
| 106 | - ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 107 | - self::substr($key, 8, 8) |
|
| 108 | - ) |
|
| 109 | - ); |
|
| 110 | - |
|
| 111 | - # b = ( ( u64 )inlen ) << 56; |
|
| 112 | - $b = new ParagonIE_Sodium_Core32_Int64( |
|
| 113 | - array(($inlen << 8) & 0xffff, 0, 0, 0) |
|
| 114 | - ); |
|
| 115 | - |
|
| 116 | - # v3 ^= k1; |
|
| 117 | - $v[3] = $v[3]->xorInt64($k[1]); |
|
| 118 | - # v2 ^= k0; |
|
| 119 | - $v[2] = $v[2]->xorInt64($k[0]); |
|
| 120 | - # v1 ^= k1; |
|
| 121 | - $v[1] = $v[1]->xorInt64($k[1]); |
|
| 122 | - # v0 ^= k0; |
|
| 123 | - $v[0] = $v[0]->xorInt64($k[0]); |
|
| 124 | - |
|
| 125 | - $left = $inlen; |
|
| 126 | - # for ( ; in != end; in += 8 ) |
|
| 127 | - while ($left >= 8) { |
|
| 128 | - # m = LOAD64_LE( in ); |
|
| 129 | - $m = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 130 | - self::substr($in, 0, 8) |
|
| 131 | - ); |
|
| 132 | - |
|
| 133 | - # v3 ^= m; |
|
| 134 | - $v[3] = $v[3]->xorInt64($m); |
|
| 135 | - |
|
| 136 | - # SIPROUND; |
|
| 137 | - # SIPROUND; |
|
| 138 | - $v = self::sipRound($v); |
|
| 139 | - $v = self::sipRound($v); |
|
| 140 | - |
|
| 141 | - # v0 ^= m; |
|
| 142 | - $v[0] = $v[0]->xorInt64($m); |
|
| 143 | - |
|
| 144 | - $in = self::substr($in, 8); |
|
| 145 | - $left -= 8; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - # switch( left ) |
|
| 149 | - # { |
|
| 150 | - # case 7: b |= ( ( u64 )in[ 6] ) << 48; |
|
| 151 | - # case 6: b |= ( ( u64 )in[ 5] ) << 40; |
|
| 152 | - # case 5: b |= ( ( u64 )in[ 4] ) << 32; |
|
| 153 | - # case 4: b |= ( ( u64 )in[ 3] ) << 24; |
|
| 154 | - # case 3: b |= ( ( u64 )in[ 2] ) << 16; |
|
| 155 | - # case 2: b |= ( ( u64 )in[ 1] ) << 8; |
|
| 156 | - # case 1: b |= ( ( u64 )in[ 0] ); break; |
|
| 157 | - # case 0: break; |
|
| 158 | - # } |
|
| 159 | - switch ($left) { |
|
| 160 | - case 7: |
|
| 161 | - $b = $b->orInt64( |
|
| 162 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 163 | - 0, self::chrToInt($in[6]) << 16 |
|
| 164 | - ) |
|
| 165 | - ); |
|
| 166 | - case 6: |
|
| 167 | - $b = $b->orInt64( |
|
| 168 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 169 | - 0, self::chrToInt($in[5]) << 8 |
|
| 170 | - ) |
|
| 171 | - ); |
|
| 172 | - case 5: |
|
| 173 | - $b = $b->orInt64( |
|
| 174 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 175 | - 0, self::chrToInt($in[4]) |
|
| 176 | - ) |
|
| 177 | - ); |
|
| 178 | - case 4: |
|
| 179 | - $b = $b->orInt64( |
|
| 180 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 181 | - self::chrToInt($in[3]) << 24, 0 |
|
| 182 | - ) |
|
| 183 | - ); |
|
| 184 | - case 3: |
|
| 185 | - $b = $b->orInt64( |
|
| 186 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 187 | - self::chrToInt($in[2]) << 16, 0 |
|
| 188 | - ) |
|
| 189 | - ); |
|
| 190 | - case 2: |
|
| 191 | - $b = $b->orInt64( |
|
| 192 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 193 | - self::chrToInt($in[1]) << 8, 0 |
|
| 194 | - ) |
|
| 195 | - ); |
|
| 196 | - case 1: |
|
| 197 | - $b = $b->orInt64( |
|
| 198 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 199 | - self::chrToInt($in[0]), 0 |
|
| 200 | - ) |
|
| 201 | - ); |
|
| 202 | - case 0: |
|
| 203 | - break; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - # v3 ^= b; |
|
| 207 | - $v[3] = $v[3]->xorInt64($b); |
|
| 208 | - |
|
| 209 | - # SIPROUND; |
|
| 210 | - # SIPROUND; |
|
| 211 | - $v = self::sipRound($v); |
|
| 212 | - $v = self::sipRound($v); |
|
| 213 | - |
|
| 214 | - # v0 ^= b; |
|
| 215 | - $v[0] = $v[0]->xorInt64($b); |
|
| 216 | - |
|
| 217 | - // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation |
|
| 218 | - # v2 ^= 0xff; |
|
| 219 | - $v[2]->limbs[3] ^= 0xff; |
|
| 220 | - |
|
| 221 | - # SIPROUND; |
|
| 222 | - # SIPROUND; |
|
| 223 | - # SIPROUND; |
|
| 224 | - # SIPROUND; |
|
| 225 | - $v = self::sipRound($v); |
|
| 226 | - $v = self::sipRound($v); |
|
| 227 | - $v = self::sipRound($v); |
|
| 228 | - $v = self::sipRound($v); |
|
| 229 | - |
|
| 230 | - # b = v0 ^ v1 ^ v2 ^ v3; |
|
| 231 | - # STORE64_LE( out, b ); |
|
| 232 | - return $v[0] |
|
| 233 | - ->xorInt64($v[1]) |
|
| 234 | - ->xorInt64($v[2]) |
|
| 235 | - ->xorInt64($v[3]) |
|
| 236 | - ->toReverseString(); |
|
| 237 | - } |
|
| 14 | + /** |
|
| 15 | + * @internal You should not use this directly from another application |
|
| 16 | + * |
|
| 17 | + * @param array<int, ParagonIE_Sodium_Core32_Int64> $v |
|
| 18 | + * @return array<int, ParagonIE_Sodium_Core32_Int64> |
|
| 19 | + */ |
|
| 20 | + public static function sipRound(array $v) |
|
| 21 | + { |
|
| 22 | + # v0 += v1; |
|
| 23 | + $v[0] = $v[0]->addInt64($v[1]); |
|
| 24 | + |
|
| 25 | + # v1 = ROTL(v1, 13); |
|
| 26 | + $v[1] = $v[1]->rotateLeft(13); |
|
| 27 | + |
|
| 28 | + # v1 ^= v0; |
|
| 29 | + $v[1] = $v[1]->xorInt64($v[0]); |
|
| 30 | + |
|
| 31 | + # v0=ROTL(v0,32); |
|
| 32 | + $v[0] = $v[0]->rotateLeft(32); |
|
| 33 | + |
|
| 34 | + # v2 += v3; |
|
| 35 | + $v[2] = $v[2]->addInt64($v[3]); |
|
| 36 | + |
|
| 37 | + # v3=ROTL(v3,16); |
|
| 38 | + $v[3] = $v[3]->rotateLeft(16); |
|
| 39 | + |
|
| 40 | + # v3 ^= v2; |
|
| 41 | + $v[3] = $v[3]->xorInt64($v[2]); |
|
| 42 | + |
|
| 43 | + # v0 += v3; |
|
| 44 | + $v[0] = $v[0]->addInt64($v[3]); |
|
| 45 | + |
|
| 46 | + # v3=ROTL(v3,21); |
|
| 47 | + $v[3] = $v[3]->rotateLeft(21); |
|
| 48 | + |
|
| 49 | + # v3 ^= v0; |
|
| 50 | + $v[3] = $v[3]->xorInt64($v[0]); |
|
| 51 | + |
|
| 52 | + # v2 += v1; |
|
| 53 | + $v[2] = $v[2]->addInt64($v[1]); |
|
| 54 | + |
|
| 55 | + # v1=ROTL(v1,17); |
|
| 56 | + $v[1] = $v[1]->rotateLeft(17); |
|
| 57 | + |
|
| 58 | + # v1 ^= v2; |
|
| 59 | + $v[1] = $v[1]->xorInt64($v[2]); |
|
| 60 | + |
|
| 61 | + # v2=ROTL(v2,32) |
|
| 62 | + $v[2] = $v[2]->rotateLeft(32); |
|
| 63 | + |
|
| 64 | + return $v; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @internal You should not use this directly from another application |
|
| 69 | + * |
|
| 70 | + * @param string $in |
|
| 71 | + * @param string $key |
|
| 72 | + * @return string |
|
| 73 | + * @throws SodiumException |
|
| 74 | + * @throws TypeError |
|
| 75 | + */ |
|
| 76 | + public static function sipHash24($in, $key) |
|
| 77 | + { |
|
| 78 | + $inlen = self::strlen($in); |
|
| 79 | + |
|
| 80 | + # /* "somepseudorandomlygeneratedbytes" */ |
|
| 81 | + # u64 v0 = 0x736f6d6570736575ULL; |
|
| 82 | + # u64 v1 = 0x646f72616e646f6dULL; |
|
| 83 | + # u64 v2 = 0x6c7967656e657261ULL; |
|
| 84 | + # u64 v3 = 0x7465646279746573ULL; |
|
| 85 | + $v = array( |
|
| 86 | + new ParagonIE_Sodium_Core32_Int64( |
|
| 87 | + array(0x736f, 0x6d65, 0x7073, 0x6575) |
|
| 88 | + ), |
|
| 89 | + new ParagonIE_Sodium_Core32_Int64( |
|
| 90 | + array(0x646f, 0x7261, 0x6e64, 0x6f6d) |
|
| 91 | + ), |
|
| 92 | + new ParagonIE_Sodium_Core32_Int64( |
|
| 93 | + array(0x6c79, 0x6765, 0x6e65, 0x7261) |
|
| 94 | + ), |
|
| 95 | + new ParagonIE_Sodium_Core32_Int64( |
|
| 96 | + array(0x7465, 0x6462, 0x7974, 0x6573) |
|
| 97 | + ) |
|
| 98 | + ); |
|
| 99 | + |
|
| 100 | + # u64 k0 = LOAD64_LE( k ); |
|
| 101 | + # u64 k1 = LOAD64_LE( k + 8 ); |
|
| 102 | + $k = array( |
|
| 103 | + ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 104 | + self::substr($key, 0, 8) |
|
| 105 | + ), |
|
| 106 | + ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 107 | + self::substr($key, 8, 8) |
|
| 108 | + ) |
|
| 109 | + ); |
|
| 110 | + |
|
| 111 | + # b = ( ( u64 )inlen ) << 56; |
|
| 112 | + $b = new ParagonIE_Sodium_Core32_Int64( |
|
| 113 | + array(($inlen << 8) & 0xffff, 0, 0, 0) |
|
| 114 | + ); |
|
| 115 | + |
|
| 116 | + # v3 ^= k1; |
|
| 117 | + $v[3] = $v[3]->xorInt64($k[1]); |
|
| 118 | + # v2 ^= k0; |
|
| 119 | + $v[2] = $v[2]->xorInt64($k[0]); |
|
| 120 | + # v1 ^= k1; |
|
| 121 | + $v[1] = $v[1]->xorInt64($k[1]); |
|
| 122 | + # v0 ^= k0; |
|
| 123 | + $v[0] = $v[0]->xorInt64($k[0]); |
|
| 124 | + |
|
| 125 | + $left = $inlen; |
|
| 126 | + # for ( ; in != end; in += 8 ) |
|
| 127 | + while ($left >= 8) { |
|
| 128 | + # m = LOAD64_LE( in ); |
|
| 129 | + $m = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 130 | + self::substr($in, 0, 8) |
|
| 131 | + ); |
|
| 132 | + |
|
| 133 | + # v3 ^= m; |
|
| 134 | + $v[3] = $v[3]->xorInt64($m); |
|
| 135 | + |
|
| 136 | + # SIPROUND; |
|
| 137 | + # SIPROUND; |
|
| 138 | + $v = self::sipRound($v); |
|
| 139 | + $v = self::sipRound($v); |
|
| 140 | + |
|
| 141 | + # v0 ^= m; |
|
| 142 | + $v[0] = $v[0]->xorInt64($m); |
|
| 143 | + |
|
| 144 | + $in = self::substr($in, 8); |
|
| 145 | + $left -= 8; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + # switch( left ) |
|
| 149 | + # { |
|
| 150 | + # case 7: b |= ( ( u64 )in[ 6] ) << 48; |
|
| 151 | + # case 6: b |= ( ( u64 )in[ 5] ) << 40; |
|
| 152 | + # case 5: b |= ( ( u64 )in[ 4] ) << 32; |
|
| 153 | + # case 4: b |= ( ( u64 )in[ 3] ) << 24; |
|
| 154 | + # case 3: b |= ( ( u64 )in[ 2] ) << 16; |
|
| 155 | + # case 2: b |= ( ( u64 )in[ 1] ) << 8; |
|
| 156 | + # case 1: b |= ( ( u64 )in[ 0] ); break; |
|
| 157 | + # case 0: break; |
|
| 158 | + # } |
|
| 159 | + switch ($left) { |
|
| 160 | + case 7: |
|
| 161 | + $b = $b->orInt64( |
|
| 162 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 163 | + 0, self::chrToInt($in[6]) << 16 |
|
| 164 | + ) |
|
| 165 | + ); |
|
| 166 | + case 6: |
|
| 167 | + $b = $b->orInt64( |
|
| 168 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 169 | + 0, self::chrToInt($in[5]) << 8 |
|
| 170 | + ) |
|
| 171 | + ); |
|
| 172 | + case 5: |
|
| 173 | + $b = $b->orInt64( |
|
| 174 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 175 | + 0, self::chrToInt($in[4]) |
|
| 176 | + ) |
|
| 177 | + ); |
|
| 178 | + case 4: |
|
| 179 | + $b = $b->orInt64( |
|
| 180 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 181 | + self::chrToInt($in[3]) << 24, 0 |
|
| 182 | + ) |
|
| 183 | + ); |
|
| 184 | + case 3: |
|
| 185 | + $b = $b->orInt64( |
|
| 186 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 187 | + self::chrToInt($in[2]) << 16, 0 |
|
| 188 | + ) |
|
| 189 | + ); |
|
| 190 | + case 2: |
|
| 191 | + $b = $b->orInt64( |
|
| 192 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 193 | + self::chrToInt($in[1]) << 8, 0 |
|
| 194 | + ) |
|
| 195 | + ); |
|
| 196 | + case 1: |
|
| 197 | + $b = $b->orInt64( |
|
| 198 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
| 199 | + self::chrToInt($in[0]), 0 |
|
| 200 | + ) |
|
| 201 | + ); |
|
| 202 | + case 0: |
|
| 203 | + break; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + # v3 ^= b; |
|
| 207 | + $v[3] = $v[3]->xorInt64($b); |
|
| 208 | + |
|
| 209 | + # SIPROUND; |
|
| 210 | + # SIPROUND; |
|
| 211 | + $v = self::sipRound($v); |
|
| 212 | + $v = self::sipRound($v); |
|
| 213 | + |
|
| 214 | + # v0 ^= b; |
|
| 215 | + $v[0] = $v[0]->xorInt64($b); |
|
| 216 | + |
|
| 217 | + // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation |
|
| 218 | + # v2 ^= 0xff; |
|
| 219 | + $v[2]->limbs[3] ^= 0xff; |
|
| 220 | + |
|
| 221 | + # SIPROUND; |
|
| 222 | + # SIPROUND; |
|
| 223 | + # SIPROUND; |
|
| 224 | + # SIPROUND; |
|
| 225 | + $v = self::sipRound($v); |
|
| 226 | + $v = self::sipRound($v); |
|
| 227 | + $v = self::sipRound($v); |
|
| 228 | + $v = self::sipRound($v); |
|
| 229 | + |
|
| 230 | + # b = v0 ^ v1 ^ v2 ^ v3; |
|
| 231 | + # STORE64_LE( out, b ); |
|
| 232 | + return $v[0] |
|
| 233 | + ->xorInt64($v[1]) |
|
| 234 | + ->xorInt64($v[2]) |
|
| 235 | + ->xorInt64($v[3]) |
|
| 236 | + ->toReverseString(); |
|
| 237 | + } |
|
| 238 | 238 | } |