From e354466439176965d934222d38c5fff46ec492b8 Mon Sep 17 00:00:00 2001 From: Brechy Date: Fri, 29 Sep 2023 15:34:12 -0300 Subject: [PATCH] fix scalar domain generation --- .gitignore | 6 +++--- eigentrust-cli/src/cli.rs | 18 ++++-------------- eigentrust/src/circuit.rs | 6 ++---- eigentrust/src/lib.rs | 40 ++++++++++++++++++++++++++------------- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index d92e63b9..cbf83f0e 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/eigentrust-cli/src/cli.rs b/eigentrust-cli/src/cli.rs index 08a4886d..7fd88ad8 100644 --- a/eigentrust-cli/src/cli.rs +++ b/eigentrust-cli/src/cli.rs @@ -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())?; @@ -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 @@ -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 = 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 = + 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::::new(scores_fp); - records_storage.save(score_records)?; info!( diff --git a/eigentrust/src/circuit.rs b/eigentrust/src/circuit.rs index 1b64c885..83aa564f 100644 --- a/eigentrust/src/circuit.rs +++ b/eigentrust/src/circuit.rs @@ -35,10 +35,8 @@ impl Circuit { } } -/// Scores report struct. -pub struct ScoresReport { - /// Participants' scores - pub scores: Vec, +/// EigenTrust report struct. +pub struct ETReport { /// Verifier public inputs pub pub_inputs: ETPublicInputs, /// Proof diff --git a/eigentrust/src/lib.rs b/eigentrust/src/lib.rs index eaf1f5d7..d6b00bea 100644 --- a/eigentrust/src/lib.rs +++ b/eigentrust/src/lib.rs @@ -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, @@ -225,17 +225,10 @@ impl Client { /// Calculates the EigenTrust global scores. pub fn calculate_scores( - &self, att: Vec, raw_kzg_params: Vec, raw_prov_key: Vec, - ) -> Result { - let rng = &mut rand::thread_rng(); + &self, att: Vec, + ) -> Result, EigenError> { let et_setup = self.et_circuit_setup(att)?; - // Parse KZG params and proving key - let kzg_params: ParamsKZG = - ParamsKZG::::read(&mut raw_kzg_params.as_slice()).unwrap(); - let proving_key: ProvingKey = - ProvingKey::from_bytes::(&raw_prov_key, SerdeFormat::Processed).unwrap(); - // Construct scores vec let scores: Vec = et_setup .address_set @@ -265,6 +258,22 @@ impl Client { }) .collect(); + Ok(scores) + } + + /// Generates an EigenTrust circuit proof. + pub fn generate_et_proof( + &self, att: Vec, raw_kzg_params: Vec, raw_prov_key: Vec, + ) -> Result { + let rng = &mut rand::thread_rng(); + let et_setup = self.et_circuit_setup(att)?; + + // Parse KZG params and proving key + let kzg_params: ParamsKZG = + ParamsKZG::::read(&mut raw_kzg_params.as_slice()).unwrap(); + let proving_key: ProvingKey = + ProvingKey::from_bytes::(&raw_prov_key, SerdeFormat::Processed).unwrap(); + // Initialize EigenTrustSet let et_circuit: EigenTrust4 = EigenTrust4::new( et_setup.attestation_matrix, @@ -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, raw_et_kzg_params: Vec, raw_th_kzg_params: Vec, raw_proving_key: Vec, threshold: u32, participant_id: u32, @@ -657,7 +666,12 @@ impl Client { pub fn get_scalar_domain(&self) -> Result { 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()),