Просмотр исходного кода

sdk: Add crypto module and rename crate to darkfi-sdk.

Luther Blissett 3 лет назад
Родитель
Сommit
247a5b0718
4 измененных файлов с 61 добавлено и 2 удалено
  1. 5 2
      src/sdk/Cargo.toml
  2. 14 0
      src/sdk/src/crypto/mod.rs
  3. 34 0
      src/sdk/src/crypto/nullifier.rs
  4. 8 0
      src/sdk/src/lib.rs

+ 5 - 2
src/sdk/Cargo.toml

@@ -1,8 +1,11 @@
 [package]
-name = "drk-sdk"
+name = "darkfi-sdk"
 version = "0.3.0"
 edition = "2021"
 
 [dependencies]
-borsh = "0.9.3"
+# Error handling
 thiserror = "1.0.37"
+
+# Cryptography
+pasta_curves = "0.4.0"

+ 14 - 0
src/sdk/src/crypto/mod.rs

@@ -0,0 +1,14 @@
+//! This module contains a bit more minimal implementations of the
+//! objects and types that can be found in `darkfi::crypto`.
+//! This is done so we can have a lot less dependencies in this SDK,
+//! and therefore make compilation of smart contracts faster in a sense.
+//!
+//! Eventually, we should strive to somehow migrate the types from
+//! `darkfi::crypto` into here, and then implement certain functionality
+//! in the library using traits.
+//! If you feel like trying, please help out with this migration, but do
+//! it properly, with care, and write documentation while you're at it.
+
+/// Nullifier definitions
+pub mod nullifier;
+pub use nullifier::Nullifier;

+ 34 - 0
src/sdk/src/crypto/nullifier.rs

@@ -0,0 +1,34 @@
+use pasta_curves::{group::ff::PrimeField, pallas};
+
+/// The `Nullifier` is represented as a base field element.
+#[repr(C)]
+#[derive(Debug, Clone, Copy)]
+pub struct Nullifier(pallas::Base);
+
+impl Nullifier {
+    /// Reference the raw inner base field element
+    pub fn inner(&self) -> pallas::Base {
+        self.0
+    }
+
+    /// Try to create a `Nullifier` type from the given 32 bytes.
+    /// Returns `Some` if the bytes fit in the base field, and `None` if not.
+    pub fn from_bytes(bytes: [u8; 32]) -> Option<Self> {
+        let n = pallas::Base::from_repr(bytes);
+        match bool::from(n.is_some()) {
+            true => Some(Self(n.unwrap())),
+            false => None,
+        }
+    }
+
+    /// Convert the `Nullifier` type into 32 raw bytes
+    pub fn to_bytes(&self) -> [u8; 32] {
+        self.0.to_repr()
+    }
+}
+
+impl From<pallas::Base> for Nullifier {
+    fn from(x: pallas::Base) -> Self {
+        Self(x)
+    }
+}

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

@@ -1,3 +1,11 @@
+/// Entrypoint used for the wasm binaries
 pub mod entrypoint;
+
+/// Error handling
 pub mod error;
+
+/// Logging infrastructure
 pub mod log;
+
+/// Crypto-related definitions
+pub mod crypto;