Skip to content

Commit

Permalink
Feat: Add solidity like bytes which supports keccak and sha256 (#184)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

## Pull Request type

<!-- Please try to limit your pull request to one type; submit multiple
pull requests if needed. -->

Please check the type of change your PR introduces:

- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no API changes)
- [ ] Build-related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying, or
link to a relevant issue. -->

`bytes` is an implementation similar to Solidity bytes written in Cairo
1.
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.

Issue Number: N/A

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

- Add bytes implementation
- Add bytes unit tests

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

<!-- If this does introduce a breaking change, please describe the
impact and migration path for existing applications below. -->

## Other information

The implementation of `bytes` differs from `byte_array` in corelib. Each
bytes array element stores 16 bytes and is designed to calculate byte
stream hashes more quickly by trading more space for less computational
cost.

<!-- Any other information that is important to this PR, such as
screenshots of how the component looks before and after the change. -->
  • Loading branch information
zkcarter authored Oct 10, 2023
1 parent f37d73d commit 69e36b9
Show file tree
Hide file tree
Showing 9 changed files with 1,799 additions and 1 deletion.
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

0 comments on commit 69e36b9

Please sign in to comment.