Skip to content

Commit

Permalink
calculate kzg challenge instead of accepting it as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
wzmuda committed Sep 6, 2024
1 parent 985b052 commit f8bb6d0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
20 changes: 15 additions & 5 deletions src/WorldIDIdentityManagerImplV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ contract WorldIDIdentityManagerImplV3 is WorldIDIdentityManagerImplV2 {
/// @notice KZG proof associated with the commitment.
uint128[3] kzgProof;

/// @notice Expected evaluation of the polynomial at a certain point equal to kzgChallenge.
/// @notice Expected evaluation of the polynomial at a certain point equal to KZG challenge.
uint256 expectedEvaluation;

/// @notice The value for the root of the tree before the `identityCommitments` have been inserted.
Expand All @@ -113,9 +113,6 @@ contract WorldIDIdentityManagerImplV3 is WorldIDIdentityManagerImplV2 {
/// by `preRoot`. Must be an element of the field `Kr`. (already in reduced form)
uint256 postRoot;

/// @notice Challenge value used in the KZG proof to evaluate the polynomial.
uint256 kzgChallenge;

/// @notice Hash of the input data calculated as described in the comment
/// to `calculateIdentityRegistrationInputHash()`.
bytes32 inputHash;
Expand Down Expand Up @@ -174,9 +171,10 @@ contract WorldIDIdentityManagerImplV3 is WorldIDIdentityManagerImplV2 {
// which it was expired.
rootHistory[params.preRoot] = uint128(block.timestamp);

bytes32 kzgChallenge = computeKzgChallenge(params.inputHash, kzgCommitmentHash);
bool success = evaluatePoint(
kzgCommitmentHash,
bytes32(params.kzgChallenge),
kzgChallenge,
bytes32(params.expectedEvaluation),
params.kzgCommitment,
params.kzgProof
Expand Down Expand Up @@ -225,4 +223,16 @@ contract WorldIDIdentityManagerImplV3 is WorldIDIdentityManagerImplV2 {
(bool success, ) = PRECOMPILE_POINT_EVALUATION.staticcall(input);
return success;
}

/// @notice Converts input values to a KZG challenge.
/// @dev The challenge is defined as a bytes32 value of a keccak256 hash of the concatenated inputs reduced by BN254 modulus.
/// @param inputHash Hash of the input data calculated as described in the comment
/// to `calculateIdentityRegistrationInputHash()`.
/// @param kzgCommitmentVersionedHash versioned hash of the KZG commitment.
/// @return challenge The reduced keccak256 hash.
function computeKzgChallenge(bytes32 inputHash, bytes32 kzgCommitmentVersionedHash) public pure returns (bytes32) {
bytes memory inputBytes = abi.encodePacked(inputHash, kzgCommitmentVersionedHash);
uint256 reducedHash = uint256(keccak256(inputBytes)) % SNARK_SCALAR_FIELD;
return bytes32(reducedHash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: insertionPreRoot,
postRoot: insertionPostRoot4844,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(identityCommitmentsSize),
startIndex: startIndex
Expand Down Expand Up @@ -127,7 +126,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: newPreRoot,
postRoot: newPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(preparedIdents.length),
startIndex: newStartIndex
Expand Down Expand Up @@ -193,7 +191,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: newPreRoot,
postRoot: newPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(preparedIdents.length),
startIndex: newStartIndex
Expand Down Expand Up @@ -260,7 +257,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: newPreRoot,
postRoot: newPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(preparedIdents.length),
startIndex: newStartIndex
Expand Down Expand Up @@ -310,7 +306,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: newPreRoot,
postRoot: newPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(identityCommitments.length),
startIndex: newStartIndex
Expand Down Expand Up @@ -356,7 +351,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: insertionPreRoot,
postRoot: insertionPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(identityCommitments.length),
startIndex: newStartIndex
Expand Down Expand Up @@ -408,7 +402,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: insertionPreRoot,
postRoot: insertionPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(identities.length),
startIndex: startIndex
Expand Down Expand Up @@ -472,7 +465,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: insertionPreRoot,
postRoot: newPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(identityCommitments.length),
startIndex: startIndex
Expand Down Expand Up @@ -504,7 +496,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: insertionPreRoot,
postRoot: insertionPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(identityCommitments.length),
startIndex: startIndex
Expand Down Expand Up @@ -551,7 +542,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: actualRoot,
postRoot: insertionPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(identityCommitments.length),
startIndex: startIndex
Expand Down Expand Up @@ -608,7 +598,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: initialRoot,
postRoot: insertionPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(identities.length),
startIndex: startIndex
Expand Down Expand Up @@ -639,7 +628,6 @@ contract WorldIDIdentityManagerIdentityRegistration4844 is WorldIDIdentityManage
expectedEvaluation: insertionExpectedEvaluation,
preRoot: initialRoot,
postRoot: insertionPostRoot,
kzgChallenge: kzgChallenge,
inputHash: insertionInputHash4844,
batchSize: uint32(identityCommitments.length),
startIndex: startIndex
Expand Down
2 changes: 0 additions & 2 deletions src/test/identity-manager/WorldIDIdentityManagerTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ contract WorldIDIdentityManagerTest is WorldIDTest {
0x3d5d4a7d6098f2147ed77be69d93179e6179479b8771c2554e5404c06f836408;
uint256 internal constant insertionPostRoot4844 =
0x0c3f30b0604dae9a378e2bf62826bf5a772e9ad745df6f8c8256dff351fecee8;
uint256 internal constant kzgChallenge =
0x1b5f5a71803049342dbd89f27e11d537400e959eea99de6e29e262d22adc3468;

uint256[8] insertionProof4844;
uint256[2] commitments;
Expand Down

0 comments on commit f8bb6d0

Please sign in to comment.