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

perf: optimize solidity verification contract #720

Closed
wants to merge 4 commits into from
Closed
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
61 changes: 15 additions & 46 deletions recursion/gnark-ffi/assets/SP1Verifier.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,33 @@ contract SP1Verifier is Verifier {
/// @notice Deserializes a proof from the given bytes.
/// @param proofBytes The proof bytes.
function deserializeProof(
bytes memory proofBytes
bytes calldata proofBytes
)
public
pure
returns (
uint256[8] memory proof,
uint256[2] memory commitments,
uint256[2] memory commitmentPok
uint256[8] calldata proof,
uint256[2] calldata commitments,
uint256[2] calldata commitmentPok
)
{
require(
proofBytes.length == 8 * 32 + 4 + 2 * 32 + 2 * 32,
"invalid proof bytes length"
);

uint256 offset = 32;
for (uint256 i = 0; i < 8; i++) {
assembly {
mstore(
add(proof, add(0, mul(32, i))),
mload(add(proofBytes, add(offset, mul(32, i))))
)
}
}

uint32 commitmentCount;
offset += 8 * 32;

// Map the calldata pointers.
assembly {
let dataLocation := add(proofBytes, offset)
let loadedData := mload(dataLocation)
commitmentCount := and(shr(224, loadedData), 0xFFFFFFFF)
}

offset += 4;
for (uint256 i = 0; i < 2; i++) {
assembly {
mstore(
add(commitments, add(0, mul(32, i))),
mload(add(proofBytes, add(offset, mul(32, i))))
)
}
}

offset += 2 * 32;
for (uint256 i = 0; i < 2; i++) {
assembly {
mstore(
add(commitmentPok, add(0, mul(32, i))),
mload(add(proofBytes, add(offset, mul(32, i))))
)
}
proof := proofBytes.offset
commitments := add(add(mul(8, 0x20), 0x4), proofBytes.offset)
commitmentPok := add(mul(2, 0x20), commitments)
}
}

/// @notice Hashes the public values to a field elements inside Bn254.
/// @param publicValues The public values.
function hashPublicValues(
bytes memory publicValues
bytes calldata publicValues
) public pure returns (bytes32) {
return sha256(publicValues) & bytes32(uint256((1 << 253) - 1));
}
Expand All @@ -74,13 +43,13 @@ contract SP1Verifier is Verifier {
/// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
function verifyProof(
bytes32 vkey,
bytes memory publicValues,
bytes memory proofBytes
bytes calldata publicValues,
bytes calldata proofBytes
) public view {
(
uint256[8] memory proof,
uint256[2] memory commitments,
uint256[2] memory commitmentPok
uint256[8] calldata proof,
uint256[2] calldata commitments,
uint256[2] calldata commitmentPok
) = deserializeProof(proofBytes);
bytes32 publicValuesDigest = hashPublicValues(publicValues);
uint256[2] memory inputs = [
Expand Down
Loading