Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/audit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ FRAGMENTS_STORAGE=/tmp/fund9-leader-1/persist/leader-1

```


*Generate encrypted tally with gamma scaling*


```bash

BLOCK0=/tmp/fund9-leader-1/artifacts/block0.bin
FRAGMENTS_STORAGE=/tmp/fund9-leader-1/persist/leader-1
GAMMA=0.1
PRECISION=5

./target/release/offline --fragments $FRAGMENTS_STORAGE --block0 $BLOCK0 --gamma $GAMMA --precision $PRECISION

```

This will create three files:
- *ledger_after_tally.json* **(decrypted ledger state after tally)** *should match official results!*
- *ledger_before_tally.json* **(encrypted ledger state before tally)**
Expand Down
17 changes: 17 additions & 0 deletions src/audit/src/offline/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use lib::offline::{

use chain_core::packer::Codec;
use color_eyre::{eyre::Context, Report};
use std::env;
use std::{error::Error, path::PathBuf};

///
Expand All @@ -36,6 +37,12 @@ pub struct Args {
/// cross reference official results
#[clap(short, long)]
official_results: Option<String>,
/// Gamma value for Quadratic scaling
#[clap(short, long)]
gamma: Option<String>,
/// Rounding precision for arithmetic
#[clap(short, long)]
precision: Option<String>,
}

fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -61,6 +68,16 @@ fn main() -> Result<(), Box<dyn Error>> {
info!("Audit Tool.");
info!("Starting Offline Tally");

if let Some(gamma) = args.gamma {
const GAMMA: &str = "QUADRATIC_VOTING_GAMMA";
std::env::set_var(GAMMA, gamma);
}

if let Some(precision) = args.precision {
const PRECISION: &str = "QUADRATIC_VOTING_PRECISION";
std::env::set_var(PRECISION, precision);
}

// Load and replay fund fragments from storage
let storage_path = PathBuf::from(args.fragments);

Expand Down
2 changes: 2 additions & 0 deletions src/chain-libs/chain-impl-mockchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ criterion = { version = "0.3.0", optional = true }
rand = "0.8"
cryptoxide = "0.4"
tracing.workspace = true
bigdecimal = "0.4.7"


[features]
property-test-api = [
Expand Down
30 changes: 29 additions & 1 deletion src/chain-libs/chain-impl-mockchain/src/vote/tally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ use crate::{
value::Value,
vote::{Choice, Options},
};
use bigdecimal::BigDecimal;
use bigdecimal::ToPrimitive;
use chain_vote::EncryptedTally;
use std::num::NonZeroI64;

use std::str::FromStr;

use std::env;
use std::fmt;
use thiserror::Error;

Expand Down Expand Up @@ -166,7 +173,28 @@ impl TallyResult {
} else {
let index = choice.as_byte() as usize;

self.results[index] = self.results[index].saturating_add(weight);
const GAMMA: &str = "QUADRATIC_VOTING_GAMMA";
const PRECISION: &str = "QUADRATIC_VOTING_PRECISION";

// Apply quadratic scaling if gamma value specified in env var. Else gamma is 1 and has no effect.
let gamma = env::var(GAMMA).unwrap_or(1.to_string());

let precision =
i64::from_str(&env::var(PRECISION).unwrap_or(1.to_string())).unwrap_or(1);

let mut gamma = BigDecimal::from_str(&gamma).unwrap_or(BigDecimal::from(1));
// Gamma must be between 0 and 1, anything else is treated as bad input; defaulting gamma to 1.
if gamma < BigDecimal::from(0) || gamma > BigDecimal::from(1) {
gamma = BigDecimal::from(1);
}
let stake = BigDecimal::from(weight.0);

let weight = (gamma * stake)
.round(precision)
.to_u64()
.unwrap_or(weight.0);

self.results[index] = self.results[index].saturating_add(Weight(weight));

Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions src/chain-libs/chain-vote/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ thiserror = "1.0"
cryptoxide = "^0.4.2"
const_format = "0.2"
base64 = "0.21.0"
bigdecimal = "0.4.7"


[dev-dependencies]
rand_chacha = "0.3"
Expand Down
26 changes: 26 additions & 0 deletions src/chain-libs/chain-vote/src/tally.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::num::NonZeroI64;
use std::num::NonZeroU64;

use std::str::FromStr;

use crate::GroupElement;
use crate::{
committee::*,
Expand All @@ -9,8 +12,14 @@ use crate::{
TallyOptimizationTable,
};
use base64::{engine::general_purpose, Engine as _};

use bigdecimal::BigDecimal;
use bigdecimal::ToPrimitive;
use cryptoxide::blake2b::Blake2b;
use cryptoxide::digest::Digest;

use std::env;

use rand_core::{CryptoRng, RngCore};

/// Secret key for opening vote
Expand Down Expand Up @@ -155,6 +164,23 @@ impl EncryptedTally {
pub fn add(&mut self, ballot: &Ballot, weight: u64) {
assert_eq!(ballot.vote().len(), self.r.len());
assert_eq!(ballot.fingerprint(), &self.fingerprint);

const GAMMA: &str = "QUADRATIC_VOTING_GAMMA";
const PRECISION: &str = "QUADRATIC_VOTING_PRECISION";

// Apply quadratic scaling if gamma value specified in env var. Else gamma is 1 and has no effect.
let gamma = env::var(GAMMA).unwrap_or(1.to_string());
let precision = i64::from_str(&env::var(PRECISION).unwrap_or(1.to_string())).unwrap_or(1);

let mut gamma = BigDecimal::from_str(&gamma).unwrap_or(BigDecimal::from(1));
// Gamma must be between 0 and 1, anything else is treated as bad input; defaulting gamma to 1.
if gamma < BigDecimal::from(0) || gamma > BigDecimal::from(1) {
gamma = BigDecimal::from(1);
}
let stake = BigDecimal::from(weight);

let weight = (gamma * stake).round(precision).to_u64().unwrap_or(weight);

for (ri, ci) in self.r.iter_mut().zip(ballot.vote().iter()) {
*ri = &*ri + &(ci * weight);
}
Expand Down
Loading