Skip to content

Commit 102d11b

Browse files
committed
Use floats and not ints as otherwise 32bit php can not represent very difficult guesses effectively
1 parent ae26e8a commit 102d11b

14 files changed

+25
-24
lines changed

src/Matchers/BaseMatch.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ public static function binom(int $n, int $k): int
138138
return $res;
139139
}
140140

141-
abstract protected function getRawGuesses(): int;
141+
abstract protected function getRawGuesses(): float;
142142

143-
public function getGuesses(): int
143+
public function getGuesses(): float
144144
{
145145
return max($this->getRawGuesses(), $this->getMinimumGuesses());
146146
}
147147

148-
protected function getMinimumGuesses(): int
148+
protected function getMinimumGuesses(): float
149149
{
150150
if (mb_strlen($this->token) < mb_strlen($this->password)) {
151151
if (mb_strlen($this->token) === 1) {

src/Matchers/Bruteforce.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public function getFeedback(bool $isSoleMatch): array
3939
];
4040
}
4141

42-
public function getRawGuesses(): int
42+
public function getRawGuesses(): float
4343
{
4444
$guesses = pow(self::BRUTEFORCE_CARDINALITY, mb_strlen($this->token));
45-
if ($guesses > PHP_INT_MAX) {
46-
return PHP_INT_MAX;
45+
if ($guesses === INF) {
46+
return PHP_FLOAT_MAX;
4747
}
4848

4949
// small detail: make bruteforce matches at minimum one guess bigger than smallest allowed
@@ -54,6 +54,6 @@ public function getRawGuesses(): int
5454
$minGuesses = Scorer::MIN_SUBMATCH_GUESSES_MULTI_CHAR + 1;
5555
}
5656

57-
return (int)max($guesses, $minGuesses);
57+
return max($guesses, $minGuesses);
5858
}
5959
}

src/Matchers/DateMatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ protected static function removeRedundantMatches(array $matches): array
408408
});
409409
}
410410

411-
protected function getRawGuesses(): int
411+
protected function getRawGuesses(): float
412412
{
413413
// base guesses: (year distance from REFERENCE_YEAR) * num_days * num_years
414414
$yearSpace = max(abs($this->year - static::getReferenceYear()), static::MIN_YEAR_SPACE);

src/Matchers/DictionaryMatch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,15 @@ protected static function getRankedDictionaries(): array
191191
return self::$rankedDictionaries;
192192
}
193193

194-
protected function getRawGuesses(): int
194+
protected function getRawGuesses(): float
195195
{
196196
$guesses = $this->rank;
197197
$guesses *= $this->getUppercaseVariations();
198198

199199
return $guesses;
200200
}
201201

202-
protected function getUppercaseVariations(): int
202+
protected function getUppercaseVariations(): float
203203
{
204204
$word = $this->token;
205205
if (preg_match(self::ALL_LOWER, $word) || mb_strtolower($word) === $word) {

src/Matchers/L33tMatch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ protected static function substitutionTableHelper(array $table, array $keys, arr
202202
return self::substitutionTableHelper($table, $otherKeys, $nextSubs);
203203
}
204204

205-
protected function getRawGuesses(): int
205+
protected function getRawGuesses(): float
206206
{
207207
return parent::getRawGuesses() * $this->getL33tVariations();
208208
}
209209

210-
protected function getL33tVariations(): int
210+
protected function getL33tVariations(): float
211211
{
212212
$variations = 1;
213213

src/Matchers/MatchInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface MatchInterface
1616
*/
1717
public static function match(string $password, array $userInputs = []): array;
1818

19-
public function getGuesses(): int;
19+
public function getGuesses(): float;
2020

2121
public function getGuessesLog10(): float;
2222
}

src/Matchers/RepeatMatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function __construct(string $password, int $begin, int $end, string $toke
114114
}
115115
}
116116

117-
protected function getRawGuesses(): int
117+
protected function getRawGuesses(): float
118118
{
119119
return $this->baseGuesses * $this->repeatCount;
120120
}

src/Matchers/ReverseDictionaryMatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static function match(string $password, array $userInputs = [], array $ra
3434
return $matches;
3535
}
3636

37-
protected function getRawGuesses(): int
37+
protected function getRawGuesses(): float
3838
{
3939
return parent::getRawGuesses() * 2;
4040
}

src/Matchers/SequenceMatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function __construct(string $password, int $begin, int $end, string $toke
110110
}
111111
}
112112

113-
protected function getRawGuesses(): int
113+
protected function getRawGuesses(): float
114114
{
115115
$firstCharacter = mb_substr($this->token, 0, 1);
116116
$guesses = 0;

src/Matchers/SpatialMatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public static function getAdjacencyGraphs(): array
214214
return self::$adjacencyGraphs;
215215
}
216216

217-
protected function getRawGuesses(): int
217+
protected function getRawGuesses(): float
218218
{
219219
if ($this->graph === 'qwerty' || $this->graph === 'dvorak') {
220220
$startingPosition = self::KEYBOARD_STARTING_POSITION;

0 commit comments

Comments
 (0)