Skip to content

Commit 908b1a4

Browse files
committed
refactor
1 parent ad5ec99 commit 908b1a4

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

rust/catalyst-voting/src/crypto/zk_unit_vector/decoding.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,34 @@ impl UnitVectorProof {
2727
let ann = (0..len)
2828
.map(|i| {
2929
let bytes = read_array(reader)?;
30-
Announcement::from_bytes(&bytes)
31-
.map_err(|e| anyhow!("Cannot decode announcement at {i}, error: {e}."))
30+
Announcement::from_bytes(&bytes).map_err(|e| {
31+
anyhow!(
32+
"Cannot decode announcement at {i}, \
33+
error: {e}."
34+
)
35+
})
3236
})
3337
.collect::<anyhow::Result<_>>()?;
3438
let dl = (0..len)
3539
.map(|i| {
3640
let bytes = read_array(reader)?;
37-
Ciphertext::from_bytes(&bytes)
38-
.map_err(|e| anyhow!("Cannot decode ciphertext at {i}, error: {e}."))
41+
Ciphertext::from_bytes(&bytes).map_err(|e| {
42+
anyhow!(
43+
"Cannot decode ciphertext at {i}, \
44+
error: {e}."
45+
)
46+
})
3947
})
4048
.collect::<anyhow::Result<_>>()?;
4149
let rr = (0..len)
4250
.map(|i| {
4351
let bytes = read_array(reader)?;
44-
ResponseRandomness::from_bytes(&bytes)
45-
.map_err(|e| anyhow!("Cannot decode response randomness at {i}, error: {e}."))
52+
ResponseRandomness::from_bytes(&bytes).map_err(|e| {
53+
anyhow!(
54+
"Cannot decode response randomness at {i}, \
55+
error: {e}."
56+
)
57+
})
4658
})
4759
.collect::<anyhow::Result<_>>()?;
4860

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,16 @@ impl Tx {
112112
let padding_tag = read_be_u8(reader).map_err(|_| anyhow!("Missing padding tag field."))?;
113113
ensure!(
114114
padding_tag == PADDING_TAG,
115-
"Invalid padding tag field value, must be equals to {PADDING_TAG}, provided: {padding_tag}.",
115+
"Invalid padding tag field value, must be equals to {PADDING_TAG}, \
116+
provided: {padding_tag}.",
116117
);
117118

118119
let fragment_tag =
119120
read_be_u8(reader).map_err(|_| anyhow!("Missing fragment tag field."))?;
120121
ensure!(
121122
fragment_tag == FRAGMENT_TAG,
122-
"Invalid fragment tag field value, must be equals to {FRAGMENT_TAG}, provided: {fragment_tag}.",
123+
"Invalid fragment tag field value, must be equals to {FRAGMENT_TAG}, \
124+
provided: {fragment_tag}.",
123125
);
124126

125127
let vote_plan_id =
@@ -148,7 +150,8 @@ impl Tx {
148150
},
149151
tag => {
150152
bail!(
151-
"Invalid vote tag value, must be equals to {PUBLIC_VOTE_TAG} or {PRIVATE_VOTE_TAG}, provided: {tag}"
153+
"Invalid vote tag value, \
154+
must be equals to {PUBLIC_VOTE_TAG} or {PRIVATE_VOTE_TAG}, provided: {tag}"
152155
)
153156
},
154157
};
@@ -160,20 +163,23 @@ impl Tx {
160163
read_be_u8(reader).map_err(|_| anyhow!("Missing inputs amount field."))?;
161164
ensure!(
162165
inputs_amount == NUMBER_OF_INPUTS,
163-
"Invalid number of inputs, expected: {NUMBER_OF_INPUTS}, provided: {inputs_amount}",
166+
"Invalid number of inputs, expected: {NUMBER_OF_INPUTS}, \
167+
provided: {inputs_amount}",
164168
);
165169

166170
let outputs_amount =
167171
read_be_u8(reader).map_err(|_| anyhow!("Missing outputs amount field."))?;
168172
ensure!(
169173
outputs_amount == NUMBER_OF_OUTPUTS,
170-
"Invalid number of outputs, expected: {NUMBER_OF_OUTPUTS}, provided: {outputs_amount}",
174+
"Invalid number of outputs, expected: {NUMBER_OF_OUTPUTS}, \
175+
provided: {outputs_amount}",
171176
);
172177

173178
let input_tag = read_be_u8(reader).map_err(|_| anyhow!("Missing input tag field."))?;
174179
ensure!(
175180
input_tag == INPUT_TAG,
176-
"Invalid input tag, expected: {INPUT_TAG}, provided: {input_tag}",
181+
"Invalid input tag, expected: {INPUT_TAG}, \
182+
provided: {input_tag}",
177183
);
178184

179185
// skip value
@@ -187,7 +193,8 @@ impl Tx {
187193
let witness_tag = read_be_u8(reader).map_err(|_| anyhow!("Missing witness tag field."))?;
188194
ensure!(
189195
witness_tag == WITNESS_TAG,
190-
"Invalid witness tag, expected: {WITNESS_TAG}, provided: {witness_tag}",
196+
"Invalid witness tag, expected: {WITNESS_TAG}, \
197+
provided: {witness_tag}",
191198
);
192199

193200
// Skip nonce field

rust/catalyst-voting/src/vote_protocol/tally/mod.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,22 @@ pub fn tally(
5858
) -> anyhow::Result<EncryptedTally> {
5959
ensure!(
6060
votes.len() == voting_powers.len(),
61-
"Votes and voting power length mismatch. Votes amount: {0}. Voting powers amount: {1}.",
61+
"Votes and voting power length mismatch. Votes amount: {0}. \
62+
Voting powers amount: {1}.",
6263
votes.len(),
6364
voting_powers.len(),
6465
);
6566

66-
let mut ciphertexts_per_voting_option = Vec::new();
67-
for (i, vote) in votes.iter().enumerate() {
68-
let ciphertext = vote
69-
.get_ciphertext_for_choice(voting_option)
70-
.ok_or(anyhow!("Invalid encrypted vote at index {i}. Does not have a ciphertext for the voting option {voting_option}.") )?;
71-
ciphertexts_per_voting_option.push(ciphertext);
72-
}
67+
let ciphertexts_per_voting_option = votes
68+
.iter()
69+
.enumerate()
70+
.map(|(i, v)| {
71+
v.get_ciphertext_for_choice(voting_option).ok_or(anyhow!(
72+
"Invalid encrypted vote at index {i}. \
73+
Does not have a ciphertext for the voting option {voting_option}."
74+
))
75+
})
76+
.collect::<anyhow::Result<Vec<_>>>()?;
7377

7478
let zero_ciphertext = Ciphertext::zero();
7579

@@ -97,9 +101,21 @@ pub fn decrypt_tally(
97101
) -> anyhow::Result<u64> {
98102
let ge = decrypt(&tally_result.0, &secret_key.0);
99103

100-
let res = setup
101-
.discrete_log_setup
102-
.discrete_log(ge)
103-
.map_err(|_| anyhow!("Cannot decrypt tally result. Provided an invalid secret key or invalid encrypted tally result."))?;
104+
let res = setup.discrete_log_setup.discrete_log(ge).map_err(|_| {
105+
anyhow!(
106+
"Cannot decrypt tally result. \
107+
Provided an invalid secret key or invalid encrypted tally result."
108+
)
109+
})?;
104110
Ok(res)
105111
}
112+
113+
#[test]
114+
fn test() {
115+
let i = 10;
116+
let voting_option = 1;
117+
println!(
118+
"Invalid encrypted vote at index {i}. \
119+
Does not have a ciphertext for the voting option {voting_option}."
120+
);
121+
}

0 commit comments

Comments
 (0)