-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* This file is part of starknet.php package. | ||
* | ||
* (c) Kuan-Cheng,Lai <[email protected]> | ||
* | ||
* @author Peter Lai <[email protected]> | ||
* @license MIT | ||
*/ | ||
|
||
namespace StarkNet\Crypto; | ||
|
||
use Exception; | ||
use StarkNet\Constants; | ||
use StarkNet\Utils; | ||
use BN\BN; | ||
|
||
class Key { | ||
/** | ||
* grindKey | ||
* Given a cryptographically-secure seed and a limit, deterministically generates a pseudorandom | ||
* key in the range [0, limit). | ||
* This is a reference implementation, and cryptographic security is not guaranteed (for example, | ||
* it may be vulnerable to side-channel attacks); this function is not recommended for use with key | ||
* generation on mainnet. | ||
* | ||
* @param BigNumber $keySeed | ||
* @return BigNumber | ||
*/ | ||
public static function grindKey ($keySeed) | ||
{ | ||
$keySeed = Utils::toBn($keySeed); | ||
$ecOrder = Utils::toBn(Constants::EC_ORDER); | ||
$maskDivOrder = Constants::MASK_256()->divide($ecOrder); | ||
$maxAllowedValue = Constants::MASK_256()->subtract($maskDivOrder[1]); | ||
for ($i=0; ; $i++) { | ||
$msg = str_pad($keySeed->toBytes() . Utils::toBn($i)->toBytes(), 33, "\0"); | ||
$key = Utils::toBn(\hash('sha256', $msg, false)); | ||
if ($key->compare($maxAllowedValue) < 0) { | ||
$result = $key->divide($ecOrder); | ||
// normalize the bignumber | ||
return new BN($result[1]->toString()); | ||
} | ||
if ($i === 100000) { | ||
throw new Exception('grindKey is broken: tried 100k vals'); | ||
} | ||
} | ||
return $maxAllowedValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Test\Unit; | ||
|
||
use InvalidArgumentException; | ||
use stdClass; | ||
use Test\TestCase; | ||
use phpseclib\Math\BigInteger as BigNumber; | ||
use StarkNet\Crypto\Key; | ||
use StarkNet\Constants; | ||
|
||
class KeyTest extends TestCase | ||
{ | ||
/** | ||
* testGrindKey | ||
* | ||
* @return void | ||
*/ | ||
public function testGrindKey() | ||
{ | ||
|
||
$result = Key::grindKey('86F3E7293141F20A8BAFF320E8EE4ACCB9D4A4BF2B4D295E8CEE784DB46E0519'); | ||
$this->assertEquals('5c8c8683596c732541a59e03007b2d30dbbbb873556fe65b5fb63c16688f941', substr($result->toString(16), 1, 64)); | ||
} | ||
} |