Parcourir la source

Create zk proving keys on initial startup and pass them around.

parazyd il y a 4 ans
Parent
commit
3a80143540
7 fichiers modifiés avec 23 ajouts et 64 suppressions
  1. 0 4
      src/bin/cashierd.rs
  2. 5 3
      src/bin/tx2.rs
  3. 12 2
      src/client.rs
  4. 1 24
      src/crypto/mint_proof.rs
  5. 1 24
      src/crypto/spend_proof.rs
  6. 0 5
      src/state.rs
  7. 4 2
      src/tx/builder.rs

+ 0 - 4
src/bin/cashierd.rs

@@ -681,10 +681,6 @@ async fn start(
 
     let rocks = Rocks::new(expand_path(&config.database_path.clone())?.as_path())?;
 
-    /*
-    let mint_pk = ProvingKey::build(11, MintContract::default());
-    let spend_pk = ProvingKey::build(11, SpendContract::default());
-    */
     info!("Building verifying key for the mint contract...");
     let mint_vk = VerifyingKey::build(11, MintContract::default());
     info!("Building verifying key for the spend contract...");

+ 5 - 3
src/bin/tx2.rs

@@ -10,7 +10,7 @@ use drk::{
         merkle_node::MerkleNode,
         note::{EncryptedNote, Note},
         nullifier::Nullifier,
-        proof::VerifyingKey,
+        proof::{ProvingKey, VerifyingKey},
     },
     state::{state_transition, ProgramState, StateUpdate},
     tx, Result,
@@ -133,7 +133,9 @@ fn main() -> Result<()> {
         }],
     };
 
-    let tx = builder.build()?;
+    let mint_pk = ProvingKey::build(K, MintContract::default());
+    let spend_pk = ProvingKey::build(K, SpendContract::default());
+    let tx = builder.build(&mint_pk, &spend_pk)?;
 
     tx.verify(&state.mint_vk, &state.spend_vk).expect("tx verify");
 
@@ -162,7 +164,7 @@ fn main() -> Result<()> {
         }],
     };
 
-    let tx = builder.build()?;
+    let tx = builder.build(&mint_pk, &spend_pk)?;
 
     let update = state_transition(&state, tx)?;
     state.apply(update);

+ 12 - 2
src/client.rs

