forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hashing and Cryptography (FuelLabs#1362)
* 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
1 parent
a57ad40
commit c158ef7
Showing
9 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
docs/src/blockchain-development/hashing_and_cryptography.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |