Skip to content

Commit

Permalink
chore(deps): update dependency twenty-first
Browse files Browse the repository at this point in the history
- use new `bfe!` and `xfe!` macros
- minor style improvements
  • Loading branch information
jan-ferdinand committed Mar 3, 2024
1 parent b740c60 commit 8aa04b6
Show file tree
Hide file tree
Showing 28 changed files with 266 additions and 322 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ strum = { version = "0.26", features = ["derive"] }
syn = "2.0"
test-strategy = "0.3.1"
thiserror = "1.0"
twenty-first = "0.37.0"
twenty-first = "0.38"
unicode-width = "0.1"

[workspace.lints.clippy]
Expand Down
9 changes: 5 additions & 4 deletions constraint-evaluation-generator/src/codegen/tasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::quote;
use quote::ToTokens;
use twenty_first::bfe;
use twenty_first::prelude::x_field_element::EXTENSION_DEGREE;
use twenty_first::prelude::BFieldElement;
use twenty_first::prelude::XFieldElement;
Expand Down Expand Up @@ -260,7 +261,7 @@ impl TasmBackend {
}

fn load_base_field_constant(bfe: BFieldElement) -> TokenStream {
let zero = Self::tokenize_bfe(BFieldElement::new(0));
let zero = Self::tokenize_bfe(bfe!(0));
let bfe = Self::tokenize_bfe(bfe);
quote!(AnInstruction::Push(#zero), AnInstruction::Push(#zero), AnInstruction::Push(#bfe),)
}
Expand Down Expand Up @@ -292,7 +293,7 @@ impl TasmBackend {
let word_offset = element_index * EXTENSION_DEGREE;
let start_to_read_offset = EXTENSION_DEGREE - 1;
let word_index = word_offset + start_to_read_offset;
let word_index = BFieldElement::from(word_index as u64);
let word_index = bfe!(word_index as u64);
let word_index = Self::tokenize_bfe(word_index);

quote!(
Expand All @@ -306,7 +307,7 @@ impl TasmBackend {
let free_mem_page = IOList::FreeMemPage;

let word_offset = element_index * EXTENSION_DEGREE;
let word_index = BFieldElement::from(word_offset as u64);
let word_index = bfe!(word_offset as u64);
let word_index = Self::tokenize_bfe(word_index);

quote!(
Expand All @@ -326,7 +327,7 @@ impl TasmBackend {
fn prepare_return_values() -> TokenStream {
let free_mem_page = IOList::FreeMemPage;
let out_array_offset_in_num_bfes = OUT_ARRAY_OFFSET * EXTENSION_DEGREE;
let out_array_offset = BFieldElement::new(out_array_offset_in_num_bfes as u64);
let out_array_offset = bfe!(out_array_offset_in_num_bfes as u64);
let out_array_offset = Self::tokenize_bfe(out_array_offset);
quote!(AnInstruction::Push(#free_mem_page + #out_array_offset),)
}
Expand Down
9 changes: 6 additions & 3 deletions constraint-evaluation-generator/src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ impl Constraints {

#[cfg(test)]
pub(crate) mod tests {
use twenty_first::bfe;
use twenty_first::xfe;

use triton_vm::prelude::BFieldElement;
use triton_vm::prelude::XFieldElement;
use triton_vm::table::challenges::ChallengeId;
Expand All @@ -229,7 +232,7 @@ pub(crate) mod tests {
pub(crate) fn mini_constraints() -> Self {
let circuit_builder = ConstraintCircuitBuilder::new();
let challenge = |c| circuit_builder.challenge(c);
let constant = |c: u32| circuit_builder.b_constant(BFieldElement::from(c));
let constant = |c: u32| circuit_builder.b_constant(bfe!(c));
let base_row = |i| circuit_builder.input(SingleRowIndicator::BaseRow(i));
let ext_row = |i| circuit_builder.input(SingleRowIndicator::ExtRow(i));

Expand Down Expand Up @@ -258,7 +261,7 @@ pub(crate) mod tests {
fn small_init_constraints() -> Vec<ConstraintCircuitMonad<SingleRowIndicator>> {
let circuit_builder = ConstraintCircuitBuilder::new();
let challenge = |c| circuit_builder.challenge(c);
let constant = |c: u32| circuit_builder.b_constant(BFieldElement::from(c));
let constant = |c: u32| circuit_builder.b_constant(bfe!(c));
let input = |i| circuit_builder.input(SingleRowIndicator::BaseRow(i));
let input_to_the_4th = |i| input(i) * input(i) * input(i) * input(i);

Expand All @@ -272,7 +275,7 @@ pub(crate) mod tests {
fn small_transition_constraints() -> Vec<ConstraintCircuitMonad<DualRowIndicator>> {
let circuit_builder = ConstraintCircuitBuilder::new();
let challenge = |c| circuit_builder.challenge(c);
let constant = |c: u32| circuit_builder.x_constant(XFieldElement::from(c));
let constant = |c: u32| circuit_builder.x_constant(xfe!(c));

let curr_base_row = |col| circuit_builder.input(DualRowIndicator::CurrentBaseRow(col));
let next_base_row = |col| circuit_builder.input(DualRowIndicator::NextBaseRow(col));
Expand Down
12 changes: 5 additions & 7 deletions triton-vm/src/aet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use itertools::Itertools;
use ndarray::s;
use ndarray::Array2;
use ndarray::Axis;
use num_traits::One;
use num_traits::Zero;
use twenty_first::prelude::*;

use crate::error::InstructionError;
Expand Down Expand Up @@ -149,11 +147,11 @@ impl AlgebraicExecutionTrace {

// padding is one 1, then as many zeros as necessary: [1, 0, 0, …]
let program_iter = program.to_bwords().into_iter();
let one_iter = [BFieldElement::one()].into_iter();
let zeros_iter = [BFieldElement::zero()].into_iter().cycle();
let one = [bfe!(1)];
let zeros = [bfe!(0); tip5::RATE];
program_iter
.chain(one_iter)
.chain(zeros_iter)
.chain(one)
.chain(zeros)
.take(padded_program_length)
.collect()
}
Expand Down Expand Up @@ -354,7 +352,7 @@ mod tests {
let program = triton_program!({&eight_nops} halt);
let padded_program = AlgebraicExecutionTrace::hash_input_pad_program(&program);

let expected = [program.to_bwords(), vec![1_u64.into()]].concat();
let expected = [program.to_bwords(), vec![bfe!(1)]].concat();
assert!(expected == padded_program);
}
}
4 changes: 2 additions & 2 deletions triton-vm/src/arithmetic_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ArithmeticDomain {
/// Errors if the domain length is not a power of 2.
pub fn of_length(length: usize) -> Result<Self> {
let domain = Self {
offset: BFieldElement::one(),
offset: bfe!(1),
generator: Self::generator_for_length(length as u64)?,
length,
};
Expand Down Expand Up @@ -90,7 +90,7 @@ impl ArithmeticDomain {
}

pub fn domain_values(&self) -> Vec<BFieldElement> {
let mut accumulator = BFieldElement::one();
let mut accumulator = bfe!(1);
let mut domain_values = Vec::with_capacity(self.length);

for _ in 0..self.length {
Expand Down
5 changes: 2 additions & 3 deletions triton-vm/src/fri.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::marker::PhantomData;

use itertools::Itertools;
use num_traits::One;
use rayon::iter::*;
use twenty_first::prelude::*;
use twenty_first::shared_math::other::log_2_ceil;
Expand Down Expand Up @@ -171,8 +170,8 @@ impl<H: AlgebraicHasher> ProverRound<H> {
}

fn split_and_fold(&self, folding_challenge: XFieldElement) -> Vec<XFieldElement> {
let one = XFieldElement::one();
let two_inverse = XFieldElement::from(2).inverse();
let one = xfe!(1);
let two_inverse = xfe!(2).inverse();

let domain_points = self.domain.domain_values();
let domain_point_inverses = BFieldElement::batch_inversion(domain_points);
Expand Down
38 changes: 12 additions & 26 deletions triton-vm/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,12 +873,7 @@ mod tests {
fn parse_push_pop() {
let program = triton_program!(push 1 push 1 add pop 2);
let instructions = program.into_iter().collect_vec();
let expected = vec![
Push(BFieldElement::one()),
Push(BFieldElement::one()),
Add,
Pop(N2),
];
let expected = vec![Push(bfe!(1)), Push(bfe!(1)), Add, Pop(N2)];

assert!(expected == instructions);
}
Expand Down Expand Up @@ -930,28 +925,19 @@ mod tests {

#[test]
fn change_arguments_of_various_instructions() {
let push = Push(0_u64.into()).change_arg(7_u64.into());
let dup = Dup(ST0).change_arg(1024_u64.into());
let swap = Swap(ST0).change_arg(1337_u64.into());
let swap_0 = Swap(ST0).change_arg(0_u64.into());
let swap_1 = Swap(ST0).change_arg(1_u64.into());
let pop_0 = Pop(N4).change_arg(0_u64.into());
let pop_2 = Pop(N1).change_arg(2_u64.into());
let nop = Nop.change_arg(7_u64.into());

assert!(push.is_ok());
assert!(dup.is_err());
assert!(swap.is_err());
assert!(swap_0.is_err());
assert!(swap_1.is_ok());
assert!(pop_0.is_err());
assert!(pop_2.is_ok());
assert!(nop.is_err());
assert!(Push(bfe!(0)).change_arg(bfe!(7)).is_ok());
assert!(Dup(ST0).change_arg(bfe!(1024)).is_err());
assert!(Swap(ST0).change_arg(bfe!(1337)).is_err());
assert!(Swap(ST0).change_arg(bfe!(0)).is_err());
assert!(Swap(ST0).change_arg(bfe!(1)).is_ok());
assert!(Pop(N4).change_arg(bfe!(0)).is_err());
assert!(Pop(N1).change_arg(bfe!(2)).is_ok());
assert!(Nop.change_arg(bfe!(7)).is_err());
}

#[test]
fn print_various_instructions() {
println!("instruction_push: {:?}", Instruction::Push(7_u64.into()));
println!("instruction_push: {:?}", Instruction::Push(bfe!(7)));
println!("instruction_assert: {}", Instruction::Assert);
println!("instruction_invert: {:?}", Instruction::Invert);
println!("instruction_dup: {}", Instruction::Dup(ST14));
Expand Down Expand Up @@ -1073,9 +1059,9 @@ mod tests {
}

fn terminal_op_stack_size_for_program(program: Program) -> usize {
let public_input = vec![BFieldElement::zero()].into();
let public_input = vec![bfe!(0)].into();
let mock_digests = vec![Digest::default()];
let non_determinism: NonDeterminism<_> = vec![BFieldElement::zero()].into();
let non_determinism: NonDeterminism<_> = vec![bfe!(0)].into();
let non_determinism = non_determinism.with_digests(mock_digests);

let mut vm_state = VMState::new(&program, public_input, non_determinism);
Expand Down
10 changes: 5 additions & 5 deletions triton-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ mod shared_tests;
/// # use triton_vm::instruction::Instruction;
/// let element_0 = BFieldElement::new(0);
/// let label = "my_label";
/// let instruction_push = Instruction::Push(42_u64.into());
/// let instruction_push = Instruction::Push(bfe!(42));
/// let dup_arg = 1;
/// let program = triton_program!(
/// push {element_0}
Expand Down Expand Up @@ -298,11 +298,11 @@ macro_rules! triton_program {
/// {&insert_me}
/// push 1
/// );
/// # let zero = BFieldElement::new(0);
/// # let zero = bfe!(0);
/// # assert_eq!(LabelledInstruction::Instruction(Push(zero)), surrounding_code[0]);
/// assert_eq!(LabelledInstruction::Instruction(Pop(N1)), surrounding_code[1]);
/// assert_eq!(LabelledInstruction::Instruction(Pop(N1)), surrounding_code[3]);
/// # let one = BFieldElement::new(1);
/// # let one = bfe!(1);
/// # assert_eq!(LabelledInstruction::Instruction(Push(one)), surrounding_code[4]);
///```
///
Expand Down Expand Up @@ -682,8 +682,8 @@ mod tests {
fn lib_prove_with_incorrect_public_output_gives_appropriate_error() {
let program = triton_program! { read_io 1 push 2 mul write_io 1 halt };
let claim = Claim::about_program(&program)
.with_input(vec![2_u64.into()])
.with_output(vec![5_u64.into()]);
.with_input(vec![bfe!(2)])
.with_output(vec![bfe!(5)]);

let stark = Stark::default();
let_assert!(Err(err) = prove(stark, &claim, &program, [].into()));
Expand Down
Loading

0 comments on commit 8aa04b6

Please sign in to comment.