Skip to content

Commit d1ab25b

Browse files
wip
1 parent 831191b commit d1ab25b

15 files changed

+2078
-557
lines changed

out.txt

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

src/get_array.nr

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use crate::getters;
2+
use dep::noir_sort;
3+
use crate::json_entry::JSONEntry;
4+
use crate::redux::JSON;
5+
use crate::keymap;
6+
use crate::lt::{lt_field_8_bit, lt_field_16_bit, assert_lt_240_bit, assert_gt_240_bit};
7+
use crate::redux_tables::{
8+
OBJECT_LAYER, ARRAY_LAYER, NUMERIC_TOKEN, LITERAL_TOKEN, STRING_TOKEN, BEGIN_OBJECT_TOKEN,
9+
BEGIN_ARRAY_TOKEN, ASCII_TO_NUMBER, ESCAPE_SEQUENCE_END_CHARS, ESCAPE_SEQUENCE_START_CHARS,
10+
ESCAPE_SEQUENCE_REPLACEMENT
11+
};
12+
use crate::keyhash::Hasher;
13+
use crate::keyhash::get_keyhash;
14+
use crate::slice_field::slice_fields;
15+
use crate::getters::JSONValue;
16+
17+
impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let MaxNumValues: u16> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues> {
18+
19+
fn get_length(self) -> u32 {
20+
assert(self.layer_context == ARRAY_LAYER, "can only get length of an array type");
21+
let parent_entry = JSONEntry::from_field(self.packed_json_entries[self.layer_index_in_transcript]);
22+
parent_entry.num_children as u32
23+
}
24+
25+
fn get_array<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Option<Self> {
26+
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
27+
let (exists, key_index) = self.key_exists_impl(key, KeyBytes);
28+
let entry: JSONEntry = JSONEntry::from_field(self.packed_json_entries[key_index]);
29+
30+
assert(entry.entry_type == BEGIN_ARRAY_TOKEN, "key does not describe an object");
31+
32+
let mut result: Option<JSON<NumBytes, NumPackedFields, MaxNumTokens, MaxNumValues>> = Option::none();
33+
if (exists) {
34+
let mut r = self;
35+
r.layer_id = entry.parent_index;
36+
r.layer_context = ARRAY_LAYER;
37+
r.layer_index_in_transcript = key_index;
38+
result = Option::some(r);
39+
}
40+
result
41+
}
42+
43+
fn get_array_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> Option<Self> {
44+
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
45+
let (exists, key_index) = self.key_exists_impl(key, key_length);
46+
let entry: JSONEntry = JSONEntry::from_field(self.packed_json_entries[key_index]);
47+
48+
// TODO: ADD A layer_context VARIABLE INTO JSON WHICH DESCRIBES WHETHER WE ARE AN OBJECT, ARRAY OR SINGLE VALUE
49+
assert(entry.entry_type == BEGIN_ARRAY_TOKEN, "key does not describe an object");
50+
51+
let mut result: Option<JSON<NumBytes, NumPackedFields, MaxNumTokens, MaxNumValues>> = Option::none();
52+
if (exists) {
53+
let mut r = self;
54+
r.layer_id = entry.parent_index;
55+
r.layer_context = ARRAY_LAYER;
56+
r.layer_index_in_transcript = key_index;
57+
result = Option::some(r);
58+
}
59+
result
60+
}
61+
62+
fn map<U, let MaxElements: u32, let MaxElementBytes: u32>(
63+
self,
64+
f: fn(JSONValue<MaxElementBytes>) -> U
65+
) -> [U; MaxElements] where U: std::default::Default {
66+
assert(self.layer_context == ARRAY_LAYER, "can only call map on an array");
67+
68+
let entry = JSONEntry::from_field(self.packed_json_entries[self.layer_index_in_transcript]);
69+
70+
let num_children = entry.num_children;
71+
let mut r: [U; MaxElements] = [U::default(); MaxElements];
72+
73+
for i in 0..MaxElements {
74+
let valid = lt_field_16_bit(i as Field, num_children);
75+
let entry_index = (entry.child_pointer + i as Field) * valid as Field;
76+
let child_entry = JSONEntry::from_field(self.packed_json_entries[entry_index]);
77+
78+
let mut parsed_string: [u8; MaxElementBytes] = [0; MaxElementBytes];
79+
for j in 0..MaxElementBytes {
80+
let byte_valid = lt_field_16_bit(j as Field, child_entry.json_length);
81+
// n.b. conditionally setting index to 0 can be removed if we ensure json is padded such that this index does not overflow json bytes
82+
let byte = self.json[(child_entry.json_pointer + i as Field) * valid as Field];
83+
// TODO improve efficiency? measure...
84+
if (byte_valid) {
85+
parsed_string[i] = byte;
86+
}
87+
}
88+
89+
if (valid) {
90+
r[i] = f(
91+
JSONValue { length: child_entry.json_length, value: parsed_string, value_type: child_entry.entry_type }
92+
);
93+
}
94+
}
95+
r
96+
}
97+
}

