diff --git a/.clog.toml b/.clog.toml deleted file mode 100644 index 3c73d774..00000000 --- a/.clog.toml +++ /dev/null @@ -1,9 +0,0 @@ -[clog] -# A repository link with the trailing '.git' which will be used to generate -# all commit and issue links -repository = "https://github.com/tari-project/tari-crypto" - -# specify the style of commit links to generate, defaults to "github" if omitted -link-style = "github" -#changelog = "changelog.md" -from-latest-tag = true \ No newline at end of file diff --git a/.github/workflows/check_licence.yml b/.github/workflows/check_licence.yml new file mode 100644 index 00000000..83c5c38d --- /dev/null +++ b/.github/workflows/check_licence.yml @@ -0,0 +1,24 @@ +on: + push: + branches: + - main + pull_request: + types: + - opened + - reopened + - synchronize + +jobs: + licenses: + name: file licenses + runs-on: ubuntu-20.04 + steps: + - name: checkout + uses: actions/checkout@v2 + - name: install ripgrep + run: | + wget https://github.com/BurntSushi/ripgrep/releases/download/13.0.0/ripgrep_13.0.0_amd64.deb + sudo dpkg -i ripgrep_13.0.0_amd64.deb + rg --version || exit 1 + - name: run the license check + run: ./scripts/file_license_check.sh \ No newline at end of file diff --git a/.github/workflows/clippy-check.yml b/.github/workflows/clippy-check.yml index 987d0b54..dc7eb032 100644 --- a/.github/workflows/clippy-check.yml +++ b/.github/workflows/clippy-check.yml @@ -23,4 +23,4 @@ jobs: uses: actions-rs/cargo@v1 with: command: lints - args: clippy --all-targets --all-features \ No newline at end of file + args: clippy --all-targets --features "wasm,ffi" \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e9840db9..f0ec11bb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -45,3 +45,13 @@ jobs: # TODO: re-add all features once https://github.com/rust-lang/packed_simd/pull/341 is used by dalek # args: --release --all-features args: --release --features wasm --features ffi + - name: docs build + uses: actions-rs/cargo@v1 + with: + command: doc + args: --features wasm --features ffi + - name: bench + uses: actions-rs/cargo@v1 + with: + command: check + args: --benches diff --git a/Cargo.toml b/Cargo.toml index 02884a8c..2c3fb4e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", t base64 = "0.10.1" blake2 = "0.9.1" bulletproofs = { version = "4.1.2", package = "tari_bulletproofs", git = "https://github.com/tari-project/bulletproofs", tag = "v4.1.2" } -curve25519-dalek = { package = "curve25519-dalek-ng", version = "4.1", default-features = false, features = ["u64_backend", "serde", "alloc"] } +curve25519-dalek = { package = "curve25519-dalek-ng", version = "4.1", default-features = false, features = ["serde", "alloc"] } digest = "0.9.0" getrandom = { version = "0.2.3", default-features = false, optional = true } lazy_static = "1.3.0" @@ -41,10 +41,9 @@ wasm-bindgen-test = "0.3.24" cbindgen = "0.17.0" [features] -default = [] +default = ["u64_backend"] +u64_backend = ["curve25519-dalek/u64_backend"] simd_backend = ["curve25519-dalek/simd_backend", "bulletproofs/simd_backend"] -simd = ["simd_backend"] -avx2 = ["simd_backend"] # deprecated alias for simd_backend wasm = ["wasm-bindgen", "getrandom/js"] ffi = [] musig = [] diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..66d64a26 --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2019, The Tari Developer Community +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index ad1972cf..0a17f0a3 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Major features of this library include: - Pedersen commitments - Schnorr Signatures - Generic Public and Secret Keys -- [Musig!](https://blockstream.com/2018/01/23/musig-key-aggregation-schnorr-signatures/) +- [Musig!](https://blockstream.com/2018/01/23/musig-key-aggregation-schnorr-signatures/) **NOT PRODUCTION READY** The `tari_crypto` crate makes heavy use of the excellent [Dalek](https://github.com/dalek-cryptography/curve25519-dalek) libraries. The default implementation for Tari ECC is the [Ristretto255 curve](https://ristretto.group). diff --git a/benches/mod.rs b/benches/mod.rs index 86a7d3fc..9f6236fc 100644 --- a/benches/mod.rs +++ b/benches/mod.rs @@ -1,24 +1,5 @@ // Copyright 2019. The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause use criterion::criterion_main; diff --git a/benches/range_proof.rs b/benches/range_proof.rs index 5420f8ea..1f37e5c0 100644 --- a/benches/range_proof.rs +++ b/benches/range_proof.rs @@ -1,24 +1,5 @@ // Copyright 2019. The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause use std::time::Duration; @@ -29,8 +10,8 @@ use tari_crypto::{ keys::SecretKey, range_proof::RangeProofService, ristretto::{ - dalek_range_proof::DalekRangeProofService, pedersen::{PedersenCommitment, PedersenCommitmentFactory}, + DalekRangeProofService, RistrettoSecretKey, }, }; diff --git a/benches/signatures.rs b/benches/signatures.rs index 0ae9ccf3..a39828f1 100644 --- a/benches/signatures.rs +++ b/benches/signatures.rs @@ -1,3 +1,6 @@ +// Copyright 2022. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + use std::time::Duration; use criterion::{criterion_group, BatchSize, Criterion}; diff --git a/build.rs b/build.rs index f8a5365c..2fc8d5d8 100644 --- a/build.rs +++ b/build.rs @@ -1,20 +1,7 @@ // Copyright 2020. The Tari Project -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause +//! Custom build step to generate FFI header if needed use std::{env, path::Path}; use cbindgen::Config; diff --git a/lints.toml b/lints.toml index 5970daa1..cd880a3e 100644 --- a/lints.toml +++ b/lints.toml @@ -7,7 +7,7 @@ deny = [ # TODO: enable lint-reasons feature # 'clippy::allow_attributes_without_reason', # Docs - # 'missing_docs', + 'missing_docs', # 'clippy::missing_errors_doc', # 'clippy::missing_safety_doc', # 'clippy::missing_panics_doc', diff --git a/rust-toolchain b/rust-toolchain deleted file mode 100644 index 9c78123d..00000000 --- a/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -nightly-2022-01-17 diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 00000000..68e2107a --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +#channel = "nightly-2022-01-17" +channel = "stable" \ No newline at end of file diff --git a/scripts/file_licence_check.sh b/scripts/file_licence_check.sh new file mode 100644 index 00000000..c0b3427f --- /dev/null +++ b/scripts/file_licence_check.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# run from the repo root + +rg -i "Copyright.*The Tari Project" --files-without-match -g '!*.{Dockerfile,asc,bat,config,config.js,css,csv,drawio,gitkeep,hbs,html,iss,json,lock,md,min.js,ps1,py,rc,scss,sh,sql,svg,toml,txt,yml}' . | sort >/tmp/rgtemp + +DIFFS=$(diff -u .license.ignore /tmp/rgtemp) + +if [ -n "$DIFFS" ]; then + echo "New files detected that either need copyright/license identifiers added, or they need to be added to .license.ignore" + echo "NB: The ignore file must be sorted alphabetically!" + + echo "Diff:" + echo "$DIFFS" + exit 1 +fi diff --git a/src/commitment.rs b/src/commitment.rs index 446591b3..b8c5fb70 100644 --- a/src/commitment.rs +++ b/src/commitment.rs @@ -1,24 +1,10 @@ // Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause + +//! A commitment is like a sealed envelope. You put some information inside the envelope, and then seal (commit) it. +//! You can't change what you've said, but also, no-one knows what you've said until you're ready to open (open) the +//! envelope and reveal its contents. Also it's a special envelope that can only be opened by a special opener that +//! you keep safe in your drawer. use std::{ cmp::Ordering, @@ -31,11 +17,6 @@ use tari_utilities::{ByteArray, ByteArrayError}; use crate::keys::{PublicKey, SecretKey}; -/// A commitment is like a sealed envelope. You put some information inside the envelope, and then seal (commit) it. -/// You can't change what you've said, but also, no-one knows what you've said until you're ready to open (open) the -/// envelope and reveal its contents. Also it's a special envelope that can only be opened by a special opener that -/// you keep safe in your drawer. -/// /// There are also different types of commitments that vary in their security guarantees, but all of them are /// represented by binary data; so [HomomorphicCommitment](trait.HomomorphicCommitment.html) implements /// [ByteArray](trait.ByteArray.html). @@ -53,10 +34,12 @@ pub struct HomomorphicCommitment

