Skip to content

Commit

Permalink
add asm keccak
Browse files Browse the repository at this point in the history
  • Loading branch information
kustosz committed Sep 6, 2023
1 parent 7a75193 commit cbdf914
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/WorldIDIdentityManagerImplV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ contract WorldIDIdentityManagerImplV2 is WorldIDIdentityManagerImplV1 {
/// and 7 are the `x` and `y` coordinates for `krs`.
/// @param preRoot The value for the root of the tree before the `identityCommitments` have been
/// inserted. Must be an element of the field `Kr`.
/// @param deletionIndices The indices of the identities that were deleted from the tree.
/// @param packedDeletionIndices The indices of the identities that were deleted from the tree.
/// @param postRoot The root obtained after deleting all of `identityCommitments` into the tree
/// described by `preRoot`. Must be an element of the field `Kr`.
///
Expand All @@ -98,8 +98,9 @@ contract WorldIDIdentityManagerImplV2 is WorldIDIdentityManagerImplV1 {
/// batch size.
function deleteIdentities(
uint256[8] calldata deletionProof,
uint32 batchSize,
uint256[] calldata packedDeletionIndices,
uint256 preRoot,
uint32[] calldata deletionIndices,
uint256 postRoot
) public virtual onlyProxy onlyInitialized onlyIdentityOperator {
// We can only operate on the latest root in reduced form.
Expand All @@ -121,7 +122,7 @@ contract WorldIDIdentityManagerImplV2 is WorldIDIdentityManagerImplV1 {
}

// Having validated the preconditions we can now check the proof itself.
bytes32 inputHash = calculateIdentityDeletionInputHash(deletionIndices, preRoot, postRoot);
bytes32 inputHash = calculateIdentityDeletionInputHash(packedDeletionIndices, preRoot, postRoot, batchSize);

// No matter what, the inputs can result in a hash that is not an element of the scalar
// field in which we're operating. We reduce it into the field before handing it to the
Expand All @@ -130,7 +131,7 @@ contract WorldIDIdentityManagerImplV2 is WorldIDIdentityManagerImplV1 {

// We need to look up the correct verifier before we can verify.
ITreeVerifier deletionVerifier =
batchDeletionVerifiers.getVerifierFor(deletionIndices.length);
batchDeletionVerifiers.getVerifierFor(batchSize);

// With that, we can properly try and verify.
try deletionVerifier.verifyProof(
Expand Down Expand Up @@ -206,7 +207,7 @@ contract WorldIDIdentityManagerImplV2 is WorldIDIdentityManagerImplV1 {
/// @notice Calculates the input hash for the identity deletion verifier.
/// @dev Implements the computation described below.
///
/// @param deletionIndices The indices of the identities that were deleted from the tree.
/// @param packedDeletionIndices The indices of the identities that were deleted from the tree.
/// @param preRoot The root value of the tree before these insertions were made.
/// @param postRoot The root value of the tree after these insertions were made.
///
Expand All @@ -217,12 +218,19 @@ contract WorldIDIdentityManagerImplV2 is WorldIDIdentityManagerImplV1 {
/// deletionIndices[0] || deletionIndices[1] || ... || deletionIndices[batchSize-1] || PreRoot || PostRoot
/// 32 || 32 || ... || 32 || 256 || 256
function calculateIdentityDeletionInputHash(
uint32[] calldata deletionIndices,
uint256[] calldata packedDeletionIndices,
uint256 preRoot,
uint256 postRoot
uint256 postRoot,
uint32 batchSize
) public view virtual onlyProxy onlyInitialized returns (bytes32 hash) {
bytes memory bytesToHash = abi.encodePacked(deletionIndices, preRoot, postRoot);

hash = keccak256(bytesToHash);
assembly {
let startOffset := mload(0x40)
let indicesByteSize := mul(batchSize, 4)
calldatacopy(startOffset, packedDeletionIndices.offset, indicesByteSize)
let rootsOffset := add(startOffset, indicesByteSize)
mstore(rootsOffset, preRoot)
mstore(add(rootsOffset, 32), postRoot)
hash := keccak256(startOffset, add(64, indicesByteSize))
}
}
}

0 comments on commit cbdf914

Please sign in to comment.