فهرست منبع

[zk/circuit/lead_contract] replace constrain instance for path with constrain equal

mohab metwally 3 سال پیش
والد
کامیت
0ff1a1acd2
3فایلهای تغییر یافته به همراه23 افزوده شده و 33 حذف شده
  1. 1 1
      src/consensus/ouroboros/epoch.rs
  2. 1 9
      src/crypto/leadcoin.rs
  3. 21 23
      src/zk/circuit/lead_contract.rs

+ 1 - 1
src/consensus/ouroboros/epoch.rs

@@ -267,7 +267,7 @@ impl Epoch {
             nonce_cm: Some(c_seed2),
             sn: Some(c_sn),
             keypair: Some(keypair),
-            root_cm: Some(mod_r_p(c_root_cm.inner())),
+            root_cm: Some(c_root_cm.inner()),
             root_sk: Some(c_root_sk.inner()),
             path: Some(c_cm_path.as_slice().try_into().unwrap()),
             path_sk: Some(c_path_sk),

+ 1 - 9
src/crypto/leadcoin.rs

@@ -26,7 +26,7 @@ pub struct LeadCoin {
     pub nonce_cm: Option<pallas::Base>, // coin nonce's commitment
     pub sn: Option<pallas::Base>,       // coin's serial number
     pub keypair: Option<Keypair>,
-    pub root_cm: Option<pallas::Scalar>, // root of coin commitment
+    pub root_cm: Option<pallas::Base>, // root of coin commitment
     pub root_sk: Option<pallas::Base>,   // coin's secret key
     pub path: Option<[MerkleNode; MERKLE_DEPTH_ORCHARD]>, // path to the coin's commitment
     pub path_sk: Option<[MerkleNode; MERKLE_DEPTH_ORCHARD]>, // path to the coin's secret key
@@ -42,14 +42,8 @@ pub struct LeadCoin {
 impl LeadCoin {
     pub fn public_inputs_as_array(&self) -> [pallas::Base; LEAD_PUBLIC_INPUT_LEN] {
         let po_nonce = self.nonce_cm.unwrap();
-        let _po_tau = pedersen_commitment_base(self.tau.unwrap(), self.root_cm.unwrap())
-            .to_affine()
-            .coordinates()
-            .unwrap();
-
         let po_cm = self.cm.unwrap().to_affine().coordinates().unwrap();
         let po_pk = self.keypair.unwrap().public.0.to_affine().coordinates().unwrap();
-
         let y_mu = self.y_mu.unwrap();
         let rho_mu = self.rho_mu.unwrap();
         let root_sk = self.root_sk.unwrap();
@@ -58,7 +52,6 @@ impl LeadCoin {
         let lottery_msg: pallas::Base =
             poseidon::Hash::<_, poseidon::P128Pow5T3, poseidon::ConstantLength<2>, 3, 2>::init()
                 .hash(lottery_msg_input);
-        //
         let po_y_pt: pallas::Point = pedersen_commitment_base(lottery_msg, mod_r_p(y_mu));
         let po_y_x = *po_y_pt.to_affine().coordinates().unwrap().x();
         let po_y_y = *po_y_pt.to_affine().coordinates().unwrap().y();
@@ -66,7 +59,6 @@ impl LeadCoin {
         let po_y: pallas::Base =
             poseidon::Hash::<_, poseidon::P128Pow5T3, poseidon::ConstantLength<2>, 3, 2>::init()
                 .hash(y_coord_arr);
-
         let cm_pos = self.idx;
         let cm_root = {
             let pos: u32 = cm_pos;

+ 21 - 23
src/zk/circuit/lead_contract.rs

@@ -122,7 +122,7 @@ pub struct LeadContract {
     pub slot: Value<pallas::Base>,
     pub mau_rho: Value<pallas::Scalar>,
     pub mau_y: Value<pallas::Scalar>,
-    pub root_cm: Value<pallas::Scalar>,
+    pub root_cm: Value<pallas::Base>,
     pub sigma1: Value<pallas::Base>,
     pub sigma2: Value<pallas::Base>,
     //pub eta : Option<u32>,
@@ -311,9 +311,11 @@ impl Circuit<pallas::Base> for LeadContract {
         let _root_sk =
             self.load_private(layouter.namespace(|| "root sk"), config.advices[0], self.root_sk)?;
 
+        let root_cm = self.load_private(layouter.namespace(||""), config.advices[0], self.root_cm)?;
+
         // staking coin secret key
         let sk: AssignedCell<Fp, Fp> =
-            self.load_private(layouter.namespace(|| "sk"), config.advices[0], self.sk).unwrap();
+            self.load_private(layouter.namespace(|| "sk"), config.advices[0], self.sk)?;
 
         let sigma1 = self.load_private(
             layouter.namespace(|| "load sigma1 "),
@@ -535,9 +537,15 @@ impl Circuit<pallas::Base> for LeadContract {
             )?;
             res
         };
-        let computed_final_root = merkle_inputs
+
+        let coin_cm_root = merkle_inputs
             .calculate_root(layouter.namespace(|| "calculate root"), coin_commit_prod)?;
 
+        layouter.assign_region(||"",
+                               |mut region| {
+                                   region.constrain_equal(coin_cm_root.cell(), root_cm.cell())
+                               }
+        );
         // lhs of the leader election lottery
         // *  y as COMIT(root_sk||nonce, mau_y)
         // beging the commitment to the coin's secret key, coin's nonce, and
@@ -663,30 +671,20 @@ impl Circuit<pallas::Base> for LeadContract {
 
         coin2_commit.constrain_equal(layouter.namespace(|| ""), &ref_coin2_cm)?;
 
-        /*
-        layouter.constrain_instance(
-            coin2_commit_x.cell(),
-            config.primary,
-            LEAD_COIN_COMMIT2_X_OFFSET,
+        layouter.constrain_instance(coin2_nonce.cell(),
+                                    config.primary,
+                                    LEAD_COIN_NONCE2_OFFSET
         )?;
-        // constrain coin's pub key y value
-        layouter.constrain_instance(
-            coin2_commit_y.cell(),
-            config.primary,
-            LEAD_COIN_COMMIT2_Y_OFFSET,
-        )?;
-         */
 
-        layouter.constrain_instance(coin2_nonce.cell(), config.primary, LEAD_COIN_NONCE2_OFFSET)?;
 
-        layouter.constrain_instance(
-            computed_final_root.cell(),
-            config.primary,
-            LEAD_COIN_COMMIT_PATH_OFFSET,
+        layouter.constrain_instance(coin_pk_x.cell(),
+                                    config.primary,
+                                    LEAD_COIN_PK_X_OFFSET
+        )?;
+        layouter.constrain_instance(coin_pk_y.cell(),
+                                    config.primary,
+                                    LEAD_COIN_PK_Y_OFFSET
         )?;
-
-        layouter.constrain_instance(coin_pk_x.cell(), config.primary, LEAD_COIN_PK_X_OFFSET)?;
-        layouter.constrain_instance(coin_pk_y.cell(), config.primary, LEAD_COIN_PK_Y_OFFSET)?;
 
         layouter.assign_region(
             || "",