Skip to content

Commit

Permalink
Initial impl of hasher and ECC key pair (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlopes11 authored and xgreenx committed Dec 20, 2022
1 parent 351374f commit 0803741
Show file tree
Hide file tree
Showing 12 changed files with 1,041 additions and 111 deletions.
166 changes: 73 additions & 93 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,119 +8,99 @@ on:
release:
types: [published]

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

env:
CARGO_TERM_COLOR: always

jobs:
cargo:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- command: fmt
args: --all -- --check
- command: clippy
args: --all-targets --all-features -- -D warnings -D clippy::dbg_macro
- command: check
args: --all-targets
- command: check
args: --all-targets --no-default-features
- command: check
args: --all-targets --all-features
- command: check
args: --verbose -p fuel-asm -p fuel-storage --target thumbv6m-none-eabi --no-default-features
- command: test
args: --all-targets --all-features
- command: test
args: --all-targets --no-default-features
- command: test
args: --all-targets --no-default-features --features serde
- command: test
args: --all-targets --no-default-features --features alloc
- command: test
args: --all-targets --features random
- command: test
args: --all-targets --features serde
- command: test
args: --all-targets --features random,profile-gas,profile-coverage,serde
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: thumbv6m-none-eabi
- name: ${{ matrix.command }} ${{ matrix.args }}
uses: actions-rs/cargo@v1
with:
command: ${{ matrix.command }}
args: ${{ matrix.args }}

cargo-toml-fmt-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Install Cargo.toml linter
uses: baptiste0928/cargo-install@v1
with:
crate: cargo-toml-lint
version: "0.1"
- name: Run Cargo.toml linter
run: git ls-files | grep Cargo.toml$ | xargs --verbose -n 1 cargo-toml-lint
- name: Checkout repository
uses: actions/checkout@v2

cargo-unused-deps-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
default: true
- uses: Swatinem/rust-cache@v1
- name: Install cargo-udeps
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-udeps
- name: Check Unused Deps
uses: actions-rs/cargo@v1
with:
command: udeps
args: --all-targets --all-features

verifications-complete:
needs:
- cargo
- cargo-toml-fmt-check
- cargo-unused-deps-check
runs-on: ubuntu-latest
steps:
- run: true
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

# Using thumbv6m-none-eabi as ARMv6-M arbitrary common choice for a bare-minimum target.
# More info: https://docs.rs/cortex-m-rt/latest/cortex_m_rt/
#
# Can be replaced by other targets that guarantee bare-minimum no-std
- name: Install toolchain no-std
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: thumbv6m-none-eabi
override: true

- name: Install rustfmt
run: rustup component add rustfmt

- name: Check formatting
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all --verbose -- --check

- name: Build no-std
uses: actions-rs/cargo@v1
with:
command: build
args: --verbose --target thumbv6m-none-eabi --no-default-features

- name: Build no-std serde
uses: actions-rs/cargo@v1
with:
command: build
args: --verbose --target thumbv6m-none-eabi --no-default-features --features serde-types-minimal

- name: Run tests all features
uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --all-features

- name: Run tests serde
uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --features serde-types

- name: Run tests no-std
uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --no-default-features

- name: Run tests serde no-std
uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --no-default-features --features serde-types-minimal

publish:
# Only do this job if publishing a release and all checks pass.
needs: verifications-complete
# Only do this job if publishing a release
needs: build
if: github.event_name == 'release' && github.event.action == 'published'
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
- name: Checkout repository
uses: actions/checkout@v2

- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

- name: Verify tag version
run: |
curl -sSLf "https://github.com/TomWright/dasel/releases/download/v1.24.3/dasel_linux_amd64" -L -o dasel && chmod +x dasel
mv ./dasel /usr/local/bin/dasel
cargo install toml-cli
./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} Cargo.toml
- name: Publish crate
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/clippy_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Clippy Check

on: [ pull_request ]

jobs:
clippy_check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- run: rustup component add clippy

- name: rust-clippy-check
uses: actions-rs/clippy-check@v1.0.7
with:
token: ${{ github.token }}
args: --all-features --all-targets -- -D warnings
2 changes: 1 addition & 1 deletion .github/workflows/scripts/verify_tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fi

# strip preceeding 'v' if it exists on tag
REF=${REF/#v}
TOML_VERSION=$(cat $MANIFEST | dasel -r toml 'package.version')
TOML_VERSION=$(toml get $MANIFEST package.version | tr -d '"')

if [ "$TOML_VERSION" != "$REF" ]; then
err "Crate version $TOML_VERSION, doesn't match tag version $REF"
Expand Down
49 changes: 33 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
[workspace]
members = [
"fuel-asm",
"fuel-storage",
"fuel-types",
"fuel-tx",
"fuel-tx/test-helpers",
"fuel-types",
"fuel-vm",
]
[package]
name = "fuel-crypto"
version = "0.1.0"
authors = ["Fuel Labs <contact@fuel.sh>"]
categories = ["cryptography::cryptocurrencies", "data-structures"]
edition = "2021"
homepage = "https://fuel.network/"
keywords = ["blockchain", "cryptocurrencies"]
license = "Apache-2.0"
repository = "https://github.com/FuelLabs/fuel-crypto"
description = "Fuel cryptographic primitives."

# We have a dependency cycle between repositories where `fuel-crypto` depends
# on `fuel-types`, while `fuel-tx` depends on both `fuel-crypto` and
# `fuel-types`. This temporarily works around the issue by forcing all
# instances of `fuel-types` within the graph to point to our local copy.
[patch.crates-io]
fuel-types = { path = "./fuel-types" }
[dependencies]
fuel-types = { version = "0.2", default-features = false }
rand = { version = "0.8", default-features = false, features = ["std_rng"], optional = true }
secp256k1 = { version = "0.20", features = ["recovery"], optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
sha2 = { version = "0.9", default-features = false }

[dev-dependencies]
fuel-crypto = { path = ".", default-features = false, features = ["random"] }
rand = { version = "0.8", default-features = false, features = ["std_rng"] }

[features]
default = ["fuel-types/default", "std"]
random = ["fuel-types/random", "rand"]
serde-types = ["fuel-types/serde-types", "secp256k1/serde", "serde-types-minimal", "serde/default", "std"]
serde-types-minimal = ["fuel-types/serde-types-minimal", "serde"]
std = ["fuel-types/default", "secp256k1"]

[[test]]
name = "test-signature"
path = "tests/signature.rs"
required-features = ["std"]
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# fuel-crypto
# Fuel Crypto

[![build](https://github.com/FuelLabs/fuel-crypto/actions/workflows/ci.yml/badge.svg)](https://github.com/FuelLabs/fuel-crypto/actions/workflows/ci.yml)
[![crates.io](https://img.shields.io/crates/v/fuel-crypto?label=latest)](https://crates.io/crates/fuel-crypto)
[![docs](https://docs.rs/fuel-crypto/badge.svg)](https://docs.rs/fuel-crypto/)
[![discord](https://img.shields.io/badge/chat%20on-discord-orange?&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/xfpK4Pe)

Fuel cryptographic primitives.

## Compile features

- `std`: Unless set, the crate will link to the core-crate instead of the std-crate. More info [here](https://docs.rust-embedded.org/book/intro/no-std.html).
- `random`: Implement `no-std` [rand](https://crates.io/crates/rand) features for the provided types.
- `serde-types`: Add support for [serde](https://crates.io/crates/serde) for the provided types.
- `serde-types-minimal`: Add support for `no-std` [serde](https://crates.io/crates/serde) for the provided types.
57 changes: 57 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/// Crypto error variants
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde-types-minimal",
derive(serde::Serialize, serde::Deserialize)
)]
pub enum Error {
/// Invalid secp256k1 secret key
InvalidSecretKey,

/// Invalid secp256k1 public key
InvalidPublicKey,

/// Invalid secp256k1 signature message
InvalidMessage,

/// Invalid secp256k1 signature
InvalidSignature,

/// Out of preallocated memory
NotEnoughMemory,
}

#[cfg(feature = "std")]
mod use_std {
use super::*;
use secp256k1::Error as Secp256k1Error;
use std::{error, fmt};

impl From<Secp256k1Error> for Error {
fn from(secp: Secp256k1Error) -> Self {
match secp {
Secp256k1Error::IncorrectSignature
| Secp256k1Error::InvalidSignature
| Secp256k1Error::InvalidTweak
| Secp256k1Error::TweakCheckFailed
| Secp256k1Error::InvalidRecoveryId => Self::InvalidSignature,
Secp256k1Error::InvalidMessage => Self::InvalidMessage,
Secp256k1Error::InvalidPublicKey => Self::InvalidPublicKey,
Secp256k1Error::InvalidSecretKey => Self::InvalidSecretKey,
Secp256k1Error::NotEnoughMemory => Self::NotEnoughMemory,
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}
}
Loading

0 comments on commit 0803741

Please sign in to comment.