-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libraries: IVerifier: Refactor verifier into interface (#41)
* libraries: darkpool: PublicInputs: Add scalar (de)serialization methods * libraries: IVerifier: Refactor verifier into interface
- Loading branch information
Showing
12 changed files
with
223 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 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,32 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import { PlonkProof, VerificationKey } from "./libraries/verifier/Types.sol"; | ||
import { ValidWalletCreateStatement, StatementSerializer } from "./libraries/darkpool/PublicInputs.sol"; | ||
import { VerificationKeys } from "./libraries/darkpool/VerificationKeys.sol"; | ||
import { IVerifier } from "./libraries/verifier/IVerifier.sol"; | ||
import { VerifierCore } from "./libraries/verifier/VerifierCore.sol"; | ||
import { BN254 } from "solidity-bn254/BN254.sol"; | ||
|
||
using StatementSerializer for ValidWalletCreateStatement; | ||
|
||
/// @title PlonK Verifier with the Jellyfish-style arithmetization | ||
/// @notice The methods on this contract are darkpool-specific | ||
contract Verifier is IVerifier { | ||
/// @notice Verify a proof of `VALID WALLET CREATE` | ||
/// @param statement The public inputs to the proof | ||
/// @param proof The proof to verify | ||
/// @return True if the proof is valid, false otherwise | ||
function verifyValidWalletCreate( | ||
ValidWalletCreateStatement memory statement, | ||
PlonkProof memory proof | ||
) | ||
external | ||
view | ||
returns (bool) | ||
{ | ||
VerificationKey memory vk = abi.decode(VerificationKeys.VALID_WALLET_CREATE_VKEY, (VerificationKey)); | ||
BN254.ScalarField[] memory publicInputs = statement.scalarSerialize(); | ||
return VerifierCore.verify(proof, publicInputs, vk); | ||
} | ||
} |
This file contains 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,74 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import { BN254 } from "solidity-bn254/BN254.sol"; | ||
|
||
// This file represents the public inputs (statements) for various proofs used by the darkpool | ||
|
||
// ------------------- | ||
// | Statement Types | | ||
// ------------------- | ||
|
||
/// @title ValidWalletCreateStatement the statement type for the `VALID WALLET CREATE` proof | ||
struct ValidWalletCreateStatement { | ||
/// @dev The commitment to the wallet's private shares | ||
BN254.ScalarField walletCommitment; | ||
/// @dev The public wallet shares of the wallet | ||
BN254.ScalarField[] publicShares; | ||
} | ||
|
||
// ------------------------ | ||
// | Scalar Serialization | | ||
// ------------------------ | ||
|
||
/// @title StatementSerializer Library for serializing statement types to scalar arrays | ||
library StatementSerializer { | ||
// --- Valid Wallet Create --- // | ||
|
||
/// @notice Serializes a ValidWalletCreateStatement into an array of scalar field elements | ||
/// @param self The statement to serialize | ||
/// @return serialized The serialized statement as an array of scalar field elements | ||
function scalarSerialize(ValidWalletCreateStatement memory self) | ||
internal | ||
pure | ||
returns (BN254.ScalarField[] memory) | ||
{ | ||
// Create array with size = 1 (for walletCommitment) + publicShares.length | ||
BN254.ScalarField[] memory serialized = new BN254.ScalarField[](1 + self.publicShares.length); | ||
|
||
// Add the wallet commitment | ||
serialized[0] = self.walletCommitment; | ||
|
||
// Add all public shares | ||
for (uint256 i = 0; i < self.publicShares.length; i++) { | ||
serialized[i + 1] = self.publicShares[i]; | ||
} | ||
|
||
return serialized; | ||
} | ||
|
||
/// @notice Deserializes an array of scalar field elements into a ValidWalletCreateStatement | ||
/// @param serialized The serialized statement as an array of scalar field elements | ||
/// @return statement The deserialized ValidWalletCreateStatement | ||
function scalarDeserialize(BN254.ScalarField[] memory serialized) | ||
internal | ||
pure | ||
returns (ValidWalletCreateStatement memory statement) | ||
{ | ||
require(serialized.length >= 1, "Invalid serialized statement length"); | ||
|
||
// Extract the wallet commitment | ||
statement.walletCommitment = serialized[0]; | ||
|
||
// Extract the public shares | ||
statement.publicShares = new BN254.ScalarField[](serialized.length - 1); | ||
for (uint256 i = 0; i < serialized.length - 1; i++) { | ||
statement.publicShares[i] = serialized[i + 1]; | ||
} | ||
|
||
return statement; | ||
} | ||
} | ||
|
||
// Enable the library for the statement type | ||
using StatementSerializer for ValidWalletCreateStatement; |
File renamed without changes.
This file contains 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,24 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
/// @title Hasher, a simple hashing interface for Merkle and sponge hashing using Poseidon2 | ||
interface IHasher { | ||
/// @notice Hash a leaf into the merkle tree | ||
/// @param idx The index of the leaf in the tree | ||
/// @param input The input to the leaf | ||
/// @param sisterLeaves The sister leaves of the current node | ||
/// @return The incremental hashes up the tree, including the root, the root is the last element of the array | ||
function merkleHash( | ||
uint256 idx, | ||
uint256 input, | ||
uint256[] memory sisterLeaves | ||
) | ||
external | ||
view | ||
returns (uint256[] memory); | ||
|
||
/// @notice Hash a series of inputs into a sponge and squeeze | ||
/// @param inputs The inputs to the sponge | ||
/// @return The hash of the inputs | ||
function spongeHash(uint256[] memory inputs) external view returns (uint256); | ||
} |
This file contains 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 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,19 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import { PlonkProof } from "./Types.sol"; | ||
import { ValidWalletCreateStatement } from "../darkpool/PublicInputs.sol"; | ||
|
||
interface IVerifier { | ||
/// @notice Verify a proof of `VALID WALLET CREATE` | ||
/// @param proof The proof to verify | ||
/// @param statement The public inputs to the proof | ||
/// @return True if the proof is valid, false otherwise | ||
function verifyValidWalletCreate( | ||
ValidWalletCreateStatement memory statement, | ||
PlonkProof memory proof | ||
) | ||
external | ||
view | ||
returns (bool); | ||
} |
File renamed without changes.
This file contains 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 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
Oops, something went wrong.