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

fix!: replace usage of u16s in generics with u32s #12

Merged
merged 2 commits into from
Sep 27, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ If the predefined JSON types are not sufficient for your use case, you can defin

The JSON struct in `dep::json_parser::json::JSON` is parametrised with the following parameters:

`struct JSON<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let MaxNumValues: u16, let MaxKeyFields: u16>`
`struct JSON<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let MaxNumValues: u32, let MaxKeyFields: u32>`

- `NumBytes` : the maximum size of the initial json blob
- `NumPackedFields` : take `NumBytes / 31`, round up to the nearest integer, then add 3!
Expand Down
8 changes: 4 additions & 4 deletions src/_comparison_tools/bounds_checker.nr
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ in this case, i == M
* @description this method is cheaper than querying `i < boundary` for `u16` and `u32` types
* cost = 3 gates + 2 gates per iteration
**/
pub fn get_validity_flags<let N: u16>(boundary: u16) -> [Field; N] {
pub fn get_validity_flags<let N: u32>(boundary: u32) -> [Field; N] {
let flags: [Field; N] = __get_validity_flags(boundary);
get_validity_flags_inner(boundary, flags)
}

unconstrained fn __get_validity_flags<let N: u16>(boundary: u16) -> [Field; N] {
unconstrained fn __get_validity_flags<let N: u32>(boundary: u32) -> [Field; N] {
let mut result: [Field; N] = [0; N];
for i in 0..N as u16 {
for i in 0..N {
if i < boundary {
result[i] = 1;
}
Expand All @@ -55,7 +55,7 @@ unconstrained fn __get_validity_flags<let N: u16>(boundary: u16) -> [Field; N] {
* aligns with what is expected from testing `i < boundary`
* N.B. this method will revert if `boundary > N`
**/
fn get_validity_flags_inner<let N: u16>(boundary: u16, flags: [Field; N]) -> [Field; N] {
fn get_validity_flags_inner<let N: u32>(boundary: u32, flags: [Field; N]) -> [Field; N] {
let initial_flag = flags[0];
let final_flag = flags[N - 1];

Expand Down
10 changes: 5 additions & 5 deletions src/_string_tools/slice_packed_field.nr
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ unconstrained fn __slice_field(f: Field, num_bytes: Field) -> [Field; 5] {
}

unconstrained fn __divmod(numerator: Field, denominator: Field) -> (Field, Field) {
let quotient = numerator as u16 / denominator as u16;
let remainder = numerator as u16 % denominator as u16;
let quotient = numerator as u32 / denominator as u32;
let remainder = numerator as u32 % denominator as u32;
(quotient as Field, remainder as Field)
}

Expand Down Expand Up @@ -473,7 +473,7 @@ fn divmod_31(numerator: Field) -> (Field, Field) {
unconstrained fn decompose(val: Field) -> [Field; 16] {
let mut r: [Field; 16] = [0; 16];

let mut it = val as u16;
let mut it = val as u32;
for i in 0..16 {
r[i] = (it & 1) as Field;
it >>= 1;
Expand All @@ -482,7 +482,7 @@ unconstrained fn decompose(val: Field) -> [Field; 16] {
}

// 5 gates?
pub fn get_last_limb_path<let OutputFields: u16>(last_limb_index: Field) -> [Field; OutputFields] {
pub fn get_last_limb_path<let OutputFields: u32>(last_limb_index: Field) -> [Field; OutputFields] {
// TODO we offset by 1 explain why (0 byte length produces 0 - 1 which = invalid array index. we just add 1 and increase array length by 1 to compensate)
let path = LAST_LIMB_PATH[last_limb_index + 1]; // 2

Expand Down Expand Up @@ -550,7 +550,7 @@ pub fn slice_field(f: Field, num_bytes: Field) -> (Field, Field) {
* @brief Given an array of fields that pack 31 bytes, return an array that slices the packed byte array at a given index for a given number of bytes
* @description Some serious dark black magic nonsense going on here. TODO: document
**/
pub fn slice_fields<let InputFields: u16, let OutputFields: u16>(
pub fn slice_fields<let InputFields: u32, let OutputFields: u32>(
data: [Field; InputFields],
start_byte: Field,
num_bytes: Field
Expand Down
6 changes: 3 additions & 3 deletions src/_string_tools/string_chopper.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::_string_tools::slice_packed_field::slice_fields;

struct StringChopper<let NeedlePackedFields: u16> {}
struct StringChopper<let NeedlePackedFields: u32> {}

impl<let NeedlePackedFields: u16> StringChopper<NeedlePackedFields> {
fn slice_string<let StringBytes: u16, let HaystackPackedFields: u16>(
impl<let NeedlePackedFields: u32> StringChopper<NeedlePackedFields> {
fn slice_string<let StringBytes: u32, let HaystackPackedFields: u32>(
_: Self,
haystack: [Field; HaystackPackedFields],
start_bytes: Field,
Expand Down
30 changes: 15 additions & 15 deletions src/_table_generation/make_tables.nr
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,12 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
single_value_layer_flags[KEY_TOKEN] = no_token_outcomes;

let mut flattened_flags: [Field; NUM_TOKENS * NUM_TOKENS * 3] = [0; NUM_TOKENS * NUM_TOKENS * 3];
let NN = NUM_TOKENS * NUM_TOKENS as Field;
let NN = (NUM_TOKENS * NUM_TOKENS) as Field;
for j in 0..NUM_TOKENS as u32 {
for k in 0..NUM_TOKENS as u32 {
flattened_flags[OBJECT_LAYER * NN + j as Field * NUM_TOKENS + k as Field] = object_layer_flags[j][k];
flattened_flags[ARRAY_LAYER * NN + j as Field * NUM_TOKENS + k as Field] = array_layer_flags[j][k];
flattened_flags[SINGLE_VALUE_LAYER * NN + j as Field * NUM_TOKENS + k as Field] = single_value_layer_flags[j][k];
flattened_flags[OBJECT_LAYER * NN + j as Field * (NUM_TOKENS as Field) + k as Field] = object_layer_flags[j][k];
flattened_flags[ARRAY_LAYER * NN + j as Field * (NUM_TOKENS as Field) + k as Field] = array_layer_flags[j][k];
flattened_flags[SINGLE_VALUE_LAYER * NN + j as Field * (NUM_TOKENS as Field) + k as Field] = single_value_layer_flags[j][k];
}
}
flattened_flags
Expand Down Expand Up @@ -541,17 +541,17 @@ unconstrained fn generate_token_flags_table() -> [Field; NUM_TOKENS * 2] {
numeric_flags.new_context = ARRAY_LAYER;
literal_flags.new_context = ARRAY_LAYER;

flags[NUM_TOKENS + NO_TOKEN] = no_token_flags;
flags[NUM_TOKENS + BEGIN_OBJECT_TOKEN] = begin_object_flags;
flags[NUM_TOKENS + END_OBJECT_TOKEN] = end_object_flags;
flags[NUM_TOKENS + BEGIN_ARRAY_TOKEN] = begin_array_flags;
flags[NUM_TOKENS + END_ARRAY_TOKEN] = end_array_flags;
flags[NUM_TOKENS + KEY_SEPARATOR_TOKEN] = no_token_flags;
flags[NUM_TOKENS + VALUE_SEPARATOR_TOKEN] = no_token_flags;
flags[NUM_TOKENS + STRING_TOKEN] = string_flags;
flags[NUM_TOKENS + NUMERIC_TOKEN] = numeric_flags;
flags[NUM_TOKENS + LITERAL_TOKEN] = literal_flags;
flags[NUM_TOKENS + KEY_TOKEN] = key_token_flags;
flags[NUM_TOKENS + (NO_TOKEN as u32)] = no_token_flags;
flags[NUM_TOKENS + (BEGIN_OBJECT_TOKEN as u32)] = begin_object_flags;
flags[NUM_TOKENS + (END_OBJECT_TOKEN as u32)] = end_object_flags;
flags[NUM_TOKENS + (BEGIN_ARRAY_TOKEN as u32)] = begin_array_flags;
flags[NUM_TOKENS + (END_ARRAY_TOKEN as u32)] = end_array_flags;
flags[NUM_TOKENS + (KEY_SEPARATOR_TOKEN as u32)] = no_token_flags;
flags[NUM_TOKENS + (VALUE_SEPARATOR_TOKEN as u32)] = no_token_flags;
flags[NUM_TOKENS + (STRING_TOKEN as u32)] = string_flags;
flags[NUM_TOKENS + (NUMERIC_TOKEN as u32)] = numeric_flags;
flags[NUM_TOKENS + (LITERAL_TOKEN as u32)] = literal_flags;
flags[NUM_TOKENS + (KEY_TOKEN as u32)] = key_token_flags;

let mut result: [Field; NUM_TOKENS * 2] = [0; NUM_TOKENS * 2];
for i in 0..(NUM_TOKENS as u32 * 2) {
Expand Down
10 changes: 5 additions & 5 deletions src/get_array.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::getters::JSONValue;
/**
* @brief getter methods for extracting array types out of a JSON struct
**/
impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let MaxNumValues: u16, let MaxKeyFields: u16> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {
impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let MaxNumValues: u32, let MaxKeyFields: u32> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {

/**
* @brief if the root JSON is an array, return its length
Expand All @@ -23,7 +23,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @brief if the root JSON is an object, extract an array given by `key`
* @description returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
**/
fn get_array<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Option<Self> {
fn get_array<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Option<Self> {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
let (exists, key_index) = self.key_exists_impl(key);
let entry: JSONEntry = self.json_entries_packed[key_index].into();
Expand All @@ -44,7 +44,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @brief if the root JSON is an object, extract an array given by `key`
* @description will revert if the array does not exist
**/
fn get_array_unchecked<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Self {
fn get_array_unchecked<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Self {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

let (entry, key_index) = self.get_json_entry_unchecked_with_key_index(key);
Expand All @@ -62,7 +62,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
/**
* @brief same as `get_array` for where the key length may be less than KeyBytes
**/
fn get_array_var<let KeyBytes: u16>(self, key: BoundedVec<u8, KeyBytes>) -> Option<Self> {
fn get_array_var<let KeyBytes: u32>(self, key: BoundedVec<u8, KeyBytes>) -> Option<Self> {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
let (exists, key_index) = self.key_exists_impl_var(key);
let entry: JSONEntry = self.json_entries_packed[key_index].into();
Expand All @@ -83,7 +83,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
/**
* @brief same as `get_array_unchecked` for where the key length may be less than KeyBytes
**/
fn get_array_unchecked_var<let KeyBytes: u16>(self, key: BoundedVec<u8, KeyBytes>) -> Self {
fn get_array_unchecked_var<let KeyBytes: u32>(self, key: BoundedVec<u8, KeyBytes>) -> Self {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

let (entry, key_index) = self.get_json_entry_unchecked_with_key_index_var(key);
Expand Down
10 changes: 5 additions & 5 deletions src/get_literal.nr
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ fn extract_literal_from_array(
/**
* @brief getter methods for extracting literal values out of a JSON struct
**/
impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let MaxNumValues: u16, let MaxKeyFields: u16> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {
impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let MaxNumValues: u32, let MaxKeyFields: u32> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {

/**
* @brief if the root JSON is an object, extract a literal value given by `key`
* @description returns an Option<JSONLiteral> which will be null if the literal does not exist
**/
fn get_literal<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Option<JSONLiteral> {
fn get_literal<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Option<JSONLiteral> {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

let (exists, entry) = self.get_json_entry(key);
Expand All @@ -87,7 +87,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @brief if the root JSON is an object, extract a literal value given by `key`
* @description will revert if the literal does not exist
**/
fn get_literal_unchecked<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> JSONLiteral {
fn get_literal_unchecked<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> JSONLiteral {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

let entry= self.get_json_entry_unchecked(key);
Expand All @@ -102,7 +102,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
/**
* @brief same as `get_literal` for where the key length may be less than KeyBytes
**/
fn get_literal_var<let KeyBytes: u16>(self, key: BoundedVec<u8, KeyBytes>) -> Option<JSONLiteral> {
fn get_literal_var<let KeyBytes: u32>(self, key: BoundedVec<u8, KeyBytes>) -> Option<JSONLiteral> {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

let (exists, entry) = self.get_json_entry_var(key);
Expand All @@ -117,7 +117,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
/**
* @brief same as `get_literal_unchecked` for where the key length may be less than KeyBytes
**/
fn get_literal_unchecked_var<let KeyBytes: u16>(self, key: BoundedVec<u8, KeyBytes>) -> JSONLiteral {
fn get_literal_unchecked_var<let KeyBytes: u32>(self, key: BoundedVec<u8, KeyBytes>) -> JSONLiteral {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

let entry= self.get_json_entry_unchecked_var(key);
Expand Down
10 changes: 5 additions & 5 deletions src/get_number.nr
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ fn extract_number_from_array(arr: [u8; U64_LENGTH_AS_BASE10_STRING], json_length
* @note numeric values must fit into a `u64` type.
* decimal values and scientific notation are not yet supported
**/
impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let MaxNumValues: u16, let MaxKeyFields: u16> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {
impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let MaxNumValues: u32, let MaxKeyFields: u32> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {

/**
* @brief if the root JSON is an object, extract a numeric value given by `key`
* @description returns an Option<u64> which will be null if the key does not exist
**/
fn get_number<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Option<u64> {
fn get_number<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Option<u64> {
let (exists, entry) = self.get_json_entry(key);
assert(
(entry.entry_type - NUMERIC_TOKEN) * exists as Field == 0, "get_number: entry exists but is not a number!"
Expand All @@ -48,7 +48,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @brief if the root JSON is an object, extract a u64 value given by `key`
* @description will revert if the number does not exist
**/
fn get_number_unchecked<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> u64 {
fn get_number_unchecked<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> u64 {
let entry = self.get_json_entry_unchecked(key);
assert(entry.entry_type == NUMERIC_TOKEN, "get_number_unchecked: entry exists but is not a number!");
let mut parsed_string: [u8; U64_LENGTH_AS_BASE10_STRING] = self.extract_string_entry(entry);
Expand All @@ -59,7 +59,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
/**
* @brief same as `get_number` for where the key length may be less than KeyBytes
**/
fn get_number_var<let KeyBytes: u16>(self, key: BoundedVec<u8, KeyBytes>) -> Option<u64> {
fn get_number_var<let KeyBytes: u32>(self, key: BoundedVec<u8, KeyBytes>) -> Option<u64> {
let (exists, entry) = self.get_json_entry_var(key);
assert(
(entry.entry_type - NUMERIC_TOKEN) * exists as Field == 0, "get_number: entry exists but is not a number!"
Expand All @@ -72,7 +72,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
/**
* @brief same as `get_number_unchecked` for where the key length may be less than KeyBytes
**/
fn get_number_unchecked_var<let KeyBytes: u16>(self, key: BoundedVec<u8, KeyBytes>) -> u64 {
fn get_number_unchecked_var<let KeyBytes: u32>(self, key: BoundedVec<u8, KeyBytes>) -> u64 {
let entry = self.get_json_entry_unchecked_var(key);
assert(entry.entry_type == NUMERIC_TOKEN, "get_number_unchecked: entry exists but is not a number!");
let mut parsed_string: [u8; U64_LENGTH_AS_BASE10_STRING] = self.extract_string_entry(entry);
Expand Down
10 changes: 5 additions & 5 deletions src/get_object.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use crate::getters::JSONValue;
/**
* @brief getter methods for extracting object types out of a JSON struct
**/
impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let MaxNumValues: u16, let MaxKeyFields: u16> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {
impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let MaxNumValues: u32, let MaxKeyFields: u32> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {

/**
* @brief if the root JSON is an object, extract a child object given by `key`
* @description returns an Option<JSON> where, if the object exists, the JSON object will have the requested object as its root value
**/
fn get_object<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Option<Self> {
fn get_object<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Option<Self> {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

let (exists, key_index) = self.key_exists_impl(key);
Expand All @@ -35,7 +35,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @brief if the root JSON is an object, extract a child object given by `key`
* @description will revert if the requested object does not exist
**/
fn get_object_unchecked<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Self {
fn get_object_unchecked<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Self {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index(key);
let entry: JSONEntry = self.json_entries_packed[key_index].into();
Expand All @@ -52,7 +52,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
/**
* @brief same as `get_object` for where the key length may be less than KeyBytes
**/
fn get_object_var<let KeyBytes: u16>(self, key: BoundedVec<u8, KeyBytes>) -> Option<Self> {
fn get_object_var<let KeyBytes: u32>(self, key: BoundedVec<u8, KeyBytes>) -> Option<Self> {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

let (exists, key_index) = self.key_exists_impl_var(key);
Expand All @@ -72,7 +72,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
/**
* @brief same as `get_object_unchecked` for where the key length may be less than KeyBytes
**/
fn get_object_unchecked_var<let KeyBytes: u16>(self, key: BoundedVec<u8, KeyBytes>) -> Self {
fn get_object_unchecked_var<let KeyBytes: u32>(self, key: BoundedVec<u8, KeyBytes>) -> Self {
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index_var(key);
let entry: JSONEntry = self.json_entries_packed[key_index].into();
Expand Down
Loading
Loading