Skip to content

Commit

Permalink
more comments, small optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
zac-williamson committed Sep 2, 2024
1 parent b7c0f8f commit 3487412
Show file tree
Hide file tree
Showing 19 changed files with 3,143 additions and 3,082 deletions.
6 changes: 3 additions & 3 deletions out.txt

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/enums.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mod ScanMode {
global GRAMMAR_SCAN = 0;
global STRING_SCAN = 1;
global NUMERIC_SCAN = 2;
global LITERAL_SCAN: Field = 3;
}

mod Token {
global NO_TOKEN = 0;
global BEGIN_OBJECT_TOKEN = 1;
global END_OBJECT_TOKEN = 2;
global BEGIN_ARRAY_TOKEN = 3;
global END_ARRAY_TOKEN: Field = 4;
global KEY_SEPARATOR_TOKEN = 5;
global VALUE_SEPARATOR_TOKEN = 6;
global STRING_TOKEN = 7;
global NUMERIC_TOKEN = 8;
global LITERAL_TOKEN =9;
global KEY_TOKEN = 10;
global NUM_TOKENS = 11;
global NUM_TOKENS_MUL_2 = 22;
}

mod Layer {
global OBJECT_LAYER = 0;
global ARRAY_LAYER = 1;
global SINGLE_VALUE_LAYER = 2;
}

96 changes: 45 additions & 51 deletions src/get_array.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::json_entry::JSONEntry;
use crate::json::JSON;
use crate::lt::lt_field_16_bit;
use crate::json_tables::{OBJECT_LAYER, ARRAY_LAYER, BEGIN_ARRAY_TOKEN};
use crate::keyhash::get_keyhash;
use crate::enums::Token::END_ARRAY_TOKEN;
use crate::enums::Layer::{OBJECT_LAYER, ARRAY_LAYER};
use crate::getters::JSONValue;

/**
Expand All @@ -14,8 +14,8 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @brief if the root JSON is an array, return its length
**/
fn get_length(self) -> u32 {
assert(self.layer_context == ARRAY_LAYER, "can only get length of an array type");
let parent_entry: JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
assert(self.layer_type_of_root == ARRAY_LAYER, "can only get length of an array type");
let parent_entry: JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
parent_entry.num_children as u32
}

Expand All @@ -24,18 +24,18 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @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> {
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
let (exists, key_index) = self.key_exists_impl(key, KeyBytes);
let entry: JSONEntry = self.packed_json_entries[key_index].into();
let entry: JSONEntry = self.json_entries_packed[key_index].into();
assert(
(entry.entry_type - BEGIN_ARRAY_TOKEN) * exists as Field == 0, "key does not describe an object"
(entry.entry_type - END_ARRAY_TOKEN) * exists as Field == 0, "key does not describe an object"
);

let mut r = self;
r.layer_id = entry.parent_index;
r.layer_type_of_root = entry.parent_index;
r.root_id = entry.id;
r.layer_context = ARRAY_LAYER;
r.layer_index_in_transcript = key_index;
r.layer_type_of_root = ARRAY_LAYER;
r.root_index_in_transcript = key_index;

Option { _is_some: exists, _value: r }
}
Expand All @@ -45,16 +45,16 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @description will revert if the array does not exist
**/
fn get_array_unchecked<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Self {
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
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, KeyBytes);
assert(entry.entry_type == BEGIN_ARRAY_TOKEN, "key does not describe an object");
assert(entry.entry_type == END_ARRAY_TOKEN, "key does not describe an object");

let mut r = self;
r.layer_id = entry.parent_index;
r.layer_type_of_root = entry.parent_index;
r.root_id = entry.id;
r.layer_context = ARRAY_LAYER;
r.layer_index_in_transcript = key_index;
r.layer_type_of_root = ARRAY_LAYER;
r.root_index_in_transcript = key_index;

