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

Commit reveal #77 #184

Merged
merged 45 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
399030a
commitment scheme implementation
LamsyA May 20, 2024
13dab31
scarb.lock
LamsyA May 20, 2024
4cbdea1
modified the reveal function to view
LamsyA May 20, 2024
de23639
refactored
LamsyA May 24, 2024
eef8175
refactored
LamsyA May 24, 2024
3eda221
offchain commitment and onchain verification done
LamsyA Jul 19, 2024
e793505
added erc20 unit test
the-first-elder May 27, 2024
19b7dff
added test against custom errors
the-first-elder May 27, 2024
4e02aac
formated
the-first-elder May 27, 2024
c4996b1
made requested changes and moved test to contract module
the-first-elder May 28, 2024
09e06ff
fixed zero address
the-first-elder May 28, 2024
23849cb
chore: Remove unused test files
julio4 May 30, 2024
9e71e10
Library calls (#194)
OkoliEvans May 30, 2024
aca1d8c
Chapter 0 > Basics: wording + grammar fixes (#207)
0xNeshi Jun 3, 2024
01ab62d
component dependencies test (#198)
No-bodyq Jun 4, 2024
7a67290
feat(staking): Add staking contract example (#202)
hudem1 Jun 4, 2024
02c599c
feat(enums): Enums in contract (#212)
hudem1 Jun 4, 2024
5cc0c10
Application chapter: grammar, wording, typos, `!addr.is_zero` -> `add…
0xNeshi Jun 5, 2024
dd4eb66
add time locked transactions example (#201)
saimeunt Jun 5, 2024
f617fee
test for upgradeable contracts (#203)
the-first-elder Jun 5, 2024
0091a36
Components chapter: wording, grammar, formatting fixes (#210)
0xNeshi Jun 5, 2024
777f885
Advanced concepts: typos, wording, grammar, formatting (#215)
0xNeshi Jun 8, 2024
2252be1
feat: nft dutch auction (#204)
0xibs Jun 9, 2024
6e49cf5
fix: chapter-related folder names (#216)
raizo07 Jun 9, 2024
60e63cc
feat: simple storage with starknet-js (#222)
egeaybars123 Jun 25, 2024
4724fe8
chore: scarb, foundry, oz updates (#227)
julio4 Jun 25, 2024
f64369b
feat: Advanced factory contract (#219)
Jun 27, 2024
6c897cd
test: countable (switchable, ownable) components (#205)
NueloSE Jul 1, 2024
731d7ff
doc: SNIP-6 implementation (#200)
Jemiiah Jul 1, 2024
93767b8
simple_vault test implementation (#220)
LamsyA Jul 19, 2024
07da159
Update es.po (#231)
sinsotec Jul 19, 2024
488f100
Add Dict Cheatsheet (#235)
LamsyA Aug 8, 2024
40cf760
Mention Foundry is an option for tests (#240)
Aug 8, 2024
06b071b
Update SUMMARY.md (#239)
0xibs Aug 8, 2024
5579b4a
fix: links, typos, preludes... (#241)
julio4 Aug 8, 2024
0c12db0
Update Cairo >2.8, `2024_07` edition (#246)
julio4 Sep 19, 2024
703600d
Random Number Generator (#238)
0xNeshi Oct 1, 2024
b5625d5
feat(merkle-tree): Contract with tests (#228)
hudem1 Oct 1, 2024
99cf787
Improvement: typos check in CI/CD (#248)
julio4 Oct 3, 2024
cfe7ca0
feat: cairo syntax hl (#249)
julio4 Oct 23, 2024
e503850
Expand Constant Product AMM's description (#252)
0xNeshi Oct 24, 2024
6dad858
Minor _Scarb.toml_ refactors (#251)
0xNeshi Oct 24, 2024
07f989b
scarb.lock
LamsyA May 20, 2024
3a336cb
feat: commit-reveal pattern
julio4 Oct 24, 2024
e6201a1
Merge branch 'main' into commit_reveal_#77
julio4 Oct 24, 2024
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
4 changes: 4 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ dependencies = [
"snforge_std",
]

[[package]]
name = "commit_reveal"
version = "0.1.0"

[[package]]
name = "components"
version = "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions listings/advanced-concepts/commit_reveal/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
15 changes: 15 additions & 0 deletions listings/advanced-concepts/commit_reveal/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "commit_reveal"
version.workspace = true
edition.workspace = true

[dependencies]
starknet.workspace = true

[dev-dependencies]
cairo_test.workspace = true

[scripts]
test.workspace = true

[[target.starknet-contract]]
71 changes: 71 additions & 0 deletions listings/advanced-concepts/commit_reveal/src/commit_reveal.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#[starknet::interface]
pub trait ICommitmentRevealTrait<T> {
fn commit(ref self: T, commitment: felt252);
fn reveal(self: @T, secret: felt252) -> bool;
}

// ANCHOR: contract
#[starknet::contract]
pub mod CommitmentRevealTraits {
use starknet::storage::{StoragePointerWriteAccess, StoragePointerReadAccess};
use core::hash::HashStateTrait;
use core::pedersen::PedersenTrait;

#[storage]
struct Storage {
commitment: felt252,
}

#[abi(embed_v0)]
impl CommitmentRevealTrait of super::ICommitmentRevealTrait<ContractState> {
fn commit(ref self: ContractState, commitment: felt252) {
self.commitment.write(commitment);
}

fn reveal(self: @ContractState, secret: felt252) -> bool {
let hash = PedersenTrait::new(secret).finalize();
self.commitment.read() == hash
}
}
}
// ANCHOR_END: contract

#[cfg(test)]
mod tests {
use starknet::SyscallResultTrait;
use super::{
CommitmentRevealTraits, ICommitmentRevealTraitDispatcher,
ICommitmentRevealTraitDispatcherTrait
};

use core::hash::HashStateTrait;
use core::pedersen::PedersenTrait;
use starknet::syscalls::deploy_syscall;

fn deploy() -> ICommitmentRevealTraitDispatcher {
let (contract_address, _) = deploy_syscall(
CommitmentRevealTraits::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
ICommitmentRevealTraitDispatcher { contract_address }
}

#[test]
fn commit_and_reveal() {
let mut contract = deploy();

// ANCHOR: offchain
// Off-chain, compute the commitment hash for secret
let secret = 'My secret';
let offchain_commitment = PedersenTrait::new(secret).finalize();

// Commit on-chain
contract.commit(offchain_commitment);

// Reveal on-chain and assert the result
let reveal_result = contract.reveal(secret);
// ANCHOR_END: offchain
assert_eq!(reveal_result, true);
}
}

1 change: 1 addition & 0 deletions listings/advanced-concepts/commit_reveal/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod commit_reveal;
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Summary
- [Writing to any storage slot](./advanced-concepts/write_to_any_slot.md)
- [Struct as mapping key](./advanced-concepts/struct-mapping-key.md)
- [Hashing](./advanced-concepts/hashing.md)
- [Commit-Reveal](./advanced-concepts/commit-reveal.md)
<!-- Hidden until #123 is solved -->
<!-- - [Hash Solidity Compatible](./ch02/hash-solidity-compatible.md) -->
- [Optimisations](./advanced-concepts/optimisations/optimisations.md)
Expand Down
38 changes: 38 additions & 0 deletions src/advanced-concepts/commit-reveal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Commit-Reveal

The Commit-Reveal pattern is a fundamental blockchain pattern that enables to:
1. Commit to a value without revealing it *(commit phase)*
2. Reveal the value later to prove they knew it in advance *(reveal phase)*

Some use cases:
- **Blind Auctions**: Bidders commit to their bids first, then reveal them after the bidding period
- **Voting Systems**: Voters commit their votes early, revealing them only after voting ends
- **Knowledge Proofs/Attestations**: Proving you knew information at a specific time without revealing it immediately
- **Fair Random Number Generation**: Players commit to random numbers that get combined later, making it harder to manipulate the outcome

## How It Works

1. **Commit Phase**:
- User generates a value (`secret`)
- User creates a hash of this value
- User submits only the hash on-chain (`commit`)

2. **Reveal Phase**:
- User submits the original value (`reveal`)
- Contract verifies that the hash of the submitted value matches the previously committed hash
- If it matches then it proves that the user knew the value at the commitment time

## Minimal commit-reveal contract:

```cairo
{{#rustdoc_include ../../listings/advanced-concepts/commit_reveal/src/commit_reveal.cairo:contract}}
```

Usage example:
```cairo
{{#include ../../listings/advanced-concepts/commit_reveal/src/commit_reveal.cairo:offchain}}
```

Some considerations:
- The commit phase must complete before any reveals can start
- Users might choose not to reveal if the outcome is unfavorable (consider adding stake/slashing mechanics to ensure reveals)