@@ -7,10 +7,12 @@ use url::Url;
 
 use crate::{
     blockchain::{rocks::columns, Rocks, RocksColumn, Slab},
+    circuit::{MintContract, SpendContract},
     crypto::{
         coin::Coin,
         keypair::{Keypair, PublicKey, SecretKey},
         merkle_node::MerkleNode,
+        proof::ProvingKey,
         OwnCoin,
     },
     serial::{serialize, Decodable, Encodable},
@@ -70,6 +72,8 @@ pub struct Client {
     pub main_keypair: Keypair,
     gateway: GatewayClient,
     wallet: WalletPtr,
+    mint_pk: ProvingKey,
+    spend_pk: ProvingKey,
 }
 
 impl Client {
@@ -93,7 +97,11 @@ impl Client {
         let slabstore = RocksColumn::<columns::Slabs>::new(rocks);
         let gateway = GatewayClient::new(gateway_addrs.0, gateway_addrs.1, slabstore)?;
 
-        let client = Client { main_keypair, gateway, wallet };
+        // TODO: These should go to a better place.
+        let mint_pk = ProvingKey::build(11, MintContract::default());
+        let spend_pk = ProvingKey::build(11, SpendContract::default());
+
+        let client = Client { main_keypair, gateway, wallet, mint_pk, spend_pk };
         Ok(client)
     }
 
@@ -172,7 +180,7 @@ impl Client {
         let builder = tx::TransactionBuilder { clear_inputs, inputs, outputs };
         let tx: tx::Transaction;
         let mut tx_data = vec![];
-        tx = builder.build()?;
+        tx = builder.build(&self.mint_pk, &self.spend_pk)?;
         tx.encode(&mut tx_data).expect("encode tx");
 
         let slab = Slab::new(tx_data);
@@ -182,7 +190,9 @@ impl Client {
         let state = &*state.lock().await;
         state_transition(state, tx)?;
 
+        debug!("Sending slab to gateway");
         self.gateway.put_slab(slab).await?;
+        debug!("Sent successfully");
         Ok(coins)
     }
 

+ 1 - 24
src/crypto/mint_proof.rs

@@ -23,23 +23,6 @@ use crate::{
     Result,
 };
 
-pub struct MintProofKeys {
-    pub vk: VerifyingKey,
-    pub pk: ProvingKey,
-}
-
-impl MintProofKeys {
-    pub fn initialize() -> Self {
-        let start = Instant::now();
-        debug!("Building proof verifying key for the mint contract...");
-        let vk = VerifyingKey::build(11, MintContract::default());
-        debug!("Building proof proving key for the mint contract...");
-        let pk = ProvingKey::build(11, MintContract::default());
-        debug!("Setup: [{:?}]", start.elapsed());
-        MintProofKeys { vk, pk }
-    }
-}
-
 pub struct MintRevealedValues {
     pub value_commit: DrkValueCommit,
     pub token_commit: DrkValueCommit,
@@ -106,6 +89,7 @@ impl Decodable for MintRevealedValues {
 
 #[allow(clippy::too_many_arguments)]
 pub fn create_mint_proof(
+    pk: &ProvingKey,
     value: u64,
     token_id: DrkTokenId,
     value_blind: DrkValueBlind,
@@ -114,8 +98,6 @@ pub fn create_mint_proof(
     coin_blind: DrkCoinBlind,
     public_key: PublicKey,
 ) -> Result<(Proof, MintRevealedValues)> {
-    const K: u32 = 11;
-
     let revealed = MintRevealedValues::compute(
         value,
         token_id,
@@ -139,11 +121,6 @@ pub fn create_mint_proof(
         asset_blind: Some(token_blind),
     };
 
-    let start = Instant::now();
-    // TODO: Don't always build this
-    let pk = ProvingKey::build(K, MintContract::default());
-    debug!("Setup: [{:?}]", start.elapsed());
-
     let start = Instant::now();
     let public_inputs = revealed.make_outputs();
     let proof = Proof::create(&pk, &[c], &public_inputs)?;

+ 1 - 24
src/crypto/spend_proof.rs

@@ -28,23 +28,6 @@ use crate::{
     Result,
 };
 
-pub struct SpendProofKeys {
-    pub vk: VerifyingKey,
-    pub pk: ProvingKey,
-}
-
-impl SpendProofKeys {
-    pub fn initialize() -> Self {
-        let start = Instant::now();
-        debug!("Building proof verifying key for the spend contract...");
-        let vk = VerifyingKey::build(11, SpendContract::default());
-        debug!("Building proof proving key for the spend contract...");
-        let pk = ProvingKey::build(11, SpendContract::default());
-        debug!("Setup: [{:?}]", start.elapsed());
-        SpendProofKeys { vk, pk }
-    }
-}
-
 pub struct SpendRevealedValues {
     pub value_commit: DrkValueCommit,
     pub token_commit: DrkValueCommit,
@@ -152,6 +135,7 @@ impl Decodable for SpendRevealedValues {
 
 #[allow(clippy::too_many_arguments)]
 pub fn create_spend_proof(
+    pk: &ProvingKey,
     value: u64,
     token_id: DrkTokenId,
     value_blind: DrkValueBlind,
@@ -163,8 +147,6 @@ pub fn create_spend_proof(
     merkle_path: Vec<MerkleNode>,
     signature_secret: SecretKey,
 ) -> Result<(Proof, SpendRevealedValues)> {
-    const K: u32 = 11;
-
     let revealed = SpendRevealedValues::compute(
         value,
         token_id,
@@ -194,11 +176,6 @@ pub fn create_spend_proof(
         sig_secret: Some(signature_secret.0),
     };
 
-    let start = Instant::now();
-    // TODO: Don't always build this
-    let pk = ProvingKey::build(K, SpendContract::default());
-    debug!("Setup: [{:?}]", start.elapsed());
-
     let start = Instant::now();
     let public_inputs = revealed.make_outputs();
     let proof = Proof::create(&pk, &[c], &public_inputs)?;

+ 0 - 5
src/state.rs

@@ -12,7 +12,6 @@ use crate::{
         proof::VerifyingKey,
         OwnCoin,
     },
-    serial::serialize,
     tx::Transaction,
     wallet::walletdb::WalletPtr,
     Result,
@@ -64,10 +63,6 @@ pub fn state_transition<S: ProgramState>(state: &S, tx: Transaction) -> VerifyRe
         // Check the public key in the clear inputs
         // It should be a valid public key for the cashier
         if !state.is_valid_cashier_public_key(&input.signature_public) {
-            debug!(
-                "CASHIER PUBLIC: {}",
-                bs58::encode(serialize(&input.signature_public)).into_string()
-            );
             log::error!(target: "STATE TRANSITION", "Invalid cashier public key");
             return Err(VerifyFailed::InvalidCashierKey(i))
         }

+ 4 - 2
src/tx/builder.rs

@@ -11,6 +11,7 @@ use crate::{
         merkle_node::MerkleNode,
         mint_proof::create_mint_proof,
         note::Note,
+        proof::ProvingKey,
         schnorr::SchnorrSecret,
         spend_proof::create_spend_proof,
     },
@@ -67,8 +68,7 @@ impl TransactionBuilder {
         total
     }
 
-    // TODO: pass proving keys as args to this function
-    pub fn build(self) -> Result<Transaction> {
+    pub fn build(self, mint_pk: &ProvingKey, spend_pk: &ProvingKey) -> Result<Transaction> {
         let mut clear_inputs = vec![];
         let token_blind = DrkValueBlind::random(&mut OsRng);
         for input in &self.clear_inputs {
@@ -94,6 +94,7 @@ impl TransactionBuilder {
             let signature_secret = SecretKey::random(&mut OsRng);
 
             let (proof, revealed) = create_spend_proof(
+                spend_pk,
                 input.note.value,
                 input.note.token_id,
                 input.note.value_blind,
@@ -128,6 +129,7 @@ impl TransactionBuilder {
             let coin_blind = DrkCoinBlind::random(&mut OsRng);
 
             let (mint_proof, revealed) = create_mint_proof(
+                mint_pk,
                 output.value,
                 output.token_id,
                 value_blind,