Skip to content

Commit 2b4965f

Browse files
committed
feat(quadratic voting): alternative
1 parent 5312962 commit 2b4965f

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

Cargo.lock

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/chain-libs/chain-impl-mockchain/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ criterion = { version = "0.3.0", optional = true }
3232
rand = "0.8"
3333
cryptoxide = "0.4"
3434
tracing.workspace = true
35+
num-integer = "0.1"
36+
3537

3638
[features]
3739
property-test-api = [

src/chain-libs/chain-impl-mockchain/src/vote/tally.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
vote::{Choice, Options},
55
};
66
use chain_vote::EncryptedTally;
7+
use num_integer::Roots;
78
use std::fmt;
89
use thiserror::Error;
910

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

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

171176
Ok(())
172177
}

src/chain-libs/chain-vote/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ thiserror = "1.0"
1414
cryptoxide = "^0.4.2"
1515
const_format = "0.2"
1616
base64 = "0.21.0"
17+
tracing = "0.1"
18+
num-integer = "0.1"
19+
1720

1821
[dev-dependencies]
1922
rand_chacha = "0.3"

src/chain-libs/chain-vote/src/tally.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::{
1111
use base64::{engine::general_purpose, Engine as _};
1212
use cryptoxide::blake2b::Blake2b;
1313
use cryptoxide::digest::Digest;
14+
use num_integer::Roots;
15+
1416
use rand_core::{CryptoRng, RngCore};
1517

1618
/// Secret key for opening vote
@@ -155,10 +157,15 @@ impl EncryptedTally {
155157
pub fn add(&mut self, ballot: &Ballot, weight: u64) {
156158
assert_eq!(ballot.vote().len(), self.r.len());
157159
assert_eq!(ballot.fingerprint(), &self.fingerprint);
160+
161+
// Returns the truncated principal square root of an integer – ⌊√x⌋
162+
// This is solving for r in r² = x, rounding toward zero. The result will satisfy r² ≤ x < (r+1)²
163+
let weight_gammad = weight.sqrt();
164+
158165
for (ri, ci) in self.r.iter_mut().zip(ballot.vote().iter()) {
159-
*ri = &*ri + &(ci * weight);
166+
*ri = &*ri + &(ci * weight_gammad);
160167
}
161-
self.max_stake += weight;
168+
self.max_stake += weight_gammad;
162169
}
163170

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

0 commit comments

Comments
 (0)