Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add solidity like bytes which supports keccak and sha256 #184

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ This repository is composed of multiple crates:
- [Searching](./src/searching/README.md)
- [Sorting](./src/sorting/README.md)
- [Storage](./src/storage/README.md)
- [Bytes](./src/bytes/README.md)

## Getting Started

Expand Down
3 changes: 2 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ members = [
"src/searching",
"src/sorting",
"src/storage",
"src/ascii"
"src/ascii",
"src/bytes"
]
name = "alexandria"
version = "0.1.0"
Expand Down
39 changes: 39 additions & 0 deletions src/bytes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# bytes

**bytes** is an implementation similar to Solidity bytes written in Cairo 1 by [zkLink](https://zk.link/). It is notable for its built-in implementation of `Keccak` and `Sha256` hash functions, offering convenience for migrating EVM Contracts written in Solidity to the Starknet ecosystem.

## Example

**bytes** implements read and write operations for all Cairo built-in types, as well as `keccak/sha256` operations. For example, in Solidity, if there is a `Token` type, and we want to calculate the keccak256 hash of an instance of this structure.

```solidity
struct Token {
uint16 tokenId;
address tokenAddress;
uint8 decimals;
}
```

In Solidity, we can achieve this by doing the following:

```solidity
keccak256(abi.encodePacked(tokenId, tokenAddress, decimals));
```

In Cairo, we can effortlessly achieve the same using `Bytes`:

```rust
use alexandria_bytes::Bytes;
use alexandria_bytes::BytesTrait;
use debug::PrintTrait;
use starknet::contract_address_const;

fn main() {
let mut bytes: Bytes = BytesTrait::new(0, array![]);
bytes.append_u16(1);
bytes.append_address(contract_address_const::<0x123>());
bytes.append_u8(16);
let keccak_hash = bytes.keccak();
keccak_hash.print();
}
```
9 changes: 9 additions & 0 deletions src/bytes/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "alexandria_bytes"
version = "0.1.0"
description = "An implementation similar to Solidity bytes written in Cairo 1"
homepage = "https://github.com/keep-starknet-strange/alexandria/tree/main/src/bytes"

[dependencies]
alexandria_math = { path = "../math" }
alexandria_data_structures = { path = "../data_structures" }
Loading