Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: PHP

on: ["push", "pull_request"]

jobs:
build_and_test:
name: Build and test starknet with ${{ matrix.php-version }}
strategy:
matrix:
php-version: ["8.4"]

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@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: vendor/bin/phpunit
2 changes: 1 addition & 1 deletion src/Crypto/FastPedersenHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/Crypto/PedersenHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ private static function pedersenHashAsPoint($elements)
$x = $x->bitwise_rightShift(1);
}
}
return $point->getX();
return Utils::toBN($point->getX());
}
}
6 changes: 3 additions & 3 deletions src/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace StarkNet;

use BN\BN;
use StarkNet\Utils;
use StarkNet\Crypto\FastPedersenHash;

Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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());
}
}
7 changes: 3 additions & 4 deletions src/TypedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace StarkNet;

use BN\BN;
use StarkNet\Utils;
use StarkNet\Crypto\FastPedersenHash;
use StarkNet\Cairo\Felt;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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());
}
}
20 changes: 10 additions & 10 deletions test/unit/PedersenHashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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());
}
}
Loading