Skip to content

Commit f4a622e

Browse files
committed
Fix accidental transition to big-endian with rust-kzg upgrade
1 parent 724f9ca commit f4a622e

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

Cargo.lock

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

crates/subspace-core-primitives/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ bench = false
1717

1818
[dependencies]
1919
blake3 = { version = "1.5.3", default-features = false }
20+
# TODO: Remove once we switch to big-endian
21+
blst = "0.3.13"
2022
bytes = { version = "1.7.1", default-features = false }
2123
derive_more = { version = "1.0.0", default-features = false, features = ["full"] }
2224
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }

crates/subspace-core-primitives/src/crypto.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ extern crate alloc;
2121
pub mod kzg;
2222

2323
use crate::Blake3Hash;
24-
use ::kzg::Fr;
2524
#[cfg(not(feature = "std"))]
2625
use alloc::format;
2726
#[cfg(not(feature = "std"))]
@@ -207,14 +206,36 @@ impl TryFrom<[u8; Self::FULL_BYTES]> for Scalar {
207206

208207
#[inline]
209208
fn try_from(value: [u8; Self::FULL_BYTES]) -> Result<Self, Self::Error> {
210-
FsFr::from_bytes(&value).map(Scalar)
209+
// TODO: The whole method should have been just the following line, but upstream `rust-kzg`
210+
// switched to big-endian and we have to maintain little-endian version for now
211+
// FsFr::from_bytes(&value).map(Scalar)
212+
let mut bls_scalar = blst::blst_scalar::default();
213+
let mut fr = blst::blst_fr::default();
214+
unsafe {
215+
blst::blst_scalar_from_lendian(&mut bls_scalar, value.as_ptr());
216+
if !blst::blst_scalar_fr_check(&bls_scalar) {
217+
return Err("Invalid scalar".to_string());
218+
}
219+
blst::blst_fr_from_scalar(&mut fr, &bls_scalar);
220+
}
221+
Ok(Self(FsFr(fr)))
211222
}
212223
}
213224

214225
impl From<&Scalar> for [u8; Scalar::FULL_BYTES] {
215226
#[inline]
216227
fn from(value: &Scalar) -> Self {
217-
value.0.to_bytes()
228+
// TODO: The whole method should have been just the following line, but upstream `rust-kzg`
229+
// switched to big-endian and we have to maintain little-endian version for now
230+
// value.0.to_bytes()
231+
let mut scalar = blst::blst_scalar::default();
232+
let mut bytes = [0u8; 32];
233+
unsafe {
234+
blst::blst_scalar_from_fr(&mut scalar, &value.0 .0);
235+
blst::blst_lendian_from_scalar(bytes.as_mut_ptr(), &scalar);
236+
}
237+
238+
bytes
218239
}
219240
}
220241

crates/subspace-core-primitives/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub type SlotNumber = u64;
164164
pub type SolutionRange = u64;
165165

166166
/// Computes the following:
167-
/// ```
167+
/// ```text
168168
/// MAX * slot_probability / (pieces_in_sector * chunks / s_buckets) / sectors
169169
/// ```
170170
pub const fn sectors_to_solution_range(
@@ -183,7 +183,7 @@ pub const fn sectors_to_solution_range(
183183
}
184184

185185
/// Computes the following:
186-
/// ```
186+
/// ```text
187187
/// MAX * slot_probability / (pieces_in_sector * chunks / s_buckets) / solution_range
188188
/// ```
189189
pub const fn solution_range_to_sectors(

0 commit comments

Comments
 (0)