Skip to content

Commit

Permalink
Hashing and Cryptography (FuelLabs#1362)
Browse files Browse the repository at this point in the history
* hashing and cryptography

* fix deps

* signature forc toml lock

* signature forc toml example dep std

* index title

* rust to sway code example

* include fix

* book end line formatting

* Update docs/src/blockchain-development/hashing_and_cryptography.md

Co-authored-by: John Adler <[email protected]>

* title, nits, formatting

* toml file

* editor level formatting fix

* editor level formatting fix

* fix formatter formatting in b512..

* fmt

Co-authored-by: SilentCicero <>
Co-authored-by: John Adler <[email protected]>
Co-authored-by: Mohammad Fawaz <[email protected]>
  • Loading branch information
3 people authored May 6, 2022
1 parent a57ad40 commit c158ef7
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- [Comments and Logging](./basics/comments_and_logging.md)
- [Control Flow](./basics/control_flow.md)
- [Blockchain Development with Sway](./blockchain-development/index.md)
- [Hashing and Cryptography](./blockchain-development/hashing_and_cryptography.md)
- [Contract Storage](./blockchain-development/storage.md)
- [Function Purity](./blockchain-development/purity.md)
- [Identifiers](./blockchain-development/identifiers.md)
Expand Down
15 changes: 15 additions & 0 deletions docs/src/blockchain-development/hashing_and_cryptography.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Hashing and Cryptography

The Sway standard library provides easy access to a selection of cryptographic hash functions (`sha256` and Ethereum-compatible `keccak256`), and Ethereum-compatible `secp256k1`-based signature recovery operations.

## Hashing

```sway
{{#include ../../../examples/hashing/src/main.sw}}
```

## Signature Recovery

```sway
{{#include ../../../examples/signatures/src/main.sw}}
```
1 change: 1 addition & 0 deletions docs/src/blockchain-development/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Sway is fundamentally a blockchain language. Because of this, it has some featur

These are also some concepts related to the FuelVM and Fuel ecosystem that you may utilize when writing Sway.

- [Hashing and Cryptography](./hashing_and_cryptography.md)
- [Contract Storage](./storage.md)
- [Function Purity](./purity.md)
- [Identifiers](./identifiers.md)
Expand Down
11 changes: 11 additions & 0 deletions examples/hashing/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[package]]
name = 'core'
dependencies = []

[[package]]
name = 'hashing'
dependencies = ['std']

[[package]]
name = 'std'
dependencies = ['core']
8 changes: 8 additions & 0 deletions examples/hashing/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "hashing"

[dependencies]
std = { path = "../../sway-lib-std" }
19 changes: 19 additions & 0 deletions examples/hashing/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
script;

use std::chain::log_b256;
use std::hash::{HashMethod, hash_pair, hash_u64, hash_value};

const VALUE_A = 0x9280359a3b96819889d30614068715d634ad0cf9bba70c0f430a8c201138f79f;

fn main() {
// Hash a single u64 value.
let hashed_u64 = hash_u64(100, HashMethod::Sha256);

// Hash a single b256 value.
let hashed_b256 = hash_value(hashed_u64, HashMethod::Keccak256);

// Hash two b256 values.
let hashed_pair = hash_pair(hashed_b256, VALUE_A, HashMethod::Sha256);

log_b256(hashed_pair);
}
11 changes: 11 additions & 0 deletions examples/signatures/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[package]]
name = 'core'
dependencies = []

[[package]]
name = 'signatures'
dependencies = ['std']

[[package]]
name = 'std'
dependencies = ['core']
8 changes: 8 additions & 0 deletions examples/signatures/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "signatures"

[dependencies]
std = { path = "../../sway-lib-std" }
26 changes: 26 additions & 0 deletions examples/signatures/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
script;

use std::result::Result;
use std::b512::B512;
use std::panic::panic;
use std::chain::log_b256;
use std::ecr::{EcRecoverError, ec_recover, ec_recover_address};

const MSG_HASH = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323;

fn main() {
let hi = 0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c;
let lo = 0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d;
let signature: B512 = ~B512::from(hi, lo);

// A recovered public key pair.
let public_key = ec_recover(signature, MSG_HASH);

// A recovered Fuel address.
let result_address: Result<Address, EcRecoverError> = ec_recover_address(signature, MSG_HASH);
if let Result::Ok(address) = result_address {
log_b256(address.value);
} else {
panic(0);
};
}

0 comments on commit c158ef7

Please sign in to comment.