Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Aggregation debugging #191

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
30 changes: 30 additions & 0 deletions ferveo/gistfile1.csv

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ferveo/gistfile1.txt

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions ferveo/parse.py

Large diffs are not rendered by default.

117 changes: 117 additions & 0 deletions ferveo/src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use core::str::FromStr;
use std::{
fs::File,
io::{self, BufRead},
path::PathBuf,
};

use hex::FromHex;
use crate::api::{PublicKey, Transcript, AggregatedTranscript};
use ferveo_common::FromBytes;
use crate::EthereumAddress;
use crate::pvss::aggregate;

#[derive(Debug)]
struct ValidatorTranscript{
validator_address: EthereumAddress,
validator_pk: PublicKey,
transcript: Transcript
}

fn parse_file(file_path: &PathBuf) -> io::Result<Vec<ValidatorTranscript>> {
let mut records = Vec::new();

let file = File::open(file_path)?;
let reader = io::BufReader::new(file);

for line in reader.lines() {
let line = line?;
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() != 3 {
eprintln!("Invalid line: {line}");
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid line",
));
}

let validator_address = parts[0].to_string();
let validator_pk = Vec::from_hex(parts[1])
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
let transcript = Vec::from_hex(parts[2])
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;

if validator_address.len() != 42 {
eprintln!(
"Invalid validator_address length: {}",
validator_address.len()
);
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid validator_address length",
));
}
if validator_pk.len() != 96 {
eprintln!("Invalid validator_pk length: {}", validator_pk.len());
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid validator_pk length",
));
}
if transcript.len() != 3784 {
eprintln!("Invalid transcript length: {}", transcript.len());
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid transcript length",
));
}

let validator_address = EthereumAddress::from_str(&validator_address).unwrap();
let validator_pk = PublicKey::from_bytes(&validator_pk).unwrap();
let transcript = Transcript::from_bytes(&transcript).unwrap();

records.push(ValidatorTranscript {
validator_address,
validator_pk,
transcript,
});
}

Ok(records)
}

#[cfg(test)]
mod test {
use crate::{api::AggregatedTranscript, Validator};

use super::*;

#[test]
fn test_parse_file() {
let filename = "gistfile1.csv";
let file_path =
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(filename);
let records = parse_file(&file_path).unwrap();
assert_eq!(records.len(), 30);

let mut transcripts = Vec::new();
let mut vmessages = Vec::new();

// verify_optimistic
for (i, record) in records.iter().enumerate(){
assert!(record.transcript.verify_optimistic());

transcripts.push(record.transcript.clone());

let validator = Validator::new(record.validator_address.to_string(), record.validator_pk, i as u32).unwrap();
vmessages.push((validator.clone(), record.transcript.clone()));
}

// Aggregate
let agg = aggregate(&transcripts).unwrap();
assert!(agg.verify_optimistic());

let at = AggregatedTranscript::new(&vmessages).unwrap();
assert!(at.verify(30, &vmessages).unwrap());
}
}
3 changes: 3 additions & 0 deletions ferveo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#[cfg(feature = "bindings-wasm")]
extern crate alloc;
extern crate core;

#[cfg(feature = "bindings-python")]
pub mod bindings_python;
Expand All @@ -16,6 +17,8 @@ pub mod pvss;
pub mod refresh;
pub mod validator;

#[cfg(test)]
mod debug;
#[cfg(test)]
mod test_common;

Expand Down
12 changes: 10 additions & 2 deletions ferveo/src/pvss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,13 @@ pub fn do_verify_full<E: Pairing>(
// We verify that e(G, Y_i) = e(A_i, ek_i) for validator i
// See #4 in 4.2.3 section of https://eprint.iacr.org/2022/898.pdf
// e(G,Y) = e(A, ek)
E::pairing(pvss_params.g, *y_i) == E::pairing(a_i, ek_i)
let r = E::pairing(pvss_params.g, *y_i) == E::pairing(a_i, ek_i);
if !r {
println!("Failed transc {} ({})", share_index, validator.address);
} else {
println!("All good with {} ({})", share_index, validator.address);
}
true
})
}

Expand All @@ -267,7 +273,9 @@ pub fn do_verify_aggregation<E: Pairing>(
return Err(Error::InvalidTranscriptAggregate);
}

// Now, we verify that the aggregated PVSS transcript is a valid aggregation
// Now, we verify that the first element of the aggregated PVSS transcript
// (i.e. the final DKG public key) is actually the sum of the first element
// of all the transcripts
let mut y = E::G1::zero();
for pvss in vss.values() {
y += pvss.coeffs[0].into_group();
Expand Down
Loading