Quellcode durchsuchen

script/research: renamed consensus_headstart as consensus_parameters and generalize to extract direct pallas creation of constants using repr bytes

aggstam vor 3 Jahren
Ursprung
Commit
710253d4ca

+ 0 - 53
script/research/consensus_headstart/src/main.rs

@@ -1,53 +0,0 @@
-/* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2023 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- */
-
-use darkfi::consensus::lead_coin::LeadCoin;
-use darkfi_serial::serialize;
-
-/// Extract currently configured consensus headstart parameter
-/// pallas::Base representation and print its bs58 encoding.
-fn main() {
-    let headstart = bs58::encode(&serialize(&LeadCoin::headstart())).into_string();
-    println!("Currently configured headstart(encoded): {headstart}");
-}
-
-#[cfg(test)]
-mod tests {
-    use darkfi::{consensus::lead_coin::LeadCoin, Error, Result};
-    use darkfi_sdk::{crypto::pasta_prelude::PrimeField, pasta::pallas};
-    use darkfi_serial::serialize;
-
-    #[test]
-    fn test_conversion() -> Result<()> {
-        let headstart = LeadCoin::headstart();
-        let encoded = bs58::encode(&serialize(&headstart)).into_string();
-        let pallas = from_bs58str(&encoded)?;
-        assert_eq!(headstart, pallas);
-
-        Ok(())
-    }
-
-    fn from_bs58str(s: &str) -> Result<pallas::Base> {
-        let decoded = bs58::decode(s).into_vec()?;
-        if decoded.len() != 32 {
-            return Err(Error::DecodeError("Decoded bs58 string is not 32 bytes long"))
-        }
-        let bytes: [u8; 32] = decoded.try_into().unwrap();
-        Ok(pallas::Base::from_repr(bytes).unwrap())
-    }
-}

+ 0 - 0
script/research/consensus_headstart/.gitignore → script/research/consensus_parameters/.gitignore


+ 2 - 6
script/research/consensus_headstart/Cargo.toml → script/research/consensus_parameters/Cargo.toml

@@ -1,7 +1,7 @@
 [package]
-name = "consensus_headstart"
+name = "consensus_parameters"
 version = "0.4.1"
-description = "Command-line tool to extract currently configured consensus headstart parameter"
+description = "Command-line tool to extract currently configured consensus parameters"
 authors = ["Dyne.org foundation <foundation@dyne.org>"]
 repository = "https://github.com/darkrenaissance/darkfi"
 license = "AGPL-3.0-only"
@@ -10,11 +10,7 @@ edition = "2021"
 [workspace]
 
 [dependencies]
-bs58 = "0.4.0"
 darkfi = {path = "../../../", features = ["blockchain"]}
-darkfi-serial = {path = "../../../src/serial"}
-
-[dev-dependencies]
 darkfi-sdk = {path = "../../../src/sdk"}
 
 [patch.crates-io]

+ 58 - 0
script/research/consensus_parameters/src/main.rs

@@ -0,0 +1,58 @@
+/* This file is part of DarkFi (https://dark.fi)
+ *
+ * Copyright (C) 2020-2023 Dyne.org foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+use darkfi::consensus::lead_coin::LeadCoin;
+use darkfi_sdk::{crypto::pasta_prelude::PrimeField, pasta::Fp};
+
+/// Convert a pallas::Base repr to corresponding bytes array
+fn to_bytes(repr: <Fp as PrimeField>::Repr) -> [u64; 4] {
+    let mut res = [0; 4];
+    res[0] = u64::from_le_bytes(repr[0..8].try_into().unwrap());
+    res[1] = u64::from_le_bytes(repr[8..16].try_into().unwrap());
+    res[2] = u64::from_le_bytes(repr[16..24].try_into().unwrap());
+    res[3] = u64::from_le_bytes(repr[24..32].try_into().unwrap());
+
+    res
+}
+
+/// Extract currently configured consensus parameters
+/// pallas::Base::Repr bytes to use as constants.
+fn main() {
+    let headstart = to_bytes(LeadCoin::headstart().to_repr());
+    println!("Constants:");
+    println!("\tconst HEADSTART: pallas::Base = pallas::Base::from_raw({headstart:?});");
+}
+
+#[cfg(test)]
+mod tests {
+    use darkfi::consensus::lead_coin::LeadCoin;
+    use darkfi_sdk::pasta::pallas;
+
+    const HEADSTART: pallas::Base = pallas::Base::from_raw([
+        11731824086999220879,
+        11830614503713258191,
+        737869762948382064,
+        46116860184273879,
+    ]);
+
+    #[test]
+    fn test_headstart() {
+        let headstart = LeadCoin::headstart();
+        assert_eq!(headstart, HEADSTART);
+    }
+}