Skip to content

Commit

Permalink
Merge pull request #2394 from dusk-network/crs
Browse files Browse the repository at this point in the history
Download CRS from URL
  • Loading branch information
xevisalle authored Sep 17, 2024
2 parents 63237db + 07a276e commit d61d225
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 21 deletions.
1 change: 1 addition & 0 deletions contracts/license/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Changed 'use_license' to check if license already nullified [#1051]
- Changed 'get_licenses' to return values by adding 'pos' to every license returned [#1040]
- Changed 'issue_license' by removing the 'pos' argument and self position determination [#1039]
- Fixed `license` tests error when creating `PublicParameters` from a slice

## [0.1.0] - 2023-07-13

Expand Down
3 changes: 2 additions & 1 deletion contracts/license/tests/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ fn use_license_get_session() {
let rng = &mut StdRng::seed_from_u64(0xbeef);

let crs = get_common_reference_string().expect("getting CRS file works");
let pp = unsafe { PublicParameters::from_slice_unchecked(crs.as_slice()) };
let pp = PublicParameters::from_slice(crs.as_slice())
.expect("PublicParameters failed to be created from slice.");

let (prover, verifier) = Compiler::compile::<LicenseCircuit>(&pp, LABEL)
.expect("Compiling circuit should succeed");
Expand Down
7 changes: 4 additions & 3 deletions rusk-profile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ pub use theme::Theme;
mod circuit;
pub use circuit::Circuit;

/// HEX representaion of the SHA-256 hash of the CRS uncompressed bytes.
/// HEX representation of the SHA-256 hash of the CRS uncompressed bytes.
/// This is the hash of the contribution number 15 of the Dusk Trusted Setup.
pub static CRS_17_HASH: &str =
"18b48f588fd4d1e88ef9e7b3cacfa29046f6f489c5c237a4b01ee4f0334772a5";
"6161605616b62356cf09fa28252c672ef53b2c8489ad5f81d87af26e105f6059";

const CRS_FNAME: &str = "dev-piecrust.crs";
const CRS_FNAME: &str = "devnet-piecrust.crs";

fn extension(p: &Path) -> Option<&str> {
p.extension()?.to_str()
Expand Down
2 changes: 2 additions & 0 deletions rusk-recovery/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Removed 'phoenix-core' dependency [#1139]
- Added closure argument to the 'deploy' method [#1630]
- Update Plonk to 0.20
- Download CRS from URL

### Removed

Expand Down
5 changes: 4 additions & 1 deletion rusk-recovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ path = "src/lib.rs"

[dependencies]
dusk-bytes = "0.1"
dusk-plonk = { version = "0.19", features = ["rkyv-impl"] }
dusk-plonk = { version = "0.20", features = ["rkyv-impl"] }
hex = "0.4"
rand = "0.8"
once_cell = "1.13"
Expand All @@ -24,6 +24,9 @@ url = "2.3"
flate2 = "1"
tar = "0.4"
cargo_toml = "0.15"
reqwest = "0.12"
tokio = { version = "1", features = ["full"] }
lazy_static = "1.5"

license-circuits = { version = "0.1", path = "../circuits/license" }

Expand Down
62 changes: 48 additions & 14 deletions rusk-recovery/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,54 @@ use execution_core::{
transfer::phoenix::TRANSCRIPT_LABEL,
};
use once_cell::sync::Lazy;
use rand::rngs::StdRng;
use rand::SeedableRng;
use std::io;
use std::{
io,
sync::{mpsc, Mutex},
thread,
};

use rusk_profile::Circuit as CircuitProfile;

use lazy_static::lazy_static;
use tracing::{info, warn};

mod circuits;

lazy_static! {
static ref CRS_URL: Mutex<String> = Mutex::new(String::default());
}

static PUB_PARAMS: Lazy<PublicParameters> = Lazy::new(|| {
let theme = Theme::default();
info!("{} CRS from cache", theme.action("Fetching"));
match rusk_profile::get_common_reference_string() {
Ok(buff) if rusk_profile::verify_common_reference_string(&buff) => unsafe {
let pp = PublicParameters::from_slice_unchecked(&buff[..]);
Ok(buff) if rusk_profile::verify_common_reference_string(&buff) => {
let pp = PublicParameters::from_slice(&buff[..])
.expect("Creating PublicParameters from slice failed.");
info!("{} CRS", theme.info("Loaded"));
pp
},
}

_ => {
warn!("{} new CRS due to cache miss", theme.warn("Building"));
let mut rng = StdRng::seed_from_u64(0xbeef);

let pp = PublicParameters::setup(1 << 17, &mut rng)
.expect("Cannot initialize Public Parameters");

rusk_profile::set_common_reference_string(pp.to_raw_var_bytes())
warn!(
"{} CRS from server due to cache miss",
theme.warn("Fetching")
);
let (tx, rx) = mpsc::channel();

thread::spawn(move || {
let pp_bytes =
fetch_pp().expect("PublicParameters download failed.");
tx.send(pp_bytes).unwrap();
})
.join()
.expect("PublicParameters download thread panicked");

let pp_bytes = rx.recv().unwrap();
let pp = PublicParameters::from_slice(pp_bytes.as_slice())
.expect("Creating PublicParameters from slice failed.");

rusk_profile::set_common_reference_string(pp_bytes)
.expect("Unable to write the CRS");

info!("{} CRS", theme.info("Cached"));
Expand All @@ -47,6 +67,15 @@ static PUB_PARAMS: Lazy<PublicParameters> = Lazy::new(|| {
}
});

#[tokio::main]
async fn fetch_pp() -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let crs_url = CRS_URL.lock().expect("Unlocking failed.").to_string();

let response = reqwest::get(crs_url).await?.bytes().await?;

Ok(response.to_vec())
}

fn check_circuits_cache(
circuit_list: Vec<CircuitProfile>,
) -> Result<(), io::Error> {
Expand Down Expand Up @@ -116,7 +145,12 @@ fn run_stored_circuits_checks(
check_circuits_cache(circuit_list).map(|_| ())
}

pub fn exec(keep_circuits: bool) -> Result<(), Box<dyn std::error::Error>> {
pub fn exec(
keep_circuits: bool,
crs_url: String,
) -> Result<(), Box<dyn std::error::Error>> {
*CRS_URL.lock().expect("Unlocking failed.") = crs_url;

// This force init is needed to check CRS and create it (if not available)
// See also: https://github.com/dusk-network/rusk/issues/767
Lazy::force(&PUB_PARAMS);
Expand Down
1 change: 1 addition & 0 deletions rusk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add TLS support for HTTP server
- Add iteration generator to FailedIterations [#1257]
- Add `node` feature flag [#1144]
- Add `RUSK_CRS_URL` environment variable

### Changed

Expand Down
12 changes: 10 additions & 2 deletions rusk/src/bin/args/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ pub enum Command {
/// Keeps untracked keys
#[clap(short, long, value_parser = BoolishValueParser::new(), env = "RUSK_KEEP_KEYS")]
keep: bool,

/// URL of the server to download the CRS from
#[clap(
long,
default_value = "https://nodes.dusk.network/trusted-setup",
env = "RUSK_CRS_URL"
)]
crs_url: String,
},

#[cfg(feature = "recovery-state")]
Expand Down Expand Up @@ -64,8 +72,8 @@ impl Command {
output,
} => super::state::recovery_state(init, force, output),
#[cfg(feature = "recovery-keys")]
Self::RecoveryKeys { keep } => {
rusk_recovery_tools::keys::exec(keep)
Self::RecoveryKeys { keep, crs_url } => {
rusk_recovery_tools::keys::exec(keep, crs_url)
}
};

Expand Down

0 comments on commit d61d225

Please sign in to comment.