r
}
Expand All @@ -63,19 +63,19 @@ 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: [u8; KeyBytes], key_length: u16) -> Option<Self> {
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
let (exists, key_index) = self.key_exists_impl(key, key_length);
let entry: JSONEntry = self.packed_json_entries[key_index].into();
let entry: JSONEntry = self.json_entries_packed[key_index].into();
// TODO: ADD A layer_context VARIABLE INTO JSON WHICH DESCRIBES WHETHER WE ARE AN OBJECT, ARRAY OR SINGLE VALUE
assert(
(entry.entry_type - BEGIN_ARRAY_TOKEN) * exists as Field == 0, "key does not describe an object"
(entry.entry_type - END_ARRAY_TOKEN) * exists as Field == 0, "key does not describe an object"
);

let mut r = self;
r.layer_id = entry.parent_index;
r.layer_type_of_root = entry.parent_index;
r.root_id = entry.id;
r.layer_context = ARRAY_LAYER;
r.layer_index_in_transcript = key_index;
r.layer_type_of_root = ARRAY_LAYER;
r.root_index_in_transcript = key_index;

Option { _is_some: exists, _value: r }
}
Expand All @@ -84,16 +84,16 @@ 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: [u8; KeyBytes], key_length: u16) -> Self {
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
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, key_length);
assert(entry.entry_type == BEGIN_ARRAY_TOKEN, "key does not describe an object");
assert(entry.entry_type == END_ARRAY_TOKEN, "key does not describe an object");

let mut r = self;
r.layer_id = entry.parent_index;
r.layer_type_of_root = entry.parent_index;
r.root_id = entry.id;
r.layer_context = ARRAY_LAYER;
r.layer_index_in_transcript = key_index;
r.layer_type_of_root = ARRAY_LAYER;
r.root_index_in_transcript = key_index;

r
}
Expand All @@ -103,22 +103,22 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @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_from_array(self, array_index: Field) -> Option<Self> {
assert(self.layer_context == ARRAY_LAYER, "can only acceess array elements from array");
assert(self.layer_type_of_root == ARRAY_LAYER, "can only acceess array elements from array");

let parent_entry: JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
let parent_entry: JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;

let entry: JSONEntry = self.packed_json_entries[entry_index].into();
let entry: JSONEntry = self.json_entries_packed[entry_index].into();
assert(
(entry.entry_type - BEGIN_ARRAY_TOKEN) * valid as Field == 0, "get_object_from_array: entry exists but is not an object!"
(entry.entry_type - END_ARRAY_TOKEN) * valid as Field == 0, "get_object_from_array: entry exists but is not an object!"
);

let mut r = self;
r.layer_id = entry.parent_index;
r.layer_type_of_root = entry.parent_index;
r.root_id = entry.id;
r.layer_context = ARRAY_LAYER;
r.layer_index_in_transcript = entry_index;
r.layer_type_of_root = ARRAY_LAYER;
r.root_index_in_transcript = entry_index;

Option { _is_some: valid, _value: r }
}
Expand All @@ -128,40 +128,39 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @description will revert if the array does not exist
**/
fn get_array_from_array_unchecked(self, array_index: Field) -> Self {
assert(self.layer_context == ARRAY_LAYER, "can only acceess array elements from array");
assert(self.layer_type_of_root == ARRAY_LAYER, "can only acceess array elements from array");

let parent_entry: JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
let parent_entry: JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
assert(valid, "array overflow");
let entry_index = (parent_entry.child_pointer + array_index);

let entry: JSONEntry = self.packed_json_entries[entry_index].into();
let entry: JSONEntry = self.json_entries_packed[entry_index].into();
assert(
entry.entry_type == BEGIN_ARRAY_TOKEN, "get_array_from_array_unchecked: entry exists but is not an array!"
entry.entry_type == END_ARRAY_TOKEN, "get_array_from_array_unchecked: entry exists but is not an array!"
);

let mut r = self;
r.layer_id = entry.parent_index;
r.layer_type_of_root = entry.parent_index;
r.root_id = entry.id;
r.layer_context = ARRAY_LAYER;
r.layer_index_in_transcript = entry_index;
r.layer_type_of_root = ARRAY_LAYER;
r.root_index_in_transcript = entry_index;
r
}

