Bläddra i källkod

sdk: Copy float10 ops from the main lib.

parazyd 3 år sedan
förälder
incheckning
f48a8f29da
4 ändrade filer med 98 tillägg och 0 borttagningar
  1. 1 0
      Cargo.lock
  2. 3 0
      src/sdk/Cargo.toml
  3. 92 0
      src/sdk/src/float10.rs
  4. 2 0
      src/sdk/src/lib.rs

+ 1 - 0
Cargo.lock

@@ -1318,6 +1318,7 @@ dependencies = [
  "bs58",
  "chacha20poly1305",
  "darkfi-serial",
+ "dashu",
  "halo2_gadgets",
  "halo2_proofs",
  "incrementalmerkletree",

+ 3 - 0
src/sdk/Cargo.toml

@@ -21,6 +21,9 @@ darkfi-serial = {version = "0.4.1", path = "../serial", features = ["derive", "c
 # Encoding
 bs58 = "0.4.0"
 
+# Big float high precision arithmetics
+dashu = {version = "0.3.1", optional = true}
+
 # Cryptography
 blake2b_simd = "1.0.1"
 blake3 = "1.3.3"

+ 92 - 0
src/sdk/src/float10.rs

@@ -0,0 +1,92 @@
+/* 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/>.
+ */
+
+//! Type aliases used in the consensus codebase.
+use core::ops::{Add, AddAssign, Div, Mul, Sub};
+
+use dashu::{
+    base::Abs,
+    float::{round::mode::Zero, FBig, Repr},
+};
+
+const B: u64 = 10;
+
+#[derive(Clone, PartialEq, PartialOrd, Debug)]
+pub struct Float10(FBig<Zero, B>);
+
+impl Float10 {
+    pub fn repr(&self) -> &Repr<B> {
+        self.0.repr()
+    }
+
+    pub fn abs(&self) -> Self {
+        Self(self.0.clone().abs())
+    }
+
+    pub fn powf(&self, exp: Self) -> Self {
+        Self(self.0.powf(&exp.0))
+    }
+
+    pub fn ln(&self) -> Self {
+        Self(self.0.ln())
+    }
+}
+
+impl Add for Float10 {
+    type Output = Self;
+
+    fn add(self, other: Self) -> Self {
+        Self(self.0 + other.0)
+    }
+}
+
+impl AddAssign for Float10 {
+    fn add_assign(&mut self, other: Self) {
+        *self = Self(self.0.clone() + other.0);
+    }
+}
+
+impl Sub for Float10 {
+    type Output = Self;
+
+    fn sub(self, other: Self) -> Self {
+        Self(self.0 - other.0)
+    }
+}
+
+impl Mul for Float10 {
+    type Output = Self;
+
+    fn mul(self, other: Self) -> Self {
+        Self(self.0 * other.0)
+    }
+}
+
+impl Div for Float10 {
+    type Output = Self;
+
+    fn div(self, other: Self) -> Self {
+        Self(self.0 / other.0)
+    }
+}
+
+impl std::fmt::Display for Float10 {
+    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+        write!(f, "{}", self.0)
+    }
+}

+ 2 - 0
src/sdk/src/lib.rs

@@ -46,3 +46,5 @@ pub use tx::ContractCall;
 
 /// Utility functions
 pub mod util;
+
+pub mod float10;