Skip to content

Commit

Permalink
Automated changes
Browse files Browse the repository at this point in the history
  • Loading branch information
critesjosh committed Dec 19, 2023
1 parent 392c2bb commit 429eac1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
60 changes: 26 additions & 34 deletions src/main.nr
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
contract EasyPrivateVoting {
use dep::protocol_types::{
address::AztecAddress,
constants::EMPTY_NULLIFIED_COMMITMENT,
};
use dep::protocol_types::constants::EMPTY_NULLIFIED_COMMITMENT;
use dep::aztec::{
context::{PrivateContext, Context},
oracle::get_secret_key::get_secret_key, // used to compute nullifier
selector::compute_selector, // used to compute function selector for calling a function
state_vars::{ map::Map, public_state::PublicState,},
types::type_serialization::{ // serialization methods for using booleans and aztec addresses
bool_serialization::{BoolSerializationMethods, BOOL_SERIALIZED_LEN},
address_serialization::{AddressSerializationMethods, AZTEC_ADDRESS_SERIALIZED_LEN},
field_serialization::{ FieldSerializationMethods, FIELD_SERIALIZED_LEN},
},
context::{PrivateContext, Context},
oracle::get_secret_key::get_secret_key, // used to compute nullifier
selector::compute_selector, // used to compute function selector for calling a function
state_vars::{ map::Map, public_state::PublicState,},
types::type_serialization::{ // serialization methods for using booleans and aztec addresses
bool_serialization::{BoolSerializationMethods, BOOL_SERIALIZED_LEN},
aztec_address_serialization::{AztecAddressSerializationMethods, AZTEC_ADDRESS_SERIALIZED_LEN},
field_serialization::{ FieldSerializationMethods, FIELD_SERIALIZED_LEN},
},
types::address::{AztecAddress},
};
struct Storage {
admin: PublicState<AztecAddress, AZTEC_ADDRESS_SERIALIZED_LEN>, // admin can end vote
tally: Map<PublicState<Field, FIELD_SERIALIZED_LEN>>, // we will store candidate as key and number of votes as value
voteEnded: PublicState<bool, BOOL_SERIALIZED_LEN>, // voteEnded is boolean
}
admin: PublicState<AztecAddress, AZTEC_ADDRESS_SERIALIZED_LEN>, // admin can end vote
tally: Map<PublicState<Field, FIELD_SERIALIZED_LEN>>, // we will store candidate as key and number of votes as value
voteEnded: PublicState<bool, BOOL_SERIALIZED_LEN>, // voteEnded is boolean
}
impl Storage {
fn init(context: Context) -> Self {
Storage {
admin: PublicState::new(
context,
1, // storage slot. this can be anything except 0. it is hashed, and hash on 0 = 0
AddressSerializationMethods,
AztecAddressSerializationMethods,
),
tally: Map::new(
context,
Expand All @@ -49,9 +47,9 @@ contract EasyPrivateVoting {
fn constructor(admin: AztecAddress) { // called when contract is deployed
context.call_public_function(
// we cannot update public state directly from private function but we can call public function (which queues it)
context.this_address(),// contract address whose method we want to call
context.this_address(), // contract address whose method we want to call
compute_selector("_initialize((Field))"), // function selector
[admin.to_field()] // parameters
[admin.address] // parameters
);
}
#[aztec(public)] // annotation to mark function as public and expose public context
Expand All @@ -62,13 +60,12 @@ contract EasyPrivateVoting {
#[aztec(private)] // annotation to mark function as private and expose private context
fn cast_vote(candidate: Field) {
let secret = get_secret_key(context.msg_sender()); // get secret key of caller of function
let nullifier = dep::std::hash::pedersen_hash([context.msg_sender().to_field(), secret.low, secret.high]); // compute nullifier with this secret key so others can't descrypt it
let nullifier = dep::std::hash::pedersen_hash([context.msg_sender(), secret.low, secret.high]); // compute nullifier with this secret key so others can't descrypt it
context.push_new_nullifier(nullifier, EMPTY_NULLIFIED_COMMITMENT); // push nullifier
context.call_public_function(
context.this_address(),
compute_selector("add_to_tally_public(Field)"),
[candidate]
);
context.this_address(),
compute_selector("add_to_tally_public(Field)"),
[candidate]);
}

#[aztec(public)]
Expand All @@ -80,18 +77,13 @@ contract EasyPrivateVoting {

#[aztec(public)]
fn end_vote() {
assert(storage.admin.read().eq(context.msg_sender()), "Only admin can end votes"); // assert that caller is admin
assert(storage.admin.read().eq(AztecAddress::new(context.msg_sender())), "Only admin can end votes"); // assert that caller is admin
storage.voteEnded.write(true);
}
unconstrained fn get_vote(candidate: Field) -> pub Field {
unconstrained fn get_vote(candidate: Field) -> Field {
storage.tally.at(candidate).read()
}
unconstrained fn compute_note_hash_and_nullifier(
contract_address: Field,
nonce: Field,
storage_slot: Field,
serialized_note: [Field; 0]
) -> pub [Field; 4] {
unconstrained fn compute_note_hash_and_nullifier(contract_address: Field, nonce: Field, storage_slot: Field, serialized_note: [Field; 0]) -> [Field; 4] {
[0, 0, 0, 0]
}
}
}
1 change: 0 additions & 1 deletion tmp
Submodule tmp deleted from bd5614

0 comments on commit 429eac1

Please sign in to comment.