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

crypto: Stricter API for nullifier and secretkey.

Luther Blissett 3 лет назад
Родитель
Сommit
9865b21a28

+ 43 - 122
Cargo.lock

@@ -207,7 +207,7 @@ dependencies = [
  "async-io",
  "autocfg",
  "blocking",
- "cfg-if 1.0.0",
+ "cfg-if",
  "event-listener",
  "futures-lite",
  "libc",
@@ -325,7 +325,7 @@ checksum = "cab84319d616cfb654d03394f38ab7e6f0919e181b1b57e1fd15e7fb4077d9a7"
 dependencies = [
  "addr2line",
  "cc",
- "cfg-if 1.0.0",
+ "cfg-if",
  "libc",
  "miniz_oxide",
  "object 0.29.0",
@@ -410,7 +410,7 @@ dependencies = [
  "arrayref",
  "arrayvec",
  "cc",
- "cfg-if 1.0.0",
+ "cfg-if",
  "constant_time_eq",
  "digest 0.10.5",
 ]
@@ -447,51 +447,6 @@ dependencies = [
  "once_cell",
 ]
 
-[[package]]
-name = "borsh"
-version = "0.9.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "15bf3650200d8bffa99015595e10f1fbd17de07abbc25bb067da79e769939bfa"
-dependencies = [
- "borsh-derive",
- "hashbrown 0.11.2",
-]
-
-[[package]]
-name = "borsh-derive"
-version = "0.9.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6441c552f230375d18e3cc377677914d2ca2b0d36e52129fe15450a2dce46775"
-dependencies = [
- "borsh-derive-internal",
- "borsh-schema-derive-internal",
- "proc-macro-crate 0.1.5",
- "proc-macro2",
- "syn",
-]
-
-[[package]]
-name = "borsh-derive-internal"
-version = "0.9.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5449c28a7b352f2d1e592a8a28bf139bc71afb0764a14f3c02500935d8c44065"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
-]
-
-[[package]]
-name = "borsh-schema-derive-internal"
-version = "0.9.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cdbd5696d8bfa21d53d9fe39a714a18538bad11492a42d066dbbc395fb1951c0"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
-]
-
 [[package]]
 name = "bs58"
 version = "0.4.0"
@@ -610,12 +565,6 @@ version = "1.0.73"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
 
-[[package]]
-name = "cfg-if"
-version = "0.1.10"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
-
 [[package]]
 name = "cfg-if"
 version = "1.0.0"
@@ -638,7 +587,7 @@ version = "0.9.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "c7fc89c7c5b9e7a02dfe45cd2367bae382f9ed31c61ca8debe5f827c420a2f08"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "cipher 0.4.3",
  "cpufeatures",
 ]
@@ -866,7 +815,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9847f90f32a50b0dcbd68bc23ff242798b13080b97b0569f6ed96a45ce4cf2cd"
 dependencies = [
  "autocfg",
- "cfg-if 1.0.0",
+ "cfg-if",
  "libc",
  "scopeguard",
  "windows-sys 0.33.0",
@@ -961,7 +910,7 @@ version = "1.3.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
 ]
 
 [[package]]
@@ -970,7 +919,7 @@ version = "0.5.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "crossbeam-utils",
 ]
 
@@ -980,7 +929,7 @@ version = "0.8.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "crossbeam-epoch",
  "crossbeam-utils",
 ]
@@ -992,7 +941,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f916dfc5d356b0ed9dae65f1db9fc9770aa2851d2662b988ccf4fe3516e86348"
 dependencies = [
  "autocfg",
- "cfg-if 1.0.0",
+ "cfg-if",
  "crossbeam-utils",
  "memoffset",
  "scopeguard",
@@ -1004,7 +953,7 @@ version = "0.3.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "1cd42583b04998a5363558e5f9291ee5a5ff6b49944332103f251e7479a82aa7"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "crossbeam-utils",
 ]
 
@@ -1014,7 +963,7 @@ version = "0.8.12"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "edbafec5fa1f196ca66527c1b12c2ec4745ca14b50f1ad8f9f6f720b55d11fac"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
 ]
 
 [[package]]