/**
* @brief if the root is an array, map over the array values, applying `fn f` to each value
**/
fn map<U, let MaxElements: u32, let MaxElementBytes: u32>(self, f: fn(JSONValue<MaxElementBytes>) -> U) -> [U; MaxElements] where U: std::default::Default {
assert(self.layer_context == ARRAY_LAYER, "can only call map on an array");
assert(self.layer_type_of_root == ARRAY_LAYER, "can only call map on an array");

let entry: JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
let entry: JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
let num_children = entry.num_children;
let mut r: [U; MaxElements] = [U::default(); MaxElements];

for i in 0..MaxElements {
let valid = lt_field_16_bit(i as Field, num_children);
let entry_index = (entry.child_pointer + i as Field) * valid as Field;
let child_entry: JSONEntry = self.packed_json_entries[entry_index].into();
let child_entry: JSONEntry = self.json_entries_packed[entry_index].into();
let mut parsed_string: [u8; MaxElementBytes] = [0; MaxElementBytes];
for j in 0..MaxElementBytes {
let byte_valid = lt_field_16_bit(j as Field, child_entry.json_length);
Expand All @@ -187,12 +186,12 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
fn test_array() {
let text = "{ \"foo\": [ [1,2,3], [[3,4]], [[]], [], { \"bar\": [\"b\", \"a\", \"z\" ]} ]}";

let mut json: JSON<_, 7, 60, 60> = JSON::parse_json(text);
let mut json: JSON<_, 7, 60, 60> = JSON::parse_json_from_string(text);

let first = json.get_array_unchecked("foo".as_bytes());
assert(first.get_length() == 5);

let A = first.get_array_from_array_unchecked(0);
let A: JSON<68, 7, 60, 60> = first.get_array_from_array_unchecked(0);
assert(A.get_length() == 3);

let B = first.get_array_from_array_unchecked(1);
Expand All @@ -217,11 +216,6 @@ fn test_array() {

let E = first.get_object_from_array_unchecked(4);

let entry_maybe: JSONEntry = E.packed_json_entries[E.layer_index_in_transcript].into();
println(f"entry = {entry_maybe}");
let child: JSONEntry = E.packed_json_entries[entry_maybe.child_pointer].into();
println(f"target? = {child}");
println(f"{E}");
let E_A = E.get_array_unchecked("bar".as_bytes());
assert(E_A.get_length() == 3);
}
24 changes: 12 additions & 12 deletions src/get_literal.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::json_entry::JSONEntry;
use crate::json::JSON;
use crate::lt::lt_field_16_bit;
use crate::json_tables::{OBJECT_LAYER, ARRAY_LAYER, LITERAL_TOKEN};
use crate::keyhash::get_keyhash;
use crate::enums::Layer::{OBJECT_LAYER, ARRAY_LAYER};
use crate::enums::Token::LITERAL_TOKEN;
use crate::getters::JSONValue;

global MAX_LITERAL_LENGTH_AS_STRING = 5;
Expand Down Expand Up @@ -72,7 +72,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @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> {
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

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

let entry= self.get_json_entry_unchecked(key);
assert(
Expand All @@ -103,7 +103,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: [u8; KeyBytes], key_length: u16) -> Option<JSONLiteral> {
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

let (exists, entry) = self.get_json_entry_var(key, key_length);
assert(
Expand All @@ -118,7 +118,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: [u8; KeyBytes], key_length: u16) -> JSONLiteral {
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");

let entry= self.get_json_entry_unchecked_var(key, key_length);
assert(
Expand All @@ -134,14 +134,14 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @description returns an Option<JSONLiteral> which will be null if the literal does not exist
**/
fn get_literal_from_array(self, array_index: Field) -> Option<JSONLiteral> {
assert(self.layer_context == ARRAY_LAYER, "can only acceess array elements from array");
assert(self.layer_type_of_root == ARRAY_LAYER, "can only acceess array elements from array");

let parent_entry : JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
let parent_entry : JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();

let valid = lt_field_16_bit(array_index, parent_entry.num_children);
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;

let entry : JSONEntry = self.packed_json_entries[entry_index].into();
let entry : JSONEntry = self.json_entries_packed[entry_index].into();

assert(
(entry.entry_type - LITERAL_TOKEN) * valid as Field == 0, "get_literal_from_array: entry exists but is not a literal!"
Expand All @@ -158,15 +158,15 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
* @description will revert if the literal does not exist
**/
fn get_literal_from_array_unchecked(self, array_index: Field) -> JSONLiteral {
assert(self.layer_context == ARRAY_LAYER, "can only acceess array elements from array");
assert(self.layer_type_of_root == ARRAY_LAYER, "can only acceess array elements from array");

let parent_entry : JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
let parent_entry : JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();

let valid = lt_field_16_bit(array_index, parent_entry.num_children);
assert(valid, "array overflow");
let entry_index = (parent_entry.child_pointer + array_index);

let entry : JSONEntry = self.packed_json_entries[entry_index].into();
let entry : JSONEntry = self.json_entries_packed[entry_index].into();

assert(
entry.entry_type == LITERAL_TOKEN, "get_literal_from_array_unchecked: entry exists but is not a literal!"
Expand Down
Loading

0 comments on commit 3487412

Please sign in to comment.