-
Notifications
You must be signed in to change notification settings - Fork 8
Translation keys #36
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
crivasr
wants to merge
14
commits into
main
Choose a base branch
from
translation-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Translation keys #36
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
31e8c97
refactor challenge types
crivasr 8f76917
add bitcoin scripts for translation keys
crivasr fb53203
add key translation
crivasr 58514f3
save trace when limit step is reached
crivasr 13f61ab
simplify selection and bound it by the conflict step
crivasr 5730d40
fix selection when no mismatch is found in ReadValueChallenge
crivasr a945652
make challenges sizes constant
crivasr 44c1fd2
rename command
crivasr 6e1d252
change docker commit
crivasr 7b628eb
add halt challenge
crivasr c511bab
use conflict_step instead of trace step
crivasr 8ce577e
add verify_challenge_step
crivasr b9b446f
revert add_with_bit_extension changes
crivasr 6abac93
fix comment
crivasr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -1498,7 +1498,7 @@ pub fn verify_wrong_chunk_value( | |
| ) { | ||
| address_in_range(stack, &chunk.range(), &address); | ||
| stack.op_verify(); | ||
|
|
||
| let chunk_table = WordTable::new(stack, chunk.data.clone()); | ||
|
|
||
| let base_addr = stack.number_u32(chunk.base_addr); | ||
|
|
@@ -1613,6 +1613,189 @@ pub fn var_to_number(stack: &mut StackTracker, var: StackVariable) -> StackVaria | |
| result | ||
| } | ||
|
|
||
| pub fn shift(stack: &mut StackTracker, tables: &StackShiftTables, amount: u8) { | ||
| if amount == 0 { | ||
| return; | ||
| } | ||
|
|
||
| let tables = [tables.shift_1, tables.shift_2, tables.shift_3]; | ||
|
|
||
| if amount > 3 { | ||
| stack.op_drop(); | ||
| stack.number(0); | ||
| } else { | ||
| stack.get_value_from_table(tables[amount as usize - 1], None); | ||
| } | ||
| } | ||
|
|
||
| const BITS_NIBBLE: u8 = 4; | ||
|
|
||
| pub fn split(stack: &mut StackTracker, tables: &StackTables, right_size: u8) { | ||
| stack.op_dup(); | ||
|
|
||
| shift(stack, &tables.rshift, right_size); | ||
| stack.op_swap(); | ||
| shift(stack, &tables.lshift, BITS_NIBBLE - right_size); | ||
| shift(stack, &tables.rshift, BITS_NIBBLE - right_size); | ||
| } | ||
|
|
||
| pub fn var_to_decisions_in_altstack( | ||
| stack: &mut StackTracker, | ||
| tables: &StackTables, | ||
| var: StackVariable, | ||
| nary_last_round: u8, | ||
| nary: u8, | ||
| rounds: u8, | ||
| ) { | ||
| let bits_nary_round = f64::log2(nary as f64) as u8; | ||
| let mut start_position = 0; | ||
| let mut remaining_bits = if nary_last_round == 0 { | ||
| bits_nary_round | ||
| } else { | ||
| f64::log2(nary_last_round as f64) as u8 | ||
| }; | ||
|
|
||
| stack.move_var(var); | ||
| stack.explode(var); | ||
| stack.number(0); | ||
| let nibbles_needed = | ||
| (remaining_bits + bits_nary_round * (rounds - 1) + BITS_NIBBLE - 1) / BITS_NIBBLE; | ||
|
|
||
| for _ in 0..nibbles_needed { | ||
| if remaining_bits > BITS_NIBBLE { | ||
| stack.op_swap(); | ||
| shift(stack, &tables.lshift, start_position); | ||
| stack.op_add(); | ||
| start_position += BITS_NIBBLE; | ||
| remaining_bits -= BITS_NIBBLE; | ||
| } else if remaining_bits == BITS_NIBBLE { | ||
| stack.op_swap(); | ||
| shift(stack, &tables.lshift, start_position); | ||
| stack.op_add(); | ||
| start_position = 0; | ||
| remaining_bits = bits_nary_round; | ||
| stack.to_altstack(); | ||
| stack.number(0); | ||
| } else { | ||
| let times_needed = | ||
| 1 + (BITS_NIBBLE - remaining_bits + bits_nary_round - 1) / bits_nary_round; | ||
| let mut current_bits = BITS_NIBBLE; | ||
| for _ in 0..times_needed { | ||
| stack.op_swap(); | ||
| split(stack, tables, remaining_bits); | ||
| shift(stack, &tables.lshift, start_position); | ||
| stack.op_rot(); | ||
| stack.op_add(); | ||
|
|
||
| if remaining_bits <= current_bits { | ||
| current_bits -= remaining_bits; | ||
| remaining_bits = bits_nary_round; | ||
| start_position = 0; | ||
| stack.to_altstack(); | ||
| stack.number(0); | ||
| } else { | ||
| remaining_bits -= current_bits; | ||
| start_position += current_bits; | ||
| } | ||
| } | ||
| stack.op_swap(); | ||
| stack.number(0); | ||
| stack.op_equalverify(); | ||
| } | ||
| } | ||
|
|
||
| stack.number(0); | ||
| stack.op_equalverify(); | ||
|
|
||
| for _ in 0..(16 - nibbles_needed) { | ||
| stack.number(0); | ||
| stack.op_equalverify(); | ||
| } | ||
| } | ||
|
|
||
| pub fn next_decision_in_altstack( | ||
| stack: &mut StackTracker, | ||
| decisions_bits: StackVariable, | ||
| rounds: u8, | ||
| max_last_round: u8, | ||
| max_nary: u8, | ||
| ) { | ||
| stack.move_var(decisions_bits); | ||
| stack.explode(decisions_bits); | ||
|
|
||
| stack.op_dup(); | ||
| stack.number(max_last_round as u32); | ||
| stack.op_equal(); | ||
|
|
||
| let (mut overflow, mut no_overflow) = stack.open_if(); | ||
| no_overflow.op_1add(); | ||
| no_overflow.to_altstack(); | ||
| no_overflow.number(0); | ||
| no_overflow.to_altstack(); | ||
|
|
||
| overflow.op_drop(); | ||
| overflow.number(0); | ||
| overflow.to_altstack(); | ||
| overflow.number(1); | ||
| overflow.to_altstack(); | ||
|
|
||
| stack.end_if(overflow, no_overflow, 1, vec![], 2); | ||
|
|
||
| for _ in 1..rounds { | ||
| stack.from_altstack(); | ||
| stack.number(1); | ||
| stack.op_equal(); | ||
|
|
||
| let (mut inc, mut no_inc) = stack.open_if(); | ||
| no_inc.to_altstack(); | ||
| no_inc.number(0); | ||
| no_inc.to_altstack(); | ||
|
|
||
| inc.op_dup(); | ||
| inc.number(max_nary as u32); | ||
| inc.op_equal(); | ||
|
|
||
| let (mut overflow, mut no_overflow) = inc.open_if(); | ||
| no_overflow.op_1add(); | ||
| no_overflow.to_altstack(); | ||
| no_overflow.number(0); | ||
| no_overflow.to_altstack(); | ||
|
|
||
| overflow.op_drop(); | ||
| overflow.number(0); | ||
| overflow.to_altstack(); | ||
| overflow.number(1); | ||
| overflow.to_altstack(); | ||
|
|
||
| inc.end_if(overflow, no_overflow, 1, vec![], 2); | ||
| stack.end_if(inc, no_inc, 1, vec![], 2); | ||
| } | ||
|
|
||
| stack.from_altstack(); | ||
| stack.op_drop(); | ||
| } | ||
|
|
||
| pub fn increment_var(stack: &mut StackTracker, var: StackVariable) -> StackVariable { | ||
| let nibbles = stack.get_size(var); | ||
| next_decision_in_altstack(stack, var, nibbles as u8, 15, 15); | ||
| stack.from_altstack_joined(nibbles, "inc") | ||
| } | ||
|
|
||
| pub fn verify_challenge_step( | ||
| stack: &mut StackTracker, | ||
| step: StackVariable, | ||
| decisions: StackVariable, | ||
| nary: u8, | ||
| nary_last_round: u8, | ||
| rounds: u8, | ||
| ) { | ||
| let tables = &StackTables::new(stack, false, false, 0b111, 0b111, 0); | ||
| var_to_decisions_in_altstack(stack, tables, step, nary_last_round, nary, rounds); | ||
| let converted_step = stack.from_altstack_joined(rounds as u32, "converted_step"); | ||
| stack.equals(decisions, true, converted_step, true); | ||
| tables.drop(stack); | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use bitvmx_cpu_definitions::memory::MemoryWitness; | ||
|
|
@@ -2477,6 +2660,93 @@ mod tests { | |
| )); | ||
| } | ||
|
|
||
| fn test_var_to_decisions_in_altstack_aux( | ||
| decisions: &[u32], | ||
| step: u64, | ||
| nary_last_round: u8, | ||
| nary: u8, | ||
| ) { | ||
| let rounds = decisions.len(); | ||
| let stack = &mut StackTracker::new(); | ||
|
|
||
| for decision in decisions.iter() { | ||
| stack.number(*decision); | ||
| } | ||
|
|
||
| let decisions = stack.join_in_stack(rounds as u32, None, Some("decisions_bits")); | ||
| let step = stack.number_u64(step); | ||
|
|
||
| verify_challenge_step(stack, step, decisions, nary, nary_last_round, rounds as u8); | ||
|
|
||
| stack.op_true(); | ||
|
|
||
| assert!(stack.run().success); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_var_to_decisions_in_altstack() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add edge case tests. 64 bits step, 0 step, max nary, 0 last round, etc |
||
| test_var_to_decisions_in_altstack_aux(&[4, 2, 4, 0], 1104, 4, 8); | ||
| test_var_to_decisions_in_altstack_aux(&[4, 2, 4, 1], 1105, 4, 8); | ||
| test_var_to_decisions_in_altstack_aux(&[4, 2, 4, 2], 1106, 4, 8); | ||
| test_var_to_decisions_in_altstack_aux(&[4, 2, 4, 3], 1107, 4, 8); | ||
|
|
||
| test_var_to_decisions_in_altstack_aux(&[0, 3, 0, 3], 99, 4, 8); | ||
| test_var_to_decisions_in_altstack_aux(&[1, 3, 0, 3], 355, 4, 8); | ||
| } | ||
|
|
||
| fn test_increment_decision_aux(decision: u64, rounds: u8, nary: u8, nary_last_round: u8) { | ||
| let stack = &mut StackTracker::new(); | ||
| let tables = &StackTables::new(stack, false, false, 0b111, 0b111, 0); | ||
|
|
||
| let max_nary = nary - 1; | ||
| let max_last_round = if nary_last_round == 0 { | ||
| max_nary | ||
| } else { | ||
| nary_last_round - 1 | ||
| }; | ||
|
|
||
| let decision_var = stack.number_u64(decision); | ||
| let next_decision_var = stack.number_u64(decision.wrapping_add(1)); | ||
|
|
||
| var_to_decisions_in_altstack( | ||
| stack, | ||
| tables, | ||
| next_decision_var, | ||
| nary_last_round, | ||
| nary, | ||
| rounds, | ||
| ); | ||
|
|
||
| var_to_decisions_in_altstack(stack, tables, decision_var, nary_last_round, nary, rounds); | ||
|
|
||
| let decision = stack.from_altstack_joined(rounds as u32, "decision_bits"); | ||
|
|
||
| next_decision_in_altstack(stack, decision, rounds, max_last_round, max_nary); | ||
| let incremented_decision_bits = stack.from_altstack_joined(rounds as u32, "decision_bits"); | ||
|
|
||
| let expected_next_decision_bits = | ||
| stack.from_altstack_joined(rounds as u32, "expected_decision_bits"); | ||
|
|
||
| stack.equals( | ||
| incremented_decision_bits, | ||
| true, | ||
| expected_next_decision_bits, | ||
| true, | ||
| ); | ||
|
|
||
| tables.drop(stack); | ||
| stack.op_true(); | ||
|
|
||
| assert!(stack.run().success); | ||
| } | ||
| #[test] | ||
| fn test_next_decision() { | ||
| test_increment_decision_aux(100, 4, 8, 4); | ||
| test_increment_decision_aux(302, 8, 8, 2); | ||
| test_increment_decision_aux(38, 8, 2, 2); | ||
| test_increment_decision_aux(892, 4, 8, 0); | ||
| } | ||
|
|
||
| mod fuzz_tests { | ||
| use super::*; | ||
| use rand::Rng; | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -10,3 +10,5 @@ thiserror = "2.0.12" | |
| hex = "0.4.3" | ||
| blake3 = "1.6.1" | ||
| serde_json = "1.0.104" | ||
| strum = "0.27" | ||
| strum_macros = "0.27" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add comments of the logic