src/get_literal.nr

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
use crate::getters;
2+
use dep::noir_sort;
3+
use crate::json_entry::JSONEntry;
4+
use crate::redux::JSON;
5+
use crate::keymap;
6+
use crate::lt::{lt_field_8_bit, lt_field_16_bit, assert_lt_240_bit, assert_gt_240_bit};
7+
use crate::redux_tables::{
8+
OBJECT_LAYER, ARRAY_LAYER, NUMERIC_TOKEN, LITERAL_TOKEN, STRING_TOKEN, BEGIN_OBJECT_TOKEN,
9+
BEGIN_ARRAY_TOKEN, ASCII_TO_NUMBER, ESCAPE_SEQUENCE_END_CHARS, ESCAPE_SEQUENCE_START_CHARS,
10+
ESCAPE_SEQUENCE_REPLACEMENT
11+
};
12+
use crate::keyhash::Hasher;
13+
use crate::keyhash::get_keyhash;
14+
use crate::slice_field::slice_fields;
15+
16+
global MAX_LITERAL_LENGTH_AS_STRING = 5;
17+
global LITERAL_OFFSET_SHIFT: [Field; 6] = [
18+
0x10000000000, 0x100000000, 0x1000000, 0x10000, 0x100,1
19+
];
20+
fn extract_literal_from_array(arr: [u8; MAX_LITERAL_LENGTH_AS_STRING], json_length: Field) -> JSONLiteral {
21+
let false_field = 0x66616c7365;
22+
let true_field = 0x74727565;
23+
let null_field = 0x6e756c6c;
24+
25+
let mut parsed_literal: Field = 0;
26+
for i in 0..MAX_LITERAL_LENGTH_AS_STRING {
27+
parsed_literal *= 0x100;
28+
29+
let value = arr[i] as Field;
30+
parsed_literal += value;
31+
}
32+
parsed_literal /= LITERAL_OFFSET_SHIFT[json_length];
33+
let is_false = parsed_literal == false_field;
34+
let is_null = parsed_literal == null_field;
35+
let is_true = parsed_literal == true_field;
36+
JSONLiteral::new(is_true, is_false, is_null)
37+
}
38+
39+
struct JSONLiteral {
40+
value: Field,
41+
}
42+
43+
impl JSONLiteral {
44+
fn new(is_true: bool, is_false: bool, is_null: bool) -> Self {
45+
assert(is_false as Field + is_null as Field + is_true as Field == 1, "parse literal: invalid entry");
46+
JSONLiteral { value: is_true as Field * 2 + is_null as Field }
47+
}
48+
fn is_true(self) -> bool {
49+
self.value == 2
50+
}
51+
fn is_null(self) -> bool {
52+
self.value == 1
53+
}
54+
fn is_false(self) -> bool {
55+
self.value == 0
56+
}
57+
fn to_bool(self) -> bool {
58+
self.value == 2
59+
}
60+
}
61+
62+
impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let MaxNumValues: u16> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues> {
63+
64+
fn get_literal<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Option<JSONLiteral> {
65+
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
66+
67+
let (exists, entry) = self.get_json_entry(key);
68+
assert(
69+
(entry.entry_type - LITERAL_TOKEN) * exists as Field == 0, "get_literal: entry exists but is not a literal!"
70+
);
71+
let mut parsed_string: [u8; MAX_LITERAL_LENGTH_AS_STRING] = self.extract_string_entry(entry);
72+
73+
Option { _is_some: exists, _value: extract_literal_from_array(parsed_string, entry.json_length) }
74+
}
75+
76+
fn get_literal_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> Option<JSONLiteral> {
77+
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
78+
79+
let (exists, entry) = self.get_json_entry_var(key, key_length);
80+
assert(
81+
(entry.entry_type - LITERAL_TOKEN) * exists as Field == 0, "get_literal_var: entry exists but is not a literal!"
82+
);
83+
let mut parsed_string: [u8; MAX_LITERAL_LENGTH_AS_STRING] = self.extract_string_entry(entry);
84+
85+
Option { _is_some: exists, _value: extract_literal_from_array(parsed_string, entry.json_length) }
86+
}
87+
88+
fn get_literal_unchecked_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> JSONLiteral {
89+
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
90+
91+
let entry= self.get_json_entry_unchecked_var(key, key_length);
92+
assert(
93+
entry.entry_type == LITERAL_TOKEN, "get_literal_unchecked_var: entry exists but is not a literal!"
94+
);
95+
let mut parsed_string: [u8; MAX_LITERAL_LENGTH_AS_STRING] = self.extract_string_entry(entry);
96+
97+
extract_literal_from_array(parsed_string, entry.json_length)
98+
}
99+
100+
fn get_literal_unchecked<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> JSONLiteral {
101+
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
102+
103+
let entry= self.get_json_entry_unchecked(key);
104+
assert(
105+
entry.entry_type == LITERAL_TOKEN, "get_literal_unchecked_var: entry exists but is not a literal!"
106+
);
107+
let mut parsed_string: [u8; MAX_LITERAL_LENGTH_AS_STRING] = self.extract_string_entry(entry);
108+
109+
extract_literal_from_array(parsed_string, entry.json_length)
110+
}
111+
}

