Skip to content

Commit

Permalink
Use a Vec for the Monero generators, preventing its massive stack usage
Browse files Browse the repository at this point in the history
The amount of stack usage did cause issues on m1 computers.
  • Loading branch information
kayabaNerve committed Sep 20, 2023
1 parent 98ab6ac commit c62d9b4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
4 changes: 2 additions & 2 deletions coins/monero/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ fn generators(prefix: &'static str, path: &str) {
pub(crate) static GENERATORS_CELL: OnceLock<Generators> = OnceLock::new();
pub fn GENERATORS() -> &'static Generators {{
GENERATORS_CELL.get_or_init(|| Generators {{
G: [
G: vec![
{G_str}
],
H: [
H: vec![
{H_str}
],
}})
Expand Down
13 changes: 6 additions & 7 deletions coins/monero/generators/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use std_shims::sync::OnceLock;
use std_shims::{sync::OnceLock, vec::Vec};

use sha3::{Digest, Keccak256};

Expand Down Expand Up @@ -56,14 +56,13 @@ const MAX_MN: usize = MAX_M * N;
/// Container struct for Bulletproofs(+) generators.
#[allow(non_snake_case)]
pub struct Generators {
pub G: [EdwardsPoint; MAX_MN],
pub H: [EdwardsPoint; MAX_MN],
pub G: Vec<EdwardsPoint>,
pub H: Vec<EdwardsPoint>,
}

/// Generate generators as needed for Bulletproofs(+), as Monero does.
pub fn bulletproofs_generators(dst: &'static [u8]) -> Generators {
let mut res =
Generators { G: [EdwardsPoint::identity(); MAX_MN], H: [EdwardsPoint::identity(); MAX_MN] };
let mut res = Generators { G: Vec::with_capacity(MAX_MN), H: Vec::with_capacity(MAX_MN) };
for i in 0 .. MAX_MN {
let i = 2 * i;

Expand All @@ -73,8 +72,8 @@ pub fn bulletproofs_generators(dst: &'static [u8]) -> Generators {

write_varint(&i.try_into().unwrap(), &mut even).unwrap();
write_varint(&(i + 1).try_into().unwrap(), &mut odd).unwrap();
res.H[i / 2] = EdwardsPoint(hash_to_point(hash(&even)));
res.G[i / 2] = EdwardsPoint(hash_to_point(hash(&odd)));
res.H.push(EdwardsPoint(hash_to_point(hash(&even))));
res.G.push(EdwardsPoint(hash_to_point(hash(&odd))));
}
res
}

0 comments on commit c62d9b4

Please sign in to comment.