Skip to content

Commit e59adee

Browse files
committed
update verification
1 parent fd87a2e commit e59adee

File tree

1 file changed

+35
-18
lines changed
  • rust/catalyst-voting/src/txs/v1

1 file changed

+35
-18
lines changed

rust/catalyst-voting/src/txs/v1/mod.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,30 +137,34 @@ impl Tx {
137137
vote_plan_id: [u8; 32], proposal_index: u8, voting_options: u8, choice: u8,
138138
election_public_key: &ElectionPublicKey, users_private_key: &PrivateKey,
139139
) -> anyhow::Result<Self> {
140-
let vote = VotePayload::new_private(
141-
&vote_plan_id,
142-
choice,
140+
Self::new_private(
141+
vote_plan_id,
142+
proposal_index,
143143
voting_options,
144+
choice,
144145
election_public_key,
146+
users_private_key,
145147
&mut default_rng(),
146-
)?;
147-
let signature = Self::sign(&vote_plan_id, proposal_index, &vote, users_private_key);
148+
)
149+
}
148150

149-
Ok(Self {
150-
vote_plan_id,
151-
proposal_index,
152-
vote,
153-
public_key: users_private_key.public_key(),
154-
signature,
155-
})
151+
/// Returns `true` if the vote is public
152+
#[must_use]
153+
pub fn is_public(&self) -> bool {
154+
matches!(self.vote, VotePayload::Public(_))
156155
}
157156

158-
/// Verify transaction signature and underlying proof for public vote
157+
/// Returns `true` if the vote is private
158+
#[must_use]
159+
pub fn is_private(&self) -> bool {
160+
matches!(self.vote, VotePayload::Private(_, _))
161+
}
162+
163+
/// Verify transaction signature
159164
///
160165
/// # Errors
161166
/// - Invalid signature
162-
/// - Invalid proof
163-
pub fn verify(&self, election_public_key: &ElectionPublicKey) -> anyhow::Result<()> {
167+
pub fn verify_signature(&self) -> anyhow::Result<()> {
164168
let bytes = Self::bytes_to_sign(
165169
&self.vote_plan_id,
166170
self.proposal_index,
@@ -171,7 +175,15 @@ impl Tx {
171175
verify_signature(&self.public_key, &bytes, &self.signature),
172176
"Invalid signature."
173177
);
178+
Ok(())
179+
}
174180

181+
/// Verify transaction proof of the private vote.
182+
/// If vote is public it returns `Ok(())`
183+
///
184+
/// # Errors
185+
/// - Invalid proof
186+
pub fn verify_proof(&self, election_public_key: &ElectionPublicKey) -> anyhow::Result<()> {
175187
if let VotePayload::Private(encrypted_vote, proof) = &self.vote {
176188
let vote_plan_id_hash = Blake2b512Hasher::new().chain_update(self.vote_plan_id);
177189
let commitment = VoterProofCommitment::from_hash(vote_plan_id_hash);
@@ -185,7 +197,6 @@ impl Tx {
185197
"Invalid proof."
186198
);
187199
}
188-
189200
Ok(())
190201
}
191202

@@ -294,7 +305,10 @@ mod tests {
294305
&users_private_key,
295306
)
296307
.unwrap();
297-
tx.verify(&election_public_key).unwrap();
308+
assert!(tx.is_public());
309+
assert!(!tx.is_private());
310+
tx.verify_signature().unwrap();
311+
tx.verify_proof(&election_public_key).unwrap();
298312

299313
let tx = Tx::new_private_with_default_rng(
300314
vote_plan_id,
@@ -305,6 +319,9 @@ mod tests {
305319
&users_private_key,
306320
)
307321
.unwrap();
308-
tx.verify(&election_public_key).unwrap();
322+
assert!(!tx.is_public());
323+
assert!(tx.is_private());
324+
tx.verify_signature().unwrap();
325+
tx.verify_proof(&election_public_key).unwrap();
309326
}
310327
}

0 commit comments

Comments
 (0)