(pub(crate) P); impl

HomomorphicCommitment

where P: PublicKey { + /// Get this commitment as a public key point pub fn as_public_key(&self) -> &P { &self.0 } + /// Converts a public key into a commitment pub fn from_public_key(p: &P) -> HomomorphicCommitment

{ HomomorphicCommitment(p.clone()) } @@ -159,7 +142,9 @@ impl PartialEq for HomomorphicCommitment

{ impl Eq for HomomorphicCommitment

{} +/// A trait for creating commitments pub trait HomomorphicCommitmentFactory { + /// The type of public key that the underlying commitment will be based on type P: PublicKey; /// Create a new commitment with the blinding factor k and value v provided. The implementing type will provide the diff --git a/src/ffi/error.rs b/src/ffi/error.rs index 625659d7..62e603d6 100644 --- a/src/ffi/error.rs +++ b/src/ffi/error.rs @@ -1,19 +1,5 @@ // Copyright 2020. The Tari Project -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause use std::{ convert::TryFrom, diff --git a/src/ffi/keys.rs b/src/ffi/keys.rs index a2d53698..d899e799 100644 --- a/src/ffi/keys.rs +++ b/src/ffi/keys.rs @@ -1,19 +1,5 @@ // Copyright 2020. The Tari Project -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause use std::{ ffi::CStr, diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs index 7ff5f016..37bcdf12 100644 --- a/src/ffi/mod.rs +++ b/src/ffi/mod.rs @@ -1,19 +1,7 @@ // Copyright 2020. The Tari Project -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause + +//! FFI interface for using this library in other langauges that support it use std::os::raw::c_char; @@ -25,6 +13,7 @@ pub use keys::{commitment, random_keypair, sign, sign_comsig, verify, verify_com const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\u{00}"); +/// The version of this library #[no_mangle] pub extern "C" fn version() -> *const c_char { VERSION.as_ptr() as *const c_char diff --git a/src/hash/blake2.rs b/src/hash/blake2.rs index 29caf05b..eeb84d0f 100644 --- a/src/hash/blake2.rs +++ b/src/hash/blake2.rs @@ -1,24 +1,7 @@ -// Copyright 2020 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Copyright 2020. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + +//! A convenience wrapper produce 256 bit hashes from Blake2b use blake2::{digest::VariableOutput, VarBlake2b}; use digest::{ @@ -82,7 +65,7 @@ mod test { use digest::{generic_array::GenericArray, Digest}; use tari_utilities::hex; - use crate::common::Blake256; + use crate::hash::blake2::Blake256; #[test] fn blake256() { diff --git a/src/hash/mod.rs b/src/hash/mod.rs index a4e8cc32..0b01faf2 100644 --- a/src/hash/mod.rs +++ b/src/hash/mod.rs @@ -1,23 +1,5 @@ -// Copyright 2020 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Copyright 2020. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause +//! Convenience wrappers for hash functions pub mod blake2; diff --git a/src/keys.rs b/src/keys.rs index ccb80b69..9ee5fcf5 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -1,24 +1,5 @@ // Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause //! General definition of public-private key pairs for use in Tari. The traits and structs //! defined here are used in the Tari domain logic layer exclusively (as opposed to any specific @@ -46,7 +27,9 @@ use tari_utilities::ByteArray; /// let p = RistrettoPublicKey::from_secret_key(&k); /// ``` pub trait SecretKey: ByteArray + Clone + PartialEq + Eq + Add + Default { + /// The length of the key, in bytes fn key_length() -> usize; + /// Generates a random secret key fn random(rng: &mut R) -> Self; } @@ -59,15 +42,21 @@ pub trait SecretKey: ByteArray + Clone + PartialEq + Eq + Add + D pub trait PublicKey: ByteArray + Add + Clone + PartialOrd + Ord + Default + Serialize + DeserializeOwned { + /// The related [SecretKey](trait.SecretKey.html) type type K: SecretKey; + /// Calculate the public key associated with the given secret key. This should not fail; if a /// failure does occur (implementation error?), the function will panic. fn from_secret_key(k: &Self::K) -> Self; + /// The length of the public key when converted to bytes fn key_length() -> usize; + /// Multiplies each of the items in `scalars` by their respective item in `points` and then adds + /// the results to produce a single public key fn batch_mul(scalars: &[Self::K], points: &[Self]) -> Self; + /// Generate a random public and secret key fn random_keypair(rng: &mut R) -> (Self::K, Self) { let k = Self::K::random(rng); let pk = Self::from_secret_key(&k); @@ -77,6 +66,7 @@ pub trait PublicKey: /// This trait provides a common mechanism to calculate a shared secret using the private and public key of two parties pub trait DiffieHellmanSharedSecret: ByteArray + Clone + PartialEq + Eq + Add + Default { + /// The type of public key type PK: PublicKey; /// Generate a shared secret from one party's private key and another party's public key fn shared_secret(k: &::K, pk: &Self::PK) -> Self::PK; diff --git a/src/lib.rs b/src/lib.rs index eecc02e1..d8d64d3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,22 @@ +// Copyright 2018 The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + +//! This crate is part of the [Tari Cryptocurrency](https://tari.com) project. +//! +//! Major features of this library include: +//! +//! - Pedersen commitments +//! - Schnorr Signatures +//! - Generic Public and Secret Keys +//! +//! The `tari_crypto` crate makes heavy use of the excellent [Dalek](https://github.com/dalek-cryptography/curve25519-dalek) +//! libraries. The default implementation for Tari ECC is the [Ristretto255 curve](https://ristretto.group). + #[macro_use] extern crate lazy_static; #[macro_use] -pub mod macros; +mod macros; pub mod commitment; pub mod hash; pub mod keys; @@ -22,5 +36,4 @@ pub mod wasm; pub mod ffi; // Re-export tari_utils -pub use hash::blake2 as common; pub use tari_utilities; diff --git a/src/macros.rs b/src/macros.rs index 6b589031..8b5dadbb 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,24 +1,5 @@ // Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause /// Adds some variations of `Add` given a definition for `Add` that takes references. i.e. assuming we have /// ```ignore diff --git a/src/musig.rs b/src/musig.rs index 797ae7a9..b3f547af 100644 --- a/src/musig.rs +++ b/src/musig.rs @@ -1,24 +1,5 @@ // Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause use std::{ops::Mul, prelude::v1::Vec}; diff --git a/src/range_proof.rs b/src/range_proof.rs index d513353b..681d4be2 100644 --- a/src/range_proof.rs +++ b/src/range_proof.rs @@ -1,24 +1,8 @@ // Copyright 2019. The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause + +//! Range proofs are used to determine if a value lies inside a particular range. Most commonly, we +//! want to prove in zero knowledge that a value is non-negative. use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -28,11 +12,13 @@ use crate::{ keys::{PublicKey, SecretKey}, }; -pub const REWIND_PROOF_MESSAGE_LENGTH: usize = 23; -pub const REWIND_CHECK_MESSAGE: &[u8; 2] = b"TR"; -pub const REWIND_USER_MESSAGE_LENGTH: usize = 21; +pub(crate) const REWIND_PROOF_MESSAGE_LENGTH: usize = 23; +pub(crate) const REWIND_CHECK_MESSAGE: &[u8; 2] = b"TR"; +pub(crate) const REWIND_USER_MESSAGE_LENGTH: usize = 21; +/// An error that has occurred when constructing or verifying a range proof #[derive(Debug, Clone, Error, PartialEq, Deserialize, Serialize)] +#[allow(missing_docs)] pub enum RangeProofError { #[error("Could not construct range proof")] ProofConstructionError, @@ -46,19 +32,23 @@ pub enum RangeProofError { InvalidRewind, } +/// A trait to be implemented for more specific services that construct and verify range proofs pub trait RangeProofService { - type P: Sized; + /// The type of proof, usually a byte array + type Proof: Sized; + /// The secret key type K: SecretKey; + /// The public key type PK: PublicKey; /// Construct a new range proof for the given secret key and value. The resulting proof will be sufficient /// evidence that the prover knows the secret key and value, and that the value lies in the range determined by /// the service. - fn construct_proof(&self, key: &Self::K, value: u64) -> Result; + fn construct_proof(&self, key: &Self::K, value: u64) -> Result; /// Verify the range proof against the given commitment. If this function returns true, it attests to the /// commitment having a value in the range [0; 2^64-1] and that the prover knew both the value and private key. - fn verify(&self, proof: &Self::P, commitment: &HomomorphicCommitment) -> bool; + fn verify(&self, proof: &Self::Proof, commitment: &HomomorphicCommitment) -> bool; /// Return the maximum range of the range proof as a power of 2. i.e. if the maximum range is 2^64, this function /// returns 64. @@ -74,12 +64,12 @@ pub trait RangeProofService { rewind_key: &Self::K, rewind_blinding_key: &Self::K, proof_message: &[u8; REWIND_USER_MESSAGE_LENGTH], - ) -> Result; + ) -> Result; /// Rewind a rewindable range proof to reveal the committed value and the 19 byte proof message. fn rewind_proof_value_only( &self, - proof: &Self::P, + proof: &Self::Proof, commitment: &HomomorphicCommitment, rewind_public_key: &Self::PK, rewind_blinding_public_key: &Self::PK, @@ -89,17 +79,19 @@ pub trait RangeProofService { /// message. fn rewind_proof_commitment_data( &self, - proof: &Self::P, + proof: &Self::Proof, commitment: &HomomorphicCommitment, rewind_key: &Self::K, rewind_blinding_key: &Self::K, ) -> Result, RangeProofError>; } -/// Rewind data extracted from a rangeproof containing the committed value and the 19 byte proof message. +/// Rewind data extracted from a range proof containing the committed value and the 19 byte proof message. #[derive(Debug, PartialEq)] pub struct RewindResult { + /// The original value `v` as a u64 value pub committed_value: u64, + /// A short message stored in the proof pub proof_message: [u8; REWIND_USER_MESSAGE_LENGTH], } @@ -119,8 +111,11 @@ impl RewindResult { pub struct FullRewindResult where K: SecretKey { + /// The original value v, stored in the commitment, as a u64 pub committed_value: u64, + /// A short message stored in the proof pub proof_message: [u8; REWIND_USER_MESSAGE_LENGTH], + /// The original blinding factor (secret key) stored in the commitment pub blinding_factor: K, } diff --git a/src/ristretto/constants.rs b/src/ristretto/constants.rs index b4663dc1..f2077c59 100644 --- a/src/ristretto/constants.rs +++ b/src/ristretto/constants.rs @@ -1,24 +1,8 @@ -// Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Copyright 2019. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + +//! Constant points for the Ristretto curve. There are 10 provided, but this library currently only +//! uses the first use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint}; diff --git a/src/ristretto/dalek_range_proof.rs b/src/ristretto/dalek_range_proof.rs index 9a66e516..2bc0b48c 100644 --- a/src/ristretto/dalek_range_proof.rs +++ b/src/ristretto/dalek_range_proof.rs @@ -1,25 +1,7 @@ // Copyright 2019. The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause +//! A wrapper around the Dalek library implementation of Bulletproof range proofs. use bulletproofs::{ range_proof::{get_rewind_nonce_from_pub_key, get_secret_nonce_from_pvt_key}, BulletproofGens, @@ -77,10 +59,10 @@ impl DalekRangeProofService { impl RangeProofService for DalekRangeProofService { type K = RistrettoSecretKey; - type P = Vec; type PK = RistrettoPublicKey; + type Proof = Vec; - fn construct_proof(&self, key: &RistrettoSecretKey, value: u64) -> Result, RangeProofError> { + fn construct_proof(&self, key: &RistrettoSecretKey, value: u64) -> Result { let mut pt = Transcript::new(b"tari"); let k = key.0; let (proof, _) = DalekProof::prove_single(&self.bp_gens, &self.pc_gens, &mut pt, value, &k, self.range) @@ -88,7 +70,7 @@ impl RangeProofService for DalekRangeProofService { Ok(proof.to_bytes()) } - fn verify(&self, proof: &Self::P, commitment: &PedersenCommitment) -> bool { + fn verify(&self, proof: &Self::Proof, commitment: &PedersenCommitment) -> bool { let rp = DalekProof::from_bytes(proof).map_err(|_| RangeProofError::InvalidProof); if rp.is_err() { return false; @@ -111,7 +93,7 @@ impl RangeProofService for DalekRangeProofService { rewind_key: &RistrettoSecretKey, rewind_blinding_key: &RistrettoSecretKey, proof_message: &[u8; REWIND_USER_MESSAGE_LENGTH], - ) -> Result, RangeProofError> { + ) -> Result { let mut pt = Transcript::new(b"tari"); let mut full_proof_message = [0u8; REWIND_PROOF_MESSAGE_LENGTH]; full_proof_message[0..REWIND_CHECK_MESSAGE.len()].clone_from_slice(REWIND_CHECK_MESSAGE); @@ -137,7 +119,7 @@ impl RangeProofService for DalekRangeProofService { fn rewind_proof_value_only( &self, - proof: &Self::P, + proof: &Self::Proof, commitment: &PedersenCommitment, rewind_public_key: &RistrettoPublicKey, rewind_blinding_public_key: &RistrettoPublicKey, @@ -171,7 +153,7 @@ impl RangeProofService for DalekRangeProofService { fn rewind_proof_commitment_data( &self, - proof: &Self::P, + proof: &Self::Proof, commitment: &PedersenCommitment, rewind_key: &RistrettoSecretKey, rewind_blinding_key: &RistrettoSecretKey, diff --git a/src/ristretto/mod.rs b/src/ristretto/mod.rs index 4edb14c3..3b781547 100644 --- a/src/ristretto/mod.rs +++ b/src/ristretto/mod.rs @@ -1,37 +1,22 @@ // Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause + +//! This module contains implementations using the Ristretto curve. pub mod constants; -pub mod dalek_range_proof; +mod dalek_range_proof; #[cfg(feature = "musig")] pub mod musig; pub mod pedersen; -pub mod ristretto_com_sig; +mod ristretto_com_sig; pub mod ristretto_keys; -pub mod ristretto_sig; +mod ristretto_sig; pub mod serialize; pub mod utils; // Re-export +pub use dalek_range_proof::DalekRangeProofService; + pub use self::{ ristretto_com_sig::RistrettoComSig, ristretto_keys::{RistrettoPublicKey, RistrettoSecretKey}, diff --git a/src/ristretto/musig.rs b/src/ristretto/musig.rs index 14aa626a..1231700b 100644 --- a/src/ristretto/musig.rs +++ b/src/ristretto/musig.rs @@ -1,24 +1,79 @@ -// Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Copyright 2019. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + +//! MuSig signature aggregation. [MuSig](https://blockstream.com/2018/01/23/musig-key-aggregation-schnorr-signatures/) +//! is a 3-round signature aggregation protocol. +//! We assume that all the public keys are known and publicly accessible. A [Joint Public Key](structs.JointKey.html) +//! is constructed by all participants. +//! 1. In the first round, participants share the hash of their nonces. +//! 2. Participants then share their public nonce, \\( R_i \\), and all participants calculate the shared nonce, +//! \\( R = \sum R_i \\). +//! 3. Each participant then calculates a partial signature, with the final signature being the sum of all the +//! partial signatures. +//! +//! This protocol is implemented as a Finite State Machine. MuSig is a simple wrapper around a `MusigState` enum that +//! holds the various states that the MuSig protocol can be in, combined with a `MuSigEvents` enum that enumerates +//! the relevant input events that can occur. Any attempt to invoke an invalid transition, or any other failure +//! condition results in the `Failure` state; in which case the MuSig protocol should be abandoned. +//! +//! Rust's type system is leveraged to prevent any rewinding of state; old state variables are destroyed when +//! transitioning to new states. The MuSig variable also _takes ownership_ of the nonce key, reducing the risk of +//! nonce reuse (though obviously it doesn't eliminate it). Let's be clear: REUSING a nonce WILL result in your secret +//! key being discovered. See +//! [this post](https://tlu.tarilabs.com/cryptography/digital_signatures/introduction_schnorr_signatures.html#musig) +//! for details. +//! +//! The API is fairly straightforward and is best illustrated with an example. Alice and Bob are going to construct a +//! 2-of-2 aggregated signature. +//! +//! ```edition2018 +//! # use tari_crypto::ristretto::{ musig::RistrettoMuSig, ristretto_keys::* }; +//! # use tari_utilities::ByteArray; +//! # use tari_crypto::keys::PublicKey; +//! # use sha2::Sha256; +//! # use digest::Digest; +//! let mut rng = rand::thread_rng(); +//! // Create a new MuSig instance. The number of signing parties must be known at this time. +//! let mut alice = RistrettoMuSig::::new(2); +//! let mut bob = RistrettoMuSig::::new(2); +//! // Set the message. This can only be done once to prevent replay attacks. Any attempt to assign another +//! // message will result in a Failure state. +//! alice = alice.set_message(b"Discworld"); +//! bob = bob.set_message(b"Discworld"); +//! // Collect public keys +//! let (k_a, p_a) = RistrettoPublicKey::random_keypair(&mut rng); +//! let (k_b, p_b) = RistrettoPublicKey::random_keypair(&mut rng); +//! // Add public keys to MuSig (in any order. They get sorted automatically when _n_ keys have been collected. +//! alice = alice.add_public_key(&p_a).add_public_key(&p_b); +//! bob = bob.add_public_key(&p_b).add_public_key(&p_a); +//! // Round 1 - Collect nonce hashes - each party does this individually and keeps the secret keys secret. +//! let (r_a, pr_a) = RistrettoPublicKey::random_keypair(&mut rng); +//! let (r_b, pr_b) = RistrettoPublicKey::random_keypair(&mut rng); +//! let h_a = Sha256::digest(pr_a.as_bytes()).to_vec(); +//! let h_b = Sha256::digest(pr_b.as_bytes()).to_vec(); +//! bob = bob +//! .add_nonce_commitment(&p_b, h_b.clone()) +//! .add_nonce_commitment(&p_a, h_a.clone()); +//! // State automatically updates: +//! assert!(bob.is_collecting_nonces()); +//! alice = alice +//! .add_nonce_commitment(&p_a, h_a.clone()) +//! .add_nonce_commitment(&p_b, h_b.clone()); +//! assert!(alice.is_collecting_nonces()); +//! // Round 2 - Collect Nonces +//! bob = bob.add_nonce(&p_b, pr_b.clone()).add_nonce(&p_a, pr_a.clone()); +//! assert!(bob.is_collecting_signatures()); +//! alice = alice.add_nonce(&p_a, pr_a.clone()).add_nonce(&p_b, pr_b.clone()); +//! assert!(alice.is_collecting_signatures()); +//! // round 3 - Collect partial signatures +//! let s_a = alice.calculate_partial_signature(&p_a, &k_a, &r_a).unwrap(); +//! let s_b = bob.calculate_partial_signature(&p_b, &k_b, &r_b).unwrap(); +//! alice = alice.add_signature(&s_a, true).add_signature(&s_b, true); +//! assert!(alice.is_finalized()); +//! bob = bob.add_signature(&s_b, true).add_signature(&s_a, true); +//! assert!(bob.is_finalized()); +//! assert_eq!(alice.get_aggregated_signature(), bob.get_aggregated_signature()); +//! ``` use std::marker::PhantomData; @@ -38,79 +93,7 @@ type JointPubKey = JointKey; type MessageHash = Vec; type MessageHashSlice = [u8]; -/// MuSig signature aggregation. [MuSig](https://blockstream.com/2018/01/23/musig-key-aggregation-schnorr-signatures/) -/// is a 3-round signature aggregation protocol. -/// We assume that all the public keys are known and publicly accessible. A [Joint Public Key](structs.JointKey.html) -/// is constructed by all participants. -/// 1. In the first round, participants share the hash of their nonces. -/// 2. Participants then share their public nonce, \\( R_i \\), and all participants calculate the shared nonce, -/// \\( R = \sum R_i \\). -/// 3. Each participant then calculates a partial signature, with the final signature being the sum of all the -/// partial signatures. -/// -/// This protocol is implemented as a Finite State Machine. MuSig is a simple wrapper around a `MusigState` enum that -/// holds the various states that the MuSig protocol can be in, combined with a `MuSigEvents` enum that enumerates -/// the relevant input events that can occur. Any attempt to invoke an invalid transition, or any other failure -/// condition results in the `Failure` state; in which case the MuSig protocol should be abandoned. -/// -/// Rust's type system is leveraged to prevent any rewinding of state; old state variables are destroyed when -/// transitioning to new states. The MuSig variable also _takes ownership_ of the nonce key, reducing the risk of -/// nonce reuse (though obviously it doesn't eliminate it). Let's be clear: REUSING a nonce WILL result in your secret -/// key being discovered. See -/// [this post](https://tlu.tarilabs.com/cryptography/digital_signatures/introduction_schnorr_signatures.html#musig) -/// for details. -/// -/// The API is fairly straightforward and is best illustrated with an example. Alice and Bob are going to construct a -/// 2-of-2 aggregated signature. -/// -/// ```edition2018 -/// # use tari_crypto::ristretto::{ musig::RistrettoMuSig, ristretto_keys::* }; -/// # use tari_utilities::ByteArray; -/// # use tari_crypto::keys::PublicKey; -/// # use sha2::Sha256; -/// # use digest::Digest; -/// let mut rng = rand::thread_rng(); -/// // Create a new MuSig instance. The number of signing parties must be known at this time. -/// let mut alice = RistrettoMuSig::::new(2); -/// let mut bob = RistrettoMuSig::::new(2); -/// // Set the message. This can only be done once to prevent replay attacks. Any attempt to assign another -/// // message will result in a Failure state. -/// alice = alice.set_message(b"Discworld"); -/// bob = bob.set_message(b"Discworld"); -/// // Collect public keys -/// let (k_a, p_a) = RistrettoPublicKey::random_keypair(&mut rng); -/// let (k_b, p_b) = RistrettoPublicKey::random_keypair(&mut rng); -/// // Add public keys to MuSig (in any order. They get sorted automatically when _n_ keys have been collected. -/// alice = alice.add_public_key(&p_a).add_public_key(&p_b); -/// bob = bob.add_public_key(&p_b).add_public_key(&p_a); -/// // Round 1 - Collect nonce hashes - each party does this individually and keeps the secret keys secret. -/// let (r_a, pr_a) = RistrettoPublicKey::random_keypair(&mut rng); -/// let (r_b, pr_b) = RistrettoPublicKey::random_keypair(&mut rng); -/// let h_a = Sha256::digest(pr_a.as_bytes()).to_vec(); -/// let h_b = Sha256::digest(pr_b.as_bytes()).to_vec(); -/// bob = bob -/// .add_nonce_commitment(&p_b, h_b.clone()) -/// .add_nonce_commitment(&p_a, h_a.clone()); -/// // State automatically updates: -/// assert!(bob.is_collecting_nonces()); -/// alice = alice -/// .add_nonce_commitment(&p_a, h_a.clone()) -/// .add_nonce_commitment(&p_b, h_b.clone()); -/// assert!(alice.is_collecting_nonces()); -/// // Round 2 - Collect Nonces -/// bob = bob.add_nonce(&p_b, pr_b.clone()).add_nonce(&p_a, pr_a.clone()); -/// assert!(bob.is_collecting_signatures()); -/// alice = alice.add_nonce(&p_a, pr_a.clone()).add_nonce(&p_b, pr_b.clone()); -/// assert!(alice.is_collecting_signatures()); -/// // round 3 - Collect partial signatures -/// let s_a = alice.calculate_partial_signature(&p_a, &k_a, &r_a).unwrap(); -/// let s_b = bob.calculate_partial_signature(&p_b, &k_b, &r_b).unwrap(); -/// alice = alice.add_signature(&s_a, true).add_signature(&s_b, true); -/// assert!(alice.is_finalized()); -/// bob = bob.add_signature(&s_b, true).add_signature(&s_a, true); -/// assert!(bob.is_finalized()); -/// assert_eq!(alice.get_aggregated_signature(), bob.get_aggregated_signature()); -/// ``` +/// A Musig ceremony struct using Ristretto pub struct RistrettoMuSig { state: MuSigState, digest_type: PhantomData, diff --git a/src/ristretto/pedersen.rs b/src/ristretto/pedersen.rs index 8c67d873..129a30fe 100644 --- a/src/ristretto/pedersen.rs +++ b/src/ristretto/pedersen.rs @@ -1,24 +1,7 @@ -// Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Copyright 2019. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + +//! Pedersen commitment types and factories for Ristretto use std::{borrow::Borrow, iter::Sum}; @@ -34,15 +17,19 @@ use crate::{ ristretto::{constants::RISTRETTO_NUMS_POINTS, RistrettoPublicKey, RistrettoSecretKey}, }; +/// The base point G pub const RISTRETTO_PEDERSEN_G: RistrettoPoint = RISTRETTO_BASEPOINT_POINT; + lazy_static! { + /// The base point H pub static ref RISTRETTO_PEDERSEN_H: RistrettoPoint = RISTRETTO_NUMS_POINTS[0]; } +/// A Pedersen commitment pub type PedersenCommitment = HomomorphicCommitment; /// Generates Pederson commitments `k.G + v.H` using the provided base -/// [RistrettoPoints](curve25519_dalek::ristretto::RistrettoPoints). +/// [RistrettoPoints](curve25519_dalek::ristretto::RistrettoPoint). #[derive(Debug, PartialEq, Eq, Clone)] #[allow(non_snake_case)] pub struct PedersenCommitmentFactory { diff --git a/src/ristretto/ristretto_com_sig.rs b/src/ristretto/ristretto_com_sig.rs index 5138b575..840ed569 100644 --- a/src/ristretto/ristretto_com_sig.rs +++ b/src/ristretto/ristretto_com_sig.rs @@ -1,24 +1,5 @@ -// Copyright 2021 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Copyright 2021. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause use crate::{ ristretto::{RistrettoPublicKey, RistrettoSecretKey}, @@ -54,7 +35,7 @@ use crate::{ /// ```rust /// # use tari_crypto::ristretto::*; /// # use tari_crypto::keys::*; -/// # use tari_crypto::common::*; +/// # use tari_crypto::hash::blake2::Blake256; /// # use digest::Digest; /// # use tari_crypto::commitment::HomomorphicCommitmentFactory; /// # use tari_crypto::ristretto::pedersen::*; @@ -81,7 +62,7 @@ use crate::{ /// # use tari_crypto::keys::*; /// # use tari_crypto::commitment::HomomorphicCommitment; /// # use tari_crypto::ristretto::pedersen::*; -/// # use tari_crypto::common::*; +/// # use tari_crypto::hash::blake2::Blake256; /// # use tari_utilities::hex::*; /// # use tari_utilities::ByteArray; /// # use digest::Digest; @@ -106,7 +87,7 @@ mod test { use crate::{ commitment::HomomorphicCommitmentFactory, - common::Blake256, + hash::blake2::Blake256, keys::{PublicKey, SecretKey}, ristretto::{ pedersen::{PedersenCommitment, PedersenCommitmentFactory}, diff --git a/src/ristretto/ristretto_keys.rs b/src/ristretto/ristretto_keys.rs index 4caca579..ba2a851e 100644 --- a/src/ristretto/ristretto_keys.rs +++ b/src/ristretto/ristretto_keys.rs @@ -1,24 +1,5 @@ -// Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Copyright 2019. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause //! The Tari-compatible implementation of Ristretto based on the curve25519-dalek implementation use std::{ diff --git a/src/ristretto/ristretto_sig.rs b/src/ristretto/ristretto_sig.rs index 6829bd9b..01b858dd 100644 --- a/src/ristretto/ristretto_sig.rs +++ b/src/ristretto/ristretto_sig.rs @@ -1,24 +1,5 @@ -// Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Copyright 2019. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause use crate::{ ristretto::{RistrettoPublicKey, RistrettoSecretKey}, @@ -58,7 +39,7 @@ use crate::{ /// # use tari_crypto::ristretto::*; /// # use tari_crypto::keys::*; /// # use tari_crypto::signatures::SchnorrSignature; -/// # use tari_crypto::common::*; +/// # use tari_crypto::hash::blake2::Blake256; /// # use digest::Digest; /// /// fn get_keypair() -> (RistrettoSecretKey, RistrettoPublicKey) { @@ -84,7 +65,7 @@ use crate::{ /// # use tari_crypto::ristretto::*; /// # use tari_crypto::keys::*; /// # use tari_crypto::signatures::SchnorrSignature; -/// # use tari_crypto::common::*; +/// # use tari_crypto::hash::blake2::Blake256; /// # use tari_utilities::hex::*; /// # use tari_utilities::ByteArray; /// # use digest::Digest; @@ -105,7 +86,7 @@ mod test { use tari_utilities::{hex::from_hex, ByteArray}; use crate::{ - common::Blake256, + hash::blake2::Blake256, keys::{PublicKey, SecretKey}, ristretto::{RistrettoPublicKey, RistrettoSchnorr, RistrettoSecretKey}, }; diff --git a/src/ristretto/serialize.rs b/src/ristretto/serialize.rs index a0bb484c..33ff7b1e 100644 --- a/src/ristretto/serialize.rs +++ b/src/ristretto/serialize.rs @@ -1,24 +1,5 @@ // Copyright 2019. The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause //! Custom serializers for Ristretto keys //! diff --git a/src/ristretto/test_common.rs b/src/ristretto/test_common.rs index 45dcdbf4..19c36d13 100644 --- a/src/ristretto/test_common.rs +++ b/src/ristretto/test_common.rs @@ -1,24 +1,5 @@ -// Copyright 2019 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Copyright 2019. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause use crate::{ keys::{PublicKey, SecretKey}, diff --git a/src/ristretto/utils.rs b/src/ristretto/utils.rs index ce8af425..e66d77f2 100644 --- a/src/ristretto/utils.rs +++ b/src/ristretto/utils.rs @@ -1,19 +1,5 @@ -// Copyright 2020. The Tari Project -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Copyright 2019. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause //! Handy utility functions for use in tests and demo scripts @@ -28,9 +14,13 @@ use crate::{ /// A set of keys and it's associated signature pub struct SignatureSet { + /// The secret nonce pub nonce: RistrettoSecretKey, + /// The public nonce pub public_nonce: RistrettoPublicKey, + /// The message signed. Note that the [SignatureSet::public_nonce] is prepended to this message before signing pub message: Vec, + /// The signature pub signature: RistrettoSchnorr, } diff --git a/src/signatures/commitment_signature.rs b/src/signatures/commitment_signature.rs index f18fed51..3342f4ce 100644 --- a/src/signatures/commitment_signature.rs +++ b/src/signatures/commitment_signature.rs @@ -1,28 +1,5 @@ -// Copyright 2021 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -//! Digital Signature module -//! This module defines generic traits for handling the digital signature operations, agnostic -//! of the underlying elliptic curve implementation +// Copyright 2021. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause use std::{ cmp::Ordering, @@ -39,7 +16,9 @@ use crate::{ keys::{PublicKey, SecretKey}, }; +/// An error when creating a commitment signature #[derive(Clone, Debug, Error, PartialEq, Eq, Deserialize, Serialize)] +#[allow(missing_docs)] pub enum CommitmentSignatureError { #[error("An invalid challenge was provided")] InvalidChallenge, @@ -78,6 +57,7 @@ where P: PublicKey, K: SecretKey, { + /// Creates a new [CommitmentSignature] pub fn new(public_nonce: HomomorphicCommitment

, u: K, v: K) -> Self { CommitmentSignature { public_nonce, u, v } } diff --git a/src/signatures/mod.rs b/src/signatures/mod.rs index 9453b2e9..7924a973 100644 --- a/src/signatures/mod.rs +++ b/src/signatures/mod.rs @@ -1,19 +1,8 @@ // Copyright 2021. The Tari Project -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause + +//! This module defines generic traits for handling the digital signature operations, agnostic +//! of the underlying elliptic curve implementation mod commitment_signature; mod schnorr; diff --git a/src/signatures/schnorr.rs b/src/signatures/schnorr.rs index b9958274..53322979 100644 --- a/src/signatures/schnorr.rs +++ b/src/signatures/schnorr.rs @@ -1,3 +1,6 @@ +// Copyright 2022. The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + //! Schnorr Signature module //! This module defines generic traits for handling the digital signature operations, agnostic //! of the underlying elliptic curve implementation @@ -13,7 +16,9 @@ use thiserror::Error; use crate::keys::{PublicKey, SecretKey}; +/// An error occurred during construction of a SchnorrSignature #[derive(Clone, Debug, Error, PartialEq, Eq, Deserialize, Serialize)] +#[allow(missing_docs)] pub enum SchnorrSignatureError { #[error("An invalid challenge was provided")] InvalidChallenge, diff --git a/src/wasm/commitments.rs b/src/wasm/commitments.rs index a9b4d58a..b7b5e120 100644 --- a/src/wasm/commitments.rs +++ b/src/wasm/commitments.rs @@ -1,24 +1,7 @@ // Copyright 2020. The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause + +//! Functions for creating and opening commitments use serde::{Deserialize, Serialize}; use tari_utilities::hex::Hex; @@ -33,13 +16,17 @@ use crate::{ }, }; +/// Returned from [commit()] #[derive(Default, Serialize, Deserialize)] pub struct CommitmentResult { + /// The commitment, if successful pub commitment: Option, + /// The error if the commitment could not be created, otherwise empty pub error: String, } -/// Commits a value and blinding factor (private key) using a Pedersen commitment. +/// Commits a value and blinding factor (private key) using a Pedersen commitment. Returns a +/// [JsValue] containing a serialized [CommitmentResult] #[wasm_bindgen] pub fn commit(key: &str, value: u64) -> JsValue { let mut result = CommitmentResult::default(); diff --git a/src/wasm/key_utils.rs b/src/wasm/key_utils.rs index 5314ffc6..176eaf56 100644 --- a/src/wasm/key_utils.rs +++ b/src/wasm/key_utils.rs @@ -1,28 +1,10 @@ // Copyright 2020. The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause //! Simple cryptographic key functions. It's generally not very efficient to use these functions to do lots of cool //! stuff with private and public keys, because the keys are translated to- and from hex every time you make a call -//! using a function from this module. You should use a [KeyRing] instead. But sometimes, these functions are handy. +//! using a function from this module. You should use a [crate::wasm::keyring::KeyRing] instead. But sometimes, these +//! functions are handy. use blake2::Digest; use rand::rngs::OsRng; @@ -31,7 +13,7 @@ use tari_utilities::hex::{from_hex, Hex}; use wasm_bindgen::prelude::*; use crate::{ - common::Blake256, + hash::blake2::Blake256, keys::{PublicKey, SecretKey}, ristretto::{ pedersen::{PedersenCommitment, PedersenCommitmentFactory}, @@ -42,24 +24,36 @@ use crate::{ }, }; +/// Result of calling [check_signature] and [check_comsig_signature] #[derive(Debug, Serialize, Deserialize, Default)] pub struct SignatureVerifyResult { + /// True if the signature was valid pub result: bool, + /// Will contain the error if one occurred, otherwise empty pub error: String, } +/// Result of calling [sign] #[derive(Debug, Serialize, Deserialize, Default)] pub struct SignResult { + /// The public nonce of the signature, if successful pub public_nonce: Option, + /// The signature, if successful pub signature: Option, + /// Will contain the error if one occurred, otherwise empty pub error: String, } +/// Result of calling [sign_comsig] #[derive(Debug, Serialize, Deserialize, Default)] pub struct ComSignResult { + /// The public nonce of the signature, if successful pub public_nonce: Option, + /// The `u` component of the signature pub u: Option, + /// The `v` component of the signature pub v: Option, + /// Will contain the error if one occurred, otherwise empty pub error: String, } @@ -166,7 +160,7 @@ pub(super) fn sign_with_key(k: &RistrettoSecretKey, e: &[u8], r: Option<&Ristret result.signature = Some(sig.get_signature().to_hex()); } -/// Checks the validity of a Schnorr signature +/// Checks the validity of a Schnorr signature. Returns a [JsValue] of a serialized [SignatureVerifyResult] #[allow(non_snake_case)] #[wasm_bindgen] pub fn check_signature(pub_nonce: &str, signature: &str, pub_key: &str, msg: &str) -> JsValue { diff --git a/src/wasm/keyring.rs b/src/wasm/keyring.rs index 565ad8f4..e6a07125 100644 --- a/src/wasm/keyring.rs +++ b/src/wasm/keyring.rs @@ -1,24 +1,5 @@ // Copyright 2020. The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause use std::collections::HashMap; diff --git a/src/wasm/mod.rs b/src/wasm/mod.rs index f34f6615..39d75d9e 100644 --- a/src/wasm/mod.rs +++ b/src/wasm/mod.rs @@ -1,24 +1,7 @@ // Copyright 2020. The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause + +//! WASM bindings and functions use wasm_bindgen::prelude::*; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -30,6 +13,7 @@ pub mod key_utils; pub mod range_proofs; pub use keyring::KeyRing; +/// The version of this library #[wasm_bindgen] pub fn version() -> String { VERSION.into() diff --git a/src/wasm/range_proofs.rs b/src/wasm/range_proofs.rs index 0d956180..5e852a9c 100644 --- a/src/wasm/range_proofs.rs +++ b/src/wasm/range_proofs.rs @@ -1,24 +1,7 @@ // Copyright 2020. The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// SPDX-License-Identifier: BSD-3-Clause + +//! Range proof proving and verification functions use serde::{Deserialize, Serialize}; use tari_utilities::hex::Hex; @@ -27,29 +10,31 @@ use wasm_bindgen::prelude::*; use crate::{ range_proof::RangeProofService, ristretto::{ - dalek_range_proof::DalekRangeProofService, pedersen::{PedersenCommitment, PedersenCommitmentFactory}, + DalekRangeProofService, RistrettoSecretKey, }, tari_utilities::hex::from_hex, }; +/// Generated from [RangeProofFactory::create_proof] #[derive(Default, Serialize, Deserialize)] pub struct RangeProofResult { proof: String, error: String, } +/// Generated when calling [RangeProofFactory::verify] #[derive(Default, Serialize, Deserialize)] pub struct VerificationResult { valid: bool, error: String, } +/// A factory to prove and verify range proofs #[wasm_bindgen] pub struct RangeProofFactory { rpf: DalekRangeProofService, - // cf: PedersenCommitmentFactory, } #[wasm_bindgen] @@ -61,7 +46,8 @@ impl RangeProofFactory { RangeProofFactory { rpf } } - /// Creates a new range proof for the given key-value pair. + /// Creates a new range proof for the given key-value pair. Returns a [JsValue] of a serialized + /// [RangeProofResult] pub fn create_proof(&self, key: &str, value: u64) -> JsValue { let mut result = RangeProofResult::default(); let key = match RistrettoSecretKey::from_hex(key) { @@ -78,7 +64,7 @@ impl RangeProofFactory { JsValue::from_serde(&result).unwrap() } - /// Verifies the given range proof and commitment. + /// Verifies the given range proof and commitment. Returns a [JsValue] of a serialized [VerificationResult] pub fn verify(&self, commitment: &str, proof: &str) -> JsValue { let mut result = VerificationResult::default(); let commitment = match PedersenCommitment::from_hex(commitment) {