Repository: ergoplatform/sigma-rust (language: rust)

Issue:
Remove byteorder dependency
Current usage of `byteorder::WriteBytesExt` is not no_std compatible. Can be replaced with `to_be_bytes()`


Discussion / hints:

Fix patch:
diff --git a/Cargo.toml b/Cargo.toml
index 4731738d0..e416e7b4a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -77,7 +77,6 @@ serde_with = { version = "3.11.0", default-features = false, features = [
 ] }
 rand = "0.8.5"
 bytes = { version = "1.1", default-features = false }
-byteorder = "1"
 futures = "0.3"
 tokio = { version = "1.15.0", features = ["full"] }
 tokio-stream = { version = "0.1.8", features = ["sync", "time"] }
diff --git a/ergo-chain-generation/Cargo.toml b/ergo-chain-generation/Cargo.toml
index b3ef0d5cf..9191aeac8 100644
--- a/ergo-chain-generation/Cargo.toml
+++ b/ergo-chain-generation/Cargo.toml
@@ -10,7 +10,6 @@ edition.workspace = true
 [dependencies]
 base16 = { workspace = true }
 blake2 = { workspace = true }
-byteorder = { workspace = true }
 ergo-lib = { workspace = true }
 num-bigint = { workspace = true }
 rand = { workspace = true }
diff --git a/ergo-chain-generation/src/chain_generation.rs b/ergo-chain-generation/src/chain_generation.rs
index 9367cb013..20ded310b 100644
--- a/ergo-chain-generation/src/chain_generation.rs
+++ b/ergo-chain-generation/src/chain_generation.rs
@@ -202,10 +202,7 @@ fn prove_block(
     let x = DlogProverInput::random();
     let x_bigint = BigInt::from_bytes_be(Sign::Plus, &x.to_bytes());
 
-    use byteorder::{BigEndian, WriteBytesExt};
-    let mut height_bytes = Vec::with_capacity(4);
-    #[allow(clippy::unwrap_used)]
-    height_bytes.write_u32::<BigEndian>(header.height).unwrap();
+    let height_bytes = header.height.to_be_bytes();
     let popow_algos = ergo_nipopow::NipopowAlgos::default();
     let big_n = popow_algos.pow_scheme.calc_big_n(version, height);
 
@@ -217,11 +214,7 @@ fn prove_block(
     let p1 = sk.public_image_bytes().unwrap();
     let p2 = x.public_image().h.sigma_serialize_bytes().unwrap();
     for i in min_nonce..max_nonce {
-        let nonce = {
-            let mut bytes = vec![];
-            bytes.write_i64::<BigEndian>(i).unwrap();
-            bytes
-        };
+        let nonce = i.to_be_bytes();
         let seed_hash = if version == 1 {
             let mut seed = msg.clone();
             seed.extend(&nonce);
@@ -238,8 +231,7 @@ fn prove_block(
             .gen_indexes(&seed_hash, big_n)
             .into_iter()
             .map(|ix| {
-                let mut index_bytes = vec![];
-                index_bytes.write_u32::<BigEndian>(ix).unwrap();
+                let index_bytes = ix.to_be_bytes();
                 generate_element(version, &msg, &p1, &p2, &index_bytes, &height_bytes)
             })
             .fold(BigInt::from(0_u8), |acc, e| acc + e);
@@ -253,7 +245,7 @@ fn prove_block(
             let autolykos_solution = AutolykosSolution {
                 miner_pk: sk.public_key().unwrap().public_key.into(),
                 pow_onetime_pk: Some(x.public_image().h),
-                nonce,
+                nonce: nonce.to_vec(),
                 pow_distance: Some(d),
             };
             // Compute header ID
diff --git a/ergo-lib/Cargo.toml b/ergo-lib/Cargo.toml
index d21362e81..8a831408f 100644
--- a/ergo-lib/Cargo.toml
+++ b/ergo-lib/Cargo.toml
@@ -76,7 +76,6 @@ ergo-merkle-tree = { workspace = true, features = ["arbitrary"] }
 sigma-test-util = { workspace = true }
 pretty_assertions = { workspace = true }
 bs58 = { workspace = true }
-byteorder = { workspace = true }
 expect-test = { workspace = true }
 
 
diff --git a/ergo-nipopow/Cargo.toml b/ergo-nipopow/Cargo.toml
index b1cd55b92..8b5d584b1 100644
--- a/ergo-nipopow/Cargo.toml
+++ b/ergo-nipopow/Cargo.toml
@@ -14,7 +14,6 @@ crate-type = ["cdylib", "rlib"]
 [dependencies]
 base16 = { workspace = true }
 bounded-integer = { workspace = true }
-byteorder = { workspace = true }
 thiserror = { workspace = true }
 derive_more = { workspace = true }
 ergotree-ir = { workspace = true, features = ["json"] }
diff --git a/ergo-nipopow/src/autolykos_pow_scheme.rs b/ergo-nipopow/src/autolykos_pow_scheme.rs
index b596288f8..9ada5ca9f 100644
--- a/ergo-nipopow/src/autolykos_pow_scheme.rs
+++ b/ergo-nipopow/src/autolykos_pow_scheme.rs
@@ -37,13 +37,10 @@ impl AutolykosPowScheme {
                 .cloned()
                 .ok_or(AutolykosPowSchemeError::MissingPowDistanceParameter)
         } else {
-            use byteorder::{BigEndian, WriteBytesExt};
             // hit for version 2
             let msg = blake2b256_hash(&header.serialize_without_pow()?).to_vec();
             let nonce = header.autolykos_solution.nonce.clone();
-            let mut height_bytes = Vec::with_capacity(4);
-            #[allow(clippy::unwrap_used)]
-            height_bytes.write_u32::<BigEndian>(header.height).unwrap();
+            let height_bytes = header.height.to_be_bytes();
 
             let mut concat = msg.clone();
             concat.extend(&nonce);
@@ -56,8 +53,7 @@ impl AutolykosPowScheme {
             let f2 = indexes.into_iter().fold(BigInt::from(0u32), |acc, idx| {
                 // This is specific to autolykos v2.
                 let mut concat = vec![];
-                #[allow(clippy::unwrap_used)]
-                concat.write_u32::<BigEndian>(idx).unwrap();
+                concat.extend_from_slice(&idx.to_be_bytes());
                 concat.extend(&height_bytes);
                 concat.extend(&self.calc_big_m());
                 acc + BigInt::from_bytes_be(Sign::Plus, &blake2b256_hash(&concat)[1..])
@@ -72,15 +68,7 @@ impl AutolykosPowScheme {
 
     /// Constant data to be added to hash function to increase its calculation time
     pub fn calc_big_m(&self) -> Vec<u8> {
-        use byteorder::{BigEndian, WriteBytesExt};
-        (0u64..1024)
-            .flat_map(|x| {
-                let mut bytes = Vec::with_capacity(8);
-                #[allow(clippy::unwrap_used)]
-                bytes.write_u64::<BigEndian>(x).unwrap();
-                bytes
-            })
-            .collect()
+        (0u64..1024).flat_map(|x| x.to_be_bytes()).collect()
     }
 
     /// Computes `J` (denoted by `seed` in Ergo implementation) line 4, algorithm 1 of Autolykos v2


Test patch: