From 46e12aedd9b88dd086bc4963195e6f917afcd6d5 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 29 May 2026 17:17:14 +0800 Subject: [PATCH 1/4] fix: return BigNumber in pedersen hash --- src/Crypto/FastPedersenHash.php | 2 +- src/Crypto/PedersenHash.php | 2 +- src/Hash.php | 6 +++--- src/TypedData.php | 7 +++---- test/unit/PedersenHashTest.php | 20 ++++++++++---------- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Crypto/FastPedersenHash.php b/src/Crypto/FastPedersenHash.php index 28102bc..3a4201a 100644 --- a/src/Crypto/FastPedersenHash.php +++ b/src/Crypto/FastPedersenHash.php @@ -67,6 +67,6 @@ public static function hash($x, $y) $p1 = $points[2]; $p2 = $points[3]; $p3 = $points[4]; - return ($hashShiftPoint->add(self::processSingleElement($xBn, $p0, $p1))->add(self::processSingleElement($yBn, $p2, $p3)))->getX(); + return Utils::toBN(($hashShiftPoint->add(self::processSingleElement($xBn, $p0, $p1))->add(self::processSingleElement($yBn, $p2, $p3)))->getX()); } } \ No newline at end of file diff --git a/src/Crypto/PedersenHash.php b/src/Crypto/PedersenHash.php index 80f1c2a..e9ea02e 100644 --- a/src/Crypto/PedersenHash.php +++ b/src/Crypto/PedersenHash.php @@ -52,6 +52,6 @@ private static function pedersenHashAsPoint($elements) $x = $x->bitwise_rightShift(1); } } - return $point->getX(); + return Utils::toBN($point->getX()); } } \ No newline at end of file diff --git a/src/Hash.php b/src/Hash.php index 24178cc..e0be88c 100644 --- a/src/Hash.php +++ b/src/Hash.php @@ -10,7 +10,6 @@ namespace StarkNet; -use BN\BN; use StarkNet\Utils; use StarkNet\Crypto\FastPedersenHash; @@ -21,7 +20,8 @@ class Hash public static function L2_ADDRESS_UPPER_BOUND() { // 2**251 - 256 - return new BN('3618502788666131106986593281521497120414687020801267626233049500247285300992'); + // return new BN('3618502788666131106986593281521497120414687020801267626233049500247285300992'); + return Utils::toBN('3618502788666131106986593281521497120414687020801267626233049500247285300992'); } /** @@ -54,6 +54,6 @@ public static function computeAddress($classHash, $constructorData, $salt, $depl $classHash, $constructorDataHash ]); - return '0x' . Utils::removeLeadingZero($rawAddress->mod(self::L2_ADDRESS_UPPER_BOUND())->toString(16)); + return '0x' . Utils::removeLeadingZero($rawAddress->divide(self::L2_ADDRESS_UPPER_BOUND())[1]->toHex()); } } \ No newline at end of file diff --git a/src/TypedData.php b/src/TypedData.php index 5d9d5e2..ec8968a 100644 --- a/src/TypedData.php +++ b/src/TypedData.php @@ -10,7 +10,6 @@ namespace StarkNet; -use BN\BN; use StarkNet\Utils; use StarkNet\Crypto\FastPedersenHash; use StarkNet\Cairo\Felt; @@ -100,7 +99,7 @@ protected static function encodeField($types, $type, $value) foreach ($value as $data) { $result[] = self::hashStruct($type, $types, $data); } - return '0x' . Utils::removeLeadingZero(FastPedersenHash::computeHashOnElements($result)->toString(16)); + return '0x' . Utils::removeLeadingZero(FastPedersenHash::computeHashOnElements($result)->toHex()); } } if (array_key_exists($type, $types)) { @@ -141,7 +140,7 @@ public static function hashStruct($typeName, $messageTypes, $message) { return '0x' . Utils::removeLeadingZero(FastPedersenHash::computeHashOnElements(array_merge([ self::hashType($typeName, $messageTypes) - ], self::encodeData($typeName, $messageTypes, $message)))->toString(16)); + ], self::encodeData($typeName, $messageTypes, $message)))->toHex()); } /** @@ -201,6 +200,6 @@ public static function messageHash($domain, $messageTypes, $messageData, $addres $address, self::hashStruct($primaryType, $messageTypes, $messageData) ]; - return '0x' . Utils::removeLeadingZero(FastPedersenHash::computeHashOnElements($message)->toString(16)); + return '0x' . Utils::removeLeadingZero(FastPedersenHash::computeHashOnElements($message)->toHex()); } } \ No newline at end of file diff --git a/test/unit/PedersenHashTest.php b/test/unit/PedersenHashTest.php index c20e37c..a5aac8c 100644 --- a/test/unit/PedersenHashTest.php +++ b/test/unit/PedersenHashTest.php @@ -44,22 +44,22 @@ public function testHash() 0, '1859938899453001548362772938057778066833094073841168374380996652312065025102' ); - $this->assertEquals('0687ea8d6d09d2106b3f9d69796bf12c54706f0cdc43b63ee73d4f9bc74b454f', $result->toString(16)); + $this->assertEquals('0687ea8d6d09d2106b3f9d69796bf12c54706f0cdc43b63ee73d4f9bc74b454f', $result->toHex()); $result = PedersenHash::hash( '2954020266725389012079514584454222423700048665778967545305932093381394777423', '215307247182100370520050591091822763712463273430149262739280891880522753123' ); - $this->assertEquals('02e77dfc2f710d7b4d70028905487511fb49576ee767575225a36db365250475', $result->toString(16)); + $this->assertEquals('02e77dfc2f710d7b4d70028905487511fb49576ee767575225a36db365250475', $result->toHex()); // $result = PedersenHash::computeHashOnElements($this->fixture); - // $this->assertEquals('22064462ea33a6ce5272a295e0f551c5da3834f80d8444e7a4df68190b1bc42', $result->toString(16)); + // $this->assertEquals('22064462ea33a6ce5272a295e0f551c5da3834f80d8444e7a4df68190b1bc42', $result->toHex()); $result = PedersenHash::computeHashOnElements([]); - $this->assertEquals('049ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804', $result->toString(16)); + $this->assertEquals('049ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804', $result->toHex()); $result = PedersenHash::computeHashOnElements([1]); - $this->assertEquals('078d74f61aeaa8286418fd34b3a12a610445eba11d00ecc82ecac2542d55f7a4', $result->toString(16)); + $this->assertEquals('078d74f61aeaa8286418fd34b3a12a610445eba11d00ecc82ecac2542d55f7a4', $result->toHex()); } /** @@ -73,21 +73,21 @@ public function testHashFast() '3d937c035c878245caf64531a5756109c53068da139362728feb561405371cb', '0x208a0a10250e382e1e4bbe2880906c2791bf6275695e02fbbc6aeff9cd8b31a' ); - $this->assertEquals('030e480bed5fe53fa909cc0f8c4d99b8f9f2c016be4c41e13a4848797979c662', $result->toString(16)); + $this->assertEquals('030e480bed5fe53fa909cc0f8c4d99b8f9f2c016be4c41e13a4848797979c662', $result->toHex()); $result = FastPedersenHash::hash( '0x58f580910a6ca59b28927c08fe6c43e2e303ca384badc365795fc645d479d45', '0x78734f65a067be9bdb39de18434d71e79f7b6466a4b66bbd979ab9e7515fe0b' ); - $this->assertEquals('068cc0b76cddd1dd4ed2301ada9b7c872b23875d5ff837b3a87993e0d9996b87', $result->toString(16)); + $this->assertEquals('068cc0b76cddd1dd4ed2301ada9b7c872b23875d5ff837b3a87993e0d9996b87', $result->toHex()); // $result = FastPedersenHash::computeHashOnElements($this->fixture); - // $this->assertEquals('22064462ea33a6ce5272a295e0f551c5da3834f80d8444e7a4df68190b1bc42', $result->toString(16)); + // $this->assertEquals('22064462ea33a6ce5272a295e0f551c5da3834f80d8444e7a4df68190b1bc42', $result->toHex()); $result = FastPedersenHash::computeHashOnElements([]); - $this->assertEquals('049ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804', $result->toString(16)); + $this->assertEquals('049ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804', $result->toHex()); $result = FastPedersenHash::computeHashOnElements([1]); - $this->assertEquals('078d74f61aeaa8286418fd34b3a12a610445eba11d00ecc82ecac2542d55f7a4', $result->toString(16)); + $this->assertEquals('078d74f61aeaa8286418fd34b3a12a610445eba11d00ecc82ecac2542d55f7a4', $result->toHex()); } } \ No newline at end of file From 46f72d17b9bd911a01c71d592aecba7f5070f403 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 29 May 2026 17:20:20 +0800 Subject: [PATCH 2/4] Add ci --- .github/workflows/php.yml | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 .github/workflows/php.yml diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..c250364 --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,43 @@ +name: PHP + +on: ["push", "pull_request"] + +jobs: + build_and_test: + name: Build and test starknet with ${{ matrix.php-version }} + strategy: + matrix: + php-version: ["7.3", "7.4", "8.0"] + + runs-on: ubuntu-latest + + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + + - name: PHP version + run: | + php --version + + - uses: actions/checkout@v2 + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v2 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + if: steps.composer-cache.outputs.cache-hit != 'true' + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Run test suite + run: vendor/bin/phpunit From f44783b5e5d4b59cf50a099ad549c0fa72fe6692 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 29 May 2026 17:23:19 +0800 Subject: [PATCH 3/4] Update ci --- .github/workflows/php.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index c250364..99d352b 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -28,7 +28,7 @@ jobs: - name: Cache Composer packages id: composer-cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: vendor key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} @@ -36,8 +36,7 @@ jobs: ${{ runner.os }}-php- - name: Install dependencies - if: steps.composer-cache.outputs.cache-hit != 'true' - run: composer install --prefer-dist --no-progress --no-suggest + run: composer install --prefer-dist --no-progress - name: Run test suite run: vendor/bin/phpunit From f32cda4b64400157172cfdeb94f5ab3f0cd99e2f Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 29 May 2026 17:24:53 +0800 Subject: [PATCH 4/4] Update CI --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 99d352b..2b8c13e 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -7,7 +7,7 @@ jobs: name: Build and test starknet with ${{ matrix.php-version }} strategy: matrix: - php-version: ["7.3", "7.4", "8.0"] + php-version: ["8.4"] runs-on: ubuntu-latest