Skip to content

Commit 5c45c45

Browse files
committed
feat(rug): fractional powers without roots
1 parent cb98492 commit 5c45c45

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/audit/src/offline/bin/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub struct Args {
4040
/// Gamma value for Quadratic scaling
4141
#[clap(short, long)]
4242
gamma: Option<String>,
43+
/// Rounding precision for arithmetic
44+
#[clap(short, long)]
45+
precision: Option<String>,
4346
}
4447

4548
fn main() -> Result<(), Box<dyn Error>> {
@@ -70,6 +73,11 @@ fn main() -> Result<(), Box<dyn Error>> {
7073
std::env::set_var(GAMMA, gamma);
7174
}
7275

76+
if let Some(precision) = args.precision {
77+
const PRECISION: &str = "QUADRATIC_VOTING_PRECISION";
78+
std::env::set_var(PRECISION, precision);
79+
}
80+
7381
// Load and replay fund fragments from storage
7482
let storage_path = PathBuf::from(args.fragments);
7583

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,14 @@ impl TallyResult {
191191
let numer = gamma.numer();
192192

193193
let stake = Float::with_val(precision, weight.0);
194-
// r = gamma in rational form
195-
let exp_r = Rational::from((*numer, *denom));
196-
let exp_f = Float::with_val(precision, &exp_r);
197-
let p = stake.clone().pow(&exp_f);
198194

199-
let weight = p
195+
// rational = gamma in rational form i.e fraction
196+
// 0.5 = 1/2
197+
let gamma = Float::with_val(precision, &Rational::from((*numer, *denom)));
198+
199+
let stake_with_gamma_scaling = stake.clone().pow(&gamma);
200+
201+
let weight = stake_with_gamma_scaling
200202
.to_integer_round(Round::Down)
201203
.unwrap_or((Integer::from(weight.0), Ordering::Less))
202204
.0

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,18 @@ impl EncryptedTally {
183183
let numer = gamma.numer();
184184

185185
let stake = Float::with_val(precision, weight);
186-
// r = gamma in rational form
187-
let exp_r = Rational::from((*numer, *denom));
188-
let exp_f = Float::with_val(precision, &exp_r);
189-
let p = stake.clone().pow(&exp_f);
190-
info!("{} to the power of {} is: {}", stake, exp_f, p);
191186

192-
let weight = p
187+
// rational = gamma in rational form i.e fraction
188+
// 0.5 = 1/2
189+
let gamma = Float::with_val(precision, &Rational::from((*numer, *denom)));
190+
191+
let stake_with_gamma_scaling = stake.clone().pow(&gamma);
192+
info!(
193+
"{} to the power of {} is: {}",
194+
stake, gamma, stake_with_gamma_scaling
195+
);
196+
197+
let weight = stake_with_gamma_scaling
193198
.to_integer_round(Round::Down)
194199
.unwrap_or((Integer::from(weight), Ordering::Less))
195200
.0

0 commit comments

Comments
 (0)