Skip to content

Commit

Permalink
feat(quadratic voting): alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
cong-or committed Jan 15, 2025
1 parent 5312962 commit 2b4965f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

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

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
num-integer = "0.1"


[features]
property-test-api = [
Expand Down
7 changes: 6 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 @@ -4,6 +4,7 @@ use crate::{
vote::{Choice, Options},
};
use chain_vote::EncryptedTally;
use num_integer::Roots;
use std::fmt;
use thiserror::Error;

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

self.results[index] = self.results[index].saturating_add(weight);
// Returns the truncated principal square root of an integer – ⌊√x⌋
// This is solving for r in r² = x, rounding toward zero. The result will satisfy r² ≤ x < (r+1)²
let weight_gammad = weight.0.sqrt();

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

Ok(())
}
Expand Down
3 changes: 3 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,9 @@ thiserror = "1.0"
cryptoxide = "^0.4.2"
const_format = "0.2"
base64 = "0.21.0"
tracing = "0.1"
num-integer = "0.1"


[dev-dependencies]
rand_chacha = "0.3"
Expand Down
11 changes: 9 additions & 2 deletions src/chain-libs/chain-vote/src/tally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::{
use base64::{engine::general_purpose, Engine as _};
use cryptoxide::blake2b::Blake2b;
use cryptoxide::digest::Digest;
use num_integer::Roots;

use rand_core::{CryptoRng, RngCore};

/// Secret key for opening vote
Expand Down Expand Up @@ -155,10 +157,15 @@ 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);

// Returns the truncated principal square root of an integer – ⌊√x⌋
// This is solving for r in r² = x, rounding toward zero. The result will satisfy r² ≤ x < (r+1)²
let weight_gammad = weight.sqrt();

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

/// Given a single committee member's `secret_key`, returns a partial decryption of
Expand Down

0 comments on commit 2b4965f

Please sign in to comment.