Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ members = [
]

[workspace.package]
version = "0.1.34"
version = "0.1.35"
edition = "2021"
license = "Apache-2.0"
authors = ["Junha Park <0xjunha@gmail.com>"]
Expand Down
2 changes: 1 addition & 1 deletion crypto/src/vrf/bandersnatch_vrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl RingVrfVerifier {
pub fn new(validator_set: &ValidatorKeySet) -> Result<Self, CryptoError> {
let ring = validator_set_to_bandersnatch_ring(validator_set)?;
Ok(Self {
core: RingVrfVerifierCore::new(ring),
core: RingVrfVerifierCore::new(ring)?,
})
}

Expand Down
47 changes: 25 additions & 22 deletions crypto/src/vrf/vrf_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub(crate) struct IetfVrfSignature {
// Additional impl
impl IetfVrfSignature {
pub(crate) fn output_hash(&self) -> [u8; 32] {
self.output.hash()[..32]
.try_into()
.expect("Should not fail; 32-byte array")
let mut out = [0u8; 32];
out.copy_from_slice(&self.output.hash()[..32]);
out
}
}

Expand All @@ -40,22 +40,27 @@ pub(crate) struct RingVrfSignature {
// Additional impl
impl RingVrfSignature {
pub(crate) fn output_hash(&self) -> [u8; 32] {
self.output.hash()[..32]
.try_into()
.expect("Should not fail; 32-byte array")
let mut out = [0u8; 32];
out.copy_from_slice(&self.output.hash()[..32]);
out
}
}

fn ring_proof_params() -> &'static RingProofParams {
fn ring_proof_params() -> Result<&'static RingProofParams, CryptoError> {
use std::sync::OnceLock;
static PARAMS: OnceLock<RingProofParams> = OnceLock::new();
PARAMS.get_or_init(|| {
static PARAMS: OnceLock<Result<RingProofParams, String>> = OnceLock::new();
let params = PARAMS.get_or_init(|| {
use bandersnatch::PcsParams;
let buf = include_bytes!("../../data/zcash-srs-2-11-uncompressed.bin");
let pcs_params = PcsParams::deserialize_uncompressed_unchecked(&mut &buf[..])
.expect("Failed to deserialize PCS params");
.map_err(|e| format!("Failed to deserialize PCS params: {e}"))?;
RingProofParams::from_pcs_params(RING_SIZE, pcs_params)
.expect("Failed to construct ring proof params from PCS params")
.map_err(|e| format!("Failed to construct ring proof params from PCS params: {e:?}"))
});

params.as_ref().map_err(|message| {
tracing::error!("{message}");
CryptoError::RingContextResourceError
})
}

Expand Down Expand Up @@ -147,7 +152,7 @@ impl RingVrfProverCore {
let pts: Vec<_> = self.ring.iter().map(|pk| pk.0).collect();

// Proof construction
let params = ring_proof_params();
let params = ring_proof_params()?;
let prover_key = params.prover_key(&pts);
let prover = params.prover(prover_key, self.prover_idx);
let proof = self.secret.prove(input, output, aux_data, &prover);
Expand Down Expand Up @@ -193,9 +198,8 @@ impl IetfVrfVerifierCore {
// `Y` hashed value; this is the actual value used as ticket-id/score
// NOTE: as far as vrf_input_data is the same, this matches the one produced
// using the ring-vrf (regardless of aux_data).
let vrf_output_hash: [u8; 32] = output.hash()[..32]
.try_into()
.expect("Should not fail; 32-byte array");
let mut vrf_output_hash = [0u8; 32];
vrf_output_hash.copy_from_slice(&output.hash()[..32]);
tracing::trace!("vrf-output-hash: {}", hex::encode(vrf_output_hash));
Ok(vrf_output_hash)
}
Expand All @@ -212,11 +216,11 @@ pub(crate) struct RingVrfVerifierCore {
}

impl RingVrfVerifierCore {
pub(crate) fn new(ring: Vec<Public>) -> Self {
pub(crate) fn new(ring: Vec<Public>) -> Result<Self, CryptoError> {
let pts: Vec<_> = ring.iter().map(|pk| pk.0).collect();
let verifier_key = ring_proof_params().verifier_key(&pts);
let verifier_key = ring_proof_params()?.verifier_key(&pts);
let commitment = verifier_key.commitment(); // The Ring Root
Self { ring, commitment }
Ok(Self { ring, commitment })
}

#[instrument(level = "debug", skip_all, name = "compute_ring_root")]
Expand Down Expand Up @@ -250,7 +254,7 @@ impl RingVrfVerifierCore {
let input = vrf_input_point(vrf_input_data)?;
let output = signature.output; // extracted from the signature

let params = ring_proof_params();
let params = ring_proof_params()?;

let verifier_key = params.verifier_key_from_commitment(self.commitment.clone());
let verifier = params.verifier(verifier_key);
Expand All @@ -261,9 +265,8 @@ impl RingVrfVerifierCore {
tracing::trace!("Ring signature verified");

// `Y` hashed value; the actual value used as ticket-id/score
let vrf_output_hash: [u8; 32] = output.hash()[..32]
.try_into()
.expect("Should not fail; 32-byte array");
let mut vrf_output_hash = [0u8; 32];
vrf_output_hash.copy_from_slice(&output.hash()[..32]);
tracing::trace!("vrf-output-hash: {}", hex::encode(vrf_output_hash));
Ok(vrf_output_hash)
}
Expand Down
1 change: 1 addition & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fr-node = { workspace = true }
fr-state = { workspace = true }
fr-storage = { workspace = true }
fr-test-utils = { workspace = true }
futures = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
Expand Down
Loading