Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2624 from danielSanchezQ/jcli-tally-decrypt
Jcli tally decrypt with shares tool
- Loading branch information
Showing
4 changed files
with
101 additions
and
7 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
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,71 @@ | ||
use super::Error; | ||
use crate::jcli_app::utils::{io, OutputFormat}; | ||
use chain_vote::EncryptedTally; | ||
use serde::Serialize; | ||
use std::io::BufRead; | ||
use std::path::PathBuf; | ||
use structopt::StructOpt; | ||
|
||
#[derive(StructOpt)] | ||
#[structopt(rename_all = "kebab-case")] | ||
pub struct TallyDecryptWithAllShares { | ||
/// The path to hex-encoded encrypted tally state. If this parameter is not | ||
/// specified, the encrypted tally state will be read from the standard | ||
/// input. | ||
#[structopt(long = "tally")] | ||
encrypted_tally: Option<PathBuf>, | ||
/// The minimum number of shares needed for decryption | ||
#[structopt(long = "threshold", default_value = "3")] | ||
threshold: usize, | ||
/// Maximum supported number of votes | ||
#[structopt(long = "maxvotes")] | ||
max_votes: u64, | ||
/// Computing table cache size, usually total_votes/number_of_options | ||
#[structopt(long = "table_size")] | ||
table_size: usize, | ||
/// The path to encoded necessary shares. If this parameter is not | ||
/// specified, the shares will be read from the standard input. | ||
#[structopt(long = "shares")] | ||
shares: Option<PathBuf>, | ||
#[structopt(flatten)] | ||
output_format: OutputFormat, | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct Output { | ||
result: Vec<Option<u64>>, | ||
} | ||
|
||
impl TallyDecryptWithAllShares { | ||
pub fn exec(&self) -> Result<(), Error> { | ||
let encrypted_tally_hex = io::read_line(&self.encrypted_tally)?; | ||
let encrypted_tally_bytes = hex::decode(encrypted_tally_hex)?; | ||
let encrypted_tally = | ||
EncryptedTally::from_bytes(&encrypted_tally_bytes).ok_or(Error::EncryptedTallyRead)?; | ||
|
||
let mut shares_file = io::open_file_read(&self.shares)?; | ||
|
||
let shares: Vec<chain_vote::TallyDecryptShare> = { | ||
let mut shares = Vec::with_capacity(self.threshold); | ||
for _ in 0..self.threshold { | ||
let mut buff = String::new(); | ||
&shares_file.read_line(&mut buff); | ||
chain_vote::TallyDecryptShare::from_bytes(&hex::decode(buff)?) | ||
.ok_or(Error::DecryptionShareRead)?; | ||
} | ||
shares | ||
}; | ||
|
||
let state = encrypted_tally.state(); | ||
let result = chain_vote::result(self.max_votes, self.table_size, &state, &shares); | ||
let output = self | ||
.output_format | ||
.format_json(serde_json::to_value(Output { | ||
result: result.votes, | ||
})?)?; | ||
|
||
println!("{}", output); | ||
|
||
Ok(()) | ||
} | ||
} |
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
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,23 @@ | ||
mod decrypt_shares; | ||
mod decryption_tally; | ||
|
||
use super::Error; | ||
use structopt::StructOpt; | ||
|
||
#[derive(StructOpt)] | ||
#[structopt(rename_all = "kebab-case")] | ||
pub enum Tally { | ||
/// Create decryption share for private voting tally. | ||
GenerateDecryptionShare(decryption_tally::TallyGenerateDecryptionShare), | ||
/// Decrypt a tally with shares | ||
DecryptWithShares(decrypt_shares::TallyDecryptWithAllShares), | ||
} | ||
|
||
impl Tally { | ||
pub fn exec(self) -> Result<(), Error> { | ||
match self { | ||
Tally::GenerateDecryptionShare(cmd) => cmd.exec(), | ||
Tally::DecryptWithShares(cmd) => cmd.exec(), | ||
} | ||
} | ||
} |