Skip to content

Commit

Permalink
feat: ecdsa example (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwen01 authored Sep 19, 2024
1 parent d1d74c4 commit 1f2a746
Show file tree
Hide file tree
Showing 11 changed files with 4,057 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"examples/uniswap/host",
"examples/multiplexer/host",
"examples/verify-quorum/host",
"crates/client-executor",
"crates/host-executor",
]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ where `[example]` is one of the following
* Calls a contract that fetches the prices of many different collateral assets.
* The source code of this contract is found [here](./examples/multiplexer/ZkOracleHelper.sol).
* Due to the size of this program, it's recommended to use the [SP1 Prover network](https://docs.succinct.xyz/generating-proofs/prover-network.html) to generate proofs for this example.
* `verify-quorum`
* Calls a contract that verifies several ECDSA signatures on chain, and sums the stake for the addresses corresponding to valid signatures.


## Acknowledgments
Expand Down
2 changes: 1 addition & 1 deletion crates/host-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct HostExecutor<T: Transport + Clone, P: Provider<T, AnyNetwork> + Clone
pub provider: P,
}

impl<'a, T: Transport + Clone, P: Provider<T, AnyNetwork> + Clone> HostExecutor<T, P> {
impl<T: Transport + Clone, P: Provider<T, AnyNetwork> + Clone> HostExecutor<T, P> {
/// Create a new [`HostExecutor`] with a specific [`Provider`] and [`BlockNumberOrTag`].
pub async fn new(provider: P, block_number: BlockNumberOrTag) -> eyre::Result<Self> {
let block = provider
Expand Down
49 changes: 49 additions & 0 deletions examples/verify-quorum/SimpleStaking.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";

/// @title SimpleStaking
/// @notice This contract models a voting scheme, where each address has some stake.
/// Eventually, when a vote is called, signatures are collected and the total stake
/// corresponding to those signatures is returned.
contract SimpleStaking {
using ECDSA for bytes32;

mapping(address => uint256) public stakeWeight;

/// @notice Returns the total stake of an address.
function getStake(address addr) public view returns (uint256) {
return stakeWeight[addr];
}

/// @notice Updates the stake of an address.
function update(address addr, uint256 weight) public {
stakeWeight[addr] = weight;
}

/// @notice Collects signatures over many messages, and returns the total stake corresponding
/// to those signatures.
///
/// Calling this function onchain could be expensive with a large
/// number of signatures -- in that case, it would be better to prove its execution
/// with SP1.
function verifySigned(
bytes32[] memory messageHashes,
bytes[] memory signatures
) public view returns (uint256) {
require(
messageHashes.length == signatures.length,
"Input arrays must have the same length"
);

uint256 totalStake = 0;

for (uint i = 0; i < messageHashes.length; i++) {
address recoveredSigner = messageHashes[i].recover(signatures[i]);
totalStake += stakeWeight[recoveredSigner];
}

return totalStake;
}
}
Loading

0 comments on commit 1f2a746

Please sign in to comment.