Skip to content

Commit

Permalink
tests: ed25519 tests and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alrxy committed Nov 16, 2024
1 parent 8ac4d11 commit 4ecc123
Show file tree
Hide file tree
Showing 20 changed files with 780 additions and 80 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ out/
docs/

# Dotenv file
.env
.env

node_modules/
241 changes: 241 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "symbiotic-tests",
"version": "1.0.0",
"description": "Test helpers for Symbiotic protocol",
"private": true,
"scripts": {
"testgen": "node test/helpers/ed25519TestGenerator.js"
},
"dependencies": {
"@noble/curves": "^1.3.0",
"ethereum-cryptography": "^2.1.3",
"ethers": "^6.11.1"
},
"engines": {
"node": ">=18.0.0"
}
}
15 changes: 10 additions & 5 deletions src/examples/simple-pos-network/SimplePosMiddleware.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ contract SimplePosMiddleware is SharedVaults, Operators, KeyStorage256 {
* @param epoch The epoch number.
* @return The start timestamp.
*/
function getEpochStart(uint48 epoch) public view returns (uint48) {
function getEpochStart(
uint48 epoch
) public view returns (uint48) {
return START_TIMESTAMP + epoch * EPOCH_DURATION;
}

Expand Down Expand Up @@ -142,10 +144,13 @@ contract SimplePosMiddleware is SharedVaults, Operators, KeyStorage256 {
* @param stakeHints Hints for determining stakes.
* @param slashHints Hints for the slashing process.
*/
function slash(uint48 epoch, bytes32 key, uint256 amount, bytes[][] memory stakeHints, bytes[] memory slashHints)
public
onlyOwner
{
function slash(
uint48 epoch,
bytes32 key,
uint256 amount,
bytes[][] memory stakeHints,
bytes[] memory slashHints
) public onlyOwner {
SlashParams memory params;
params.epochStart = getEpochStart(epoch);
params.operator = operatorByKey(abi.encode(key));
Expand Down
14 changes: 11 additions & 3 deletions src/key-storage/KeyStorage256.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ abstract contract KeyStorage256 is BaseMiddleware {
* @param key The key for which to find the associated operator
* @return The address of the operator linked to the specified key
*/
function operatorByKey(bytes memory key) public view override returns (address) {
function operatorByKey(
bytes memory key
) public view override returns (address) {
return keyToOperator[abi.decode(key, (bytes32))];
}

Expand All @@ -34,8 +36,14 @@ abstract contract KeyStorage256 is BaseMiddleware {
* @param operator The address of the operator
* @return The key associated with the specified operator
*/
function operatorKey(address operator) public view override returns (bytes memory) {
return abi.encode(keys[operator].getActive(getCaptureTimestamp())[0]);
function operatorKey(
address operator
) public view override returns (bytes memory) {
bytes32[] memory active = keys[operator].getActive(getCaptureTimestamp());
if (active.length == 0) {
return abi.encode(ZERO_BYTES32);
}
return abi.encode(active[0]);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/key-storage/KeyStorageBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ abstract contract KeyStorageBytes is BaseManager {
* @param key The BLS key for which to find the associated operator
* @return The address of the operator linked to the specified BLS key
*/
function operatorByKey(bytes memory key) public view returns (address) {
function operatorByKey(
bytes memory key
) public view returns (address) {
return _keyData[key].getAddress();
}

Expand All @@ -32,7 +34,9 @@ abstract contract KeyStorageBytes is BaseManager {
* @param operator The address of the operator
* @return The BLS key associated with the specified operator
*/
function operatorKey(address operator) public view returns (bytes memory) {
function operatorKey(
address operator
) public view returns (bytes memory) {
if (keyUpdateTimestamp[operator] == getCaptureTimestamp()) {
return prevKeys[operator];
}
Expand Down
4 changes: 3 additions & 1 deletion src/libraries/Ed25519.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ pragma solidity ^0.8;

library Ed25519 {
// Computes (v^(2^250-1), v^11) mod p
function pow22501(uint256 v) private pure returns (uint256 p22501, uint256 p11) {
function pow22501(
uint256 v
) private pure returns (uint256 p22501, uint256 p11) {
p11 = mulmod(v, v, 0x7fffffff_ffffffff_ffffffff_ffffffff_ffffffff_ffffffff_ffffffff_ffffffed);
p22501 = mulmod(p11, p11, 0x7fffffff_ffffffff_ffffffff_ffffffff_ffffffff_ffffffff_ffffffff_ffffffed);
p22501 = mulmod(
Expand Down
Loading

0 comments on commit 4ecc123

Please sign in to comment.