Skip to content

Commit

Permalink
refactor: eliminate exponentiations in the IR (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Besancon authored Aug 9, 2024
1 parent 0f5feff commit 2ed3288
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 392 deletions.
32 changes: 2 additions & 30 deletions air-script/tests/binary/binary.masm
Original file line number Diff line number Diff line change
Expand Up @@ -75,39 +75,11 @@ end # END PROC compute_integrity_constraint_divisor
# This procedure pushes 2 quadratic extension field elements to the stack
proc.compute_integrity_constraints
# integrity constraint 0 for main
padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900200 movdn.3 movdn.3 drop drop ext2mul
# integrity constraint 1 for main
padw mem_loadw.4294900001 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900001 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900001 movdn.3 movdn.3 drop drop padw mem_loadw.4294900001 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900001 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900200 drop drop ext2mul
end # END PROC compute_integrity_constraints
Expand Down
4 changes: 2 additions & 2 deletions air-script/tests/binary/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl Air for BinaryAir {
fn evaluate_transition<E: FieldElement<BaseField = Felt>>(&self, frame: &EvaluationFrame<E>, periodic_values: &[E], result: &mut [E]) {
let main_current = frame.current();
let main_next = frame.next();
result[0] = main_current[0].exp(E::PositiveInteger::from(2_u64)) - main_current[0] - E::ZERO;
result[1] = main_current[1].exp(E::PositiveInteger::from(2_u64)) - main_current[1] - E::ZERO;
result[0] = main_current[0] * main_current[0] - main_current[0] - E::ZERO;
result[1] = main_current[1] * main_current[1] - main_current[1] - E::ZERO;
}

fn evaluate_aux_transition<F, E>(&self, main_frame: &EvaluationFrame<F>, aux_frame: &EvaluationFrame<E>, _periodic_values: &[F], aux_rand_elements: &AuxTraceRandElements<E>, result: &mut [E])
Expand Down
144 changes: 9 additions & 135 deletions air-script/tests/bitwise/bitwise.masm
Original file line number Diff line number Diff line change
Expand Up @@ -187,169 +187,43 @@ end # END PROC compute_integrity_constraint_divisor
# This procedure pushes 17 quadratic extension field elements to the stack
proc.compute_integrity_constraints
# integrity constraint 0 for main
padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900200 movdn.3 movdn.3 drop drop ext2mul
# integrity constraint 1 for main
padw mem_loadw.500000000 drop drop padw mem_loadw.4294900000 drop drop padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop ext2sub ext2mul push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900200 drop drop ext2mul
# integrity constraint 2 for main
padw mem_loadw.4294900003 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900003 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900003 movdn.3 movdn.3 drop drop padw mem_loadw.4294900003 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900003 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900201 movdn.3 movdn.3 drop drop ext2mul
# integrity constraint 3 for main
padw mem_loadw.4294900004 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900004 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900004 movdn.3 movdn.3 drop drop padw mem_loadw.4294900004 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900004 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900201 drop drop ext2mul
# integrity constraint 4 for main
padw mem_loadw.4294900005 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900005 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900005 movdn.3 movdn.3 drop drop padw mem_loadw.4294900005 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900005 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900202 movdn.3 movdn.3 drop drop ext2mul
# integrity constraint 5 for main
padw mem_loadw.4294900006 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900006 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900006 movdn.3 movdn.3 drop drop padw mem_loadw.4294900006 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900006 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900202 drop drop ext2mul
# integrity constraint 6 for main
padw mem_loadw.4294900007 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900007 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900007 movdn.3 movdn.3 drop drop padw mem_loadw.4294900007 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900007 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900203 movdn.3 movdn.3 drop drop ext2mul
# integrity constraint 7 for main
padw mem_loadw.4294900008 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900008 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900008 movdn.3 movdn.3 drop drop padw mem_loadw.4294900008 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900008 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900203 drop drop ext2mul
# integrity constraint 8 for main
padw mem_loadw.4294900009 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900009 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900009 movdn.3 movdn.3 drop drop padw mem_loadw.4294900009 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900009 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900204 movdn.3 movdn.3 drop drop ext2mul
# integrity constraint 9 for main
padw mem_loadw.4294900010 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900010 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
padw mem_loadw.4294900010 movdn.3 movdn.3 drop drop padw mem_loadw.4294900010 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900010 movdn.3 movdn.3 drop drop ext2sub push.0 push.0 ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900204 drop drop ext2mul
# integrity constraint 10 for main
Expand Down
18 changes: 9 additions & 9 deletions air-script/tests/bitwise/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ impl Air for BitwiseAir {
fn evaluate_transition<E: FieldElement<BaseField = Felt>>(&self, frame: &EvaluationFrame<E>, periodic_values: &[E], result: &mut [E]) {
let main_current = frame.current();
let main_next = frame.next();
result[0] = main_current[0].exp(E::PositiveInteger::from(2_u64)) - main_current[0] - E::ZERO;
result[0] = main_current[0] * main_current[0] - main_current[0] - E::ZERO;
result[1] = periodic_values[1] * (main_next[0] - main_current[0]) - E::ZERO;
result[2] = main_current[3].exp(E::PositiveInteger::from(2_u64)) - main_current[3] - E::ZERO;
result[3] = main_current[4].exp(E::PositiveInteger::from(2_u64)) - main_current[4] - E::ZERO;
result[4] = main_current[5].exp(E::PositiveInteger::from(2_u64)) - main_current[5] - E::ZERO;
result[5] = main_current[6].exp(E::PositiveInteger::from(2_u64)) - main_current[6] - E::ZERO;
result[6] = main_current[7].exp(E::PositiveInteger::from(2_u64)) - main_current[7] - E::ZERO;
result[7] = main_current[8].exp(E::PositiveInteger::from(2_u64)) - main_current[8] - E::ZERO;
result[8] = main_current[9].exp(E::PositiveInteger::from(2_u64)) - main_current[9] - E::ZERO;
result[9] = main_current[10].exp(E::PositiveInteger::from(2_u64)) - main_current[10] - E::ZERO;
result[2] = main_current[3] * main_current[3] - main_current[3] - E::ZERO;
result[3] = main_current[4] * main_current[4] - main_current[4] - E::ZERO;
result[4] = main_current[5] * main_current[5] - main_current[5] - E::ZERO;
result[5] = main_current[6] * main_current[6] - main_current[6] - E::ZERO;
result[6] = main_current[7] * main_current[7] - main_current[7] - E::ZERO;
result[7] = main_current[8] * main_current[8] - main_current[8] - E::ZERO;
result[8] = main_current[9] * main_current[9] - main_current[9] - E::ZERO;
result[9] = main_current[10] * main_current[10] - main_current[10] - E::ZERO;
result[10] = periodic_values[0] * (main_current[1] - (E::ONE * main_current[3] + E::from(2_u64) * main_current[4] + E::from(4_u64) * main_current[5] + E::from(8_u64) * main_current[6])) - E::ZERO;
result[11] = periodic_values[0] * (main_current[2] - (E::ONE * main_current[7] + E::from(2_u64) * main_current[8] + E::from(4_u64) * main_current[9] + E::from(8_u64) * main_current[10])) - E::ZERO;
result[12] = periodic_values[1] * (main_next[1] - (main_current[1] * E::from(16_u64) + E::ONE * main_current[3] + E::from(2_u64) * main_current[4] + E::from(4_u64) * main_current[5] + E::from(8_u64) * main_current[6])) - E::ZERO;
Expand Down
64 changes: 4 additions & 60 deletions air-script/tests/evaluators/evaluators.masm
Original file line number Diff line number Diff line change
Expand Up @@ -87,75 +87,19 @@ proc.compute_integrity_constraints
# Multiply by the composition coefficient
padw mem_loadw.4294900201 movdn.3 movdn.3 drop drop ext2mul
# integrity constraint 3 for main
padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop ext2sub
padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900000 movdn.3 movdn.3 drop drop ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900201 drop drop ext2mul
# integrity constraint 4 for main
padw mem_loadw.4294900001 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900001 movdn.3 movdn.3 drop drop ext2sub
padw mem_loadw.4294900001 movdn.3 movdn.3 drop drop padw mem_loadw.4294900001 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900001 movdn.3 movdn.3 drop drop ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900202 movdn.3 movdn.3 drop drop ext2mul
# integrity constraint 5 for main
padw mem_loadw.4294900002 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900002 movdn.3 movdn.3 drop drop ext2sub
padw mem_loadw.4294900002 movdn.3 movdn.3 drop drop padw mem_loadw.4294900002 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900002 movdn.3 movdn.3 drop drop ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900202 drop drop ext2mul
# integrity constraint 6 for main
padw mem_loadw.4294900003 movdn.3 movdn.3 drop drop
# push the accumulator to the stack
push.1 movdn.2 push.0 movdn.2
# => [b1, b0, r1, r0, ...]
# square 1 times
dup.1 dup.1 ext2mul
# multiply
dup.1 dup.1 movdn.5 movdn.5
# => [b1, b0, r1, r0, b1, b0, ...] (4 cycles)
ext2mul movdn.3 movdn.3
# => [b1, b0, r1', r0', ...] (5 cycles)
# clean stack
drop drop
# => [r1, r0, ...] (2 cycles)
padw mem_loadw.4294900003 movdn.3 movdn.3 drop drop ext2sub
padw mem_loadw.4294900003 movdn.3 movdn.3 drop drop padw mem_loadw.4294900003 movdn.3 movdn.3 drop drop ext2mul padw mem_loadw.4294900003 movdn.3 movdn.3 drop drop ext2sub
# Multiply by the composition coefficient
padw mem_loadw.4294900203 movdn.3 movdn.3 drop drop ext2mul
end # END PROC compute_integrity_constraints
Expand Down
8 changes: 4 additions & 4 deletions air-script/tests/evaluators/evaluators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ impl Air for EvaluatorsAir {
result[0] = main_next[0] - main_current[0];
result[1] = main_next[2] - main_current[2];
result[2] = main_next[6] - main_current[6];
result[3] = main_current[0].exp(E::PositiveInteger::from(2_u64)) - main_current[0];
result[4] = main_current[1].exp(E::PositiveInteger::from(2_u64)) - main_current[1];
result[5] = main_current[2].exp(E::PositiveInteger::from(2_u64)) - main_current[2];
result[6] = main_current[3].exp(E::PositiveInteger::from(2_u64)) - main_current[3];
result[3] = main_current[0] * main_current[0] - main_current[0];
result[4] = main_current[1] * main_current[1] - main_current[1];
result[5] = main_current[2] * main_current[2] - main_current[2];
result[6] = main_current[3] * main_current[3] - main_current[3];
}

fn evaluate_aux_transition<F, E>(&self, main_frame: &EvaluationFrame<F>, aux_frame: &EvaluationFrame<E>, _periodic_values: &[F], aux_rand_elements: &AuxTraceRandElements<E>, result: &mut [E])
Expand Down
2 changes: 1 addition & 1 deletion air-script/tests/functions/functions_complex.air
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ boundary_constraints {
integrity_constraints {
# let val = $alpha[0] + v
let f = get_multiplicity_flags(s0, s1)
let z = v^4 * f[3] + v^2 * f[2] + v * f[1] + f[0]
let z = v^7 * f[3] + v^2 * f[2] + v * f[1] + f[0]
# let folded_value = fold_scalar_and_vec(v, b)
enf b_range' = b_range * (z * t - t + 1)
# enf b_range' = b_range * 2
Expand Down
Loading

0 comments on commit 2ed3288

Please sign in to comment.