@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P2', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Curve25519_Ge_P2', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -38,15 +38,15 @@ discard block |
||
| 38 | 38 | ParagonIE_Sodium_Core_Curve25519_Fe $y = null, |
| 39 | 39 | ParagonIE_Sodium_Core_Curve25519_Fe $z = null |
| 40 | 40 | ) { |
| 41 | - if ($x === null) { |
|
| 41 | + if ( $x === null ) { |
|
| 42 | 42 | $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 43 | 43 | } |
| 44 | 44 | $this->X = $x; |
| 45 | - if ($y === null) { |
|
| 45 | + if ( $y === null ) { |
|
| 46 | 46 | $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 47 | 47 | } |
| 48 | 48 | $this->Y = $y; |
| 49 | - if ($z === null) { |
|
| 49 | + if ( $z === null ) { |
|
| 50 | 50 | $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 51 | 51 | } |
| 52 | 52 | $this->Z = $z; |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P1p1', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Curve25519_Ge_P1p1', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | /** |
@@ -44,19 +44,19 @@ discard block |
||
| 44 | 44 | ParagonIE_Sodium_Core_Curve25519_Fe $z = null, |
| 45 | 45 | ParagonIE_Sodium_Core_Curve25519_Fe $t = null |
| 46 | 46 | ) { |
| 47 | - if ($x === null) { |
|
| 47 | + if ( $x === null ) { |
|
| 48 | 48 | $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 49 | 49 | } |
| 50 | 50 | $this->X = $x; |
| 51 | - if ($y === null) { |
|
| 51 | + if ( $y === null ) { |
|
| 52 | 52 | $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 53 | 53 | } |
| 54 | 54 | $this->Y = $y; |
| 55 | - if ($z === null) { |
|
| 55 | + if ( $z === null ) { |
|
| 56 | 56 | $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 57 | 57 | } |
| 58 | 58 | $this->Z = $z; |
| 59 | - if ($t === null) { |
|
| 59 | + if ( $t === null ) { |
|
| 60 | 60 | $t = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 61 | 61 | } |
| 62 | 62 | $this->T = $t; |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | |
| 4 | -if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Cached', false)) { |
|
| 4 | +if ( class_exists( 'ParagonIE_Sodium_Core_Curve25519_Ge_Cached', false ) ) { |
|
| 5 | 5 | return; |
| 6 | 6 | } |
| 7 | 7 | /** |
@@ -45,19 +45,19 @@ discard block |
||
| 45 | 45 | ParagonIE_Sodium_Core_Curve25519_Fe $Z = null, |
| 46 | 46 | ParagonIE_Sodium_Core_Curve25519_Fe $T2d = null |
| 47 | 47 | ) { |
| 48 | - if ($YplusX === null) { |
|
| 48 | + if ( $YplusX === null ) { |
|
| 49 | 49 | $YplusX = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 50 | 50 | } |
| 51 | 51 | $this->YplusX = $YplusX; |
| 52 | - if ($YminusX === null) { |
|
| 52 | + if ( $YminusX === null ) { |
|
| 53 | 53 | $YminusX = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 54 | 54 | } |
| 55 | 55 | $this->YminusX = $YminusX; |
| 56 | - if ($Z === null) { |
|
| 56 | + if ( $Z === null ) { |
|
| 57 | 57 | $Z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 58 | 58 | } |
| 59 | 59 | $this->Z = $Z; |
| 60 | - if ($T2d === null) { |
|
| 60 | + if ( $T2d === null ) { |
|
| 61 | 61 | $T2d = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 62 | 62 | } |
| 63 | 63 | $this->T2d = $T2d; |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Precomp', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Curve25519_Ge_Precomp', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -38,15 +38,15 @@ discard block |
||
| 38 | 38 | ParagonIE_Sodium_Core_Curve25519_Fe $yminusx = null, |
| 39 | 39 | ParagonIE_Sodium_Core_Curve25519_Fe $xy2d = null |
| 40 | 40 | ) { |
| 41 | - if ($yplusx === null) { |
|
| 41 | + if ( $yplusx === null ) { |
|
| 42 | 42 | $yplusx = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 43 | 43 | } |
| 44 | 44 | $this->yplusx = $yplusx; |
| 45 | - if ($yminusx === null) { |
|
| 45 | + if ( $yminusx === null ) { |
|
| 46 | 46 | $yminusx = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 47 | 47 | } |
| 48 | 48 | $this->yminusx = $yminusx; |
| 49 | - if ($xy2d === null) { |
|
| 49 | + if ( $xy2d === null ) { |
|
| 50 | 50 | $xy2d = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 51 | 51 | } |
| 52 | 52 | $this->xy2d = $xy2d; |
@@ -15,10 +15,10 @@ discard block |
||
| 15 | 15 | * @param int $b |
| 16 | 16 | * @return ParagonIE_Sodium_Core_Curve25519_Fe |
| 17 | 17 | */ |
| 18 | - public static function fe_cneg(ParagonIE_Sodium_Core_Curve25519_Fe $f, $b) |
|
| 18 | + public static function fe_cneg( ParagonIE_Sodium_Core_Curve25519_Fe $f, $b ) |
|
| 19 | 19 | { |
| 20 | - $negf = self::fe_neg($f); |
|
| 21 | - return self::fe_cmov($f, $negf, $b); |
|
| 20 | + $negf = self::fe_neg( $f ); |
|
| 21 | + return self::fe_cmov( $f, $negf, $b ); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
@@ -26,9 +26,9 @@ discard block |
||
| 26 | 26 | * @return ParagonIE_Sodium_Core_Curve25519_Fe |
| 27 | 27 | * @throws SodiumException |
| 28 | 28 | */ |
| 29 | - public static function fe_abs(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 29 | + public static function fe_abs( ParagonIE_Sodium_Core_Curve25519_Fe $f ) |
|
| 30 | 30 | { |
| 31 | - return self::fe_cneg($f, self::fe_isnegative($f)); |
|
| 31 | + return self::fe_cneg( $f, self::fe_isnegative( $f ) ); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | /** |
@@ -40,20 +40,20 @@ discard block |
||
| 40 | 40 | * @return int |
| 41 | 41 | * @throws SodiumException |
| 42 | 42 | */ |
| 43 | - public static function fe_iszero(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
| 43 | + public static function fe_iszero( ParagonIE_Sodium_Core_Curve25519_Fe $f ) |
|
| 44 | 44 | { |
| 45 | 45 | static $zero; |
| 46 | - if ($zero === null) { |
|
| 47 | - $zero = str_repeat("\x00", 32); |
|
| 46 | + if ( $zero === null ) { |
|
| 47 | + $zero = str_repeat( "\x00", 32 ); |
|
| 48 | 48 | } |
| 49 | 49 | /** @var string $zero */ |
| 50 | - $str = self::fe_tobytes($f); |
|
| 50 | + $str = self::fe_tobytes( $f ); |
|
| 51 | 51 | |
| 52 | 52 | $d = 0; |
| 53 | - for ($i = 0; $i < 32; ++$i) { |
|
| 54 | - $d |= self::chrToInt($str[$i]); |
|
| 53 | + for ( $i = 0; $i < 32; ++$i ) { |
|
| 54 | + $d |= self::chrToInt( $str[ $i ] ); |
|
| 55 | 55 | } |
| 56 | - return (($d - 1) >> 31) & 1; |
|
| 56 | + return ( ( $d - 1 ) >> 31 ) & 1; |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | |
@@ -68,15 +68,15 @@ discard block |
||
| 68 | 68 | ParagonIE_Sodium_Core_Curve25519_Fe $u, |
| 69 | 69 | ParagonIE_Sodium_Core_Curve25519_Fe $v |
| 70 | 70 | ) { |
| 71 | - $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); |
|
| 71 | + $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( self::$sqrtm1 ); |
|
| 72 | 72 | |
| 73 | 73 | $v3 = self::fe_mul( |
| 74 | - self::fe_sq($v), |
|
| 74 | + self::fe_sq( $v ), |
|
| 75 | 75 | $v |
| 76 | 76 | ); /* v3 = v^3 */ |
| 77 | 77 | $x = self::fe_mul( |
| 78 | 78 | self::fe_mul( |
| 79 | - self::fe_sq($v3), |
|
| 79 | + self::fe_sq( $v3 ), |
|
| 80 | 80 | $u |
| 81 | 81 | ), |
| 82 | 82 | $v |
@@ -84,30 +84,30 @@ discard block |
||
| 84 | 84 | |
| 85 | 85 | $x = self::fe_mul( |
| 86 | 86 | self::fe_mul( |
| 87 | - self::fe_pow22523($x), /* x = (uv^7)^((q-5)/8) */ |
|
| 87 | + self::fe_pow22523( $x ), /* x = (uv^7)^((q-5)/8) */ |
|
| 88 | 88 | $v3 |
| 89 | 89 | ), |
| 90 | 90 | $u |
| 91 | 91 | ); /* x = uv^3(uv^7)^((q-5)/8) */ |
| 92 | 92 | |
| 93 | 93 | $vxx = self::fe_mul( |
| 94 | - self::fe_sq($x), |
|
| 94 | + self::fe_sq( $x ), |
|
| 95 | 95 | $v |
| 96 | 96 | ); /* vx^2 */ |
| 97 | 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) */ |
|
| 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 | 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); |
|
| 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 | 106 | |
| 107 | - $x_sqrtm1 = self::fe_mul($x, $sqrtm1); /* x*sqrt(-1) */ |
|
| 107 | + $x_sqrtm1 = self::fe_mul( $x, $sqrtm1 ); /* x*sqrt(-1) */ |
|
| 108 | 108 | |
| 109 | 109 | $x = self::fe_abs( |
| 110 | - self::fe_cmov($x, $x_sqrtm1, $has_p_root | $has_f_root) |
|
| 110 | + self::fe_cmov( $x, $x_sqrtm1, $has_p_root | $has_f_root ) |
|
| 111 | 111 | ); |
| 112 | 112 | return array( |
| 113 | 113 | 'x' => $x, |
@@ -120,17 +120,17 @@ discard block |
||
| 120 | 120 | * @return int |
| 121 | 121 | * @throws SodiumException |
| 122 | 122 | */ |
| 123 | - public static function ristretto255_point_is_canonical($s) |
|
| 123 | + public static function ristretto255_point_is_canonical( $s ) |
|
| 124 | 124 | { |
| 125 | - $c = (self::chrToInt($s[31]) & 0x7f) ^ 0x7f; |
|
| 126 | - for ($i = 30; $i > 0; --$i) { |
|
| 127 | - $c |= self::chrToInt($s[$i]) ^ 0xff; |
|
| 125 | + $c = ( self::chrToInt( $s[ 31 ] ) & 0x7f ) ^ 0x7f; |
|
| 126 | + for ( $i = 30; $i > 0; --$i ) { |
|
| 127 | + $c |= self::chrToInt( $s[ $i ] ) ^ 0xff; |
|
| 128 | 128 | } |
| 129 | - $c = ($c - 1) >> 8; |
|
| 130 | - $d = (0xed - 1 - self::chrToInt($s[0])) >> 8; |
|
| 131 | - $e = self::chrToInt($s[31]) >> 7; |
|
| 129 | + $c = ( $c - 1 ) >> 8; |
|
| 130 | + $d = ( 0xed - 1 - self::chrToInt( $s[ 0 ] ) ) >> 8; |
|
| 131 | + $e = self::chrToInt( $s[ 31 ] ) >> 7; |
|
| 132 | 132 | |
| 133 | - return 1 - ((($c & $d) | $e | self::chrToInt($s[0])) & 1); |
|
| 133 | + return 1 - ( ( ( $c & $d ) | $e | self::chrToInt( $s[ 0 ] ) ) & 1 ); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -139,53 +139,53 @@ discard block |
||
| 139 | 139 | * @return array{h: ParagonIE_Sodium_Core_Curve25519_Ge_P3, res: int} |
| 140 | 140 | * @throws SodiumException |
| 141 | 141 | */ |
| 142 | - public static function ristretto255_frombytes($s, $skipCanonicalCheck = false) |
|
| 142 | + public static function ristretto255_frombytes( $s, $skipCanonicalCheck = false ) |
|
| 143 | 143 | { |
| 144 | - if (!$skipCanonicalCheck) { |
|
| 145 | - if (!self::ristretto255_point_is_canonical($s)) { |
|
| 146 | - throw new SodiumException('S is not canonical'); |
|
| 144 | + if ( ! $skipCanonicalCheck ) { |
|
| 145 | + if ( ! self::ristretto255_point_is_canonical( $s ) ) { |
|
| 146 | + throw new SodiumException( 'S is not canonical' ); |
|
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | - $s_ = self::fe_frombytes($s); |
|
| 151 | - $ss = self::fe_sq($s_); /* ss = s^2 */ |
|
| 150 | + $s_ = self::fe_frombytes( $s ); |
|
| 151 | + $ss = self::fe_sq( $s_ ); /* ss = s^2 */ |
|
| 152 | 152 | |
| 153 | - $u1 = self::fe_sub(self::fe_1(), $ss); /* u1 = 1-ss */ |
|
| 154 | - $u1u1 = self::fe_sq($u1); /* u1u1 = u1^2 */ |
|
| 153 | + $u1 = self::fe_sub( self::fe_1(), $ss ); /* u1 = 1-ss */ |
|
| 154 | + $u1u1 = self::fe_sq( $u1 ); /* u1u1 = u1^2 */ |
|
| 155 | 155 | |
| 156 | - $u2 = self::fe_add(self::fe_1(), $ss); /* u2 = 1+ss */ |
|
| 157 | - $u2u2 = self::fe_sq($u2); /* u2u2 = u2^2 */ |
|
| 156 | + $u2 = self::fe_add( self::fe_1(), $ss ); /* u2 = 1+ss */ |
|
| 157 | + $u2u2 = self::fe_sq( $u2 ); /* u2u2 = u2^2 */ |
|
| 158 | 158 | |
| 159 | 159 | $v = self::fe_mul( |
| 160 | - ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d), |
|
| 160 | + ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( self::$d ), |
|
| 161 | 161 | $u1u1 |
| 162 | 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 */ |
|
| 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 | 166 | |
| 167 | 167 | // fe25519_1(one); |
| 168 | 168 | // notsquare = ristretto255_sqrt_ratio_m1(inv_sqrt, one, v_u2u2); |
| 169 | 169 | $one = self::fe_1(); |
| 170 | - $result = self::ristretto255_sqrt_ratio_m1($one, $v_u2u2); |
|
| 171 | - $inv_sqrt = $result['x']; |
|
| 172 | - $notsquare = $result['nonsquare']; |
|
| 170 | + $result = self::ristretto255_sqrt_ratio_m1( $one, $v_u2u2 ); |
|
| 171 | + $inv_sqrt = $result[ 'x' ]; |
|
| 172 | + $notsquare = $result[ 'nonsquare' ]; |
|
| 173 | 173 | |
| 174 | 174 | $h = new ParagonIE_Sodium_Core_Curve25519_Ge_P3(); |
| 175 | 175 | |
| 176 | - $h->X = self::fe_mul($inv_sqrt, $u2); |
|
| 177 | - $h->Y = self::fe_mul(self::fe_mul($inv_sqrt, $h->X), $v); |
|
| 176 | + $h->X = self::fe_mul( $inv_sqrt, $u2 ); |
|
| 177 | + $h->Y = self::fe_mul( self::fe_mul( $inv_sqrt, $h->X ), $v ); |
|
| 178 | 178 | |
| 179 | - $h->X = self::fe_mul($h->X, $s_); |
|
| 179 | + $h->X = self::fe_mul( $h->X, $s_ ); |
|
| 180 | 180 | $h->X = self::fe_abs( |
| 181 | - self::fe_add($h->X, $h->X) |
|
| 181 | + self::fe_add( $h->X, $h->X ) |
|
| 182 | 182 | ); |
| 183 | - $h->Y = self::fe_mul($u1, $h->Y); |
|
| 183 | + $h->Y = self::fe_mul( $u1, $h->Y ); |
|
| 184 | 184 | $h->Z = self::fe_1(); |
| 185 | - $h->T = self::fe_mul($h->X, $h->Y); |
|
| 185 | + $h->T = self::fe_mul( $h->X, $h->Y ); |
|
| 186 | 186 | |
| 187 | - $res = - ((1 - $notsquare) | self::fe_isnegative($h->T) | self::fe_iszero($h->Y)); |
|
| 188 | - return array('h' => $h, 'res' => $res); |
|
| 187 | + $res = - ( ( 1 - $notsquare ) | self::fe_isnegative( $h->T ) | self::fe_iszero( $h->Y ) ); |
|
| 188 | + return array( 'h' => $h, 'res' => $res ); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | /** |
@@ -193,45 +193,45 @@ discard block |
||
| 193 | 193 | * @return string |
| 194 | 194 | * @throws SodiumException |
| 195 | 195 | */ |
| 196 | - public static function ristretto255_p3_tobytes(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h) |
|
| 196 | + public static function ristretto255_p3_tobytes( ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h ) |
|
| 197 | 197 | { |
| 198 | - $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); |
|
| 199 | - $invsqrtamd = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$invsqrtamd); |
|
| 198 | + $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( self::$sqrtm1 ); |
|
| 199 | + $invsqrtamd = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( self::$invsqrtamd ); |
|
| 200 | 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 */ |
|
| 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 | 205 | |
| 206 | - $u1_u2u2 = self::fe_mul(self::fe_sq($u2), $u1); /* u1_u2u2 = u1*u2^2 */ |
|
| 206 | + $u1_u2u2 = self::fe_mul( self::fe_sq( $u2 ), $u1 ); /* u1_u2u2 = u1*u2^2 */ |
|
| 207 | 207 | $one = self::fe_1(); |
| 208 | 208 | |
| 209 | 209 | // fe25519_1(one); |
| 210 | 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']; |
|
| 211 | + $result = self::ristretto255_sqrt_ratio_m1( $one, $u1_u2u2 ); |
|
| 212 | + $inv_sqrt = $result[ 'x' ]; |
|
| 213 | 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 */ |
|
| 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 | 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); |
|
| 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 | 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); |
|
| 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 | 224 | |
| 225 | - $x_ = self::fe_copy($h->X); |
|
| 226 | - $y_ = self::fe_copy($h->Y); |
|
| 227 | - $den_inv = self::fe_copy($den2); |
|
| 225 | + $x_ = self::fe_copy( $h->X ); |
|
| 226 | + $y_ = self::fe_copy( $h->Y ); |
|
| 227 | + $den_inv = self::fe_copy( $den2 ); |
|
| 228 | 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); |
|
| 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 | 232 | |
| 233 | - $x_z_inv = self::fe_mul($x_, $z_inv); |
|
| 234 | - $y_ = self::fe_cneg($y_, self::fe_isnegative($x_z_inv)); |
|
| 233 | + $x_z_inv = self::fe_mul( $x_, $z_inv ); |
|
| 234 | + $y_ = self::fe_cneg( $y_, self::fe_isnegative( $x_z_inv ) ); |
|
| 235 | 235 | |
| 236 | 236 | |
| 237 | 237 | // fe25519_sub(s_, h->Z, y_); |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | self::fe_abs( |
| 243 | 243 | self::fe_mul( |
| 244 | 244 | $den_inv, |
| 245 | - self::fe_sub($h->Z, $y_) |
|
| 245 | + self::fe_sub( $h->Z, $y_ ) |
|
| 246 | 246 | ) |
| 247 | 247 | ) |
| 248 | 248 | ); |
@@ -254,39 +254,39 @@ discard block |
||
| 254 | 254 | * |
| 255 | 255 | * @throws SodiumException |
| 256 | 256 | */ |
| 257 | - public static function ristretto255_elligator(ParagonIE_Sodium_Core_Curve25519_Fe $t) |
|
| 257 | + public static function ristretto255_elligator( ParagonIE_Sodium_Core_Curve25519_Fe $t ) |
|
| 258 | 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); |
|
| 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 | 264 | |
| 265 | 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 */ |
|
| 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 | 270 | |
| 271 | 271 | $v = self::fe_mul( |
| 272 | 272 | self::fe_sub( |
| 273 | 273 | $c, |
| 274 | - self::fe_mul($r, $d) |
|
| 274 | + self::fe_mul( $r, $d ) |
|
| 275 | 275 | ), |
| 276 | 276 | $rpd |
| 277 | 277 | ); /* v = (c-r*d)*(r+d) */ |
| 278 | 278 | |
| 279 | - $result = self::ristretto255_sqrt_ratio_m1($u, $v); |
|
| 280 | - $s = $result['x']; |
|
| 281 | - $wasnt_square = 1 - $result['nonsquare']; |
|
| 279 | + $result = self::ristretto255_sqrt_ratio_m1( $u, $v ); |
|
| 280 | + $s = $result[ 'x' ]; |
|
| 281 | + $wasnt_square = 1 - $result[ 'nonsquare' ]; |
|
| 282 | 282 | |
| 283 | 283 | $s_prime = self::fe_neg( |
| 284 | 284 | self::fe_abs( |
| 285 | - self::fe_mul($s, $t) |
|
| 285 | + self::fe_mul( $s, $t ) |
|
| 286 | 286 | ) |
| 287 | 287 | ); /* s_prime = -|s*t| */ |
| 288 | - $s = self::fe_cmov($s, $s_prime, $wasnt_square); |
|
| 289 | - $c = self::fe_cmov($c, $r, $wasnt_square); |
|
| 288 | + $s = self::fe_cmov( $s, $s_prime, $wasnt_square ); |
|
| 289 | + $c = self::fe_cmov( $c, $r, $wasnt_square ); |
|
| 290 | 290 | |
| 291 | 291 | // fe25519_sub(n, r, one); /* n = r-1 */ |
| 292 | 292 | // fe25519_mul(n, n, c); /* n = c*(r-1) */ |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | $n = self::fe_sub( |
| 296 | 296 | self::fe_mul( |
| 297 | 297 | self::fe_mul( |
| 298 | - self::fe_sub($r, $one), |
|
| 298 | + self::fe_sub( $r, $one ), |
|
| 299 | 299 | $c |
| 300 | 300 | ), |
| 301 | 301 | $sqdmone |
@@ -304,20 +304,20 @@ discard block |
||
| 304 | 304 | ); /* n = c*(r-1)*(d-1)^2-v */ |
| 305 | 305 | |
| 306 | 306 | $w0 = self::fe_mul( |
| 307 | - self::fe_add($s, $s), |
|
| 307 | + self::fe_add( $s, $s ), |
|
| 308 | 308 | $v |
| 309 | 309 | ); /* w0 = 2s*v */ |
| 310 | 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 */ |
|
| 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 | 315 | |
| 316 | 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) |
|
| 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 | 321 | ); |
| 322 | 322 | } |
| 323 | 323 | |
@@ -326,32 +326,32 @@ discard block |
||
| 326 | 326 | * @return string |
| 327 | 327 | * @throws SodiumException |
| 328 | 328 | */ |
| 329 | - public static function ristretto255_from_hash($h) |
|
| 329 | + public static function ristretto255_from_hash( $h ) |
|
| 330 | 330 | { |
| 331 | - if (self::strlen($h) !== 64) { |
|
| 332 | - throw new SodiumException('Hash must be 64 bytes'); |
|
| 331 | + if ( self::strlen( $h ) !== 64 ) { |
|
| 332 | + throw new SodiumException( 'Hash must be 64 bytes' ); |
|
| 333 | 333 | } |
| 334 | 334 | //fe25519_frombytes(r0, h); |
| 335 | 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)); |
|
| 336 | + $r0 = self::fe_frombytes( self::substr( $h, 0, 32 ) ); |
|
| 337 | + $r1 = self::fe_frombytes( self::substr( $h, 32, 32 ) ); |
|
| 338 | 338 | |
| 339 | 339 | //ristretto255_elligator(&p0, r0); |
| 340 | 340 | //ristretto255_elligator(&p1, r1); |
| 341 | - $p0 = self::ristretto255_elligator($r0); |
|
| 342 | - $p1 = self::ristretto255_elligator($r1); |
|
| 341 | + $p0 = self::ristretto255_elligator( $r0 ); |
|
| 342 | + $p1 = self::ristretto255_elligator( $r1 ); |
|
| 343 | 343 | |
| 344 | 344 | //ge25519_p3_to_cached(&p1_cached, &p1); |
| 345 | 345 | //ge25519_add_cached(&p_p1p1, &p0, &p1_cached); |
| 346 | 346 | $p_p1p1 = self::ge_add( |
| 347 | 347 | $p0, |
| 348 | - self::ge_p3_to_cached($p1) |
|
| 348 | + self::ge_p3_to_cached( $p1 ) |
|
| 349 | 349 | ); |
| 350 | 350 | |
| 351 | 351 | //ge25519_p1p1_to_p3(&p, &p_p1p1); |
| 352 | 352 | //ristretto255_p3_tobytes(s, &p); |
| 353 | 353 | return self::ristretto255_p3_tobytes( |
| 354 | - self::ge_p1p1_to_p3($p_p1p1) |
|
| 354 | + self::ge_p1p1_to_p3( $p_p1p1 ) |
|
| 355 | 355 | ); |
| 356 | 356 | } |
| 357 | 357 | |
@@ -360,10 +360,10 @@ discard block |
||
| 360 | 360 | * @return int |
| 361 | 361 | * @throws SodiumException |
| 362 | 362 | */ |
| 363 | - public static function is_valid_point($p) |
|
| 363 | + public static function is_valid_point( $p ) |
|
| 364 | 364 | { |
| 365 | - $result = self::ristretto255_frombytes($p); |
|
| 366 | - if ($result['res'] !== 0) { |
|
| 365 | + $result = self::ristretto255_frombytes( $p ); |
|
| 366 | + if ( $result[ 'res' ] !== 0 ) { |
|
| 367 | 367 | return 0; |
| 368 | 368 | } |
| 369 | 369 | return 1; |
@@ -375,19 +375,19 @@ discard block |
||
| 375 | 375 | * @return string |
| 376 | 376 | * @throws SodiumException |
| 377 | 377 | */ |
| 378 | - public static function ristretto255_add($p, $q) |
|
| 378 | + public static function ristretto255_add( $p, $q ) |
|
| 379 | 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'); |
|
| 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 | 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); |
|
| 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 | 391 | } |
| 392 | 392 | |
| 393 | 393 | /** |
@@ -396,19 +396,19 @@ discard block |
||
| 396 | 396 | * @return string |
| 397 | 397 | * @throws SodiumException |
| 398 | 398 | */ |
| 399 | - public static function ristretto255_sub($p, $q) |
|
| 399 | + public static function ristretto255_sub( $p, $q ) |
|
| 400 | 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'); |
|
| 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 | 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); |
|
| 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 | 412 | } |
| 413 | 413 | |
| 414 | 414 | |
@@ -420,46 +420,46 @@ discard block |
||
| 420 | 420 | * @throws SodiumException |
| 421 | 421 | * @psalm-suppress PossiblyInvalidArgument hash API |
| 422 | 422 | */ |
| 423 | - protected static function h2c_string_to_hash_sha256($hLen, $ctx, $msg) |
|
| 423 | + protected static function h2c_string_to_hash_sha256( $hLen, $ctx, $msg ) |
|
| 424 | 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'); |
|
| 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 | 429 | } |
| 430 | 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); |
|
| 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 | 436 | $ctx_len = 32; |
| 437 | 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]); |
|
| 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 | 460 | } |
| 461 | 461 | } |
| 462 | - return self::intArrayToString(array_slice($h, 0, $hLen)); |
|
| 462 | + return self::intArrayToString( array_slice( $h, 0, $hLen ) ); |
|
| 463 | 463 | } |
| 464 | 464 | |
| 465 | 465 | /** |
@@ -470,46 +470,46 @@ discard block |
||
| 470 | 470 | * @throws SodiumException |
| 471 | 471 | * @psalm-suppress PossiblyInvalidArgument hash API |
| 472 | 472 | */ |
| 473 | - protected static function h2c_string_to_hash_sha512($hLen, $ctx, $msg) |
|
| 473 | + protected static function h2c_string_to_hash_sha512( $hLen, $ctx, $msg ) |
|
| 474 | 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'); |
|
| 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 | 479 | } |
| 480 | 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); |
|
| 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 | 486 | $ctx_len = 32; |
| 487 | 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]); |
|
| 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 | 510 | } |
| 511 | 511 | } |
| 512 | - return self::intArrayToString(array_slice($h, 0, $hLen)); |
|
| 512 | + return self::intArrayToString( array_slice( $h, 0, $hLen ) ); |
|
| 513 | 513 | } |
| 514 | 514 | |
| 515 | 515 | /** |
@@ -520,15 +520,15 @@ discard block |
||
| 520 | 520 | * @return string |
| 521 | 521 | * @throws SodiumException |
| 522 | 522 | */ |
| 523 | - public static function h2c_string_to_hash($hLen, $ctx, $msg, $hash_alg) |
|
| 523 | + public static function h2c_string_to_hash( $hLen, $ctx, $msg, $hash_alg ) |
|
| 524 | 524 | { |
| 525 | - switch ($hash_alg) { |
|
| 525 | + switch ( $hash_alg ) { |
|
| 526 | 526 | case self::CORE_H2C_SHA256: |
| 527 | - return self::h2c_string_to_hash_sha256($hLen, $ctx, $msg); |
|
| 527 | + return self::h2c_string_to_hash_sha256( $hLen, $ctx, $msg ); |
|
| 528 | 528 | case self::CORE_H2C_SHA512: |
| 529 | - return self::h2c_string_to_hash_sha512($hLen, $ctx, $msg); |
|
| 529 | + return self::h2c_string_to_hash_sha512( $hLen, $ctx, $msg ); |
|
| 530 | 530 | default: |
| 531 | - throw new SodiumException('Invalid H2C hash algorithm'); |
|
| 531 | + throw new SodiumException( 'Invalid H2C hash algorithm' ); |
|
| 532 | 532 | } |
| 533 | 533 | } |
| 534 | 534 | |
@@ -539,10 +539,10 @@ discard block |
||
| 539 | 539 | * @return string |
| 540 | 540 | * @throws SodiumException |
| 541 | 541 | */ |
| 542 | - protected static function _string_to_element($ctx, $msg, $hash_alg) |
|
| 542 | + protected static function _string_to_element( $ctx, $msg, $hash_alg ) |
|
| 543 | 543 | { |
| 544 | 544 | return self::ristretto255_from_hash( |
| 545 | - self::h2c_string_to_hash(self::crypto_core_ristretto255_HASHBYTES, $ctx, $msg, $hash_alg) |
|
| 545 | + self::h2c_string_to_hash( self::crypto_core_ristretto255_HASHBYTES, $ctx, $msg, $hash_alg ) |
|
| 546 | 546 | ); |
| 547 | 547 | } |
| 548 | 548 | |
@@ -554,7 +554,7 @@ discard block |
||
| 554 | 554 | public static function ristretto255_random() |
| 555 | 555 | { |
| 556 | 556 | return self::ristretto255_from_hash( |
| 557 | - ParagonIE_Sodium_Compat::randombytes_buf(self::crypto_core_ristretto255_HASHBYTES) |
|
| 557 | + ParagonIE_Sodium_Compat::randombytes_buf( self::crypto_core_ristretto255_HASHBYTES ) |
|
| 558 | 558 | ); |
| 559 | 559 | } |
| 560 | 560 | |
@@ -572,9 +572,9 @@ discard block |
||
| 572 | 572 | * @return string |
| 573 | 573 | * @throws SodiumException |
| 574 | 574 | */ |
| 575 | - public static function ristretto255_scalar_complement($s) |
|
| 575 | + public static function ristretto255_scalar_complement( $s ) |
|
| 576 | 576 | { |
| 577 | - return self::scalar_complement($s); |
|
| 577 | + return self::scalar_complement( $s ); |
|
| 578 | 578 | } |
| 579 | 579 | |
| 580 | 580 | |
@@ -582,9 +582,9 @@ discard block |
||
| 582 | 582 | * @param string $s |
| 583 | 583 | * @return string |
| 584 | 584 | */ |
| 585 | - public static function ristretto255_scalar_invert($s) |
|
| 585 | + public static function ristretto255_scalar_invert( $s ) |
|
| 586 | 586 | { |
| 587 | - return self::sc25519_invert($s); |
|
| 587 | + return self::sc25519_invert( $s ); |
|
| 588 | 588 | } |
| 589 | 589 | |
| 590 | 590 | /** |
@@ -592,9 +592,9 @@ discard block |
||
| 592 | 592 | * @return string |
| 593 | 593 | * @throws SodiumException |
| 594 | 594 | */ |
| 595 | - public static function ristretto255_scalar_negate($s) |
|
| 595 | + public static function ristretto255_scalar_negate( $s ) |
|
| 596 | 596 | { |
| 597 | - return self::scalar_negate($s); |
|
| 597 | + return self::scalar_negate( $s ); |
|
| 598 | 598 | } |
| 599 | 599 | |
| 600 | 600 | /** |
@@ -602,9 +602,9 @@ discard block |
||
| 602 | 602 | * @param string $y |
| 603 | 603 | * @return string |
| 604 | 604 | */ |
| 605 | - public static function ristretto255_scalar_add($x, $y) |
|
| 605 | + public static function ristretto255_scalar_add( $x, $y ) |
|
| 606 | 606 | { |
| 607 | - return self::scalar_add($x, $y); |
|
| 607 | + return self::scalar_add( $x, $y ); |
|
| 608 | 608 | } |
| 609 | 609 | |
| 610 | 610 | /** |
@@ -612,9 +612,9 @@ discard block |
||
| 612 | 612 | * @param string $y |
| 613 | 613 | * @return string |
| 614 | 614 | */ |
| 615 | - public static function ristretto255_scalar_sub($x, $y) |
|
| 615 | + public static function ristretto255_scalar_sub( $x, $y ) |
|
| 616 | 616 | { |
| 617 | - return self::scalar_sub($x, $y); |
|
| 617 | + return self::scalar_sub( $x, $y ); |
|
| 618 | 618 | } |
| 619 | 619 | |
| 620 | 620 | /** |
@@ -622,9 +622,9 @@ discard block |
||
| 622 | 622 | * @param string $y |
| 623 | 623 | * @return string |
| 624 | 624 | */ |
| 625 | - public static function ristretto255_scalar_mul($x, $y) |
|
| 625 | + public static function ristretto255_scalar_mul( $x, $y ) |
|
| 626 | 626 | { |
| 627 | - return self::sc25519_mul($x, $y); |
|
| 627 | + return self::sc25519_mul( $x, $y ); |
|
| 628 | 628 | } |
| 629 | 629 | |
| 630 | 630 | /** |
@@ -634,28 +634,28 @@ discard block |
||
| 634 | 634 | * @return string |
| 635 | 635 | * @throws SodiumException |
| 636 | 636 | */ |
| 637 | - public static function ristretto255_scalar_from_string($ctx, $msg, $hash_alg) |
|
| 637 | + public static function ristretto255_scalar_from_string( $ctx, $msg, $hash_alg ) |
|
| 638 | 638 | { |
| 639 | - $h = array_fill(0, 64, 0); |
|
| 639 | + $h = array_fill( 0, 64, 0 ); |
|
| 640 | 640 | $h_be = self::stringToIntArray( |
| 641 | 641 | self::h2c_string_to_hash( |
| 642 | 642 | self::HASH_SC_L, $ctx, $msg, $hash_alg |
| 643 | 643 | ) |
| 644 | 644 | ); |
| 645 | 645 | |
| 646 | - for ($i = 0; $i < self::HASH_SC_L; ++$i) { |
|
| 647 | - $h[$i] = $h_be[self::HASH_SC_L - 1 - $i]; |
|
| 646 | + for ( $i = 0; $i < self::HASH_SC_L; ++$i ) { |
|
| 647 | + $h[ $i ] = $h_be[ self::HASH_SC_L - 1 - $i ]; |
|
| 648 | 648 | } |
| 649 | - return self::ristretto255_scalar_reduce(self::intArrayToString($h)); |
|
| 649 | + return self::ristretto255_scalar_reduce( self::intArrayToString( $h ) ); |
|
| 650 | 650 | } |
| 651 | 651 | |
| 652 | 652 | /** |
| 653 | 653 | * @param string $s |
| 654 | 654 | * @return string |
| 655 | 655 | */ |
| 656 | - public static function ristretto255_scalar_reduce($s) |
|
| 656 | + public static function ristretto255_scalar_reduce( $s ) |
|
| 657 | 657 | { |
| 658 | - return self::sc_reduce($s); |
|
| 658 | + return self::sc_reduce( $s ); |
|
| 659 | 659 | } |
| 660 | 660 | |
| 661 | 661 | /** |
@@ -664,26 +664,26 @@ discard block |
||
| 664 | 664 | * @return string |
| 665 | 665 | * @throws SodiumException |
| 666 | 666 | */ |
| 667 | - public static function scalarmult_ristretto255($n, $p) |
|
| 667 | + public static function scalarmult_ristretto255( $n, $p ) |
|
| 668 | 668 | { |
| 669 | - if (self::strlen($n) !== 32) { |
|
| 670 | - throw new SodiumException('Scalar must be 32 bytes, ' . self::strlen($p) . ' given.'); |
|
| 669 | + if ( self::strlen( $n ) !== 32 ) { |
|
| 670 | + throw new SodiumException( 'Scalar must be 32 bytes, ' . self::strlen( $p ) . ' given.' ); |
|
| 671 | 671 | } |
| 672 | - if (self::strlen($p) !== 32) { |
|
| 673 | - throw new SodiumException('Point must be 32 bytes, ' . self::strlen($p) . ' given.'); |
|
| 672 | + if ( self::strlen( $p ) !== 32 ) { |
|
| 673 | + throw new SodiumException( 'Point must be 32 bytes, ' . self::strlen( $p ) . ' given.' ); |
|
| 674 | 674 | } |
| 675 | - $result = self::ristretto255_frombytes($p); |
|
| 676 | - if ($result['res'] !== 0) { |
|
| 677 | - throw new SodiumException('Could not multiply points'); |
|
| 675 | + $result = self::ristretto255_frombytes( $p ); |
|
| 676 | + if ( $result[ 'res' ] !== 0 ) { |
|
| 677 | + throw new SodiumException( 'Could not multiply points' ); |
|
| 678 | 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'); |
|
| 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 | 687 | } |
| 688 | 688 | return $q; |
| 689 | 689 | } |
@@ -693,14 +693,14 @@ discard block |
||
| 693 | 693 | * @return string |
| 694 | 694 | * @throws SodiumException |
| 695 | 695 | */ |
| 696 | - public static function scalarmult_ristretto255_base($n) |
|
| 696 | + public static function scalarmult_ristretto255_base( $n ) |
|
| 697 | 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'); |
|
| 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 | 704 | } |
| 705 | 705 | return $q; |
| 706 | 706 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -21,18 +21,18 @@ discard block |
||
| 21 | 21 | * @throws InvalidArgumentException |
| 22 | 22 | * @throws TypeError |
| 23 | 23 | */ |
| 24 | - public function __construct($key = '', $iv = '', $counter = '') |
|
| 24 | + public function __construct( $key = '', $iv = '', $counter = '' ) |
|
| 25 | 25 | { |
| 26 | - if (self::strlen($iv) !== 12) { |
|
| 27 | - throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.'); |
|
| 26 | + if ( self::strlen( $iv ) !== 12 ) { |
|
| 27 | + throw new InvalidArgumentException( 'ChaCha20 expects a 96-bit nonce in IETF mode.' ); |
|
| 28 | 28 | } |
| 29 | - parent::__construct($key, self::substr($iv, 0, 8), $counter); |
|
| 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)); |
|
| 31 | + if ( ! empty( $counter ) ) { |
|
| 32 | + $this->container[ 12 ] = self::load_4( self::substr( $counter, 0, 4 ) ); |
|
| 33 | 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)); |
|
| 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 | 37 | } |
| 38 | 38 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (!class_exists('SodiumException', false)) { |
|
| 3 | +if ( ! class_exists( 'SodiumException', false ) ) { |
|
| 4 | 4 | /** |
| 5 | 5 | * Class SodiumException |
| 6 | 6 | */ |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_BLAKE2b', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -20,18 +20,18 @@ discard block |
||
| 20 | 20 | * @var array<int, array<int, int>> |
| 21 | 21 | */ |
| 22 | 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) |
|
| 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 | 35 | ); |
| 36 | 36 | |
| 37 | 37 | const BLOCKBYTES = 128; |
@@ -49,9 +49,9 @@ discard block |
||
| 49 | 49 | * @throws SodiumException |
| 50 | 50 | * @throws TypeError |
| 51 | 51 | */ |
| 52 | - public static function new64($high, $low) |
|
| 52 | + public static function new64( $high, $low ) |
|
| 53 | 53 | { |
| 54 | - return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high); |
|
| 54 | + return ParagonIE_Sodium_Core32_Int64::fromInts( $low, $high ); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -65,10 +65,10 @@ discard block |
||
| 65 | 65 | * @throws SodiumException |
| 66 | 66 | * @throws TypeError |
| 67 | 67 | */ |
| 68 | - protected static function to64($num) |
|
| 68 | + protected static function to64( $num ) |
|
| 69 | 69 | { |
| 70 | - list($hi, $lo) = self::numericTo64BitInteger($num); |
|
| 71 | - return self::new64($hi, $lo); |
|
| 70 | + list( $hi, $lo ) = self::numericTo64BitInteger( $num ); |
|
| 71 | + return self::new64( $hi, $lo ); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -81,9 +81,9 @@ discard block |
||
| 81 | 81 | * @param ParagonIE_Sodium_Core32_Int64 $y |
| 82 | 82 | * @return ParagonIE_Sodium_Core32_Int64 |
| 83 | 83 | */ |
| 84 | - protected static function add64($x, $y) |
|
| 84 | + protected static function add64( $x, $y ) |
|
| 85 | 85 | { |
| 86 | - return $x->addInt64($y); |
|
| 86 | + return $x->addInt64( $y ); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -94,9 +94,9 @@ discard block |
||
| 94 | 94 | * @param ParagonIE_Sodium_Core32_Int64 $z |
| 95 | 95 | * @return ParagonIE_Sodium_Core32_Int64 |
| 96 | 96 | */ |
| 97 | - public static function add364($x, $y, $z) |
|
| 97 | + public static function add364( $x, $y, $z ) |
|
| 98 | 98 | { |
| 99 | - return $x->addInt64($y)->addInt64($z); |
|
| 99 | + return $x->addInt64( $y )->addInt64( $z ); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
@@ -107,9 +107,9 @@ discard block |
||
| 107 | 107 | * @return ParagonIE_Sodium_Core32_Int64 |
| 108 | 108 | * @throws TypeError |
| 109 | 109 | */ |
| 110 | - public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) |
|
| 110 | + public static function xor64( ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y ) |
|
| 111 | 111 | { |
| 112 | - return $x->xorInt64($y); |
|
| 112 | + return $x->xorInt64( $y ); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | /** |
@@ -121,9 +121,9 @@ discard block |
||
| 121 | 121 | * @throws SodiumException |
| 122 | 122 | * @throws TypeError |
| 123 | 123 | */ |
| 124 | - public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) |
|
| 124 | + public static function rotr64( ParagonIE_Sodium_Core32_Int64 $x, $c ) |
|
| 125 | 125 | { |
| 126 | - return $x->rotateRight($c); |
|
| 126 | + return $x->rotateRight( $c ); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | /** |
@@ -135,19 +135,19 @@ discard block |
||
| 135 | 135 | * @throws SodiumException |
| 136 | 136 | * @throws TypeError |
| 137 | 137 | */ |
| 138 | - public static function load64($x, $i) |
|
| 138 | + public static function load64( $x, $i ) |
|
| 139 | 139 | { |
| 140 | 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); |
|
| 141 | + $l = (int)( $x[ $i ] ) |
|
| 142 | + | ( (int)( $x[ $i + 1 ] ) << 8 ) |
|
| 143 | + | ( (int)( $x[ $i + 2 ] ) << 16 ) |
|
| 144 | + | ( (int)( $x[ $i + 3 ] ) << 24 ); |
|
| 145 | 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); |
|
| 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 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -164,17 +164,17 @@ discard block |
||
| 164 | 164 | * @psalm-suppress MixedArrayAssignment |
| 165 | 165 | * @psalm-suppress MixedArrayOffset |
| 166 | 166 | */ |
| 167 | - public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) |
|
| 167 | + public static function store64( SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u ) |
|
| 168 | 168 | { |
| 169 | 169 | $v = clone $u; |
| 170 | 170 | $maxLength = $x->getSize() - 1; |
| 171 | - for ($j = 0; $j < 8; ++$j) { |
|
| 172 | - $k = 3 - ($j >> 1); |
|
| 173 | - $x[$i] = $v->limbs[$k] & 0xff; |
|
| 171 | + for ( $j = 0; $j < 8; ++$j ) { |
|
| 172 | + $k = 3 - ( $j >> 1 ); |
|
| 173 | + $x[ $i ] = $v->limbs[ $k ] & 0xff; |
|
| 174 | 174 | if (++$i > $maxLength) { |
| 175 | 175 | return; |
| 176 | 176 | } |
| 177 | - $v->limbs[$k] >>= 8; |
|
| 177 | + $v->limbs[ $k ] >>= 8; |
|
| 178 | 178 | } |
| 179 | 179 | } |
| 180 | 180 | |
@@ -190,18 +190,18 @@ discard block |
||
| 190 | 190 | public static function pseudoConstructor() |
| 191 | 191 | { |
| 192 | 192 | static $called = false; |
| 193 | - if ($called) { |
|
| 193 | + if ( $called ) { |
|
| 194 | 194 | return; |
| 195 | 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); |
|
| 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 | 205 | |
| 206 | 206 | $called = true; |
| 207 | 207 | } |
@@ -223,26 +223,26 @@ discard block |
||
| 223 | 223 | */ |
| 224 | 224 | protected static function context() |
| 225 | 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]; |
|
| 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 | 236 | } |
| 237 | - for ($i = 256; $i--;) { |
|
| 238 | - $ctx[3][$i] = 0; |
|
| 237 | + for ( $i = 256; $i--; ) { |
|
| 238 | + $ctx[ 3 ][ $i ] = 0; |
|
| 239 | 239 | } |
| 240 | 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; |
|
| 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 | 246 | |
| 247 | 247 | return $ctx; |
| 248 | 248 | } |
@@ -260,43 +260,43 @@ discard block |
||
| 260 | 260 | * @psalm-suppress MixedArrayAssignment |
| 261 | 261 | * @psalm-suppress MixedAssignment |
| 262 | 262 | */ |
| 263 | - protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
| 263 | + protected static function compress( SplFixedArray $ctx, SplFixedArray $buf ) |
|
| 264 | 264 | { |
| 265 | - $m = new SplFixedArray(16); |
|
| 266 | - $v = new SplFixedArray(16); |
|
| 265 | + $m = new SplFixedArray( 16 ); |
|
| 266 | + $v = new SplFixedArray( 16 ); |
|
| 267 | 267 | |
| 268 | - for ($i = 16; $i--;) { |
|
| 269 | - $m[$i] = self::load64($buf, $i << 3); |
|
| 268 | + for ( $i = 16; $i--; ) { |
|
| 269 | + $m[ $i ] = self::load64( $buf, $i << 3 ); |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - for ($i = 8; $i--;) { |
|
| 273 | - $v[$i] = $ctx[0][$i]; |
|
| 272 | + for ( $i = 8; $i--; ) { |
|
| 273 | + $v[ $i ] = $ctx[ 0 ][ $i ]; |
|
| 274 | 274 | } |
| 275 | 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); |
|
| 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 | 295 | } |
| 296 | 296 | |
| 297 | - for ($i = 8; $i--;) { |
|
| 298 | - $ctx[0][$i] = self::xor64( |
|
| 299 | - $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
| 297 | + for ( $i = 8; $i--; ) { |
|
| 298 | + $ctx[ 0 ][ $i ] = self::xor64( |
|
| 299 | + $ctx[ 0 ][ $i ], self::xor64( $v[ $i ], $v[ $i + 8 ] ) |
|
| 300 | 300 | ); |
| 301 | 301 | } |
| 302 | 302 | } |
@@ -318,16 +318,16 @@ discard block |
||
| 318 | 318 | * @psalm-suppress MixedArgument |
| 319 | 319 | * @psalm-suppress MixedArrayOffset |
| 320 | 320 | */ |
| 321 | - public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
| 321 | + public static function G( $r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m ) |
|
| 322 | 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); |
|
| 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 | 331 | return $v; |
| 332 | 332 | } |
| 333 | 333 | |
@@ -343,25 +343,25 @@ discard block |
||
| 343 | 343 | * @psalm-suppress MixedArrayAccess |
| 344 | 344 | * @psalm-suppress MixedArrayAssignment |
| 345 | 345 | */ |
| 346 | - public static function increment_counter($ctx, $inc) |
|
| 346 | + public static function increment_counter( $ctx, $inc ) |
|
| 347 | 347 | { |
| 348 | - if ($inc < 0) { |
|
| 349 | - throw new SodiumException('Increasing by a negative number makes no sense.'); |
|
| 348 | + if ( $inc < 0 ) { |
|
| 349 | + throw new SodiumException( 'Increasing by a negative number makes no sense.' ); |
|
| 350 | 350 | } |
| 351 | - $t = self::to64($inc); |
|
| 351 | + $t = self::to64( $inc ); |
|
| 352 | 352 | # S->t is $ctx[1] in our implementation |
| 353 | 353 | |
| 354 | 354 | # S->t[0] = ( uint64_t )( t >> 0 ); |
| 355 | - $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
| 355 | + $ctx[ 1 ][ 0 ] = self::add64( $ctx[ 1 ][ 0 ], $t ); |
|
| 356 | 356 | |
| 357 | 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'); |
|
| 358 | + if ( ! ( $ctx[ 1 ][ 0 ] instanceof ParagonIE_Sodium_Core32_Int64 ) ) { |
|
| 359 | + throw new TypeError( 'Not an int64' ); |
|
| 360 | 360 | } |
| 361 | 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)); |
|
| 362 | + $c = $ctx[ 1 ][ 0 ]; |
|
| 363 | + if ( $c->isLessThanInt( $inc ) ) { |
|
| 364 | + $ctx[ 1 ][ 1 ] = self::add64( $ctx[ 1 ][ 1 ], self::to64( 1 ) ); |
|
| 365 | 365 | } |
| 366 | 366 | } |
| 367 | 367 | |
@@ -382,37 +382,37 @@ discard block |
||
| 382 | 382 | * @psalm-suppress MixedMethodCall |
| 383 | 383 | * @psalm-suppress MixedOperand |
| 384 | 384 | */ |
| 385 | - public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
| 385 | + public static function update( SplFixedArray $ctx, SplFixedArray $p, $plen ) |
|
| 386 | 386 | { |
| 387 | 387 | self::pseudoConstructor(); |
| 388 | 388 | |
| 389 | 389 | $offset = 0; |
| 390 | - while ($plen > 0) { |
|
| 391 | - $left = $ctx[4]; |
|
| 390 | + while ( $plen > 0 ) { |
|
| 391 | + $left = $ctx[ 4 ]; |
|
| 392 | 392 | $fill = 256 - $left; |
| 393 | 393 | |
| 394 | - if ($plen > $fill) { |
|
| 394 | + if ( $plen > $fill ) { |
|
| 395 | 395 | # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
| 396 | - for ($i = $fill; $i--;) { |
|
| 397 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
| 396 | + for ( $i = $fill; $i--; ) { |
|
| 397 | + $ctx[ 3 ][ $i + $left ] = $p[ $i + $offset ]; |
|
| 398 | 398 | } |
| 399 | 399 | |
| 400 | 400 | # S->buflen += fill; |
| 401 | - $ctx[4] += $fill; |
|
| 401 | + $ctx[ 4 ] += $fill; |
|
| 402 | 402 | |
| 403 | 403 | # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
| 404 | - self::increment_counter($ctx, 128); |
|
| 404 | + self::increment_counter( $ctx, 128 ); |
|
| 405 | 405 | |
| 406 | 406 | # blake2b_compress( S, S->buf ); /* Compress */ |
| 407 | - self::compress($ctx, $ctx[3]); |
|
| 407 | + self::compress( $ctx, $ctx[ 3 ] ); |
|
| 408 | 408 | |
| 409 | 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]; |
|
| 410 | + for ( $i = 128; $i--; ) { |
|
| 411 | + $ctx[ 3 ][ $i ] = $ctx[ 3 ][ $i + 128 ]; |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | # S->buflen -= BLAKE2B_BLOCKBYTES; |
| 415 | - $ctx[4] -= 128; |
|
| 415 | + $ctx[ 4 ] -= 128; |
|
| 416 | 416 | |
| 417 | 417 | # in += fill; |
| 418 | 418 | $offset += $fill; |
@@ -420,10 +420,10 @@ discard block |
||
| 420 | 420 | # inlen -= fill; |
| 421 | 421 | $plen -= $fill; |
| 422 | 422 | } else { |
| 423 | - for ($i = $plen; $i--;) { |
|
| 424 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
| 423 | + for ( $i = $plen; $i--; ) { |
|
| 424 | + $ctx[ 3 ][ $i + $left ] = $p[ $i + $offset ]; |
|
| 425 | 425 | } |
| 426 | - $ctx[4] += $plen; |
|
| 426 | + $ctx[ 4 ] += $plen; |
|
| 427 | 427 | $offset += $plen; |
| 428 | 428 | $plen -= $plen; |
| 429 | 429 | } |
@@ -446,34 +446,34 @@ discard block |
||
| 446 | 446 | * @psalm-suppress MixedMethodCall |
| 447 | 447 | * @psalm-suppress MixedOperand |
| 448 | 448 | */ |
| 449 | - public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
| 449 | + public static function finish( SplFixedArray $ctx, SplFixedArray $out ) |
|
| 450 | 450 | { |
| 451 | 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'); |
|
| 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 | 458 | } |
| 459 | - for ($i = $ctx[4]; $i--;) { |
|
| 460 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
| 459 | + for ( $i = $ctx[ 4 ]; $i--; ) { |
|
| 460 | + $ctx[ 3 ][ $i ] = $ctx[ 3 ][ $i + 128 ]; |
|
| 461 | 461 | } |
| 462 | 462 | } |
| 463 | 463 | |
| 464 | - self::increment_counter($ctx, $ctx[4]); |
|
| 465 | - $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
| 464 | + self::increment_counter( $ctx, $ctx[ 4 ] ); |
|
| 465 | + $ctx[ 2 ][ 0 ] = self::new64( 0xffffffff, 0xffffffff ); |
|
| 466 | 466 | |
| 467 | - for ($i = 256 - $ctx[4]; $i--;) { |
|
| 467 | + for ( $i = 256 - $ctx[ 4 ]; $i--; ) { |
|
| 468 | 468 | /** @var int $i */ |
| 469 | - $ctx[3][$i + $ctx[4]] = 0; |
|
| 469 | + $ctx[ 3 ][ $i + $ctx[ 4 ] ] = 0; |
|
| 470 | 470 | } |
| 471 | 471 | |
| 472 | - self::compress($ctx, $ctx[3]); |
|
| 472 | + self::compress( $ctx, $ctx[ 3 ] ); |
|
| 473 | 473 | |
| 474 | - $i = (int) (($out->getSize() - 1) / 8); |
|
| 475 | - for (; $i >= 0; --$i) { |
|
| 476 | - self::store64($out, $i << 3, $ctx[0][$i]); |
|
| 474 | + $i = (int)( ( $out->getSize() - 1 ) / 8 ); |
|
| 475 | + for ( ; $i >= 0; --$i ) { |
|
| 476 | + self::store64( $out, $i << 3, $ctx[ 0 ][ $i ] ); |
|
| 477 | 477 | } |
| 478 | 478 | return $out; |
| 479 | 479 | } |
@@ -503,68 +503,68 @@ discard block |
||
| 503 | 503 | self::pseudoConstructor(); |
| 504 | 504 | $klen = 0; |
| 505 | 505 | |
| 506 | - if ($key !== null) { |
|
| 507 | - if (count($key) > 64) { |
|
| 508 | - throw new SodiumException('Invalid key size'); |
|
| 506 | + if ( $key !== null ) { |
|
| 507 | + if ( count( $key ) > 64 ) { |
|
| 508 | + throw new SodiumException( 'Invalid key size' ); |
|
| 509 | 509 | } |
| 510 | - $klen = count($key); |
|
| 510 | + $klen = count( $key ); |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | - if ($outlen > 64) { |
|
| 514 | - throw new SodiumException('Invalid output size'); |
|
| 513 | + if ( $outlen > 64 ) { |
|
| 514 | + throw new SodiumException( 'Invalid output size' ); |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | $ctx = self::context(); |
| 518 | 518 | |
| 519 | - $p = new SplFixedArray(64); |
|
| 519 | + $p = new SplFixedArray( 64 ); |
|
| 520 | 520 | // Zero our param buffer... |
| 521 | - for ($i = 64; --$i;) { |
|
| 522 | - $p[$i] = 0; |
|
| 521 | + for ( $i = 64; --$i; ) { |
|
| 522 | + $p[ $i ] = 0; |
|
| 523 | 523 | } |
| 524 | 524 | |
| 525 | - $p[0] = $outlen; // digest_length |
|
| 526 | - $p[1] = $klen; // key_length |
|
| 527 | - $p[2] = 1; // fanout |
|
| 528 | - $p[3] = 1; // depth |
|
| 525 | + $p[ 0 ] = $outlen; // digest_length |
|
| 526 | + $p[ 1 ] = $klen; // key_length |
|
| 527 | + $p[ 2 ] = 1; // fanout |
|
| 528 | + $p[ 3 ] = 1; // depth |
|
| 529 | 529 | |
| 530 | - if ($salt instanceof SplFixedArray) { |
|
| 530 | + if ( $salt instanceof SplFixedArray ) { |
|
| 531 | 531 | // salt: [32] through [47] |
| 532 | - for ($i = 0; $i < 16; ++$i) { |
|
| 533 | - $p[32 + $i] = (int) $salt[$i]; |
|
| 532 | + for ( $i = 0; $i < 16; ++$i ) { |
|
| 533 | + $p[ 32 + $i ] = (int)$salt[ $i ]; |
|
| 534 | 534 | } |
| 535 | 535 | } |
| 536 | - if ($personal instanceof SplFixedArray) { |
|
| 536 | + if ( $personal instanceof SplFixedArray ) { |
|
| 537 | 537 | // personal: [48] through [63] |
| 538 | - for ($i = 0; $i < 16; ++$i) { |
|
| 539 | - $p[48 + $i] = (int) $personal[$i]; |
|
| 538 | + for ( $i = 0; $i < 16; ++$i ) { |
|
| 539 | + $p[ 48 + $i ] = (int)$personal[ $i ]; |
|
| 540 | 540 | } |
| 541 | 541 | } |
| 542 | 542 | |
| 543 | - $ctx[0][0] = self::xor64( |
|
| 544 | - $ctx[0][0], |
|
| 545 | - self::load64($p, 0) |
|
| 543 | + $ctx[ 0 ][ 0 ] = self::xor64( |
|
| 544 | + $ctx[ 0 ][ 0 ], |
|
| 545 | + self::load64( $p, 0 ) |
|
| 546 | 546 | ); |
| 547 | 547 | |
| 548 | - if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { |
|
| 548 | + if ( $salt instanceof SplFixedArray || $personal instanceof SplFixedArray ) { |
|
| 549 | 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) |
|
| 550 | + for ( $i = 1; $i < 8; ++$i ) { |
|
| 551 | + $ctx[ 0 ][ $i ] = self::xor64( |
|
| 552 | + $ctx[ 0 ][ $i ], |
|
| 553 | + self::load64( $p, $i << 3 ) |
|
| 554 | 554 | ); |
| 555 | 555 | } |
| 556 | 556 | } |
| 557 | 557 | |
| 558 | - if ($klen > 0 && $key instanceof SplFixedArray) { |
|
| 559 | - $block = new SplFixedArray(128); |
|
| 560 | - for ($i = 128; $i--;) { |
|
| 561 | - $block[$i] = 0; |
|
| 558 | + if ( $klen > 0 && $key instanceof SplFixedArray ) { |
|
| 559 | + $block = new SplFixedArray( 128 ); |
|
| 560 | + for ( $i = 128; $i--; ) { |
|
| 561 | + $block[ $i ] = 0; |
|
| 562 | 562 | } |
| 563 | - for ($i = $klen; $i--;) { |
|
| 564 | - $block[$i] = $key[$i]; |
|
| 563 | + for ( $i = $klen; $i--; ) { |
|
| 564 | + $block[ $i ] = $key[ $i ]; |
|
| 565 | 565 | } |
| 566 | - self::update($ctx, $block, 128); |
|
| 567 | - $ctx[4] = 128; |
|
| 566 | + self::update( $ctx, $block, 128 ); |
|
| 567 | + $ctx[ 4 ] = 128; |
|
| 568 | 568 | } |
| 569 | 569 | |
| 570 | 570 | return $ctx; |
@@ -579,10 +579,10 @@ discard block |
||
| 579 | 579 | * @return SplFixedArray |
| 580 | 580 | * @psalm-suppress MixedArgumentTypeCoercion |
| 581 | 581 | */ |
| 582 | - public static function stringToSplFixedArray($str = '') |
|
| 582 | + public static function stringToSplFixedArray( $str = '' ) |
|
| 583 | 583 | { |
| 584 | - $values = unpack('C*', $str); |
|
| 585 | - return SplFixedArray::fromArray(array_values($values)); |
|
| 584 | + $values = unpack( 'C*', $str ); |
|
| 585 | + return SplFixedArray::fromArray( array_values( $values ) ); |
|
| 586 | 586 | } |
| 587 | 587 | |
| 588 | 588 | /** |
@@ -593,15 +593,15 @@ discard block |
||
| 593 | 593 | * @param SplFixedArray $a |
| 594 | 594 | * @return string |
| 595 | 595 | */ |
| 596 | - public static function SplFixedArrayToString(SplFixedArray $a) |
|
| 596 | + public static function SplFixedArrayToString( SplFixedArray $a ) |
|
| 597 | 597 | { |
| 598 | 598 | /** |
| 599 | 599 | * @var array<int, string|int> |
| 600 | 600 | */ |
| 601 | 601 | $arr = $a->toArray(); |
| 602 | 602 | $c = $a->count(); |
| 603 | - array_unshift($arr, str_repeat('C', $c)); |
|
| 604 | - return (string) (call_user_func_array('pack', $arr)); |
|
| 603 | + array_unshift( $arr, str_repeat( 'C', $c ) ); |
|
| 604 | + return (string)( call_user_func_array( 'pack', $arr ) ); |
|
| 605 | 605 | } |
| 606 | 606 | |
| 607 | 607 | /** |
@@ -615,48 +615,48 @@ discard block |
||
| 615 | 615 | * @psalm-suppress MixedArrayAssignment |
| 616 | 616 | * @psalm-suppress MixedMethodCall |
| 617 | 617 | */ |
| 618 | - public static function contextToString(SplFixedArray $ctx) |
|
| 618 | + public static function contextToString( SplFixedArray $ctx ) |
|
| 619 | 619 | { |
| 620 | 620 | $str = ''; |
| 621 | 621 | /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ |
| 622 | - $ctxA = $ctx[0]->toArray(); |
|
| 622 | + $ctxA = $ctx[ 0 ]->toArray(); |
|
| 623 | 623 | |
| 624 | 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'); |
|
| 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 | 628 | } |
| 629 | 629 | /** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */ |
| 630 | - $ctxAi = $ctxA[$i]; |
|
| 630 | + $ctxAi = $ctxA[ $i ]; |
|
| 631 | 631 | $str .= $ctxAi->toReverseString(); |
| 632 | 632 | } |
| 633 | 633 | |
| 634 | 634 | # uint64_t t[2]; |
| 635 | 635 | # uint64_t f[2]; |
| 636 | - for ($i = 1; $i < 3; ++$i) { |
|
| 636 | + for ( $i = 1; $i < 3; ++$i ) { |
|
| 637 | 637 | /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ |
| 638 | - $ctxA = $ctx[$i]->toArray(); |
|
| 638 | + $ctxA = $ctx[ $i ]->toArray(); |
|
| 639 | 639 | /** @var ParagonIE_Sodium_Core32_Int64 $ctxA1 */ |
| 640 | - $ctxA1 = $ctxA[0]; |
|
| 640 | + $ctxA1 = $ctxA[ 0 ]; |
|
| 641 | 641 | /** @var ParagonIE_Sodium_Core32_Int64 $ctxA2 */ |
| 642 | - $ctxA2 = $ctxA[1]; |
|
| 642 | + $ctxA2 = $ctxA[ 1 ]; |
|
| 643 | 643 | |
| 644 | 644 | $str .= $ctxA1->toReverseString(); |
| 645 | 645 | $str .= $ctxA2->toReverseString(); |
| 646 | 646 | } |
| 647 | 647 | |
| 648 | 648 | # uint8_t buf[2 * 128]; |
| 649 | - $str .= self::SplFixedArrayToString($ctx[3]); |
|
| 649 | + $str .= self::SplFixedArrayToString( $ctx[ 3 ] ); |
|
| 650 | 650 | |
| 651 | 651 | /** @var int $ctx4 */ |
| 652 | - $ctx4 = $ctx[4]; |
|
| 652 | + $ctx4 = $ctx[ 4 ]; |
|
| 653 | 653 | |
| 654 | 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), |
|
| 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 | 660 | "\x00\x00\x00\x00" |
| 661 | 661 | /* |
| 662 | 662 | self::intToChr(($ctx4 >> 32) & 0xff), |
@@ -664,9 +664,9 @@ discard block |
||
| 664 | 664 | self::intToChr(($ctx4 >> 48) & 0xff), |
| 665 | 665 | self::intToChr(($ctx4 >> 56) & 0xff) |
| 666 | 666 | */ |
| 667 | - )); |
|
| 667 | + ) ); |
|
| 668 | 668 | # uint8_t last_node; |
| 669 | - return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); |
|
| 669 | + return $str . self::intToChr( $ctx[ 5 ] ) . str_repeat( "\x00", 23 ); |
|
| 670 | 670 | } |
| 671 | 671 | |
| 672 | 672 | /** |
@@ -682,37 +682,37 @@ discard block |
||
| 682 | 682 | * @psalm-suppress MixedArrayAccess |
| 683 | 683 | * @psalm-suppress MixedArrayAssignment |
| 684 | 684 | */ |
| 685 | - public static function stringToContext($string) |
|
| 685 | + public static function stringToContext( $string ) |
|
| 686 | 686 | { |
| 687 | 687 | $ctx = self::context(); |
| 688 | 688 | |
| 689 | 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) |
|
| 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 | 693 | ); |
| 694 | 694 | } |
| 695 | 695 | |
| 696 | 696 | # uint64_t t[2]; |
| 697 | 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) |
|
| 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 | 701 | ); |
| 702 | - $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 703 | - self::substr($string, 64 + (($i - 1) << 4), 8) |
|
| 702 | + $ctx[ $i ][ 0 ] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
| 703 | + self::substr( $string, 64 + ( ( $i - 1 ) << 4 ), 8 ) |
|
| 704 | 704 | ); |
| 705 | 705 | } |
| 706 | 706 | |
| 707 | 707 | # uint8_t buf[2 * 128]; |
| 708 | - $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
| 708 | + $ctx[ 3 ] = self::stringToSplFixedArray( self::substr( $string, 96, 256 ) ); |
|
| 709 | 709 | |
| 710 | 710 | # uint8_t buf[2 * 128]; |
| 711 | 711 | $int = 0; |
| 712 | - for ($i = 0; $i < 8; ++$i) { |
|
| 713 | - $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
| 712 | + for ( $i = 0; $i < 8; ++$i ) { |
|
| 713 | + $int |= self::chrToInt( $string[ 352 + $i ] ) << ( $i << 3 ); |
|
| 714 | 714 | } |
| 715 | - $ctx[4] = $int; |
|
| 715 | + $ctx[ 4 ] = $int; |
|
| 716 | 716 | |
| 717 | 717 | return $ctx; |
| 718 | 718 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core32_Ed25519', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_Ed25519', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -22,10 +22,10 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | public static function keypair() |
| 24 | 24 | { |
| 25 | - $seed = random_bytes(self::SEED_BYTES); |
|
| 25 | + $seed = random_bytes( self::SEED_BYTES ); |
|
| 26 | 26 | $pk = ''; |
| 27 | 27 | $sk = ''; |
| 28 | - self::seed_keypair($pk, $sk, $seed); |
|
| 28 | + self::seed_keypair( $pk, $sk, $seed ); |
|
| 29 | 29 | return $sk . $pk; |
| 30 | 30 | } |
| 31 | 31 | |
@@ -39,14 +39,14 @@ discard block |
||
| 39 | 39 | * @throws SodiumException |
| 40 | 40 | * @throws TypeError |
| 41 | 41 | */ |
| 42 | - public static function seed_keypair(&$pk, &$sk, $seed) |
|
| 42 | + public static function seed_keypair( &$pk, &$sk, $seed ) |
|
| 43 | 43 | { |
| 44 | - if (self::strlen($seed) !== self::SEED_BYTES) { |
|
| 45 | - throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
|
| 44 | + if ( self::strlen( $seed ) !== self::SEED_BYTES ) { |
|
| 45 | + throw new RangeException( 'crypto_sign keypair seed must be 32 bytes long' ); |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** @var string $pk */ |
| 49 | - $pk = self::publickey_from_secretkey($seed); |
|
| 49 | + $pk = self::publickey_from_secretkey( $seed ); |
|
| 50 | 50 | $sk = $seed . $pk; |
| 51 | 51 | return $sk; |
| 52 | 52 | } |
@@ -58,12 +58,12 @@ discard block |
||
| 58 | 58 | * @return string |
| 59 | 59 | * @throws TypeError |
| 60 | 60 | */ |
| 61 | - public static function secretkey($keypair) |
|
| 61 | + public static function secretkey( $keypair ) |
|
| 62 | 62 | { |
| 63 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
| 64 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
| 63 | + if ( self::strlen( $keypair ) !== self::KEYPAIR_BYTES ) { |
|
| 64 | + throw new RangeException( 'crypto_sign keypair must be 96 bytes long' ); |
|
| 65 | 65 | } |
| 66 | - return self::substr($keypair, 0, 64); |
|
| 66 | + return self::substr( $keypair, 0, 64 ); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | /** |
@@ -74,12 +74,12 @@ discard block |
||
| 74 | 74 | * @throws RangeException |
| 75 | 75 | * @throws TypeError |
| 76 | 76 | */ |
| 77 | - public static function publickey($keypair) |
|
| 77 | + public static function publickey( $keypair ) |
|
| 78 | 78 | { |
| 79 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
| 80 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
| 79 | + if ( self::strlen( $keypair ) !== self::KEYPAIR_BYTES ) { |
|
| 80 | + throw new RangeException( 'crypto_sign keypair must be 96 bytes long' ); |
|
| 81 | 81 | } |
| 82 | - return self::substr($keypair, 64, 32); |
|
| 82 | + return self::substr( $keypair, 64, 32 ); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -90,17 +90,17 @@ discard block |
||
| 90 | 90 | * @throws SodiumException |
| 91 | 91 | * @throws TypeError |
| 92 | 92 | */ |
| 93 | - public static function publickey_from_secretkey($sk) |
|
| 93 | + public static function publickey_from_secretkey( $sk ) |
|
| 94 | 94 | { |
| 95 | 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 |
|
| 96 | + $sk = hash( 'sha512', self::substr( $sk, 0, 32 ), true ); |
|
| 97 | + $sk[ 0 ] = self::intToChr( |
|
| 98 | + self::chrToInt( $sk[ 0 ] ) & 248 |
|
| 99 | 99 | ); |
| 100 | - $sk[31] = self::intToChr( |
|
| 101 | - (self::chrToInt($sk[31]) & 63) | 64 |
|
| 100 | + $sk[ 31 ] = self::intToChr( |
|
| 101 | + ( self::chrToInt( $sk[ 31 ] ) & 63 ) | 64 |
|
| 102 | 102 | ); |
| 103 | - return self::sk_to_pk($sk); |
|
| 103 | + return self::sk_to_pk( $sk ); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /** |
@@ -109,15 +109,15 @@ discard block |
||
| 109 | 109 | * @throws SodiumException |
| 110 | 110 | * @throws TypeError |
| 111 | 111 | */ |
| 112 | - public static function pk_to_curve25519($pk) |
|
| 112 | + public static function pk_to_curve25519( $pk ) |
|
| 113 | 113 | { |
| 114 | - if (self::small_order($pk)) { |
|
| 115 | - throw new SodiumException('Public key is on a small order'); |
|
| 114 | + if ( self::small_order( $pk ) ) { |
|
| 115 | + throw new SodiumException( 'Public key is on a small order' ); |
|
| 116 | 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'); |
|
| 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 | 121 | } |
| 122 | 122 | |
| 123 | 123 | # fe_1(one_minus_y); |
@@ -135,12 +135,12 @@ discard block |
||
| 135 | 135 | # fe_add(x, x, A.Y); |
| 136 | 136 | # fe_mul(x, x, one_minus_y); |
| 137 | 137 | $x = self::fe_mul( |
| 138 | - self::fe_add(self::fe_1(), $A->Y), |
|
| 138 | + self::fe_add( self::fe_1(), $A->Y ), |
|
| 139 | 139 | $one_minux_y |
| 140 | 140 | ); |
| 141 | 141 | |
| 142 | 142 | # fe_tobytes(curve25519_pk, x); |
| 143 | - return self::fe_tobytes($x); |
|
| 143 | + return self::fe_tobytes( $x ); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | /** |
@@ -151,11 +151,11 @@ discard block |
||
| 151 | 151 | * @throws SodiumException |
| 152 | 152 | * @throws TypeError |
| 153 | 153 | */ |
| 154 | - public static function sk_to_pk($sk) |
|
| 154 | + public static function sk_to_pk( $sk ) |
|
| 155 | 155 | { |
| 156 | 156 | return self::ge_p3_tobytes( |
| 157 | 157 | self::ge_scalarmult_base( |
| 158 | - self::substr($sk, 0, 32) |
|
| 158 | + self::substr( $sk, 0, 32 ) |
|
| 159 | 159 | ) |
| 160 | 160 | ); |
| 161 | 161 | } |
@@ -169,10 +169,10 @@ discard block |
||
| 169 | 169 | * @throws SodiumException |
| 170 | 170 | * @throws TypeError |
| 171 | 171 | */ |
| 172 | - public static function sign($message, $sk) |
|
| 172 | + public static function sign( $message, $sk ) |
|
| 173 | 173 | { |
| 174 | 174 | /** @var string $signature */ |
| 175 | - $signature = self::sign_detached($message, $sk); |
|
| 175 | + $signature = self::sign_detached( $message, $sk ); |
|
| 176 | 176 | return $signature . $message; |
| 177 | 177 | } |
| 178 | 178 | |
@@ -185,18 +185,18 @@ discard block |
||
| 185 | 185 | * @throws SodiumException |
| 186 | 186 | * @throws TypeError |
| 187 | 187 | */ |
| 188 | - public static function sign_open($message, $pk) |
|
| 188 | + public static function sign_open( $message, $pk ) |
|
| 189 | 189 | { |
| 190 | 190 | /** @var string $signature */ |
| 191 | - $signature = self::substr($message, 0, 64); |
|
| 191 | + $signature = self::substr( $message, 0, 64 ); |
|
| 192 | 192 | |
| 193 | 193 | /** @var string $message */ |
| 194 | - $message = self::substr($message, 64); |
|
| 194 | + $message = self::substr( $message, 64 ); |
|
| 195 | 195 | |
| 196 | - if (self::verify_detached($signature, $message, $pk)) { |
|
| 196 | + if ( self::verify_detached( $signature, $message, $pk ) ) { |
|
| 197 | 197 | return $message; |
| 198 | 198 | } |
| 199 | - throw new SodiumException('Invalid signature'); |
|
| 199 | + throw new SodiumException( 'Invalid signature' ); |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | /** |
@@ -209,56 +209,56 @@ discard block |
||
| 209 | 209 | * @throws TypeError |
| 210 | 210 | * @psalm-suppress PossiblyInvalidArgument |
| 211 | 211 | */ |
| 212 | - public static function sign_detached($message, $sk) |
|
| 212 | + public static function sign_detached( $message, $sk ) |
|
| 213 | 213 | { |
| 214 | 214 | # crypto_hash_sha512(az, sk, 32); |
| 215 | - $az = hash('sha512', self::substr($sk, 0, 32), true); |
|
| 215 | + $az = hash( 'sha512', self::substr( $sk, 0, 32 ), true ); |
|
| 216 | 216 | |
| 217 | 217 | # az[0] &= 248; |
| 218 | 218 | # az[31] &= 63; |
| 219 | 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); |
|
| 220 | + $az[ 0 ] = self::intToChr( self::chrToInt( $az[ 0 ] ) & 248 ); |
|
| 221 | + $az[ 31 ] = self::intToChr( ( self::chrToInt( $az[ 31 ] ) & 63 ) | 64 ); |
|
| 222 | 222 | |
| 223 | 223 | # crypto_hash_sha512_init(&hs); |
| 224 | 224 | # crypto_hash_sha512_update(&hs, az + 32, 32); |
| 225 | 225 | # crypto_hash_sha512_update(&hs, m, mlen); |
| 226 | 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); |
|
| 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 | 231 | |
| 232 | 232 | # memmove(sig + 32, sk + 32, 32); |
| 233 | - $pk = self::substr($sk, 32, 32); |
|
| 233 | + $pk = self::substr( $sk, 32, 32 ); |
|
| 234 | 234 | |
| 235 | 235 | # sc_reduce(nonce); |
| 236 | 236 | # ge_scalarmult_base(&R, nonce); |
| 237 | 237 | # ge_p3_tobytes(sig, &R); |
| 238 | - $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
| 238 | + $nonce = self::sc_reduce( $nonceHash ) . self::substr( $nonceHash, 32 ); |
|
| 239 | 239 | $sig = self::ge_p3_tobytes( |
| 240 | - self::ge_scalarmult_base($nonce) |
|
| 240 | + self::ge_scalarmult_base( $nonce ) |
|
| 241 | 241 | ); |
| 242 | 242 | |
| 243 | 243 | # crypto_hash_sha512_init(&hs); |
| 244 | 244 | # crypto_hash_sha512_update(&hs, sig, 64); |
| 245 | 245 | # crypto_hash_sha512_update(&hs, m, mlen); |
| 246 | 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); |
|
| 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 | 252 | |
| 253 | 253 | # sc_reduce(hram); |
| 254 | 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); |
|
| 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 | 258 | |
| 259 | 259 | try { |
| 260 | - ParagonIE_Sodium_Compat::memzero($az); |
|
| 261 | - } catch (SodiumException $ex) { |
|
| 260 | + ParagonIE_Sodium_Compat::memzero( $az ); |
|
| 261 | + } catch ( SodiumException $ex ) { |
|
| 262 | 262 | $az = null; |
| 263 | 263 | } |
| 264 | 264 | return $sig; |
@@ -274,26 +274,26 @@ discard block |
||
| 274 | 274 | * @throws SodiumException |
| 275 | 275 | * @throws TypeError |
| 276 | 276 | */ |
| 277 | - public static function verify_detached($sig, $message, $pk) |
|
| 277 | + public static function verify_detached( $sig, $message, $pk ) |
|
| 278 | 278 | { |
| 279 | - if (self::strlen($sig) < 64) { |
|
| 280 | - throw new SodiumException('Signature is too short'); |
|
| 279 | + if ( self::strlen( $sig ) < 64 ) { |
|
| 280 | + throw new SodiumException( 'Signature is too short' ); |
|
| 281 | 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'); |
|
| 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 | 284 | } |
| 285 | - if (self::small_order($sig)) { |
|
| 286 | - throw new SodiumException('Signature is on too small of an order'); |
|
| 285 | + if ( self::small_order( $sig ) ) { |
|
| 286 | + throw new SodiumException( 'Signature is on too small of an order' ); |
|
| 287 | 287 | } |
| 288 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
| 289 | - throw new SodiumException('Invalid signature'); |
|
| 288 | + if ( ( self::chrToInt( $sig[ 63 ] ) & 224 ) !== 0 ) { |
|
| 289 | + throw new SodiumException( 'Invalid signature' ); |
|
| 290 | 290 | } |
| 291 | 291 | $d = 0; |
| 292 | - for ($i = 0; $i < 32; ++$i) { |
|
| 293 | - $d |= self::chrToInt($pk[$i]); |
|
| 292 | + for ( $i = 0; $i < 32; ++$i ) { |
|
| 293 | + $d |= self::chrToInt( $pk[ $i ] ); |
|
| 294 | 294 | } |
| 295 | - if ($d === 0) { |
|
| 296 | - throw new SodiumException('All zero public key'); |
|
| 295 | + if ( $d === 0 ) { |
|
| 296 | + throw new SodiumException( 'All zero public key' ); |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
@@ -303,34 +303,34 @@ discard block |
||
| 303 | 303 | ParagonIE_Sodium_Compat::$fastMult = true; |
| 304 | 304 | |
| 305 | 305 | /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
| 306 | - $A = self::ge_frombytes_negate_vartime($pk); |
|
| 306 | + $A = self::ge_frombytes_negate_vartime( $pk ); |
|
| 307 | 307 | |
| 308 | 308 | /** @var string $hDigest */ |
| 309 | 309 | $hDigest = hash( |
| 310 | 310 | 'sha512', |
| 311 | - self::substr($sig, 0, 32) . |
|
| 312 | - self::substr($pk, 0, 32) . |
|
| 311 | + self::substr( $sig, 0, 32 ) . |
|
| 312 | + self::substr( $pk, 0, 32 ) . |
|
| 313 | 313 | $message, |
| 314 | 314 | true |
| 315 | 315 | ); |
| 316 | 316 | |
| 317 | 317 | /** @var string $h */ |
| 318 | - $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
| 318 | + $h = self::sc_reduce( $hDigest ) . self::substr( $hDigest, 32 ); |
|
| 319 | 319 | |
| 320 | 320 | /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ |
| 321 | 321 | $R = self::ge_double_scalarmult_vartime( |
| 322 | 322 | $h, |
| 323 | 323 | $A, |
| 324 | - self::substr($sig, 32) |
|
| 324 | + self::substr( $sig, 32 ) |
|
| 325 | 325 | ); |
| 326 | 326 | |
| 327 | 327 | /** @var string $rcheck */ |
| 328 | - $rcheck = self::ge_tobytes($R); |
|
| 328 | + $rcheck = self::ge_tobytes( $R ); |
|
| 329 | 329 | |
| 330 | 330 | // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
| 331 | 331 | ParagonIE_Sodium_Compat::$fastMult = $orig; |
| 332 | 332 | |
| 333 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
| 333 | + return self::verify_32( $rcheck, self::substr( $sig, 0, 32 ) ); |
|
| 334 | 334 | } |
| 335 | 335 | |
| 336 | 336 | /** |
@@ -341,10 +341,10 @@ discard block |
||
| 341 | 341 | * @throws SodiumException |
| 342 | 342 | * @throws TypeError |
| 343 | 343 | */ |
| 344 | - public static function check_S_lt_L($S) |
|
| 344 | + public static function check_S_lt_L( $S ) |
|
| 345 | 345 | { |
| 346 | - if (self::strlen($S) < 32) { |
|
| 347 | - throw new SodiumException('Signature must be 32 bytes'); |
|
| 346 | + if ( self::strlen( $S ) < 32 ) { |
|
| 347 | + throw new SodiumException( 'Signature must be 32 bytes' ); |
|
| 348 | 348 | } |
| 349 | 349 | static $L = array( |
| 350 | 350 | 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
@@ -359,14 +359,14 @@ discard block |
||
| 359 | 359 | |
| 360 | 360 | do { |
| 361 | 361 | --$i; |
| 362 | - $x = self::chrToInt($S[$i]); |
|
| 362 | + $x = self::chrToInt( $S[ $i ] ); |
|
| 363 | 363 | $c |= ( |
| 364 | - (($x - $L[$i]) >> 8) & $n |
|
| 364 | + ( ( $x - $L[ $i ] ) >> 8 ) & $n |
|
| 365 | 365 | ); |
| 366 | 366 | $n &= ( |
| 367 | - (($x ^ $L[$i]) - 1) >> 8 |
|
| 367 | + ( ( $x ^ $L[ $i ] ) - 1 ) >> 8 |
|
| 368 | 368 | ); |
| 369 | - } while ($i !== 0); |
|
| 369 | + } while ( $i !== 0 ); |
|
| 370 | 370 | |
| 371 | 371 | return $c === 0; |
| 372 | 372 | } |
@@ -377,7 +377,7 @@ discard block |
||
| 377 | 377 | * @throws SodiumException |
| 378 | 378 | * @throws TypeError |
| 379 | 379 | */ |
| 380 | - public static function small_order($R) |
|
| 380 | + public static function small_order( $R ) |
|
| 381 | 381 | { |
| 382 | 382 | static $blocklist = array( |
| 383 | 383 | /* 0 (order 4) */ |
@@ -466,14 +466,14 @@ discard block |
||
| 466 | 466 | ) |
| 467 | 467 | ); |
| 468 | 468 | /** @var array<int, array<int, int>> $blocklist */ |
| 469 | - $countBlocklist = count($blocklist); |
|
| 469 | + $countBlocklist = count( $blocklist ); |
|
| 470 | 470 | |
| 471 | - for ($i = 0; $i < $countBlocklist; ++$i) { |
|
| 471 | + for ( $i = 0; $i < $countBlocklist; ++$i ) { |
|
| 472 | 472 | $c = 0; |
| 473 | - for ($j = 0; $j < 32; ++$j) { |
|
| 474 | - $c |= self::chrToInt($R[$j]) ^ $blocklist[$i][$j]; |
|
| 473 | + for ( $j = 0; $j < 32; ++$j ) { |
|
| 474 | + $c |= self::chrToInt( $R[ $j ] ) ^ $blocklist[ $i ][ $j ]; |
|
| 475 | 475 | } |
| 476 | - if ($c === 0) { |
|
| 476 | + if ( $c === 0 ) { |
|
| 477 | 477 | return true; |
| 478 | 478 | } |
| 479 | 479 | } |