Skip to content

chore: use bool for ValidationFlag fields #77

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
50 changes: 25 additions & 25 deletions src/_table_generation/make_tables.nr
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,27 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
// output is layer type
// 11 tokens , 3 layers = 11 * 11 * 3 = 121 * 3 = 343
// object contexts
let no_change = ValidationFlags { push_layer: 0, push_layer_type_of_root: 0, pop_layer: 0 };
let error_flags =
ValidationFlags { push_layer: 0x1000000, push_layer_type_of_root: 0, pop_layer: 0 };
let no_change =
ValidationFlags { push_layer: false, push_layer_type_of_root: false, pop_layer: false };
let error_flags_field = 0x1000000;
let begin_new_object_flags = ValidationFlags {
push_layer: 1,
push_layer_type_of_root: OBJECT_LAYER as Field,
pop_layer: 0,
push_layer: true,
push_layer_type_of_root: OBJECT_LAYER != 0,
pop_layer: false,
};
let begin_new_array_flags = ValidationFlags {
push_layer: 1,
push_layer_type_of_root: ARRAY_LAYER as Field,
pop_layer: 0,
push_layer: true,
push_layer_type_of_root: ARRAY_LAYER != 0,
pop_layer: false,
};
let end_object_or_array_flags: ValidationFlags =
ValidationFlags { push_layer: 0, push_layer_type_of_root: 0, pop_layer: 1 };
ValidationFlags { push_layer: false, push_layer_type_of_root: false, pop_layer: true };

let token_ids: [u32; NUM_TOKENS] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let error_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|_| error_flags.to_field());
let error_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|_| error_flags_field);
let object_layer_begin_object_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == KEY_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -98,13 +98,13 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
let object_layer_key_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = no_change.to_field();
if (token != KEY_SEPARATOR_TOKEN) {
result = error_flags.to_field();
result = error_flags_field;
}
result
});

let object_layer_key_separator_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -118,7 +118,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
});

let object_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == VALUE_SEPARATOR_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -129,7 +129,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
});

let object_layer_end_object_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == VALUE_SEPARATOR_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -144,7 +144,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
});

let object_layer_value_separator_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == KEY_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -157,7 +157,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
[[0; NUM_TOKENS]; NUM_TOKENS];

let no_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == NO_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -177,7 +177,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
object_layer_flags[KEY_TOKEN] = object_layer_key_token_outcomes;

let array_layer_begin_array_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token: u32| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -194,7 +194,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
});

let array_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == VALUE_SEPARATOR_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -205,7 +205,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
});

let array_layer_value_separator_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -219,7 +219,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
});

let array_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == VALUE_SEPARATOR_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -229,7 +229,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
result
});
let array_layer_end_array_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == VALUE_SEPARATOR_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -243,7 +243,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
result
});
let array_layer_end_object_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
if (token == VALUE_SEPARATOR_TOKEN) {
result = no_change.to_field();
}
Expand All @@ -266,7 +266,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
array_layer_flags[KEY_TOKEN] = error_token_outcomes;

let single_value_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
let mut result = error_flags.to_field();
let mut result = error_flags_field;
// we have reached the end of json
if (token == NO_TOKEN) {
result = no_change.to_field();
Expand Down
6 changes: 3 additions & 3 deletions src/json.nr
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,17 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max

// 1 gate
// we encode an error flag into `push_layer` by making its value such that `depth` will exceed the size of `parent_layer_stack`
depth += push_layer - pop_layer;
depth += push_layer as Field - pop_layer as Field;
std::as_witness(depth);

// 6.5 gates
let parent_layer = parent_layer_stack[cast_num_to_u32(depth)];
let mut updated_layer = (1 - pop_layer - push_layer);
let mut updated_layer = (1 - pop_layer as Field - push_layer as Field);
std::as_witness(updated_layer);
updated_layer =
updated_layer * current_layer as Field + push_layer_type_of_root as Field;
std::as_witness(updated_layer);
updated_layer = updated_layer + parent_layer * pop_layer;
updated_layer = updated_layer + parent_layer as Field * pop_layer as Field;
std::as_witness(updated_layer);
current_layer = cast_num_to_u32(updated_layer);

Expand Down
30 changes: 13 additions & 17 deletions src/transcript_entry.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ use crate::json_tables::ASCII_TO_TOKEN_TABLE;
use crate::utils::cast_num_to_u32;

pub(crate) struct ValidationFlags {
pub(crate) push_layer: Field,
pub(crate) push_layer_type_of_root: Field,
pub(crate) pop_layer: Field,
pub(crate) push_layer: bool,
pub(crate) push_layer_type_of_root: bool,
pub(crate) pop_layer: bool,
}

impl ValidationFlags {
pub(crate) fn to_field(self) -> Field {
self.push_layer + self.push_layer_type_of_root * 0x100 + self.pop_layer * 0x10000
self.push_layer as Field
+ self.push_layer_type_of_root as Field * 0x100
+ self.pop_layer as Field * 0x10000
}

unconstrained fn __from_field(f: Field) -> Self {
let bytes: [u8; 4] = f.to_be_bytes();
let mut push_layer = bytes[3] as Field;
let push_layer_type_of_root = bytes[2] as Field;
let pop_layer = bytes[1] as Field;
let mut push_layer = bytes[3] != 0;
let push_layer_type_of_root = bytes[2] != 0;
let pop_layer = bytes[1] != 0;
let error = bytes[0] as Field;

assert(error == 0, "ValidationFlags: grammar error");
Expand All @@ -28,20 +30,15 @@ impl ValidationFlags {
// an out of bounds error will be triggered
// n.b. reason for doing this is that by only having 3 flags stored in our lookup table,
// we can extract them all with 1 add gate. combined with 2 bool checks = 3 gates instead of 5/6 gates if we had 4 flags
push_layer = push_layer + error * 0x1000000;
ValidationFlags { push_layer, push_layer_type_of_root, pop_layer }
}

// 3 gates
pub(crate) fn from_field(f: Field) -> Self {
// Safety: check the comments below
let r = unsafe { ValidationFlags::__from_field(f) };
// checks pop_layer is a valid boolean
assert(r.pop_layer * r.pop_layer == r.pop_layer);
// checks push_layer_type_of_root is a valid boolean
assert(r.push_layer_type_of_root * r.push_layer_type_of_root == r.push_layer_type_of_root);
// checks the input field is a valid combination of the outputs of the decomposition
assert(r.pop_layer * 0x10000 + r.push_layer_type_of_root * 0x100 + r.push_layer == f);
// checks the input field is a valid combination of the outputs of the decomposition d dddddd
assert_eq(r.to_field(), f);
r
}
}
Expand Down Expand Up @@ -78,8 +75,7 @@ impl RawTranscriptEntry {
result.length.assert_max_bit_size::<16>();
result.index.assert_max_bit_size::<16>();
result.encoded_ascii.assert_max_bit_size::<14>();

assert(result.encoded_ascii + result.index * 0x10000 + result.length * 0x100000000 == felt);
assert_eq(result.to_field(), felt);
result
}

Expand Down Expand Up @@ -195,7 +191,7 @@ impl TranscriptEntry {
// checks that token is in range
result.token.assert_max_bit_size::<8>();
// checks that the input is a valid combination of the outputs of the decomposition
assert(result.token + result.index * 0x100 + result.length * 0x1000000 == felt);
assert_eq(result.to_field(), felt);
result
}

Expand Down