Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ed25519 #5

Merged
merged 3 commits into from
Nov 26, 2024
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "lib/core"]
path = lib/core
url = https://github.com/symbioticfi/core
[submodule "lib/crypto-lib"]
path = lib/crypto-lib
url = https://github.com/get-smooth/crypto-lib
1 change: 1 addition & 0 deletions lib/crypto-lib
Submodule crypto-lib added at f2c00e
3 changes: 2 additions & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/
@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/
@symbiotic/=lib/core/src/
@symbiotic-test=lib/core/test
@symbiotic-test=lib/core/test
@crypto-lib/=lib/crypto-lib/src/
1,611 changes: 51 additions & 1,560 deletions src/libraries/Ed25519.sol

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ abstract contract SelfRegisterOperators is BaseMiddleware, BaseSig, EIP712Upgrad
* @param key The public key to verify
* @param signature The signature to verify
*/
function _verifyKey(address operator, bytes memory key, bytes memory signature) internal view {
function _verifyKey(address operator, bytes memory key, bytes memory signature) internal {
if (key.length != 0 && !_verifyKeySignature(operator, key, signature)) {
revert InvalidSignature();
}
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/extensions/sigs/BaseSig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ abstract contract BaseSig {
address operator,
bytes memory key_,
bytes memory signature
) internal view virtual returns (bool);
) internal virtual returns (bool);
}
32 changes: 13 additions & 19 deletions src/middleware/extensions/sigs/Ed25519Sig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,31 @@ abstract contract Ed25519Sig is BaseSig {
/**
* @notice Verifies that a signature was created by the owner of a key
* @param operator The address of the operator that owns the key
* @param key_ The public key to verify against, encoded as bytes
* @param signature The Ed25519 signature to verify, containing r and s components
* @param key_ The public key to verify against
* @param signature The Ed25519 signature to verify
* @return True if the signature was created by the key owner, false otherwise
* @dev The key is expected to be a bytes32 that represents an Ed25519 public key
* The signature is expected to be 64 bytes containing r (32 bytes) and s (32 bytes)
* @dev The key must be a valid Ed25519 public key point compressed to 32 bytes
* The signature must be 64 bytes containing r and s components encoded as uint256
*/
function _verifyKeySignature(
address operator,
bytes memory key_,
bytes memory signature
) internal pure override returns (bool) {
) internal override returns (bool) {
bytes32 key = abi.decode(key_, (bytes32));
bytes32 message = keccak256(abi.encodePacked(operator, key));
return check(key, signature, message);
bytes memory message = abi.encode(keccak256(abi.encodePacked(operator, key)));
return verify(message, signature, key);
}

/**
* @notice Checks an Ed25519 signature against a message and public key
* @param key The Ed25519 public key
* @param signature The Ed25519 signature to verify
* @notice Verifies an Ed25519 signature against a message and public key
* @param message The message that was signed
* @param signature The Ed25519 signature to verify
* @param key The Ed25519 public key compressed to 32 bytes
* @return True if the signature is valid, false otherwise
* @dev Wrapper around Ed25519.check
* @dev Wrapper around Ed25519.verify which handles decompression and curve operations
*/
function check(bytes32 key, bytes memory signature, bytes32 message) internal pure returns (bool) {
bytes32 r;
bytes32 s;
assembly {
r := mload(add(signature, 32))
s := mload(add(signature, 64))
}
return Ed25519.check(key, r, s, message, bytes9(0));
function verify(bytes memory message, bytes memory signature, bytes32 key) internal returns (bool) {
return Ed25519.verify(message, signature, key);
}
}
10 changes: 5 additions & 5 deletions test/helpers/ed25519TestData.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"operator": "0xc2c1697Fe88772f844D1b622F4fc0E6E0b16Cb77",
"key": "0x121516a0d84c94a28d9f9cd2263d3fe8eceee3cef43aa043e388cce914f85205",
"signature": "0x5d590ecf3f7019846aba3c363ddb8f640109f9fa35a5d8ce027407e8d4507045b651075cfbecbb86ede90a4f20ac99fa969419c81ee4d7066868bfcd5e47d50d",
"invalidKey": "0x98c6edf296f06a1b23e56d56020c0ea8289d07a8b0a23e953b24239697cf5a96",
"invalidSignature": "0x8f3d84ae06d3d912328d6c5c97622d880c2de9255d50c697314fc5f61ce0d211b8603ac2c3287ed607364d8cd1e89675a1f3e0892c11ddc7116cf92221b63600"
"operator": "0xFFa6DD45436695c0185c9E1721638bc951b6853d",
"key": "0xf5240b978fc69dc2fdc62775572794e8cd0fd8ac4c0510336fb07232e8086692",
"signature": "0x78dd2320878fc2fb9f818d56442926eab183f7b94acfb5eaf5d7a24a018f4c200582b0b0cfc195991e1b827187abd35eb099dda6cd6f3458eb53d086db82c808",
"invalidKey": "0xc3e65061bd3c7857f68ab167faa3da83d964d2ec9d2447d367a7cdb4a3256d24",
"invalidSignature": "0x4b829ff953a071273aca67046a71a7f2c0d74ab8dcb3636cfeaf6c4a8f3c9ba5a6de5754c5189fc0e38e3df225657043fbfc0786f4b676ffab319bd92af20307"
}
11 changes: 0 additions & 11 deletions test/helpers/ed25519TestGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ function generateTestData(operatorAddress) {
])
);

// Add 9 bytes of zeros to the message
message = Buffer.concat([
message,
Buffer.alloc(9)
]);

// Sign the message
const signature = "0x" + bytesToHex(ed25519.sign(message, privateKey));

Expand Down Expand Up @@ -71,11 +65,6 @@ function generateInvalidTestData(operatorAddress) {
])
);

message = Buffer.concat([
message,
Buffer.alloc(9)
]);

// Sign with privateKey2 (mismatch)
const signature = "0x" + bytesToHex(ed25519.sign(message, privateKey2));

Expand Down
Loading