@@ -1215,8 +1164,8 @@ dependencies = [
  "crypto_api_chachapoly",
  "darkfi-derive",
  "darkfi-derive-internal",
+ "darkfi-sdk",
  "dirs",
- "drk-sdk",
  "ed25519-compact",
  "fast-socks5",
  "futures",
@@ -1270,7 +1219,7 @@ name = "darkfi-derive"
 version = "0.3.0"
 dependencies = [
  "darkfi-derive-internal",
- "proc-macro-crate 1.2.1",
+ "proc-macro-crate",
  "proc-macro2",
  "syn",
 ]
@@ -1284,6 +1233,14 @@ dependencies = [
  "syn",
 ]
 
+[[package]]
+name = "darkfi-sdk"
+version = "0.3.0"
+dependencies = [
+ "pasta_curves",
+ "thiserror",
+]
+
 [[package]]
 name = "darkfid"
 version = "0.3.0"
@@ -1529,7 +1486,7 @@ version = "2.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "dirs-sys-next",
 ]
 
@@ -1615,15 +1572,6 @@ dependencies = [
  "url",
 ]
 
-[[package]]
-name = "drk-sdk"
-version = "0.3.0"
-dependencies = [
- "borsh",
- "thiserror",
- "wee_alloc",
-]
-
 [[package]]
 name = "dryoc"
 version = "0.3.14"
@@ -2147,7 +2095,7 @@ version = "0.1.16"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "libc",
  "wasi 0.9.0+wasi-snapshot-preview1",
 ]
@@ -2158,7 +2106,7 @@ version = "0.2.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "libc",
  "wasi 0.11.0+wasi-snapshot-preview1",
 ]
@@ -2419,7 +2367,7 @@ version = "0.1.12"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
 ]
 
 [[package]]
@@ -2579,7 +2527,7 @@ version = "0.7.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "winapi",
 ]
 
@@ -2634,7 +2582,7 @@ version = "0.4.17"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "value-bag",
 ]
 
@@ -2692,12 +2640,6 @@ dependencies = [
  "autocfg",
 ]
 
-[[package]]
-name = "memory_units"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
-
 [[package]]
 name = "minimal-lexical"
 version = "0.2.1"
@@ -2757,7 +2699,7 @@ checksum = "e322c04a9e3440c327fca7b6c8a63e6890a32fa2ad689db972425f07e0d22abb"
 dependencies = [
  "autocfg",
  "bitflags",
- "cfg-if 1.0.0",
+ "cfg-if",
  "libc",
 ]
 
@@ -2883,7 +2825,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "12fc0523e3bd51a692c8850d075d74dc062ccf251c0110668cbd921917118a13"
 dependencies = [
  "bitflags",
- "cfg-if 1.0.0",
+ "cfg-if",
  "foreign-types",
  "libc",
  "once_cell",
@@ -2960,7 +2902,7 @@ version = "0.8.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "instant",
  "libc",
  "redox_syscall",
@@ -2974,7 +2916,7 @@ version = "0.9.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "libc",
  "redox_syscall",
  "smallvec",
@@ -3149,7 +3091,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "899b00b9c8ab553c743b3e11e87c5c7d423b2a2de229ba95b24a756344748011"
 dependencies = [
  "autocfg",
- "cfg-if 1.0.0",
+ "cfg-if",
  "libc",
  "log",
  "wepoll-ffi",
@@ -3187,15 +3129,6 @@ dependencies = [
  "unicode-width",
 ]
 
-[[package]]
-name = "proc-macro-crate"
-version = "0.1.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785"
-dependencies = [
- "toml",
-]
-
 [[package]]
 name = "proc-macro-crate"
 version = "1.2.1"
@@ -3765,7 +3698,7 @@ version = "0.10.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "cpufeatures",
  "digest 0.10.5",
 ]
@@ -3777,7 +3710,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800"
 dependencies = [
  "block-buffer 0.9.0",
- "cfg-if 1.0.0",
+ "cfg-if",
  "cpufeatures",
  "digest 0.9.0",
  "opaque-debug",
@@ -3789,7 +3722,7 @@ version = "0.10.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "cpufeatures",
  "digest 0.10.5",
 ]
@@ -4235,7 +4168,7 @@ version = "3.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "fastrand",
  "libc",
  "redox_syscall",
@@ -4393,7 +4326,7 @@ version = "0.1.37"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "log",
  "pin-project-lite",
  "tracing-attributes",
@@ -4679,7 +4612,7 @@ version = "0.2.83"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "wasm-bindgen-macro",
 ]
 
