Browse Source

updated lead proof/example with halo2 api

mohab metwally 4 years ago
parent
commit
f7b4c9af27
5 changed files with 37 additions and 36 deletions
  1. 4 4
      Cargo.toml
  2. 19 18
      example/lead.rs
  3. 2 2
      src/crypto/mod.rs
  4. 10 10
      src/zk/circuit/lead_contract.rs
  5. 2 2
      src/zk/circuit/mod.rs

+ 4 - 4
Cargo.toml

@@ -314,7 +314,7 @@ name = "zk"
 path = "example/zk.rs"
 path = "example/zk.rs"
 required-features = ["crypto"]
 required-features = ["crypto"]
 
 
-#[[example]]
-#name = "lead"
-#path = "example/lead.rs"
-#required-features = ["node"]
+[[example]]
+name = "lead"
+path = "example/lead.rs"
+required-features = ["node"]

+ 19 - 18
example/lead.rs

@@ -1,4 +1,4 @@
-use halo2_proofs::{arithmetic::Field, dev::MockProver};
+use halo2_proofs::{arithmetic::Field, dev::MockProver, circuit::Value};
 use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
 use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
 use pasta_curves::{
 use pasta_curves::{
     arithmetic::CurveAffine,
     arithmetic::CurveAffine,
@@ -18,6 +18,7 @@ use darkfi::{
     zk::circuit::lead_contract::LeadContract,
     zk::circuit::lead_contract::LeadContract,
 };
 };
 
 
+
 const MERKLE_DEPTH: u8 = MERKLE_DEPTH_ORCHARD as u8;
 const MERKLE_DEPTH: u8 = MERKLE_DEPTH_ORCHARD as u8;
 
 
 fn create_coins_sks(len: usize) -> (Vec<MerkleNode>, Vec<[MerkleNode; MERKLE_DEPTH_ORCHARD]>) {
 fn create_coins_sks(len: usize) -> (Vec<MerkleNode>, Vec<[MerkleNode; MERKLE_DEPTH_ORCHARD]>) {
@@ -183,23 +184,23 @@ fn main() {
     let mau_rho: pallas::Base = pallas::Base::from(rhou64);
     let mau_rho: pallas::Base = pallas::Base::from(rhou64);
 
 
     let contract = LeadContract {
     let contract = LeadContract {
-        path: coin.path,
-        coin_pk_x: coin.pk_x,
-        coin_pk_y: coin.pk_y,
-        root_sk: coin.root_sk,
-        sf_root_sk: Some(mod_r_p(coin.root_sk.unwrap())),
-        path_sk: coin.path_sk,
-        coin_timestamp: coin.tau, //
-        coin_nonce: coin.nonce,
-        coin_opening_1: Some(mod_r_p(coin.opening1.unwrap())),
-        value: coin.value,
-        coin_opening_2: Some(mod_r_p(coin.opening2.unwrap())),
-        cm_pos: Some(coin.idx),
-        //sn_c1: Some(coin.sn.unwrap()),
-        slot: Some(coin.sl.unwrap()),
-        mau_rho: Some(mod_r_p(mau_rho)),
-        mau_y: Some(mod_r_p(mau_y)),
-        root_cm: Some(coin.root_cm.unwrap()),
+        path: Value::known(coin.path.unwrap()),
+        coin_pk_x: Value::known(coin.pk_x.unwrap()),
+        coin_pk_y: Value::known(coin.pk_y.unwrap()),
+        root_sk: Value::known(coin.root_sk.unwrap()),
+        sf_root_sk: Value::known(mod_r_p(coin.root_sk.unwrap())),
+        path_sk: Value::known(coin.path_sk.unwrap()),
+        coin_timestamp: Value::known(coin.tau.unwrap()), //
+        coin_nonce: Value::known(coin.nonce.unwrap()),
+        coin_opening_1: Value::known(mod_r_p(coin.opening1.unwrap())),
+        value: Value::known(coin.value.unwrap()),
+        coin_opening_2: Value::known(mod_r_p(coin.opening2.unwrap())),
+        cm_pos: Value::known(coin.idx),
+        //sn_c1: Value::known(coin.sn.unwrap()),
+        slot: Value::known(coin.sl.unwrap()),
+        mau_rho: Value::known(mod_r_p(mau_rho)),
+        mau_y: Value::known(mod_r_p(mau_y)),
+        root_cm: Value::known(coin.root_cm.unwrap()),
     };
     };
 
 
     //let proof = create_lead_proof(lead_pk.clone(), coin.clone()).unwrap();
     //let proof = create_lead_proof(lead_pk.clone(), coin.clone()).unwrap();

+ 2 - 2
src/crypto/mod.rs

@@ -21,8 +21,8 @@ pub use burn_proof::BurnRevealedValues;
 pub use mint_proof::MintRevealedValues;
 pub use mint_proof::MintRevealedValues;
 pub use proof::Proof;
 pub use proof::Proof;
 
 
-//pub mod lead_proof;
-//pub mod leadcoin;
+pub mod lead_proof;
+pub mod leadcoin;
 
 
 use keypair::SecretKey;
 use keypair::SecretKey;
 
 

+ 10 - 10
src/zk/circuit/lead_contract.rs

@@ -591,8 +591,7 @@ impl Circuit<pallas::Base> for LeadContract {
         )?;
         )?;
 
 
         // ===========================
         // ===========================
-        let path: Option<[pallas::Base; MERKLE_DEPTH_ORCHARD]> =
-            self.path.map(|typed_path| gen_const_array(|i| typed_path[i].inner()));
+        let path : Value<[pallas::Base;MERKLE_DEPTH_ORCHARD]> = self.path.map(|typed_path| gen_const_array(|i| typed_path[i].inner()));
 
 
         let merkle_inputs = MerklePath::construct(
         let merkle_inputs = MerklePath::construct(
             [config.merkle_chip_1(), config.merkle_chip_2()],
             [config.merkle_chip_1(), config.merkle_chip_2()],
@@ -621,7 +620,7 @@ impl Circuit<pallas::Base> for LeadContract {
             LEAD_COIN_COMMIT_PATH_OFFSET,
             LEAD_COIN_COMMIT_PATH_OFFSET,
         )?;
         )?;
 
 
-        let _node = MerkleNode::from_bytes(&self.root_sk.unwrap().to_repr()).unwrap();
+        //let _node = MerkleNode::from_bytes(&self.root_sk.unwrap().to_repr()).unwrap();
         //let serialized = serde_json::to_string(&node).unwrap();
         //let serialized = serde_json::to_string(&node).unwrap();
         //println!("root_sk: {}", serialized);
         //println!("root_sk: {}", serialized);
 
 
@@ -656,14 +655,15 @@ impl Circuit<pallas::Base> for LeadContract {
             y_commit_r.mul(layouter.namespace(|| "coin serial number commit R"), mau_y)?
             y_commit_r.mul(layouter.namespace(|| "coin serial number commit R"), mau_y)?
         };
         };
         let y_commit = com.add(layouter.namespace(|| "nonce commit"), &blind)?;
         let y_commit = com.add(layouter.namespace(|| "nonce commit"), &blind)?;
-
+        let y_commit_base = y_commit.inner().x();
+        /*
         // ============================
         // ============================
-        let _y_commit_bytes: [u8; 32] = y_commit.inner().point().unwrap().to_bytes();
+        let _y_commit_bytes: [u8; 32] = y_commit.inner().point().into().unwrap().to_bytes();
         let y_commit_base_bytes: [u8; 32] = [0; 32];
         let y_commit_base_bytes: [u8; 32] = [0; 32];
-        // FIXME: why is it assigning to itself?
-        // for i in 0..23 {
-        // y_commit_base_bytes[i] = y_commit_base_bytes[i];
-        // }
+        //note! due to 24bytes size limitation in the comparision gate we need first 24bytes
+        for i in 0..23 {
+            y_commit_base_bytes[i] = y_commit_base_bytes[i];
+        }
         let y_commit_base_temp = pallas::Base::from_repr(y_commit_base_bytes).unwrap();
         let y_commit_base_temp = pallas::Base::from_repr(y_commit_base_bytes).unwrap();
 
 
         let y_commit_base = self.load_private(
         let y_commit_base = self.load_private(
@@ -671,7 +671,7 @@ impl Circuit<pallas::Base> for LeadContract {
             config.advices[0],
             config.advices[0],
             Value::known(y_commit_base_temp),
             Value::known(y_commit_base_temp),
         )?;
         )?;
-
+        */
         // ============================
         // ============================
         // constraint rho
         // constraint rho
         // ============================
         // ============================

+ 2 - 2
src/zk/circuit/mod.rs

@@ -4,5 +4,5 @@ pub use burn_contract::BurnContract;
 pub mod mint_contract;
 pub mod mint_contract;
 pub use mint_contract::MintContract;
 pub use mint_contract::MintContract;
 
 
-//pub mod lead_contract;
-//pub use lead_contract::LeadContract;
+pub mod lead_contract;
+pub use lead_contract::LeadContract;