Skip to content

Commit 58fe098

Browse files
committed
VRF: do not use num directly but use vendor
1 parent 82fa4e3 commit 58fe098

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

vrf/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use ledger::AccountIndex;
44
use message::VrfMessage;
55
use mina_node_account::AccountPublicKey;
66
use mina_p2p_messages::v2::EpochSeed;
7-
use num::{rational::Ratio, BigInt, ToPrimitive};
7+
use num_bigint_generic::BigInt;
8+
use num_rational_generic::Ratio;
9+
use num_traits::ToPrimitive;
810
use output::VrfOutput;
911
use serde::{Deserialize, Serialize};
1012
use thiserror::Error;
@@ -28,7 +30,7 @@ pub enum VrfError {
2830
HexDecodeError(#[from] hex::FromHexError),
2931

3032
#[error("Failed to parse decimal big integer from string: {0}")]
31-
BigIntParseError(#[from] num::bigint::ParseBigIntError),
33+
BigIntParseError(#[from] num_bigint_generic::ParseBigIntError),
3234

3335
#[error("Field conversion error: {0}")]
3436
FieldHelpersError(#[from] o1_utils::field_helpers::FieldHelpersError),
@@ -175,7 +177,7 @@ mod test {
175177
use std::str::FromStr;
176178

177179
use ledger::AccountIndex;
178-
use num::BigInt;
180+
use num_bigint_generic::BigInt;
179181

180182
use mina_node_account::AccountSecretKey;
181183
use mina_p2p_messages::{

vrf/src/output.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use poseidon::hash::params::MINA_VRF_OUTPUT;
99
use serde::{Deserialize, Serialize};
1010
use sha2::{Digest, Sha256};
1111

12-
use crate::{BaseField, BigInt2048, ScalarField};
12+
use crate::{BaseField, BigInt2048, BigRational2048, ScalarField};
1313

1414
use super::serialize::{ark_deserialize, ark_serialize};
1515

@@ -88,14 +88,14 @@ impl VrfOutput {
8888
// ocaml: Bignum_bigint.(shift_left one length_in_bits))
8989
// where: length_in_bits = Int.min 256 (Field.size_in_bits - 2)
9090
// Field.size_in_bits = 255
91-
let two_tpo_256 = BigInt::one() << 253u32;
91+
let two_tpo_256 = BigInt2048::one() << 253u32;
9292

9393
let vrf_out: BigInt2048 = BigInt2048::from_bytes_be(
9494
num_bigint_generic::Sign::Plus,
9595
&self.truncated().into_bigint().to_bytes_be(),
9696
);
9797

98-
BigRational::new(vrf_out, two_tpo_256).to_f64().unwrap()
98+
BigRational2048::new(vrf_out, two_tpo_256).to_f64().unwrap()
9999
}
100100

101101
pub fn to_base_58(&self) -> String {

vrf/src/threshold.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use ark_ff::{self, BigInteger, BigInteger256, One, Zero};
22
use itertools::unfold;
3-
use num::{rational::Ratio, BigInt, FromPrimitive, Signed};
3+
use num_bigint_generic::{BigInt, Sign};
4+
use num_rational_generic::Ratio;
5+
use num_traits::{FromPrimitive, Signed};
46

57
use crate::{BigInt2048, BigInt256, BigInt4096, BigRational2048, BigRational4096};
68

@@ -20,7 +22,10 @@ impl Threshold {
2022
// 1. set up parameters to calculate threshold
2123
// Note: IMO all these parameters can be represented as constants. They do not change. The calculation is most likely in the
2224
// code to adjust them in the future. We could create an utility that generates these params using f and log terms
23-
let f = BigRational2048::new(BigInt::from_u8(3).unwrap(), BigInt::from_u8(4).unwrap());
25+
let f = BigRational2048::new(
26+
BigInt2048::from_u8(3).unwrap(),
27+
BigInt2048::from_u8(4).unwrap(),
28+
);
2429

2530
let base = BigRational2048::one() - f;
2631

@@ -29,15 +34,15 @@ impl Threshold {
2934
let (per_term_precission, terms_needed, _) = Self::bit_params(&abs_log_base);
3035

3136
let terms_needed: i32 = terms_needed.try_into().unwrap();
32-
let mut linear_term_integer_part = BigInt::zero();
37+
let mut linear_term_integer_part = BigInt4096::zero();
3338

3439
let abs_log_base: BigRational4096 = abs_log_base.to_nlimbs::<64>();
3540

3641
let coefficients = (1..terms_needed).map(|x| {
3742
let c = abs_log_base.pow(x) / Self::factorial(x.into());
3843
let c_frac = if x == 1 {
39-
let c_whole = c.to_integer();
40-
let c_frac = c - bigint_to_bigrational(&c_whole);
44+
let c_whole: BigInt4096 = c.to_integer();
45+
let c_frac = c - bigint_to_bigrational::<64>(&c_whole);
4146
linear_term_integer_part = c_whole;
4247
c_frac
4348
} else {
@@ -62,7 +67,7 @@ impl Threshold {
6267
let input =
6368
BigRational4096::new(numer.to_nlimbs(), two_tpo_per_term_precission.to_nlimbs());
6469

65-
let denom = BigInt::one() << per_term_precission;
70+
let denom = BigInt2048::one() << per_term_precission;
6671

6772
let (res, _) = coefficients.into_iter().fold(
6873
(BigRational4096::zero(), BigRational4096::one()),
@@ -90,8 +95,8 @@ impl Threshold {
9095
}
9196

9297
fn terms_needed(log_base: &BigRational2048, bits_of_precission: u32) -> i32 {
93-
let two = BigInt4096::one() + BigInt::one();
94-
let lower_bound = bigint_to_bigrational(&two.pow(bits_of_precission));
98+
let two: BigInt4096 = BigInt4096::one() + BigInt4096::one();
99+
let lower_bound: BigRational4096 = bigint_to_bigrational(&two.pow(bits_of_precission));
95100

96101
let mut n = 0;
97102
let log_base: BigRational4096 = log_base.to_nlimbs();
@@ -190,7 +195,7 @@ pub fn get_fractional(vrf_out: BigInteger256) -> Ratio<BigInt2048> {
190195
// Field.size_in_bits = 255
191196
let two_tpo_256 = BigInt2048::one() << 253u32;
192197

193-
let vrf_out = BigInt2048::from_bytes_be(num::bigint::Sign::Plus, &vrf_out.to_bytes_be());
198+
let vrf_out = BigInt2048::from_bytes_be(Sign::Plus, &vrf_out.to_bytes_be());
194199

195200
Ratio::new(vrf_out, two_tpo_256)
196201
}

0 commit comments

Comments
 (0)