forked from visoftsolutions/noir_rs
-
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.
feat: add str support for args + add name/symbol/decimal to token (Az…
…tecProtocol#3862) Fixes AztecProtocol#2889 by adding support for str as input arg and adding a compressed string struct.
- Loading branch information
Showing
37 changed files
with
503 additions
and
43 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
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
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
16 changes: 16 additions & 0 deletions
16
yarn-project/aztec-nr/aztec/src/types/type_serialization/u8_serialization.nr
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,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 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 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> { | ||
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 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 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
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
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
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
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
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
Oops, something went wrong.