Skip to content

Commit

Permalink
Add grindKey
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Jul 8, 2024
1 parent db79e0b commit b3739a3
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ class Constants
{
public static function ZERO()
{
return Utils::toBN(0);
return Utils::toBn(0);
}
public static function ONE()
{
return Utils::toBN(1);
return Utils::toBn(1);
}
public static function TWO()
{
return Utils::toBN(2);
return Utils::toBn(2);
}
public static function MASK_250()
{
return Utils::toBN('1809251394333065553493296640760748560207343510400633813116524750123642650623');
return Utils::toBn('1809251394333065553493296640760748560207343510400633813116524750123642650623');
}

public static function MASK_256()
{
return Utils::toBn('115792089237316195423570985008687907853269984665640564039457584007913129639936');
}

const FIELD_PRIME = '800000000000011000000000000000000000000000000000000000000000001';
Expand Down
2 changes: 1 addition & 1 deletion src/Crypto/Curve.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace StarkNet\Crypto;

use Elliptic\EC;
use StarkNet\Constants;
use Elliptic\Curve\PresetCurve;
use StarkNet\Constants;

class Curve
{
Expand Down
3 changes: 2 additions & 1 deletion src/Crypto/FastPedersenHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use StarkNet\Utils;
use StarkNet\Crypto\Curve;

class FastPedersenHash {
class FastPedersenHash
{
public const LOW_PART_BITS = 248;

// 2 ** 248 - 1
Expand Down
50 changes: 50 additions & 0 deletions src/Crypto/Key.php
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;
}
}
3 changes: 2 additions & 1 deletion src/Crypto/PedersenHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use StarkNet\Utils;
use StarkNet\Crypto\Curve;

class PedersenHash {
class PedersenHash
{
/**
* hash
* pedersen hash
Expand Down
25 changes: 25 additions & 0 deletions test/unit/KeyTest.php
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));
}
}

0 comments on commit b3739a3

Please sign in to comment.