-
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.
- Loading branch information
Showing
10 changed files
with
179 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: CI | ||
on: push | ||
|
||
jobs: | ||
tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: software-mansion/setup-scarb@v1 | ||
- run: scarb fmt --check | ||
- run: scarb test | ||
format: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: software-mansion/setup-scarb@v1 | ||
- run: scarb fmt --check |
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 |
---|---|---|
@@ -1,11 +1,2 @@ | ||
node.json | ||
accounts.json | ||
artifacts | ||
tests/__pycache__ | ||
goerli.deployments.txt | ||
.vscode | ||
|
||
cairo/ | ||
# Added by cargo | ||
|
||
/target | ||
target/ |
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 @@ | ||
scarb 2.3.0 |
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,6 @@ | ||
# Code generated by scarb DO NOT EDIT. | ||
version = 1 | ||
|
||
[[package]] | ||
name = "commit_reveal" | ||
version = "0.1.0" |
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,10 @@ | ||
[package] | ||
name = "commit_reveal" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
starknet = "2.3.0" | ||
|
||
[tool.fmt] | ||
max-line-length = 120 | ||
sort-module-level-items = true |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
mod voting_contract; | ||
use voting_contract::VotingContract; | ||
|
||
mod test_voting_contract; | ||
#[cfg(test)] | ||
mod tests { | ||
mod voting_contract; | ||
} |
This file was deleted.
Oops, something went wrong.
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,101 @@ | ||
use commit_reveal::voting_contract::{IVotingContractDispatcher, IVotingContractDispatcherTrait, VotingContract}; | ||
use core::option::OptionTrait; | ||
use core::traits::TryInto; | ||
use starknet::{deploy_syscall, ContractAddress, contract_address_try_from_felt252, testing::set_contract_address}; | ||
|
||
const SALT: felt252 = 42; | ||
const VALUE_TO_SALT: felt252 = 'lama'; | ||
|
||
// Computed using: https://www.stark-utils.xyz/signature | ||
const PEDERSEN_HASH: felt252 = 0x38cb18d2caa96cd242db94dbc924881817745fb1bb1ecc15d5dbd0e8bc795b; | ||
|
||
fn deploy_contract() -> IVotingContractDispatcher { | ||
let class_hash = VotingContract::TEST_CLASS_HASH.try_into().unwrap(); | ||
let (contract_address, _) = deploy_syscall(class_hash, 0, array![].span(), false).unwrap(); | ||
IVotingContractDispatcher { contract_address } | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn test_hash_salt_with_value() { | ||
let voting_contract = deploy_contract(); | ||
|
||
let hash = voting_contract.hash_salt_with_value(SALT, VALUE_TO_SALT); | ||
assert(hash == PEDERSEN_HASH, 'Should be PEDERSEN_HASH'); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn test_commit_hash() { | ||
let voting_contract = deploy_contract(); | ||
let caller = 'caller 1'.try_into().unwrap(); | ||
|
||
set_contract_address(caller); | ||
voting_contract.commit_hash(PEDERSEN_HASH); | ||
|
||
let hash = voting_contract.get_hash_for(caller); | ||
assert(hash == PEDERSEN_HASH, 'Hash not correctly committed'); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn test_get_hash_for_nothing_committed() { | ||
let voting_contract = deploy_contract(); | ||
let caller = 'caller 2'.try_into().unwrap(); | ||
|
||
set_contract_address(caller); | ||
let hash = voting_contract.get_hash_for(caller); | ||
assert(hash == 0, 'Hash should be zero'); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
#[should_panic(expected: ('No hash committed', 'ENTRYPOINT_FAILED'))] | ||
fn test_reveal_with_nothing_committed() { | ||
let voting_contract = deploy_contract(); | ||
|
||
set_contract_address('caller 3'.try_into().unwrap()); | ||
voting_contract.reveal(1, 1); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn test_reveal() { | ||
let voting_contract = deploy_contract(); | ||
set_contract_address('caller 4'.try_into().unwrap()); | ||
|
||
let mut vote = voting_contract.get_vote_for_response(VALUE_TO_SALT); | ||
assert(vote == 0, 'Hash should be zero'); | ||
voting_contract.commit_hash(PEDERSEN_HASH); | ||
|
||
vote = voting_contract.get_vote_for_response(VALUE_TO_SALT); | ||
assert(vote == 0, 'Hash should be zero'); | ||
voting_contract.reveal(SALT, VALUE_TO_SALT); | ||
|
||
vote = voting_contract.get_vote_for_response(VALUE_TO_SALT); | ||
assert(vote == 1, 'Hash should be one'); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn test_reveal_value_reset() { | ||
let voting_contract = deploy_contract(); | ||
let caller = 'caller 5'.try_into().unwrap(); | ||
|
||
set_contract_address(caller); | ||
voting_contract.commit_hash(PEDERSEN_HASH); | ||
voting_contract.reveal(SALT, VALUE_TO_SALT); | ||
let hash = voting_contract.get_hash_for(caller); | ||
assert(hash == 0, 'Hash should be zero'); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
#[should_panic(expected: ('You are trying to cheat', 'ENTRYPOINT_FAILED'))] | ||
fn test_reveal_cheating() { | ||
let voting_contract = deploy_contract(); | ||
set_contract_address('caller 6'.try_into().unwrap()); | ||
|
||
voting_contract.commit_hash(PEDERSEN_HASH); | ||
voting_contract.reveal(SALT - 1, VALUE_TO_SALT); | ||
} |
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 |
---|---|---|
@@ -1,41 +1,52 @@ | ||
#[contract] | ||
use starknet::ContractAddress; | ||
|
||
#[starknet::interface] | ||
trait IVotingContract<TContractState> { | ||
fn hash_salt_with_value(self: @TContractState, salt: felt252, value_to_hash: felt252) -> felt252; | ||
fn get_vote_for_response(self: @TContractState, response: felt252) -> felt252; | ||
fn get_hash_for(self: @TContractState, address: ContractAddress) -> felt252; | ||
fn commit_hash(ref self: TContractState, hash: felt252); | ||
fn reveal(ref self: TContractState, number: felt252, response: felt252); | ||
} | ||
|
||
#[starknet::contract] | ||
mod VotingContract { | ||
use starknet::get_caller_address; | ||
use starknet::ContractAddress; | ||
use hash::{HashStateTrait, HashStateExTrait}; | ||
use pedersen::PedersenTrait; | ||
use starknet::{ContractAddress, get_caller_address}; | ||
|
||
#[storage] | ||
struct Storage { | ||
caller_address_hash: LegacyMap<ContractAddress, felt252>, | ||
vote_for_response: LegacyMap<felt252, felt252>, | ||
} | ||
|
||
#[view] | ||
fn hash_salt_with_value(salt: felt252, value_to_hash: felt252) -> felt252 { | ||
pedersen(salt, value_to_hash) | ||
} | ||
#[external(v0)] | ||
impl VotingContractImpl of super::IVotingContract<ContractState> { | ||
fn hash_salt_with_value(self: @ContractState, salt: felt252, value_to_hash: felt252) -> felt252 { | ||
PedersenTrait::new(salt).update_with(value_to_hash).finalize() | ||
} | ||
|
||
#[view] | ||
fn get_vote_for_response(response: felt252) -> felt252 { | ||
vote_for_response::read(response) | ||
} | ||
fn get_vote_for_response(self: @ContractState, response: felt252) -> felt252 { | ||
self.vote_for_response.read(response) | ||
} | ||
|
||
#[view] | ||
fn get_hash_for(address: ContractAddress) -> felt252 { | ||
caller_address_hash::read(address) | ||
} | ||
fn get_hash_for(self: @ContractState, address: ContractAddress) -> felt252 { | ||
self.caller_address_hash.read(address) | ||
} | ||
|
||
#[external] | ||
fn commit_hash(hash: felt252) { | ||
caller_address_hash::write(get_caller_address(), hash); | ||
} | ||
fn commit_hash(ref self: ContractState, hash: felt252) { | ||
self.caller_address_hash.write(get_caller_address(), hash); | ||
} | ||
|
||
#[external] | ||
fn reveal(number: felt252, response: felt252) { | ||
let caller_address = get_caller_address(); | ||
let committed_hash = caller_address_hash::read(caller_address); | ||
assert(committed_hash != 0, 'No hash committed'); | ||
let current_hash = hash_salt_with_value(number, response); | ||
assert(current_hash == committed_hash, 'You are trying to cheat'); | ||
caller_address_hash::write(caller_address, 0); | ||
vote_for_response::write(response, vote_for_response::read(response) + 1); | ||
fn reveal(ref self: ContractState, number: felt252, response: felt252) { | ||
let caller_address = get_caller_address(); | ||
let committed_hash = self.get_hash_for(caller_address); | ||
assert(committed_hash != 0, 'No hash committed'); | ||
let current_hash = self.hash_salt_with_value(number, response); | ||
assert(current_hash == committed_hash, 'You are trying to cheat'); | ||
self.caller_address_hash.write(caller_address, 0); | ||
self.vote_for_response.write(response, self.vote_for_response.read(response) + 1); | ||
} | ||
} | ||
} |