Skip to content

Commit

Permalink
Add starknet keccak
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Jul 8, 2024
1 parent ab393fd commit 52d73fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,15 @@ public static function hexToNumber($hexNumber)
}
return intval(self::toBn($hexNumber)->toString());
}

/**
* keccak
*
* @param string $value
* @return string
*/
public static function keccak($value)
{
return self::toBn(self::sha3($value))->bitwise_and(Constants::MASK_250())->toHex();
}
}
14 changes: 14 additions & 0 deletions test/unit/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Test\TestCase;
use phpseclib\Math\BigInteger as BigNumber;
use StarkNet\Utils;
use StarkNet\Constants;

class UtilsTest extends TestCase
{
Expand Down Expand Up @@ -250,4 +251,17 @@ public function testToBn()
$this->expectException(InvalidArgumentException::class);
$bn = Utils::toBn(new stdClass);
}

/**
* testKeccak
*
* @return void
*/
public function testKeccak()
{
$result = Utils::keccak('hello');
$this->assertEquals('8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8', $result);
$bn = Utils::toBn($result);
$this->assertTrue($bn->compare(Constants::MASK_250()) < 0);
}
}

0 comments on commit 52d73fe

Please sign in to comment.