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

feat: strip down light sync #30

Merged
merged 5 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
99 changes: 53 additions & 46 deletions packages/smart-vaults/src/library/MultiSignerSignatureLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ import { MultiSignerLib } from "./MultiSignerLib.sol";
* @author Splits
*/
library MultiSignerSignatureLib {
/* -------------------------------------------------------------------------- */
/* ERRORS */
/* -------------------------------------------------------------------------- */

/**
* @notice Thrown when trying to access an empty signer.
* @param index Index of the empty signer.
*/
error SignerNotPresent(uint8 index);

/* -------------------------------------------------------------------------- */
/* CONSTANTS */
/* -------------------------------------------------------------------------- */

// Size in bytes allocated for each signer in the data structure.
uint256 public constant SIGNER_SIZE = 65;

// Signer type indicating an empty slot in the signer set.
uint8 public constant EMPTY_SIGNER_TYPE = 0;
uint256 public constant SIGNER_SIZE = 66;

// Signer type representing an externally owned account (EOA) signer.
uint8 public constant EOA_SIGNER_TYPE = 1;

// Signer type representing a passkey-based signer.
uint8 public constant PASSKEY_SIGNER_TYPE = 2;

// Signer type indicating a removed signer in the signer set.
uint8 public constant REMOVED_SIGNER_TYPE = 3;

/* -------------------------------------------------------------------------- */
/* STRUCTS */
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -87,16 +91,13 @@ library MultiSignerSignatureLib {
/**
* @notice validates if `hash_` was signed by the signer set present in `$` and 'signers_`.
* @param $_ Storage reference to MultiSigner storage.
* @param signers_ list of 256 possible signers. Has higher preference over signers present in storage at a given
* index.
* @param threshold_ signer set threshold used for verification.
* @param signerUpdates_ list of signer additions made to the signer set.
* @param hash_ blob of data that needs to be verified.
* @param signature_ abi.encode(SignatureWrapper[]).
*/
function isValidSignature(
MultiSignerLib.MultiSignerStorage storage $_,
bytes memory signers_,
uint8 threshold_,
bytes memory signerUpdates_,
bytes32 hash_,
bytes memory signature_
)
Expand All @@ -108,21 +109,19 @@ library MultiSignerSignatureLib {

SignatureWrapper[] memory signatures = abi.decode(signature_, (SignatureWrapper[]));

uint8 threshold = $_.threshold;
uint256 alreadySigned;
uint256 mask;
uint8 signerIndex;
bytes memory signer;
uint8 signerType;
for (uint256 i; i < threshold_; i++) {
for (uint256 i; i < threshold; i++) {
signerIndex = signatures[i].signerIndex;
mask = (1 << signerIndex);

(signer, signerType) = getSignerAtIndex(signers_, signatures[i].signerIndex);
signer = $_.signers[signerIndex];

if (signerType == EMPTY_SIGNER_TYPE) {
signer = $_.signers[signerIndex];
} else if (signerType == REMOVED_SIGNER_TYPE) {
isValid = false;
if (signer.length == 0) {
signer = getSignerAtIndex(signerUpdates_, signatures[i].signerIndex);
}

if (
Expand All @@ -135,37 +134,45 @@ library MultiSignerSignatureLib {
}
}

function getSignerAtIndex(bytes memory signers_, uint8 index_) internal pure returns (bytes memory, uint8) {
uint256 start = uint256(index_) * SIGNER_SIZE;

uint8 signerType;
assembly {
signerType := byte(0, mload(add(signers_, add(32, start))))
}

uint256 returnLength;
if (signerType == EMPTY_SIGNER_TYPE || signerType == REMOVED_SIGNER_TYPE) {
return (new bytes(0), signerType);
} else if (signerType == EOA_SIGNER_TYPE) {
returnLength = MultiSignerLib.EOA_SIGNER_SIZE;
} else if (signerType == PASSKEY_SIGNER_TYPE) {
returnLength = MultiSignerLib.PASSKEY_SIGNER_SIZE;
}

bytes memory signer = new bytes(returnLength);
function getSignerAtIndex(bytes memory signerUpdates_, uint8 index_) internal pure returns (bytes memory) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this fn can be cleaned up a bit but dont want to hold up submitting this review for it

uint256 numUpdates = signerUpdates_.length;

assembly {
let dataPtr := add(add(signers_, 33), start)
let itemPtr := add(signer, 32)
uint8 currentIndex;
for (uint256 i = 0; i < numUpdates; i += SIGNER_SIZE) {
assembly {
currentIndex := byte(0, mload(add(signerUpdates_, add(32, i))))
}

switch returnLength
case 32 { mstore(itemPtr, mload(dataPtr)) }
case 64 {
mstore(itemPtr, mload(dataPtr))
mstore(add(itemPtr, 32), mload(add(dataPtr, 32)))
if (currentIndex == index_) {
uint8 signerType;
uint256 returnLength;

assembly {
signerType := byte(0, mload(add(add(signerUpdates_, add(32, i)), 1)))
}

if (signerType == EOA_SIGNER_TYPE) {
returnLength = MultiSignerLib.EOA_SIGNER_SIZE;
} else if (signerType == PASSKEY_SIGNER_TYPE) {
returnLength = MultiSignerLib.PASSKEY_SIGNER_SIZE;
}

bytes memory signer = new bytes(returnLength);
assembly {
let dataPtr := add(add(signerUpdates_, i), 34) // Skip index and signer type bytes
let destPtr := add(signer, 32)

switch returnLength
case 32 { mstore(destPtr, mload(dataPtr)) }
case 64 {
mstore(destPtr, mload(dataPtr))
mstore(add(destPtr, 32), mload(add(dataPtr, 32)))
}
}
return signer;
}
}

return (signer, signerType);
revert SignerNotPresent(index_);
r0ohafza marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading