Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sw linguist-language=Rust
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ Cargo.lock

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

*/Cargo.lock
*/Forc.lock

*/out/*
*/target/*
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# fuel-crypto
Various Cryptographic Primitives in Sway for the Fuel VM

# Testing

## BLS

To run tests for bls folder:
```
cd testing/tests_bls12_381
forc test
```

## Testing with a script
You can use scripts locally to do intermediate tests. To run a script a local Fuel node must be spun up.

### Spin Up a Fuel node
From [here](https://fuellabs.github.io/sway/v0.19.0/introduction/overview.html).
In a separate tab in your terminal, spin up a local Fuel node:


`fuel-core --db-type in-memory`

This starts a Fuel node with a volatile database that will be cleared when shut down (good for testing purposes).

Make sure `fuel-core` is up to date. This can be done with [fuelup](https://github.com/FuelLabs/fuelup). Also, make sure there's only 1 `fuel-core` installed (check this with `which -a fuel-core`).

### Create and run a script

For example in `bls12_381/src` create `main.sw`. Change in `bls12_381/Forc.toml` `entry` to `main.sw`.

Start the file with `script;` and whatever code is in `fn main () { .. }` will be executed with the following command:

```
forc run --unsigned --pretty-print
```

The `--unsigned` part is to avoid signing with a contract. The `--pretty-print` is for if you do some logging; it will get printed nicely.

# FuelVM Instruction Set

Find all assembly instructions that can be used [here](https://github.com/FuelLabs/fuel-specs/blob/master/specs/vm/instruction_set.md#sub-subtract).
2 changes: 2 additions & 0 deletions bls12_381/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
15 changes: 15 additions & 0 deletions bls12_381/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[project]
name = "bls12_381"
version = "0.1.0"
authors = ["Hashcloak"]
edition = "2021"
license = "Apache-2.0"

[dependencies]
fuels = { version = "0.23", features = ["fuel-core-lib"] }
tokio = { version = "1.12", features = ["rt", "macros"] }

[[test]]
harness = true
name = "integration_tests"
path = "tests/harness.rs"
8 changes: 8 additions & 0 deletions bls12_381/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Hashcloak"]
entry = "lib.sw"
license = "Apache-2.0"
name = "bls12_381"

[dependencies]
utils = { path = "../utils" }
114 changes: 114 additions & 0 deletions bls12_381/src/f12.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
library fp12;

dep fp6;

use fp6::Fp6;
use utils::choice::{ConstantTimeEq};
use core::ops::{Eq, Add, Subtract, Multiply};

// Element in F_{p^12}
pub struct Fp12 {
c0: Fp6,
c1: Fp6,
}

impl ConditionallySelectable for Fp12 {
// Select a if choice == 1 or select b if choice == 0, in constant time
fn conditional_select(a: Self, b: Self, choice: Choice) -> Self {
Fp12 {
c0: ~Fp6::conditional_select(a.c0, b.c0, choice),
c1: ~Fp6::conditional_select(a.c1, b.c1, choice),
}
}
}

impl ConstantTimeEq for Fp12 {
// returns (self == other), as a choice
fn ct_eq(self, other: Self) -> Choice {
self.c0.ct_eq(other.c0) & self.c1.ct_eq(other.c1)
}
}

impl Fp12 {
fn eq(self, other: Self) -> bool {
self.ct_eq(other).unwrap_as_bool()
}

pub fn zero() -> Self {
Fp12 {
c0: ~Fp6::zero(),
c1: ~Fp6::zero(),
}
}

pub fn one() -> Self {
Fp12 {
c0: ~Fp6::one(),
c1: ~Fp6::zero(),
}
}

fn from(f: Fp) -> Fp12 {
Fp12 {
c0: ~Fp6::from(f),
c1: ~Fp6::zero(),
}
}

fn from(f: Fp2) -> Fp12 {
Fp12 {
c0: ~Fp6::from(f),
c1: ~Fp6::zero(),
}
}

fn from(f: Fp6) -> Fp12 {
Fp12 {
c0: f,
c1: ~Fp6::zero(),
}
}

fn is_zero(self) -> Choice {
self.c0.is_zero().binary_and(self.c1.is_zero())
}

fn neg(self) -> Self {
Fp12 {
c0: self.c0.neg(),
c1: self.c1.neg(),
}
}

fn add(self, rhs: Fp12) -> Self {
Fp12 {
c0: self.c0 + rhs.c0,
c1: self.c1 + rhs.c1,
}
}

fn sub(self, rhs: Fp12) -> Self {
Fp12 {
c0: self.c0 - rhs.c0,
c1: self.c1 - rhs.c1,
}
}
}

impl Eq for Fp12 {
fn eq(self, other: Self) -> bool {
self.eq(other)
}
}

impl Add for Fp12 {
fn add(self, other: Fp12) -> Self {
self.add(other)
}
}

impl Subtract for Fp12 {
fn subtract(self, other: Fp12) -> Self {
self.sub(other)
}
}
Loading