Skip to content

Commit

Permalink
Add FastPedersenHash.php
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Jul 5, 2024
1 parent 3cc1117 commit 6ba412a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Crypto/FastPedersenHash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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 StarkNet\Constants;
use StarkNet\Utils;
use StarkNet\Crypto\Curve;

class FastPedersenHash {
public const LOW_PART_BITS = 248;

// 2 ** 248 - 1
public static function LOW_BITS_MASK()
{
return Utils::toBn('452312848583266388373324160190187140051835877600158453279131187530910662655');
}

public static function processSingleElement($element, $p1, $p2) {
$cmpZero = $element->compare(Constants::ZERO());
assert($cmpZero >= 0 && $element->compare(Utils::toBN('0x' . Constants::FIELD_PRIME)) < 0, "Element value is out of range");
$highNibble = $element->bitwise_rightShift(self::LOW_PART_BITS)->toHex();
$lowPart = $element->bitwise_and(self::LOW_BITS_MASK())->toHex();
if ($highNibble === '') {
$highNibble = '0';
}
if ($lowPart === '') {
$lowPart = '0';
}
return $p1->mul($lowPart)->add($p2->mul($highNibble));
}

public static function hash($x, $y) {
$xBn = Utils::toBn($x);
$yBn = Utils::toBn($y);
$points = Curve::constantPoints();
$hashShiftPoint = $points[0];
$p0 = $points[2];
$p1 = $points[2 + self::LOW_PART_BITS];
$p2 = $points[2 + Constants::N_ELEMENT_BITS_HASH];
$p3 = $points[2 + self::LOW_PART_BITS + Constants::N_ELEMENT_BITS_HASH];
return ($hashShiftPoint->add(self::processSingleElement($xBn, $p0, $p1))->add(self::processSingleElement($yBn, $p2, $p3)))->getX();
}
}
18 changes: 18 additions & 0 deletions test/unit/PedersenHashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
use Test\TestCase;
use phpseclib\Math\BigInteger as BigNumber;
use StarkNet\Crypto\PedersenHash;
use StarkNet\Crypto\FastPedersenHash;

/**
* TODO: more test for pedersen hash
*/
class PedersenHashTest extends TestCase
{
/**
Expand All @@ -23,4 +27,18 @@ public function testHash()
$result = PedersenHash::hash(['2954020266725389012079514584454222423700048665778967545305932093381394777423', '215307247182100370520050591091822763712463273430149262739280891880522753123']);
$this->assertEquals('02e77dfc2f710d7b4d70028905487511fb49576ee767575225a36db365250475', $result);
}

/**
* testHashFast
*
* @return void
*/
public function testHashFast()
{
$result = FastPedersenHash::hash(0, '1859938899453001548362772938057778066833094073841168374380996652312065025102');
$this->assertEquals('0687ea8d6d09d2106b3f9d69796bf12c54706f0cdc43b63ee73d4f9bc74b454f', $result->toString(16));

$result = FastPedersenHash::hash('2954020266725389012079514584454222423700048665778967545305932093381394777423', '215307247182100370520050591091822763712463273430149262739280891880522753123');
$this->assertEquals('02e77dfc2f710d7b4d70028905487511fb49576ee767575225a36db365250475', $result->toString(16));
}
}

0 comments on commit 6ba412a

Please sign in to comment.