src/get_number.nr

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use crate::getters;
2+
use dep::noir_sort;
3+
use crate::json_entry::JSONEntry;
4+
use crate::redux::JSON;
5+
use crate::keymap;
6+
use crate::lt::{lt_field_8_bit, lt_field_16_bit, assert_lt_240_bit, assert_gt_240_bit};
7+
use crate::redux_tables::{
8+
OBJECT_LAYER, ARRAY_LAYER, NUMERIC_TOKEN, LITERAL_TOKEN, STRING_TOKEN, BEGIN_OBJECT_TOKEN,
9+
BEGIN_ARRAY_TOKEN, ASCII_TO_NUMBER, ESCAPE_SEQUENCE_END_CHARS, ESCAPE_SEQUENCE_START_CHARS,
10+
ESCAPE_SEQUENCE_REPLACEMENT
11+
};
12+
use crate::keyhash::Hasher;
13+
use crate::keyhash::get_keyhash;
14+
use crate::slice_field::slice_fields;
15+
16+
global U64_LENGTH_AS_BASE10_STRING = 20;
17+
global NUMBER_OFFSET_SHIFT: [Field; 21] = [
18+
100000000000000000000,10000000000000000000,1000000000000000000,100000000000000000,10000000000000000,1000000000000000,100000000000000,10000000000000, 1000000000000, 100000000000, 10000000000, 1000000000, 100000000,10000000,1000000,100000,10000,1000,100,10,1
19+
];
20+
fn extract_number_from_array(arr: [u8; U64_LENGTH_AS_BASE10_STRING], json_length: Field) -> u64 {
21+
let mut parsed_number: Field = 0;
22+
for i in 0..U64_LENGTH_AS_BASE10_STRING {
23+
parsed_number *= 10;
24+
25+
let value = ASCII_TO_NUMBER[arr[i]] as Field;
26+
parsed_number += value;
27+
}
28+
parsed_number /= NUMBER_OFFSET_SHIFT[json_length];
29+
parsed_number as u64
30+
}
31+
impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let MaxNumValues: u16> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues> {
32+
33+
fn get_number<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Option<u64> {
34+
let (exists, entry) = self.get_json_entry(key);
35+
assert(
36+
(entry.entry_type - NUMERIC_TOKEN) * exists as Field == 0, "get_number: entry exists but is not a number!"
37+
);
38+
let mut parsed_string: [u8; U64_LENGTH_AS_BASE10_STRING] = self.extract_string_entry(entry);
39+
40+
Option { _is_some: exists, _value: extract_number_from_array(parsed_string, entry.json_length) }
41+
}
42+
43+
fn get_number_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> Option<u64> {
44+
let (exists, entry) = self.get_json_entry_var(key, key_length);
45+
assert(
46+
(entry.entry_type - NUMERIC_TOKEN) * exists as Field == 0, "get_number: entry exists but is not a number!"
47+
);
48+
let mut parsed_string: [u8; U64_LENGTH_AS_BASE10_STRING] = self.extract_string_entry(entry);
49+
50+
Option { _is_some: exists, _value: extract_number_from_array(parsed_string, entry.json_length) }
51+
}
52+
53+
fn get_number_unchecked<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> u64 {
54+
let entry = self.get_json_entry_unchecked(key);
55+
assert(entry.entry_type == NUMERIC_TOKEN, "get_number_unchecked: entry exists but is not a number!");
56+
let mut parsed_string: [u8; U64_LENGTH_AS_BASE10_STRING] = self.extract_string_entry(entry);
57+
58+
extract_number_from_array(parsed_string, entry.json_length)
59+
}
60+
61+
fn get_number_unchecked_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> u64 {
62+
let entry = self.get_json_entry_unchecked_var(key, key_length);
63+
assert(entry.entry_type == NUMERIC_TOKEN, "get_number_unchecked: entry exists but is not a number!");
64+
let mut parsed_string: [u8; U64_LENGTH_AS_BASE10_STRING] = self.extract_string_entry(entry);
65+
66+
extract_number_from_array(parsed_string, entry.json_length)
67+
}
68+
69+
fn get_array_element_as_number(self, array_index: Field) -> Option<u64> {
70+
assert(self.layer_context == ARRAY_LAYER, "can only acceess array elements from array");
71+
72+
let parent_entry = JSONEntry::from_field(self.packed_json_entries[self.layer_index_in_transcript]);
73+
74+
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
75+
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
76+
77+
let entry = JSONEntry::from_field(self.packed_json_entries[entry_index]);
78+
79+
assert(
80+
(entry.entry_type - NUMERIC_TOKEN) * valid as Field == 0, "get_number: entry exists but is not a number!"
81+
);
82+
83+
let mut parsed_string: [u8; U64_LENGTH_AS_BASE10_STRING] = self.extract_string_entry(entry);
84+
let result = extract_number_from_array(parsed_string, entry.json_length);
85+
86+
Option { _is_some: valid, _value: result }
87+
}
88+
89+
fn get_number_from_array_unchecked(self, array_index: Field) -> u64 {
90+
assert(self.layer_context == ARRAY_LAYER, "can only acceess array elements from array");
91+
92+
let parent_entry = JSONEntry::from_field(self.packed_json_entries[self.layer_index_in_transcript]);
93+
94+
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
95+
assert(valid, "array overflow");
96+
let entry_index = (parent_entry.child_pointer + array_index);
97+
98+
let entry = JSONEntry::from_field(self.packed_json_entries[entry_index]);
99+
100+
assert(
101+
(entry.entry_type - NUMERIC_TOKEN) * valid as Field == 0, "get_number: entry exists but is not a number!"
102+
);
103+
104+
let mut parsed_string: [u8; U64_LENGTH_AS_BASE10_STRING] = self.extract_string_entry(entry);
105+
extract_number_from_array(parsed_string, entry.json_length)
106+
}
107+
}

