-
Notifications
You must be signed in to change notification settings - Fork 23
Add ZKJWTUtils #182
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
Open
ernestognw
wants to merge
18
commits into
master
Choose a base branch
from
feature/zk-jwt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add ZKJWTUtils #182
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9f34f2f
Update CHANGELOG
ernestognw 95ec54a
Add ZKJWTUtils
ernestognw 05f454f
Merge branch 'master' into feature/zk-jwt
ernestognw 49e42ca
up
ernestognw 9764154
Use email-tx-builder IVerifier interface instead
ernestognw 6e32f26
Run npm i
ernestognw c535f77
Fix paymaster pragma
ernestognw 2d3f469
update dependencies
ernestognw 2d1ec65
Merge branch 'master' into feature/zk-jwt
ernestognw 972466c
up
ernestognw 0c31be2
fix tests
ernestognw a394733
Update tests
ernestognw b70eacb
WIP: Fuzzing
ernestognw c21ab75
up
ernestognw 29335c2
Almost there
ernestognw 7a80935
Fix test (?)
ernestognw 7123fa8
Merge branch 'master' into feature/zk-jwt
ernestognw afa5a78
up
ernestognw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {IVerifier, EmailProof} from "@zk-email/email-tx-builder/src/interfaces/IVerifier.sol"; | ||
|
||
contract ZKJWTVerifierMock is IVerifier { | ||
function commandBytes() external pure returns (uint256) { | ||
// Same as in https://github.com/zkemail/email-tx-builder/blob/1452943807a5fdc732e1113c34792c76cf7dd031/packages/contracts/src/utils/Verifier.sol#L15 | ||
return 605; | ||
} | ||
|
||
function verifyEmailProof(EmailProof memory proof) external pure returns (bool) { | ||
return proof.proof.length > 0 && bytes1(proof.proof[0]) == 0x01; // boolean true | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.24; | ||
|
||
import {Bytes} from "@openzeppelin/contracts/utils/Bytes.sol"; | ||
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol"; | ||
import {IDKIMRegistry} from "@zk-email/contracts/DKIMRegistry.sol"; | ||
import {IVerifier, EmailProof} from "@zk-email/email-tx-builder/src/interfaces/IVerifier.sol"; | ||
import {CommandUtils} from "@zk-email/email-tx-builder/src/libraries/CommandUtils.sol"; | ||
|
||
/** | ||
* @dev Library for https://docs.zk.email[ZK JWT] validation utilities. | ||
* | ||
* ZK JWT is a protocol that enables JWT-based authentication and authorization for smart contracts | ||
* using zero-knowledge proofs. It allows users to prove ownership of a JWT token without revealing | ||
* the token content or private keys. See https://datatracker.ietf.org/doc/html/rfc7519[RFC-7519] for | ||
* details on the JWT verification process. | ||
* | ||
* The validation process involves several key components: | ||
* | ||
* * A https://docs.zk.email/jwt-tx-builder/architecture[JWT Registry] verification mechanism to ensure | ||
* the JWT was issued from a valid issuer with valid public key. The registry validates the `kid|iss|azp` format | ||
* used in JWT verification (i.e. key id, issuer, and authorized party). | ||
* * A https://docs.zk.email/email-tx-builder/architecture/command-templates[command template] validation | ||
* mechanism to ensure the JWT command matches the expected format and parameters. | ||
* * A https://docs.zk.email/jwt-tx-builder/architecture[zero-knowledge proof] verification mechanism to ensure | ||
* the JWT was actually issued and received without revealing its contents through RSA signature validation | ||
* and selective claim disclosure. | ||
* | ||
* NOTE: This library adapts the email authentication infrastructure for JWT verification, | ||
* reusing the EmailProof structure which contains JWT-specific data encoded in the domainName field | ||
* as "kid|iss|azp" format. | ||
*/ | ||
library ZKJWTUtils { | ||
using CommandUtils for bytes[]; | ||
using Bytes for bytes; | ||
using Strings for string; | ||
|
||
/// @dev Enumeration of possible JWT proof validation errors. | ||
/// See https://docs.zk.email/jwt-tx-builder/architecture[ZK JWT Architecture] for validation flow details. | ||
enum JWTProofError { | ||
NoError, | ||
JWTPublicKeyHash, // The JWT public key hash verification fails | ||
MaskedCommandLength, // The masked command length exceeds the maximum allowed by the circuit | ||
MismatchedCommand, // The command does not match the proof command | ||
JWTProof // The JWT proof verification fails | ||
} | ||
|
||
/// @dev Enumeration of possible string cases used to compare the command with the expected proven command. | ||
enum Case { | ||
CHECKSUM, // Computes a checksum of the command. | ||
LOWERCASE, // Converts the command to hex lowercase. | ||
UPPERCASE, // Converts the command to hex uppercase. | ||
ANY | ||
} | ||
|
||
/// @dev Validates a ZK JWT proof with default "signHash" command template. | ||
function isValidZKJWT( | ||
EmailProof memory jwtProof, | ||
IDKIMRegistry jwtRegistry, | ||
IVerifier verifier, | ||
bytes32 hash | ||
) internal view returns (JWTProofError) { | ||
string[] memory signHashTemplate = new string[](2); | ||
signHashTemplate[0] = "signHash"; | ||
signHashTemplate[1] = CommandUtils.UINT_MATCHER; // UINT_MATCHER is always lowercase | ||
return isValidZKJWT(jwtProof, jwtRegistry, verifier, signHashTemplate, _asSingletonArray(hash), Case.LOWERCASE); | ||
} | ||
|
||
/** | ||
* @dev Validates a ZK JWT proof against a command template. | ||
* | ||
* This function takes a JWT proof, a JWT registry contract, and a verifier contract | ||
* as inputs. It performs several validation checks and returns a {JWTProofError} indicating the result. | ||
* Returns {JWTProofError.NoError} if all validations pass, or a specific {JWTProofError} indicating | ||
* which validation check failed. | ||
* | ||
* NOTE: Attempts to validate the command for all possible string {Case} values. | ||
*/ | ||
function isValidZKJWT( | ||
EmailProof memory jwtProof, | ||
IDKIMRegistry jwtRegistry, | ||
IVerifier verifier, | ||
string[] memory template, | ||
bytes[] memory templateParams | ||
) internal view returns (JWTProofError) { | ||
return isValidZKJWT(jwtProof, jwtRegistry, verifier, template, templateParams, Case.ANY); | ||
} | ||
|
||
/** | ||
* @dev Validates a ZK JWT proof against a template with a specific string {Case}. | ||
* | ||
* Useful for templates with Ethereum address matchers (i.e. `{ethAddr}`), which are case-sensitive | ||
* (e.g., `["someCommand", "{address}"]`). | ||
*/ | ||
function isValidZKJWT( | ||
EmailProof memory jwtProof, | ||
IDKIMRegistry jwtRegistry, | ||
IVerifier verifier, | ||
string[] memory template, | ||
bytes[] memory templateParams, | ||
Case stringCase | ||
) internal view returns (JWTProofError) { | ||
if (bytes(jwtProof.maskedCommand).length > verifier.commandBytes()) { | ||
return JWTProofError.MaskedCommandLength; | ||
} else if (!_commandMatch(jwtProof, template, templateParams, stringCase)) { | ||
return JWTProofError.MismatchedCommand; | ||
} else if (!jwtRegistry.isDKIMPublicKeyHashValid(jwtProof.domainName, jwtProof.publicKeyHash)) { | ||
// Validate JWT public key and authorized party through registry | ||
// The domainName contains "kid|iss|azp" format for JWT validation | ||
return JWTProofError.JWTPublicKeyHash; | ||
} | ||
|
||
// Verify the zero-knowledge proof of JWT signature | ||
// TODO: Is `verifyEmailProof` supposed to be non-view? | ||
return verifier.verifyEmailProof(jwtProof) ? JWTProofError.NoError : JWTProofError.JWTProof; | ||
} | ||
|
||
/// @dev Compares the command in the JWT proof with the expected command template. | ||
function _commandMatch( | ||
EmailProof memory jwtProof, | ||
string[] memory template, | ||
bytes[] memory templateParams, | ||
Case stringCase | ||
) private pure returns (bool) { | ||
// Convert template to expected command format | ||
uint256 commandPrefixLength = bytes(jwtProof.maskedCommand).indexOf(bytes1(" ")); | ||
string memory command = string(bytes(jwtProof.maskedCommand).slice(commandPrefixLength + 1)); | ||
|
||
if (stringCase != Case.ANY) | ||
return templateParams.computeExpectedCommand(template, uint8(stringCase)).equal(command); | ||
return | ||
templateParams.computeExpectedCommand(template, uint8(Case.LOWERCASE)).equal(command) || | ||
templateParams.computeExpectedCommand(template, uint8(Case.UPPERCASE)).equal(command) || | ||
templateParams.computeExpectedCommand(template, uint8(Case.CHECKSUM)).equal(command); | ||
} | ||
|
||
/// @dev Creates an array in memory with only one value for each of the elements provided. | ||
function _asSingletonArray(bytes32 element) private pure returns (bytes[] memory array) { | ||
assembly ("memory-safe") { | ||
array := mload(0x40) // Load free memory pointer | ||
mstore(array, 1) // Set array length to 1 | ||
mstore(add(array, 0x20), element) // Store the single element | ||
mstore(0x40, add(array, 0x40)) // Update memory pointer | ||
} | ||
} | ||
} |
Submodule @openzeppelin-contracts
updated
236 files
Submodule @openzeppelin-contracts-upgradeable
updated
147 files
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.