@@ -4704,7 +4637,7 @@ version = "0.4.33"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "23639446165ca5a5de86ae1d8896b737ae80319560fbaa4c2887b7da6e7ebd7d"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "js-sys",
  "wasm-bindgen",
  "web-sys",
@@ -4754,7 +4687,7 @@ version = "2.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ea8d8361c9d006ea3d7797de7bd6b1492ffd0f91a22430cfda6c1658ad57bedf"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "indexmap",
  "js-sys",
  "loupe",
@@ -4886,7 +4819,7 @@ version = "2.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ad0358af9c154724587731175553805648d9acb8f6657880d165e378672b7e53"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "enum-iterator",
  "enumset",
  "leb128",
@@ -4912,7 +4845,7 @@ version = "2.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "440dc3d93c9ca47865a4f4edd037ea81bf983b5796b59b3d712d844b32dbef15"
 dependencies = [
- "cfg-if 1.0.0",
+ "cfg-if",
  "enumset",
  "leb128",
  "loupe",
@@ -4990,7 +4923,7 @@ checksum = "30d965fa61f4dc4cdb35a54daaf7ecec3563fbb94154a6c35433f879466247dd"
 dependencies = [
  "backtrace",
  "cc",
- "cfg-if 1.0.0",
+ "cfg-if",
  "corosensei",
  "enum-iterator",
  "indexmap",
@@ -5057,18 +4990,6 @@ dependencies = [
  "untrusted",
 ]
 
-[[package]]
-name = "wee_alloc"
-version = "0.4.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"
-dependencies = [
- "cfg-if 0.1.10",
- "libc",
- "memory_units",
- "winapi",
-]
-
 [[package]]
 name = "weezl"
 version = "0.1.7"

+ 3 - 2
Cargo.toml

@@ -119,7 +119,7 @@ pasta_curves = {version = "0.4.0", optional = true}
 sha2 = {version = "0.10.6", optional = true}
 
 # Smart contract runtime
-drk-sdk = {path = "src/sdk", optional = true}
+darkfi-sdk = {path = "src/sdk", optional = true}
 wasmer = {version = "2.3.0", optional = true}
 wasmer-compiler-singlepass = {version = "2.3.0", optional = true}
 wasmer-middlewares = {version = "2.3.0", optional = true}
@@ -325,7 +325,8 @@ wasm-runtime = [
     "wasmer-compiler-singlepass",
     "wasmer-middlewares",
 
-    "drk-sdk",
+    "darkfi-sdk",
+    "node",
 ]
 
 websockets = [

+ 2 - 2
bin/daod/src/dao_contract/propose/wallet.rs

@@ -89,7 +89,7 @@ impl Builder {
             let leaf_pos: u64 = input.leaf_position.into();
 
             let prover_witnesses = vec![
-                Witness::Base(Value::known(input.secret.0)),
+                Witness::Base(Value::known(input.secret.inner())),
                 Witness::Base(Value::known(note.serial)),
                 Witness::Base(Value::known(pallas::Base::from(0))),
                 Witness::Base(Value::known(pallas::Base::from(0))),
@@ -100,7 +100,7 @@ impl Builder {
                 Witness::Base(Value::known(gov_token_blind)),
                 Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
                 Witness::MerklePath(Value::known(input.merkle_path.clone().try_into().unwrap())),
-                Witness::Base(Value::known(input.signature_secret.0)),
+                Witness::Base(Value::known(input.signature_secret.inner())),
             ];
 
             let public_key = PublicKey::from_secret(input.secret);

+ 4 - 9
bin/daod/src/dao_contract/vote/wallet.rs

@@ -95,7 +95,7 @@ impl Builder {
             let leaf_pos: u64 = input.leaf_position.into();
 
             let prover_witnesses = vec![
-                Witness::Base(Value::known(input.secret.0)),
+                Witness::Base(Value::known(input.secret.inner())),
                 Witness::Base(Value::known(note.serial)),
                 Witness::Base(Value::known(pallas::Base::from(0))),
                 Witness::Base(Value::known(pallas::Base::from(0))),
@@ -106,7 +106,7 @@ impl Builder {
                 Witness::Base(Value::known(gov_token_blind)),
                 Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
                 Witness::MerklePath(Value::known(input.merkle_path.clone().try_into().unwrap())),
-                Witness::Base(Value::known(input.signature_secret.0)),
+                Witness::Base(Value::known(input.signature_secret.inner())),
             ];
 
             let public_key = PublicKey::from_secret(input.secret);
@@ -140,7 +140,7 @@ impl Builder {
             let token_commit = poseidon_hash::<2>([note.token_id, gov_token_blind]);
             assert_eq!(self.dao.gov_token_id, note.token_id);
 
-            let nullifier = poseidon_hash::<2>([input.secret.0, note.serial]);
+            let nullifier = Nullifier::from(poseidon_hash::<2>([input.secret.0, note.serial]));
 
             let vote_commit = pedersen_commitment_u64(note.value, vote_value_blind);
             let vote_commit_coords = vote_commit.to_affine().coordinates().unwrap();
@@ -164,12 +164,7 @@ impl Builder {
                 .expect("DAO::vote() proving error!");
             proofs.push(input_proof);
 
-            let input = Input {
-                nullifier: Nullifier(nullifier),
-                vote_commit,
-                merkle_root,
-                signature_public,
-            };
+            let input = Input { nullifier, vote_commit, merkle_root, signature_public };
             inputs.push(input);
         }
 

+ 3 - 1
bin/vanityaddr/src/main.rs

@@ -117,7 +117,9 @@ fn main() {
 
         println!(
             "{{\"address\":\"{}\",\"attempts\":{},\"secret\":\"{:?}\"}}",
-            addr.address, attempts, addr.secret.0
+            addr.address,
+            attempts,
+            addr.secret.inner()
         );
 
         exit(0);

+ 2 - 1
example/tx.rs

@@ -12,6 +12,7 @@ use darkfi::{
         note::{EncryptedNote, Note},
         nullifier::Nullifier,
         proof::{ProvingKey, VerifyingKey},
+        util::poseidon_hash,
     },
     node::state::{state_transition, ProgramState, StateUpdate},
     tx::builder::{
@@ -95,7 +96,7 @@ impl MemoryState {
             // If it's our own coin, witness it and append to the vector.
             if let Some((note, secret)) = self.try_decrypt_note(enc_note) {
                 let leaf_position = self.tree.witness().unwrap();
-                let nullifier = Nullifier::new(secret, note.serial);
+                let nullifier = Nullifier::from(poseidon_hash::<2>([secret.inner(), note.serial]));
                 let own_coin = OwnCoin { coin, note, secret, nullifier, leaf_position };
                 self.own_coins.push(own_coin);
             }

+ 4 - 4
src/crypto/burn_proof.rs

@@ -53,7 +53,7 @@ impl BurnRevealedValues {
         user_data_blind: DrkUserDataBlind,
         signature_secret: SecretKey,
     ) -> Self {
-        let nullifier = poseidon_hash::<2>([secret.0, serial]);
+        let nullifier = Nullifier::from(poseidon_hash::<2>([secret.inner(), serial]));
 
         let public_key = PublicKey::from_secret(secret);
         let coords = public_key.0.to_affine().coordinates().unwrap();
@@ -91,7 +91,7 @@ impl BurnRevealedValues {
         BurnRevealedValues {
             value_commit,
             token_commit,
-            nullifier: Nullifier(nullifier),
+            nullifier,
             merkle_root,
             spend_hook,
             user_data_enc,
@@ -156,7 +156,7 @@ pub fn create_burn_proof(
     let leaf_position: u64 = leaf_position.into();
 
     let c = BurnContract {
-        secret_key: Value::known(secret.0),
+        secret_key: Value::known(secret.inner()),
         serial: Value::known(serial),
         value: Value::known(DrkValue::from(value)),
         token: Value::known(token_id),
@@ -168,7 +168,7 @@ pub fn create_burn_proof(
         spend_hook: Value::known(spend_hook),
         user_data: Value::known(user_data),
         user_data_blind: Value::known(user_data_blind),
-        sig_secret: Value::known(signature_secret.0),
+        sig_secret: Value::known(signature_secret.inner()),
     };
 
     let start = Instant::now();

+ 1 - 1
src/crypto/diffie_hellman.rs

@@ -23,7 +23,7 @@ pub fn sapling_ka_agree(esk: &SecretKey, pk_d: &PublicKey) -> PublicKey {
     // of bits instead of individual bits).
     // We want that to be fast because it's in the hot path for trial decryption of
     // notes on chain.
-    let esk_s = mod_r_p(esk.0);
+    let esk_s = mod_r_p(esk.inner());
     let mut wnaf = group::Wnaf::new();
     PublicKey(wnaf.scalar(&esk_s).base(pk_d.0).clear_cofactor())
 }

+ 11 - 1
src/crypto/keypair.rs

@@ -38,7 +38,7 @@ impl Keypair {
 }
 
 #[derive(Copy, Clone, PartialEq, Eq, Debug, SerialDecodable, SerialEncodable)]
-pub struct SecretKey(pub pallas::Base);
+pub struct SecretKey(pallas::Base);
 
 impl SecretKey {
     pub fn random(mut rng: impl RngCore) -> Self {
@@ -56,6 +56,16 @@ impl SecretKey {
             None => Err(Error::SecretKeyFromBytes),
         }
     }
+
+    pub fn inner(&self) -> pallas::Base {
+        self.0
+    }
+}
+
+impl From<pallas::Base> for SecretKey {
+    fn from(x: pallas::Base) -> Self {
+        Self(x)
+    }
 }
 
 impl FromStr for SecretKey {

+ 15 - 13
src/crypto/nullifier.rs

@@ -1,28 +1,30 @@
 use pasta_curves::{group::ff::PrimeField, pallas};
 
-use crate::{
-    crypto::{keypair::SecretKey, util::poseidon_hash},
-    serial::{SerialDecodable, SerialEncodable},
-};
+use crate::serial::{SerialDecodable, SerialEncodable};
 
 #[derive(Clone, Copy, Debug, PartialEq, Eq, SerialEncodable, SerialDecodable)]
-pub struct Nullifier(pub pallas::Base);
+pub struct Nullifier(pallas::Base);
 
 impl Nullifier {
-    pub fn new(secret: SecretKey, serial: pallas::Base) -> Self {
-        let nullifier = poseidon_hash::<2>([secret.0, serial]);
-        Nullifier(nullifier)
-    }
-
-    pub fn from_bytes(bytes: [u8; 32]) -> Self {
-        pallas::Base::from_repr(bytes).map(Nullifier).unwrap()
+    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,
+        }
     }
 
     pub fn to_bytes(self) -> [u8; 32] {
         self.0.to_repr()
     }
 
-    pub(crate) fn inner(&self) -> pallas::Base {
+    pub fn inner(&self) -> pallas::Base {
         self.0
     }
 }
+
+impl From<pallas::Base> for Nullifier {
+    fn from(x: pallas::Base) -> Self {
+        Self(x)
+    }
+}

+ 1 - 1
src/crypto/schnorr.rs

@@ -40,7 +40,7 @@ impl SchnorrSecret for SecretKey {
         let commit = NullifierK.generator() * mask;
 
         let challenge = hash_to_scalar(DRK_SCHNORR_DOMAIN, &commit.to_bytes(), message);
-        let response = mask + challenge * mod_r_p(self.0);
+        let response = mask + challenge * mod_r_p(self.inner());
 
         Signature { commit, response }
     }

+ 3 - 1
src/node/state.rs

@@ -12,6 +12,7 @@ use crate::{
         note::{EncryptedNote, Note},
         nullifier::Nullifier,
         proof::VerifyingKey,
+        util::poseidon_hash,
     },
     tx::Transaction,
     wallet::walletdb::WalletPtr,
@@ -162,7 +163,8 @@ impl State {
                 if let Some(note) = State::try_decrypt_note(enc_note, *secret) {
                     debug!(target: "state_apply", "Received a coin: amount {}", note.value);
                     let leaf_position = self.tree.witness().unwrap();
-                    let nullifier = Nullifier::new(*secret, note.serial);
+                    let nullifier =
+                        Nullifier::from(poseidon_hash::<2>([secret.inner(), note.serial]));
                     let own_coin = OwnCoin {
                         coin,
                         note: note.clone(),

+ 5 - 0
src/sdk/Cargo.toml

@@ -1,6 +1,11 @@
 [package]
 name = "darkfi-sdk"
 version = "0.3.0"
+homepage = "https://dark.fi"
+description = "SDK used for developing smart contracts on DarkFi"
+authors = ["darkfi <dev@dark.fi>"]
+repository = "https://github.com/darkrenaissance/darkfi"
+license = "AGPL-3.0-only"
 edition = "2021"
 
 [dependencies]

+ 9 - 13
src/stakeholder/mod.rs

@@ -1,14 +1,14 @@
+use std::fmt;
+
 use async_executor::Executor;
 use async_std::sync::Arc;
 use halo2_proofs::arithmetic::Field;
+use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
 use log::{debug, error, info};
-use std::fmt;
-
+use pasta_curves::{group::ff::PrimeField, pallas};
 use rand::rngs::OsRng;
 use std::{thread, time::Duration};
-
-use crate::zk::circuit::{BurnContract, LeadContract, MintContract};
-use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
+use url::Url;
 
 use crate::{
     blockchain::{Blockchain, Epoch, EpochConsensus},
@@ -27,7 +27,8 @@ use crate::{
         note::{EncryptedNote, Note},
         nullifier::Nullifier,
         proof::{Proof, ProvingKey, VerifyingKey},
-        schnorr::{SchnorrSecret},
+        schnorr::{SchnorrPublic, SchnorrSecret, Signature},
+        util::poseidon_hash,
     },
     net::{MessageSubscription, P2p, Settings, SettingsPtr},
     node::state::{state_transition, ProgramState, StateUpdate},
@@ -38,15 +39,10 @@ use crate::{
         Transaction,
     },
     util::{path::expand_path, time::Timestamp},
+    zk::circuit::{BurnContract, LeadContract, MintContract},
     Result,
 };
 
-use url::Url;
-
-use pasta_curves::pallas;
-
-use group::ff::PrimeField;
-
 const LOG_T: &str = "stakeholder";
 const TREE_LEN: usize = 100;
 
@@ -200,7 +196,7 @@ impl StakeholderState {
             // If it's our own coin, witness it and append to the vector.
             if let Some((note, secret)) = self.try_decrypt_note(enc_note) {
                 let leaf_position = self.tree.witness().unwrap();
-                let nullifier = Nullifier::new(secret, note.serial);
+                let nullifier = Nullifier::from(poseidon_hash::<2>([secret.inner(), note.serial]));
                 let own_coin = OwnCoin { coin, note, secret, nullifier, leaf_position };
                 self.own_coins.push(own_coin);
             }

+ 3 - 3
src/wallet/walletdb.rs

@@ -494,10 +494,10 @@ mod tests {
     use crate::crypto::{
         merkle_node::MerkleNode,
         types::{DrkCoinBlind, DrkSerial, DrkValueBlind},
+        util::poseidon_hash,
     };
-    use group::ff::Field;
     use incrementalmerkletree::Tree;
-    use pasta_curves::pallas;
+    use pasta_curves::{group::ff::Field, pallas};
     use rand::rngs::OsRng;
 
     const WPASS: &str = "darkfi";
@@ -515,7 +515,7 @@ mod tests {
         };
 
         let coin = Coin(pallas::Base::random(&mut OsRng));
-        let nullifier = Nullifier::new(*s, serial);
+        let nullifier = Nullifier::from(poseidon_hash::<2>([s.inner(), serial]));
         let leaf_position: incrementalmerkletree::Position = 0.into();
 
         OwnCoin { coin, note, secret: *s, nullifier, leaf_position }

+ 3 - 3
src/zk/circuit/burn_contract.rs

@@ -597,7 +597,7 @@ mod tests {
         let merkle_path = tree.authentication_path(leaf_pos, &merkle_root).unwrap();
         let leaf_pos: u64 = leaf_pos.into();
 
-        let nullifier = [secret.0, serial];
+        let nullifier = [secret.inner(), serial];
         let nullifier =
             poseidon::Hash::<_, P128Pow5T3, ConstantLength<2>, 3, 2>::init().hash(nullifier);
 
@@ -627,7 +627,7 @@ mod tests {
         ];
 
         let circuit = BurnContract {
-            secret_key: Value::known(secret.0),
+            secret_key: Value::known(secret.inner()),
             serial: Value::known(serial),
             value: Value::known(pallas::Base::from(value)),
             token: Value::known(token_id),
@@ -639,7 +639,7 @@ mod tests {
             spend_hook: Value::known(spend_hook),
             user_data: Value::known(user_data),
             user_data_blind: Value::known(user_data_blind),
-            sig_secret: Value::known(sig_secret.0),
+            sig_secret: Value::known(sig_secret.inner()),
         };
 
         use plotters::prelude::*;

+ 5 - 7
tests/burn_proof.rs

@@ -2,8 +2,9 @@ use darkfi::{
     crypto::{
         keypair::{PublicKey, SecretKey},
         merkle_node::MerkleNode,
+        nullifier::Nullifier,
         proof::{ProvingKey, VerifyingKey},
-        util::{pedersen_commitment_base, pedersen_commitment_u64},
+        util::{pedersen_commitment_base, pedersen_commitment_u64, poseidon_hash},
         Proof,
     },
     zk::{
@@ -73,7 +74,7 @@ fn burn_proof() -> Result<()> {
     let leaf_pos: u64 = leaf_pos.into();
 
     let prover_witnesses = vec![
-        Witness::Base(Value::known(secret.0)),
+        Witness::Base(Value::known(secret.inner())),
         Witness::Base(Value::known(serial)),
         Witness::Base(Value::known(pallas::Base::from(value))),
         Witness::Base(Value::known(token_id)),
@@ -82,14 +83,11 @@ fn burn_proof() -> Result<()> {
         Witness::Scalar(Value::known(token_blind)),
         Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
         Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())),
-        Witness::Base(Value::known(sig_secret.0)),
+        Witness::Base(Value::known(sig_secret.inner())),
     ];
 
     // Create the public inputs
-    let nullifier = [secret.0, serial];
-    let nullifier =
-        poseidon::Hash::<_, poseidon::P128Pow5T3, poseidon::ConstantLength<2>, 3, 2>::init()
-            .hash(nullifier);
+    let nullifier = Nullifier::from(poseidon_hash::<2>([secret.inner(), serial]));
 
     let value_commit = pedersen_commitment_u64(value, value_blind);
     let value_coords = value_commit.to_affine().coordinates().unwrap();

+ 1 - 1
tests/zkvm_opcodes.rs

@@ -81,7 +81,7 @@ fn zkvm_opcodes() -> Result<()> {
     let d_m = [pallas::Base::one(), blind, *value_coords.x(), *value_coords.y()];
     let d = poseidon::Hash::<_, P128Pow5T3, ConstantLength<4>, 3, 2>::init().hash(d_m);
 
-    let public = PublicKey::from_secret(SecretKey(secret));
+    let public = PublicKey::from_secret(SecretKey::from(secret));
     let public_coords = public.0.to_affine().coordinates().unwrap();
 
     let public_inputs = vec![