Skip to content

Commit 536ee2b

Browse files
committed
evaluate votes
1 parent 765b4d0 commit 536ee2b

File tree

9 files changed

+35
-27
lines changed

9 files changed

+35
-27
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ export SNAPSHOT_PATH="/Users/chef/Desktop/cypher-poll/artifacts/snapshot"
5555
![reg](https://github.com/jonas089/cypher-poll/blob/master/assets/register.png)
5656
### Voting Success
5757
![vote](https://github.com/jonas089/cypher-poll/blob/master/assets/vote.png)
58+
59+
60+
## The vote argument
61+
The vote must be the same for both `register` and `vote`. With `vote` a leaf in the Tree is redeemed that was inserted during `register`. Trying to redeem an invalid vote will result in an error => an incorrect leaf.

artifacts/snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"zero_node":[102,104,122,173,248,98,189,119,108,143,193,139,142,159,142,32,8,151,20,133,110,226,51,179,144,42,89,29,13,95,41,37],"zero_levels":[[102,104,122,173,248,98,189,119,108,143,193,139,142,159,142,32,8,151,20,133,110,226,51,179,144,42,89,29,13,95,41,37],[46,235,116,166,23,127,88,141,128,192,199,82,185,149,86,144,45,223,150,130,208,185,6,245,170,42,219,175,132,102,164,233],[18,35,52,154,64,210,238,16,189,27,235,181,136,158,248,1,140,139,193,51,89,237,148,179,135,129,10,249,108,110,66,104],[91,130,182,149,167,172,38,104,225,136,183,95,125,79,167,159,170,80,65,23,209,253,252,190,138,70,145,92,26,138,81,145],[12,33,31,155,83,132,198,136,72,162,9,172,31,147,144,83,48,18,140,183,16,174,88,55,121,192,113,39,239,136,255,92]],"filled":[[225,205,255,120,135,72,161,122,55,116,114,125,193,98,201,206,102,31,78,48,214,171,66,0,151,27,33,135,29,212,79,69],[151,43,149,204,180,205,26,128,3,228,207,107,157,57,203,167,86,177,12,245,72,245,26,29,228,95,208,156,205,4,236,106],[31,234,197,43,104,110,243,125,205,217,229,65,60,203,242,239,140,142,162,86,213,76,99,107,202,88,211,149,148,70,54,190],[172,1,103,141,11,50,72,109,67,175,38,122,181,157,106,64,12,27,215,92,11,124,163,236,150,235,83,128,76,94,0,15],[238,119,83,225,224,100,59,143,106,44,132,54,173,83,8,246,251,129,142,83,95,231,200,10,73,22,240,195,198,121,76,29]],"root":[132,217,143,81,33,188,147,65,32,172,235,113,182,122,216,211,40,112,247,195,42,74,248,129,212,129,82,184,27,158,36,249],"index":1,"depth":5}
1+
{"zero_node":[102,104,122,173,248,98,189,119,108,143,193,139,142,159,142,32,8,151,20,133,110,226,51,179,144,42,89,29,13,95,41,37],"zero_levels":[[102,104,122,173,248,98,189,119,108,143,193,139,142,159,142,32,8,151,20,133,110,226,51,179,144,42,89,29,13,95,41,37],[46,235,116,166,23,127,88,141,128,192,199,82,185,149,86,144,45,223,150,130,208,185,6,245,170,42,219,175,132,102,164,233],[18,35,52,154,64,210,238,16,189,27,235,181,136,158,248,1,140,139,193,51,89,237,148,179,135,129,10,249,108,110,66,104],[91,130,182,149,167,172,38,104,225,136,183,95,125,79,167,159,170,80,65,23,209,253,252,190,138,70,145,92,26,138,81,145],[12,33,31,155,83,132,198,136,72,162,9,172,31,147,144,83,48,18,140,183,16,174,88,55,121,192,113,39,239,136,255,92]],"filled":[[146,34,98,208,123,76,102,148,10,88,26,86,19,167,75,66,19,254,221,9,239,13,74,228,109,9,9,237,202,62,185,128],[59,43,166,151,188,240,34,69,108,90,18,10,61,131,162,200,226,169,18,36,212,13,203,14,181,143,59,231,229,64,231,139],[164,163,253,84,31,58,81,98,229,125,223,0,183,169,88,226,125,140,150,173,193,40,9,114,141,129,140,115,223,132,34,137],[43,155,164,151,203,162,145,121,94,89,75,196,137,11,202,60,214,95,213,87,34,92,94,226,81,12,95,111,242,225,164,173],[103,208,243,31,171,21,66,191,172,66,119,250,49,58,37,30,143,81,210,220,181,145,31,204,64,158,125,6,122,191,210,21]],"root":[252,112,41,186,210,51,108,202,186,55,81,200,31,64,34,20,44,129,214,211,58,32,150,57,77,2,223,137,91,154,158,227],"index":1,"depth":5}

client/src/lib.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ pub enum Command {
4646
#[arg(short, long)]
4747
username: String,
4848
#[arg(short, long)]
49-
vote: String
49+
vote: String,
5050
},
5151
Vote {
5252
#[arg(short, long)]
5353
public_key_path: String,
5454
#[arg(short, long)]
55-
vote: String
55+
vote: String,
5656
},
5757
}
5858

