Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
fix scalar domain generation
Browse files Browse the repository at this point in the history
  • Loading branch information
brech1 committed Sep 29, 2023
1 parent 98610a7 commit e354466
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# Assets
/eigentrust-cli/assets/attestation_station.rs
/eigentrust-cli/assets/et-proving-key.bin
/eigentrust-cli/assets/et-public-inputs.bin
/eigentrust-cli/assets/et-proof.bin
/eigentrust-cli/assets/*-proving-key.bin
/eigentrust-cli/assets/*-public-inputs.bin
/eigentrust-cli/assets/*-proof.bin
/eigentrust-cli/assets/kzg-params-*.bin
18 changes: 4 additions & 14 deletions eigentrust-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ pub async fn handle_et_proof(config: ClientConfig) -> Result<(), EigenError> {
let kzg_params = EigenFile::KzgParams(ET_PARAMS_K).load()?;

// Generate proof
let report = client.calculate_scores(attestations?, kzg_params, proving_key)?;
let report = client.generate_et_proof(attestations?, kzg_params, proving_key)?;

EigenFile::Proof(Circuit::EigenTrust).save(report.proof)?;
EigenFile::PublicInputs(Circuit::EigenTrust).save(report.pub_inputs.to_bytes())?;
Expand Down Expand Up @@ -383,7 +383,6 @@ pub async fn handle_scores(
) -> Result<(), EigenError> {
let mnemonic = load_mnemonic();
let client = Client::new(config, mnemonic);

let att_fp = get_file_path("attestations", FileType::Csv)?;

// Get or Fetch attestations
Expand Down Expand Up @@ -416,22 +415,13 @@ pub async fn handle_scores(
},
};

let proving_key = EigenFile::ProvingKey(Circuit::EigenTrust).load()?;
let kzg_params = EigenFile::KzgParams(ET_PARAMS_K).load()?;

// Calculate scores
let score_records: Vec<ScoreRecord> = client
.calculate_scores(attestations, kzg_params, proving_key)?
.scores
.into_iter()
.map(ScoreRecord::from_score)
.collect();

let scores_fp = get_file_path("scores", FileType::Csv)?;
let score_records: Vec<ScoreRecord> =
client.calculate_scores(attestations)?.into_iter().map(ScoreRecord::from_score).collect();

// Save scores
let scores_fp = get_file_path("scores", FileType::Csv)?;
let mut records_storage = CSVFileStorage::<ScoreRecord>::new(scores_fp);

records_storage.save(score_records)?;

info!(
Expand Down
6 changes: 2 additions & 4 deletions eigentrust/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ impl Circuit {
}
}

/// Scores report struct.
pub struct ScoresReport {
/// Participants' scores
pub scores: Vec<Score>,
/// EigenTrust report struct.
pub struct ETReport {
/// Verifier public inputs
pub pub_inputs: ETPublicInputs,
/// Proof
Expand Down
40 changes: 27 additions & 13 deletions eigentrust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use att_station::{
AttestationCreatedFilter, AttestationData as ContractAttestationData, AttestationStation,
};
use attestation::{build_att_key, AttestationEth, AttestationRaw, SignedAttestationRaw};
use circuit::{ETSetup, ScoresReport, ThPublicInputs, ThReport, ThSetup};
use circuit::{ETReport, ETSetup, ThPublicInputs, ThReport, ThSetup};
use eigentrust_zk::{
circuits::{
threshold::native::Threshold, ECDSAPublicKey, EigenTrust4, NativeAggregator4,
Expand Down Expand Up @@ -225,17 +225,10 @@ impl Client {

/// Calculates the EigenTrust global scores.
pub fn calculate_scores(
&self, att: Vec<SignedAttestationRaw>, raw_kzg_params: Vec<u8>, raw_prov_key: Vec<u8>,
) -> Result<ScoresReport, EigenError> {
let rng = &mut rand::thread_rng();
&self, att: Vec<SignedAttestationRaw>,
) -> Result<Vec<Score>, EigenError> {
let et_setup = self.et_circuit_setup(att)?;

// Parse KZG params and proving key
let kzg_params: ParamsKZG<Bn256> =
ParamsKZG::<Bn256>::read(&mut raw_kzg_params.as_slice()).unwrap();
let proving_key: ProvingKey<G1Affine> =
ProvingKey::from_bytes::<EigenTrust4>(&raw_prov_key, SerdeFormat::Processed).unwrap();

// Construct scores vec
let scores: Vec<Score> = et_setup
.address_set
Expand Down Expand Up @@ -265,6 +258,22 @@ impl Client {
})
.collect();

Ok(scores)
}

/// Generates an EigenTrust circuit proof.
pub fn generate_et_proof(
&self, att: Vec<SignedAttestationRaw>, raw_kzg_params: Vec<u8>, raw_prov_key: Vec<u8>,
) -> Result<ETReport, EigenError> {
let rng = &mut rand::thread_rng();
let et_setup = self.et_circuit_setup(att)?;

// Parse KZG params and proving key
let kzg_params: ParamsKZG<Bn256> =
ParamsKZG::<Bn256>::read(&mut raw_kzg_params.as_slice()).unwrap();
let proving_key: ProvingKey<G1Affine> =
ProvingKey::from_bytes::<EigenTrust4>(&raw_prov_key, SerdeFormat::Processed).unwrap();

// Initialize EigenTrustSet
let et_circuit: EigenTrust4 = EigenTrust4::new(
et_setup.attestation_matrix,
Expand All @@ -282,10 +291,10 @@ impl Client {
)
.map_err(|e| EigenError::ProvingError(format!("Failed to generate proof: {}", e)))?;

Ok(ScoresReport { scores, pub_inputs: et_setup.pub_inputs, proof })
Ok(ETReport { pub_inputs: et_setup.pub_inputs, proof })
}

/// Generates Threshold circuit proof for the selected participant
/// Generates Threshold circuit proof for the selected participant.
pub fn generate_th_proof(
&self, att: Vec<SignedAttestationRaw>, raw_et_kzg_params: Vec<u8>,
raw_th_kzg_params: Vec<u8>, raw_proving_key: Vec<u8>, threshold: u32, participant_id: u32,
Expand Down Expand Up @@ -657,7 +666,12 @@ impl Client {
pub fn get_scalar_domain(&self) -> Result<Scalar, EigenError> {
let domain_bytes = H160::from_str(&self.config.domain)
.map_err(|e| EigenError::ParsingError(format!("Error parsing domain: {}", e)))?;
let domain_opt = Scalar::from_bytes(H256::from(domain_bytes).as_fixed_bytes());
let domain_bytes_256 = H256::from(domain_bytes);

let mut domain = domain_bytes_256.as_fixed_bytes().clone();
domain.reverse();

let domain_opt = Scalar::from_bytes(&domain);

match domain_opt.is_some().into() {
true => Ok(domain_opt.unwrap()),
Expand Down

0 comments on commit e354466

Please sign in to comment.