src/get_object.nr

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use crate::getters;
2+
use dep::noir_sort;
3+
use crate::json_entry::JSONEntry;
4+
use crate::redux::JSON;
5+
use crate::keymap;
6+
use crate::lt::{lt_field_8_bit, lt_field_16_bit, assert_lt_240_bit, assert_gt_240_bit};
7+
use crate::redux_tables::{
8+
OBJECT_LAYER, ARRAY_LAYER, NUMERIC_TOKEN, LITERAL_TOKEN, STRING_TOKEN, BEGIN_OBJECT_TOKEN,
9+
BEGIN_ARRAY_TOKEN, ASCII_TO_NUMBER, ESCAPE_SEQUENCE_END_CHARS, ESCAPE_SEQUENCE_START_CHARS,
10+
ESCAPE_SEQUENCE_REPLACEMENT
11+
};
12+
use crate::keyhash::Hasher;
13+
use crate::keyhash::get_keyhash;
14+
use crate::slice_field::slice_fields;
15+
16+
impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let MaxNumValues: u16> JSON<NumBytes,NumPackedFields, MaxNumTokens, MaxNumValues> {
17+
18+
fn get_object_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> Option<Self> {
19+
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
20+
21+
let (exists, key_index) = self.key_exists_impl(key, key_length);
22+
let entry: JSONEntry = JSONEntry::from_field(self.packed_json_entries[key_index]);
23+
assert(
24+
(entry.entry_type - BEGIN_OBJECT_TOKEN) * exists as Field == 0, "get_object: entry exists but is not an object!"
25+
);
26+
27+
let mut r = self;
28+
r.layer_id = entry.parent_index;
29+
r.layer_context = OBJECT_LAYER;
30+
r.layer_index_in_transcript = key_index;
31+
Option { _is_some: exists, _value: r }
32+
}
33+
34+
fn get_object<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Option<Self> {
35+
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
36+
37+
let (exists, key_index) = self.key_exists_impl(key, KeyBytes);
38+
let entry: JSONEntry = JSONEntry::from_field(self.packed_json_entries[key_index]);
39+
assert(
40+
(entry.entry_type - BEGIN_OBJECT_TOKEN) * exists as Field == 0, "get_object: entry exists but is not an object!"
41+
);
42+
43+
let mut r = self;
44+
r.layer_id = entry.parent_index;
45+
r.layer_context = OBJECT_LAYER;
46+
r.layer_index_in_transcript = key_index;
47+
Option { _is_some: exists, _value: r }
48+
}
49+
50+
fn get_object_unchecked_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> Self {
51+
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
52+
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index_var(key, key_length);
53+
let entry: JSONEntry = JSONEntry::from_field(self.packed_json_entries[key_index]);
54+
assert(entry.entry_type == BEGIN_OBJECT_TOKEN, "get_object: entry exists but is not an object!");
55+
56+
let mut r = self;
57+
r.layer_id = entry.parent_index;
58+
r.layer_context = OBJECT_LAYER;
59+
r.layer_index_in_transcript = key_index;
60+
r
61+
}
62+
63+
fn get_object_unchecked<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Self {
64+
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
65+
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index_var(key, KeyBytes);
66+
let entry: JSONEntry = JSONEntry::from_field(self.packed_json_entries[key_index]);
67+
assert(entry.entry_type == BEGIN_OBJECT_TOKEN, "get_object: entry exists but is not an object!");
68+
69+
let mut r = self;
70+
r.layer_id = entry.parent_index;
71+
r.layer_context = OBJECT_LAYER;
72+
r.layer_index_in_transcript = key_index;
73+
r
74+
}
75+
}

0 commit comments

Comments
 (0)