-
Notifications
You must be signed in to change notification settings - Fork 607
feat: add str support for args + add name/symbol/decimal to token #3862
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d449a1a
feat: add str support for args + add name/symbol/decimal to token
LHerskind b268838
chore: fix gitignore
LHerskind dc93e4c
chore: fix minor issues
LHerskind c0b855e
chore: update deployTokenContract in browser.ts
LHerskind e5556d0
chore: rename FCS to FixedstringCompressedString
LHerskind aa1d144
chore: fix serializes string test of encoder
LHerskind 6a29a86
chore: address comments
LHerskind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
16 changes: 16 additions & 0 deletions
16
yarn-project/aztec-nr/aztec/src/types/type_serialization/u8_serialization.nr
This file contains hidden or 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,16 @@ | ||
| use crate::types::type_serialization::TypeSerializationInterface; | ||
|
|
||
| global U8_SERIALIZED_LEN: Field = 1; | ||
|
|
||
| fn deserializeU8(fields: [Field; U8_SERIALIZED_LEN]) -> u8 { | ||
| fields[0] as u8 | ||
| } | ||
|
|
||
| fn serializeU8(value: u8) -> [Field; U8_SERIALIZED_LEN] { | ||
| [value as Field] | ||
| } | ||
|
|
||
| global U8SerializationMethods = TypeSerializationInterface { | ||
| deserialize: deserializeU8, | ||
| serialize: serializeU8, | ||
| }; |
This file contains hidden or 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,9 @@ | ||
| [package] | ||
| name = "compressed_string" | ||
| authors = [""] | ||
| compiler_version = ">=0.18.0" | ||
| type = "lib" | ||
|
|
||
| [dependencies] | ||
| aztec = {path = "../aztec"} | ||
| protocol_types = {path = "../../noir-protocol-circuits/src/crates/types"} |
142 changes: 142 additions & 0 deletions
142
yarn-project/aztec-nr/compressed-string/src/compressed_string.nr
This file contains hidden or 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,142 @@ | ||
| use dep::aztec::types::type_serialization::TypeSerializationInterface; | ||
| use dep::protocol_types::utils::field::field_from_bytes; | ||
| use dep::std; | ||
|
|
||
| // A Fixedsize Compressed String. | ||
| // Essentially a special version of Compressed String for practical use. | ||
| struct FieldCompressedString{ | ||
| value: Field | ||
| } | ||
|
|
||
| impl FieldCompressedString{ | ||
| pub fn is_eq(self, other: FieldCompressedString) -> bool { | ||
| self.value == other.value | ||
| } | ||
|
|
||
| pub fn from_field(input_field: Field) -> Self { | ||
| Self {value: input_field} | ||
| } | ||
|
|
||
| pub fn from_string(input_string: str<31>) -> Self { | ||
| Self {value: field_from_bytes(input_string.as_bytes(), true)} | ||
| } | ||
|
|
||
| pub fn to_bytes(self) -> [u8; 31] { | ||
| let mut result = [0; 31]; | ||
| let bytes = self.value.to_be_bytes(31); | ||
| for i in 0..31 { | ||
| result[i] = bytes[i]; | ||
| } | ||
| result | ||
| } | ||
|
|
||
| pub fn serialize(self) -> [Field; 1] { | ||
| [self.value] | ||
| } | ||
|
|
||
| pub fn deserialize(input: [Field; 1]) -> Self { | ||
| Self { value: input[0] } | ||
| } | ||
| } | ||
|
|
||
| fn deserialize(fields: [Field; 1]) -> FieldCompressedString { | ||
| FieldCompressedString { value: fields[0] } | ||
| } | ||
|
|
||
| fn serialize(value: FieldCompressedString) -> [Field; 1] { | ||
| value.serialize() | ||
| } | ||
| global FieldCompressedStringSerializationMethods = TypeSerializationInterface { | ||
| deserialize, | ||
| serialize, | ||
| }; | ||
|
|
||
| // The general Compressed String. | ||
| // Compresses M bytes into N fields. | ||
| // Can be used for longer strings that don't fit in a single field. | ||
| // Each field can store 31 characters, so N should be M/31 rounded up. | ||
| struct CompressedString<N, M> { | ||
|
benesjan marked this conversation as resolved.
|
||
| value: [Field; N] | ||
| } | ||
|
|
||
| impl<N, M> CompressedString<N, M> { | ||
| pub fn from_string(input_string: str<M>) -> Self { | ||
| let mut fields = [0; N]; | ||
| let byts = input_string.as_bytes(); | ||
|
|
||
| let mut r_index = 0 as u32; | ||
|
|
||
| for i in 0..N { | ||
| let mut temp = [0 as u8; 31]; | ||
| for j in 0..31 { | ||
| if r_index < M { | ||
| temp[j] = byts[r_index]; | ||
| r_index += 1; | ||
| } | ||
| } | ||
|
|
||
| fields[i] = field_from_bytes(temp, true); | ||
| } | ||
|
|
||
| Self { value: fields } | ||
| } | ||
|
|
||
| pub fn to_bytes(self) -> [u8; M] { | ||
| let mut result = [0; M]; | ||
| let mut w_index = 0 as u32; | ||
| for i in 0..N { | ||
| let bytes = self.value[i].to_be_bytes(31); | ||
| for j in 0..31 { | ||
| if w_index < M { | ||
| result[w_index] = bytes[j]; | ||
| w_index += 1; | ||
| } | ||
| } | ||
| } | ||
| result | ||
| } | ||
|
|
||
| pub fn serialize(self) -> [Field; N] { | ||
| self.value | ||
| } | ||
|
|
||
| pub fn deserialize(input: [Field; N]) -> Self { | ||
| Self { value: input } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_short_string() { | ||
| let i = "Hello world"; | ||
| let b = i.as_bytes(); | ||
| let name: CompressedString<1,11> = CompressedString::from_string(i); | ||
| let p = b == name.to_bytes(); | ||
| assert(p, "invalid recover"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_long_string() { | ||
| let i = "Hello world. I'm setting up a very long text of blibbablubb such that we can see if works as planned for longer names."; | ||
| let b = i.as_bytes(); | ||
| let name: CompressedString<4,118> = CompressedString::from_string(i); | ||
| let p = b == name.to_bytes(); | ||
| assert(p, "invalid recover"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_long_string_work_with_too_many_fields() { | ||
| let i = "Hello world. I'm setting up a very long text of blibbablubb such that we can see if works as planned for longer names."; | ||
| let b = i.as_bytes(); | ||
| let name: CompressedString<5,118> = CompressedString::from_string(i); | ||
| let p = b == name.to_bytes(); | ||
| assert(p, "invalid recover"); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| fn test_long_string_fail_with_too_few_fields() { | ||
| let i = "Hello world. I'm setting up a very long text of blibbablubb such that we can see if works as planned for longer names."; | ||
| let b = i.as_bytes(); | ||
| let name: CompressedString<3,118> = CompressedString::from_string(i); | ||
| let p = b == name.to_bytes(); | ||
| assert(p, "invalid recover"); | ||
| } | ||
This file contains hidden or 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,4 @@ | ||
| mod compressed_string; | ||
|
|
||
| use crate::compressed_string::{CompressedString}; | ||
| use crate::compressed_string::{FieldCompressedString, FieldCompressedStringSerializationMethods}; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for my understanding - why 31 and not 32?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
32 * 8 = 256. We don't have that many bits to use in a field.