@@ -66,7 +66,7 @@ pub fn run(cli: Cli) {
6666
private_key_path,
6767
username,
6868
random_seed,
69-
vote
69+
vote,
7070
} => {
7171
// construct the serialized registration payload
7272
let public_key_string: String =
@@ -109,7 +109,7 @@ pub fn run(cli: Cli) {
109109
signature_serialized,
110110
public_key_string,
111111
identity: public_identity,
112-
username
112+
username,
113113
};
114114
// todo: submit payload to server
115115
// should return a tree snapshot
@@ -126,7 +126,10 @@ pub fn run(cli: Cli) {
126126
snapshot_file.write(&response.bytes().unwrap()).unwrap();
127127
}
128128
// voting requires the exact tree snapshot of the leaf
129-
Command::Vote { public_key_path, vote } => {
129+
Command::Vote {
130+
public_key_path,
131+
vote,
132+
} => {
130133
let snapshot_path: PathBuf = PathBuf::from(env::var("SNAPSHOT_PATH").unwrap());
131134
let nullifier_path: PathBuf = PathBuf::from(env::var("NULLIFIER_PATH").unwrap());
132135
let mut snapshot_file = File::open(snapshot_path).unwrap();

risc0-prover/src/verifier.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use risc0_types::CircuitOutputs;
33
use methods::VOTING_ID;
44
use risc0_zkvm::Receipt;
55

6-
pub fn verify_vote(receipt: Receipt, root_history: Vec<Vec<u8>>) -> bool {
6+
pub fn verify_vote(receipt: Receipt, root_history: Vec<Vec<u8>>) -> String {
77
// expect valid proof
88
receipt.verify(VOTING_ID).expect("Failed to verify proof");
99
// decode journal and verify root_history
1010
let journal: CircuitOutputs = receipt.journal.decode().expect("Failed to decode journal");
1111
for root in journal.root_history {
1212
if !root_history.contains(&root) {
13-
return false;
13+
eprintln!("Rejected: Invalid vote!")
1414
}
1515
}
16-
true
16+
journal.vote
1717
}

risc0-types/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ pub struct CircuitInputs {
1313
pub struct CircuitOutputs {
1414
pub nullifier: Vec<u8>,
1515
pub root_history: Vec<Vec<u8>>,
16-
pub vote: String
16+
pub vote: String,
1717
}

scripts/register.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cargo run -p client register --data Hello --public-key-path /Users/chef/Desktop/cypher-poll/resources/test/key.asc --private-key-path /Users/chef/Desktop/cypher-poll/resources/test/key.sec.asc --random-seed Hello --username jonas089
1+
cargo run -p client register --data Hello --public-key-path /Users/chef/Desktop/cypher-poll/resources/test/key.asc --private-key-path /Users/chef/Desktop/cypher-poll/resources/test/key.sec.asc --random-seed Hello --username jonas089 --vote Overlord

scripts/vote.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cargo run -p client vote --public-key-path /Users/chef/Desktop/cypher-poll/resources/test/key.asc
1+
cargo run -p client vote --public-key-path /Users/chef/Desktop/cypher-poll/resources/test/key.asc --vote Overlord

service/src/main.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type GitHubUser = String;
3232
struct ServiceState {
3333
github_users: HashSet<GitHubUser>,
3434
tree_state: InMemoryTreeState,
35-
votes: Vec<String>
35+
votes: Vec<String>,
3636
}
3737
impl ServiceState {
3838
// register a voter, takes a risc0 receipt as input (currently not prover-generic)
@@ -112,7 +112,7 @@ async fn main() {
112112
let service_state: ServiceState = ServiceState {
113113
github_users: HashSet::new(),
114114
tree_state,
115-
votes: Vec::new()
115+
votes: Vec::new(),
116116
};
117117
let shared_state = Arc::new(Mutex::new(service_state));
118118
let app = Router::new()
@@ -163,13 +163,17 @@ async fn vote(
163163
Extension(state): Extension<Arc<Mutex<ServiceState>>>,
164164
Json(payload): Json<Receipt>,
165165
) -> impl IntoResponse {
166-
let is_valid: bool = verify_vote(payload, state.lock().await.tree_state.root_history.clone());
167-
if is_valid {
168-
println!("Accepted: Valid vote!");
169-
} else {
170-
println!("Rejected: Invalid vote!");
171-
}
172-
(StatusCode::OK, format!("Is Valid: {}", is_valid))
166+
let vote: String = verify_vote(payload, state.lock().await.tree_state.root_history.clone());
167+
state.lock().await.votes.push(vote.clone());
168+
println!("Accepted: Anonymous User voted for {}", &vote);
169+
println!(
170+
"Current State of the Election: {:?}",
171+
&state.lock().await.votes
172+
);
173+
(
174+
StatusCode::OK,
175+
format!("Accepted: Anonymous User voted for {}", &vote),
176+
)
173177
}
174178

175179
#[tokio::test]
@@ -187,7 +191,7 @@ async fn submit_zk_vote() {
187191
let mut service_state: ServiceState = ServiceState {
188192
github_users: HashSet::new(),
189193
tree_state,
190-
votes: Vec::new()
194+
votes: Vec::new(),
191195
};
192196
let mut identity: UniqueIdentity = UniqueIdentity {
193197
identity: None,
@@ -242,6 +246,5 @@ async fn submit_zk_vote() {
242246
vote: "Overlord".to_string(),
243247
public_key_string: public_key_string.clone(),
244248
});
245-
let is_valid: bool = verify_vote(proof, service_state.tree_state.root_history.clone());
246-
assert!(is_valid)
249+
verify_vote(proof, service_state.tree_state.root_history.clone());
247250
}

zk-associated/src/prover/logic.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use super::merkle::compute_root;
1010
use crate::storage::TreeRoot;
1111
use crypto::gpg::GpgSigner;
1212
use crypto::identity::{Identity, UniqueIdentity};
13-
use crypto::{CryptoHasherSha256, hash};
1413
pub fn prover_logic(inputs: &mut CircuitInputs) -> CircuitOutputs {
1514
let mut gpg_signer: GpgSigner = GpgSigner {
1615
secret_key_asc_path: None,
@@ -26,7 +25,6 @@ pub fn prover_logic(inputs: &mut CircuitInputs) -> CircuitOutputs {
2625
uid.compute_public_identity(gpg_signer.signed_public_key.unwrap(), inputs.vote.clone());
2726
let identity: Identity = uid.identity.unwrap();
2827
let new_root: TreeRoot = compute_root(&mut inputs.snapshot, identity);
29-
// check that the input hash is valid
3028

3129
if !inputs.root_history.contains(&new_root) {
3230
println!("Root: {:?}", &new_root);
@@ -36,6 +34,6 @@ pub fn prover_logic(inputs: &mut CircuitInputs) -> CircuitOutputs {
3634
CircuitOutputs {
3735
nullifier: inputs.nullifier.clone(),
3836
root_history: inputs.root_history.clone(),
39-
vote: inputs.vote.clone()
37+
vote: inputs.vote.clone(),
4038
}
4139
}

0 commit comments

Comments
 (0)