Skip to content

Commit

Permalink
feat: initial code (#1)
Browse files Browse the repository at this point in the history
This moves out the WASM build of TariCrypto to its own separate repo.
This clean's up the main TariCrypto library and the WASM build to be
more clean and without feature compiler flags.
  • Loading branch information
SWvheerden committed Aug 8, 2023
1 parent b63cab4 commit 19eff1f
Show file tree
Hide file tree
Showing 21 changed files with 2,956 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .github/workflows/check_licence.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Check licences
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
37 changes: 37 additions & 0 deletions .github/workflows/clippy-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Clippy and FMT
on: [push, pull_request]

jobs:
clippy_check:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install components
uses: actions-rs/toolchain@v1
with:
components: clippy, rustfmt
toolchain: nightly
override: true
- name: Check formatting
uses: actions-rs/cargo@v1
with:
command: fmt
toolchain: nightly
args: --all -- --check
- name: Install cargo-lints
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-lints
- name: Clippy lints
uses: actions-rs/cargo@v1
with:
command: lints
toolchain: nightly
args: clippy --all-targets --all-features
- name: Cargo check
uses: actions-rs/cargo@v1
with:
command: check
args: --release --all-targets
20 changes: 20 additions & 0 deletions .github/workflows/pr_title.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: PR
on:
pull_request:
types:
- opened
- reopened
- edited
- synchronize

jobs:
check-title:
runs-on: ubuntu-latest
steps:
- name: install
run: |
npm install -g @commitlint/cli @commitlint/config-conventional
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
- name: lint
run: |
echo "${{github.event.pull_request.title}}" | commitlint
41 changes: 41 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
on:
push:
branches: [main]
pull_request:

name: Wasm Test

jobs:
wasm:
name: wasm test
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- name: toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
target: wasm32-unknown-unknown
override: true
- name: install wasm-pack
run: npm install -g wasm-pack@0.11.1
- name: make wasm
run: make wasm
- name: make wasm-node
run: make wasm-node
- name: make wasm-test
run: make wasm-test
- name: upload pkg
if: ${{ github.event_name == 'push' }}
uses: actions/upload-artifact@v3
with:
name: wasm-bundler
path: pkg
- name: upload nodejs
if: ${{ github.event_name == 'push' }}
uses: actions/upload-artifact@v3
with:
name: wasm-nodejs
path: tari_js
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
target
/**/.env

# These are backup files generated by rustfmt
**/*.rs.bk

# Ignore IDE workspace files
.idea
*.iml
.vs/
*.userprefs
.vscode/
*.code-workspace
tags

# Ignore OS files
.DS_Store

# Custom formatting files
.rustfmt.toml
.clippy.toml

# Ignore Code Coverage Report files
cov_raw/
coverage_report/
default.profraw

Cargo.lock
*.log

# Generated during build
/libtari/tari_crypto.h

# C-demo output
bin/
37 changes: 37 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "tari_crypto_wasm"
description = "Tari Cryptography WASM library"
authors = ["The Tari Development Community"]
repository = "https://github.com/tari-project/tari"
categories = ["cryptography"]
homepage = "https://tari.com"
readme = "README.md"
license = "BSD-3-Clause"
version = "0.17.0"
edition = "2018"

[dependencies]
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", rev = "c06726322683c4671b93388b444d108103cd7b55", default-features = false, features = ["zero"] }
tari_crypto = { git = "https://github.com/swvheerden/tari-crypto.git", rev = "bd451c1d79a42510c38f73e24c6b24f88b5117ba" }
blake2 = { version = "0.10", default-features = false }
digest = { version = "0.10", default-features = false }
getrandom = { version = "0.2", features = ["js"]}
rand_core = { version = "0.6" , default-features = false}
serde = { version = "1.0"}
serde-wasm-bindgen = { version = "0.4" }
wasm-bindgen = { version = "^0.2", features = ["serde-serialize"]}

[dev-dependencies]
wasm-bindgen-test = { version = "0.3" }
rand = { version = "0.8" }

[lib]
# Disable benchmarks to allow Criterion to take over
bench = false
crate-type = ["lib", "cdylib"]

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-Oz"]

[package.metadata.cargo-machete]
ignored = ["getrandom"]
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2019. The Tari Project
# SPDX-License-Identifier: BSD-3-Clause

ifeq ($(shell uname),Darwin)
LDFLAGS := -Ltarget/release/
else
LDFLAGS := -Ltarget/release/
endif

SRC = libtari
BIN = bin
PWD = $(shell pwd)

CC=cc

CFLAGS =

clean:
rm $(SRC)/tari_crypto.h
rm $(BIN)/demo


wasm:
wasm-pack build .

wasm-node:
wasm-pack build --target nodejs -d tari_js .

wasm-test:
wasm-pack test --node
75 changes: 74 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
# tari-crypto-wasm
# Tari Crypto

![](https://github.com/tari-project/tari-crypto/workflows/Security%20audit/badge.svg)
![](https://github.com/tari-project/tari-crypto/workflows/Clippy/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/tari-project/tari-crypto/badge.svg?branch=main)](https://coveralls.io/github/tari-project/tari-crypto?branch=main)

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
- [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).

# Compiling to WebAssembly

To build the WebAssembly module, the `wasm` feature must be enabled:

$ wasm-pack build . -- --features "wasm"

To generate a module for use in node.js, use this command:

$ wasm-pack build --target nodejs -d tari_js . -- --features "wasm"

To run the wasm bindings unit tests, use this command:

$ wasm-pack test --node --features wasm

Note: Node v10+ is needed for the WASM

## Example (Node.js)

```js
const keys = KeyRing.new();

// Create new keypair
keys.new_key("Alice");
keys.new_key("Bob");
console.log(`${keys.len()} keys in ring`); // 2
console.log("kA = ", keys.private_key("Alice"));
console.log("PB = ", keys.public_key("Bob"));
keys.free();
```

# Benchmarks

To run the benchmarks:

$ cargo bench

The benchmarks use Criterion and will produce nice graphs (if you have gnuplot installed)

To run the benchmarks with SIMD instructions:

$ cargo bench --features "avx2"

# Building the C FFI module

To build the C bindings, you can run

make ffi

To build the release version (recommended):

make ffi-release

To run the small demo:

make demo
./bin/demo
45 changes: 45 additions & 0 deletions key_sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.
//

// If you get a "module not found" error, see README.md for details on how to generate the node package
let tari_crypto = require('./pkg');

console.log(`Tari crypto. Version ${tari_crypto.version()}`);
const [k, P] = tari_crypto.generate_keypair();

console.log(`POST /free_tari/allocate/${P}`);
let msg = `Hello Tari from ${P}`;
console.log(`Message: |${msg}|`);
const sig = tari_crypto.sign(k, msg);
if (sig.error) {
console.log(`Error getting signature ${sig.error}`);
} else {
let body = { public_nonce: sig.public_nonce, signature: sig.signature };
console.log(JSON.stringify(body));
let check = tari_crypto.check_signature(sig.public_nonce, sig.signature, P, msg);
if (check.result === true) {
console.log("Signature is valid!");
} else {
console.log(`Invalid signature: ${check.error}`);
}
}
Loading

0 comments on commit 19eff1f

Please sign in to comment.