Prechádzať zdrojové kódy

consensus::coins: fixed leadcoin generation, cargo fmt

aggstam 3 rokov pred
rodič
commit
e9a8b594a8

+ 20 - 2
src/consensus/coins.rs

@@ -94,7 +94,7 @@ fn create_coins(
         seeds.push(rho);
     }
     let (sks, root_sks, path_sks) = create_coins_sks();
-
+    let mut tree_cm = BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(*EPOCH_LENGTH as usize);
     // Leadcoins matrix were each row represents a slot and contains its competing coins.
     let mut coins: Vec<Vec<LeadCoin>> = vec![];
     for i in 0..*EPOCH_LENGTH {
@@ -113,6 +113,7 @@ fn create_coins(
                     path_sks[index],
                     seeds[index],
                     sks[index],
+                    &mut tree_cm,
                 );
                 slot_coins.push(coin);
             }
@@ -131,6 +132,7 @@ fn create_coins(
             path_sks[index],
             seeds[index],
             sks[index],
+            &mut tree_cm,
         );
         coins.push(vec![coin]);
     }
@@ -192,6 +194,7 @@ fn create_leadcoin(
     c_path_sk: [MerkleNode; MERKLE_DEPTH_ORCHARD],
     seed: u64,
     sk: SecretKey,
+    tree_cm: &mut BridgeTree<MerkleNode, MERKLE_DEPTH>,
 ) -> LeadCoin {
     // keypair
     let keypair: Keypair = Keypair::new(sk);
@@ -200,7 +203,7 @@ fn create_leadcoin(
     let one = pallas::Base::one();
     let c_cm1_blind: DrkValueBlind = pallas::Scalar::random(&mut rng);
     let c_cm2_blind: DrkValueBlind = pallas::Scalar::random(&mut rng);
-    let mut tree_cm = BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(*EPOCH_LENGTH as usize);
+
     let c_v = pallas::Base::from(value);
     // coin relative slot index in the epoch
     let c_sl = pallas::Base::from(u64::try_from(i).unwrap());
@@ -235,6 +238,21 @@ fn create_leadcoin(
     let leaf_position = tree_cm.witness();
     let c_root_cm = tree_cm.root(0).unwrap();
     let c_cm_path = tree_cm.authentication_path(leaf_position.unwrap(), &c_root_cm).unwrap();
+    /*
+    let c_root_cm = {
+        let mut current = MerkleNode::from(c_cm_base);
+        let pos = leaf_position.unwrap();
+        for (level, sibling) in c_cm_path.iter().enumerate() {
+            let level = level as u8;
+            current = if pos & (1 << level) == 0 {
+                MerkleNode::combine(level.into(), &current, sibling)
+            } else {
+                MerkleNode::combine(level.into(), sibling, &current)
+            };
+        }
+        current
+    };
+    */
 
     let coin_nonce2_msg = [c_seed, c_root_sk.inner()];
     let c_seed2: pallas::Base =

+ 5 - 10
src/crypto/leadcoin.rs

@@ -27,7 +27,7 @@ pub struct LeadCoin {
     pub sn: Option<pallas::Base>,       // coin's serial number
     pub keypair: Option<Keypair>,
     pub root_cm: Option<pallas::Base>, // root of coin commitment
-    pub root_sk: Option<pallas::Base>,   // coin's secret key
+    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
     pub c1_blind: Option<pallas::Scalar>, // coin opening
@@ -57,16 +57,11 @@ impl LeadCoin {
         let po_y_y = *po_y_pt.to_affine().coordinates().unwrap().y();
         let y_coord_arr = [po_y_x, po_y_y];
         let po_y: pallas::Base =
-            poseidon::Hash::<_, poseidon::P128Pow5T3, poseidon::ConstantLength<2>, 3, 2>::init().hash(y_coord_arr);
+            poseidon::Hash::<_, poseidon::P128Pow5T3, poseidon::ConstantLength<2>, 3, 2>::init()
+                .hash(y_coord_arr);
         let cm_pos = self.idx;
-        let public_inputs: [pallas::Base; LEAD_PUBLIC_INPUT_LEN] = [
-            *po_cm.x(),
-            *po_cm.y(),
-            po_nonce,
-            *po_pk.x(),
-            *po_pk.y(),
-            po_y,
-        ];
+        let public_inputs: [pallas::Base; LEAD_PUBLIC_INPUT_LEN] =
+            [*po_cm.x(), *po_cm.y(), po_nonce, *po_pk.x(), *po_pk.y(), po_y];
         public_inputs
     }
 

+ 14 - 28
src/zk/circuit/lead_contract.rs

@@ -309,7 +309,8 @@ 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)?;
+        let root_cm =
+            self.load_private(layouter.namespace(|| ""), config.advices[0], self.root_cm)?;
 
         // staking coin secret key
         let sk: AssignedCell<Fp, Fp> =
@@ -654,22 +655,10 @@ impl Circuit<pallas::Base> for LeadContract {
             LEAD_COIN_COMMIT_Y_OFFSET,
         )?;
 
-        layouter.constrain_instance(
-            coin2_nonce.cell(),
-            config.primary,
-            LEAD_COIN_NONCE2_OFFSET
-        )?;
+        layouter.constrain_instance(coin2_nonce.cell(), config.primary, LEAD_COIN_NONCE2_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.constrain_instance(
             y_commit_base.cell(),
@@ -677,23 +666,20 @@ impl Circuit<pallas::Base> for LeadContract {
             LEAD_Y_COMMIT_BASE_OFFSET,
         )?;
 
-        rho_commit.constrain_equal(
-            layouter.namespace(||""),
-            &rho
-        )?;
+        rho_commit.constrain_equal(layouter.namespace(|| ""), &rho)?;
         let ref_coin2_cm = NonIdentityPoint::new(
             ecc_chip.clone(),
             layouter.namespace(|| "witness coin2 cm"),
             self.coin2_commit.map(|x| x.to_affine()),
         )?;
-        coin2_commit.constrain_equal(
-            layouter.namespace(||""),
-            &ref_coin2_cm
-        )?;
-        layouter.assign_region(||"", |mut region| {
-            region.constrain_equal(sn_commit.cell(),coin1_sn.cell())?;
-            region.constrain_equal(coin_cm_root.cell(), root_cm.cell())
-        });
+        coin2_commit.constrain_equal(layouter.namespace(|| ""), &ref_coin2_cm)?;
+        layouter.assign_region(
+            || "",
+            |mut region| {
+                region.constrain_equal(sn_commit.cell(), coin1_sn.cell())?;
+                region.constrain_equal(coin_cm_root.cell(), root_cm.cell())
+            },
+        );
 
         Ok(())
     }