Skip to content

Commit

Permalink
chore: submit memory prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
eigmax committed Oct 15, 2023
1 parent 9005b80 commit 5192107
Show file tree
Hide file tree
Showing 7 changed files with 468 additions and 62 deletions.
7 changes: 1 addition & 6 deletions src/arithmetic/addcy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ use crate::arithmetic::utils::u32_to_array;
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};

/// Generate row for ADD, SUB, GT and LT operations.
pub(crate) fn generate<F: PrimeField64>(
lv: &mut [F],
filter: usize,
left_in: u32,
right_in: u32,
) {
pub(crate) fn generate<F: PrimeField64>(lv: &mut [F], filter: usize, left_in: u32, right_in: u32) {
u32_to_array(&mut lv[INPUT_REGISTER_0], left_in);
u32_to_array(&mut lv[INPUT_REGISTER_1], right_in);
u32_to_array(&mut lv[INPUT_REGISTER_2], 0);
Expand Down
26 changes: 5 additions & 21 deletions src/arithmetic/arithmetic_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,11 @@ mod tests {
};

// 123 + 456 == 579
let add = Operation::binary(BinaryOperator::Add, (123), (456));
let add = Operation::binary(BinaryOperator::Add, 123, 456);
// (123 * 456) % 1007 == 703
let mulmod = Operation::ternary(
TernaryOperator::MulMod,
123,
457,
1007,
);
let mulmod = Operation::ternary(TernaryOperator::MulMod, 123, 456, 1007);
// (1234 + 567) % 1007 == 794
let addmod = Operation::ternary(
TernaryOperator::AddMod,
1234,
567,
1007,
);
let addmod = Operation::ternary(TernaryOperator::AddMod, 1234, 567, 1007);
// 123 * 456 == 56088
let mul = Operation::binary(BinaryOperator::Mul, 123, 456);
// 128 / 13 == 9
Expand Down Expand Up @@ -365,7 +355,7 @@ mod tests {
(9, 1),
(10, 0),
(11, 9),
(13, 0xAB),
// (13, 0xAB),
];

for (row, expected) in expected_output {
Expand Down Expand Up @@ -397,13 +387,7 @@ mod tests {
let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25);

let ops = (0..super::RANGE_MAX)
.map(|_| {
Operation::binary(
BinaryOperator::Mul,
rng.gen::<u32>(),
rng.gen::<u32>(),
)
})
.map(|_| Operation::binary(BinaryOperator::Mul, rng.gen::<u32>(), rng.gen::<u32>()))
.collect::<Vec<_>>();

let pols = stark.generate_trace(ops);
Expand Down
20 changes: 8 additions & 12 deletions src/arithmetic/divmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use plonky2::iop::ext_target::ExtensionTarget;
use plonky2::plonk::circuit_builder::CircuitBuilder;

use crate::arithmetic::columns::*;
use crate::arithmetic::modular::{
generate_modular_op, modular_constr_poly, modular_constr_poly_ext_circuit,
};
use crate::arithmetic::utils::*;
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};

Expand All @@ -22,10 +25,7 @@ pub(crate) fn generate_divmod<F: PrimeField64>(
) {
let input_limbs = read_value_i64_limbs::<N_LIMBS, _>(lv, input_limbs_range);
let pol_input: [i64; 2 * N_LIMBS - 1] = pol_extend(input_limbs);
//let (out, quo_input) = generate_modular_op(lv, nv, filter, pol_input, modulus_range);
// FIXME
let out = [F::ZERO; N_LIMBS];
let quo_input = [F::ZERO; N_LIMBS * 2];
let (out, quo_input) = generate_modular_op(lv, nv, filter, pol_input, modulus_range);

debug_assert!(
&quo_input[N_LIMBS..].iter().all(|&x| x == F::ZERO),
Expand Down Expand Up @@ -100,11 +100,9 @@ pub(crate) fn eval_packed_divmod_helper<P: PackedField>(
quo[..N_LIMBS].copy_from_slice(&lv[quo_range]);
quo
};
let rem: [P; N_LIMBS] = read_value(lv, rem_range);
let rem = read_value(lv, rem_range);

// let mut constr_poly = modular_constr_poly(lv, nv, yield_constr, filter, rem, den, quo);
// FIXME
let mut constr_poly = [P::ZEROS; N_LIMBS * 2];
let mut constr_poly = modular_constr_poly(lv, nv, yield_constr, filter, rem, den, quo);

let input = num;
pol_sub_assign(&mut constr_poly, input);
Expand Down Expand Up @@ -164,10 +162,8 @@ pub(crate) fn eval_ext_circuit_divmod_helper<F: RichField + Extendable<D>, const
};
let rem: [ExtensionTarget<D>; N_LIMBS] = read_value(lv, rem_range);

//let mut constr_poly =
// modular_constr_poly_ext_circuit(lv, nv, builder, yield_constr, filter, rem, den, quo);
// FIXME
let mut constr_poly = [ExtensionTarget::<D>::default(); 2 * N_LIMBS];
let mut constr_poly =
modular_constr_poly_ext_circuit(lv, nv, builder, yield_constr, filter, rem, den, quo);

let input = num;
pol_sub_assign_ext_circuit(builder, &mut constr_poly, input);
Expand Down
33 changes: 16 additions & 17 deletions src/arithmetic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
pub mod addcy;
pub mod arithmetic_stark;
pub mod columns;
pub mod shift;
pub mod addcy;
pub mod divmod;
pub mod modular;
pub mod mul;
pub mod shift;
pub mod utils;

use crate::util::*;
use num::Zero;
use plonky2::field::types::PrimeField64;
use crate::util::*;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum BinaryOperator {
Expand All @@ -30,7 +31,7 @@ impl BinaryOperator {
BinaryOperator::Add => input0.overflowing_add(input1).0,
BinaryOperator::Mul => input0.overflowing_mul(input1).0,
BinaryOperator::Shl => {
if input0 < 256 {
if input0 < 32 {
input1 << input0
} else {
u32::zero()
Expand All @@ -45,7 +46,7 @@ impl BinaryOperator {
}
}
BinaryOperator::Shr => {
if input0 < 256 {
if input0 < 32 {
input1 >> input0
} else {
u32::zero()
Expand Down Expand Up @@ -99,9 +100,9 @@ pub(crate) enum TernaryOperator {
impl TernaryOperator {
pub(crate) fn result(&self, input0: u32, input1: u32, input2: u32) -> u32 {
match self {
TernaryOperator::AddMod => ((input0 + input1) % input2),
TernaryOperator::MulMod => ((input0 * input1) % input2),
TernaryOperator::SubMod => ((input0 - input1) % input2),
TernaryOperator::AddMod => (input0 + input1) % input2,
TernaryOperator::MulMod => (input0 * input1) % input2,
TernaryOperator::SubMod => (input0 - input1) % input2,
}
}

Expand Down Expand Up @@ -222,8 +223,7 @@ fn ternary_op_to_rows<F: PrimeField64>(

row1[row_filter] = F::ONE;

// FIXME
// modular::generate(&mut row1, &mut row2, row_filter, input0, input1, input2);
modular::generate(&mut row1, &mut row2, row_filter, input0, input1, input2);

(row1, Some(row2))
}
Expand Down Expand Up @@ -260,12 +260,11 @@ fn binary_op_to_rows<F: PrimeField64>(
let mut nv = vec![F::ZERO; columns::NUM_ARITH_COLUMNS];
shift::generate(&mut row, &mut nv, false, input0, input1, result);
(row, Some(nv))
}
/*
BinaryOperator::Byte => {
byte::generate(&mut row, input0, input1);
(row, None)
}
*/
} /*
BinaryOperator::Byte => {
byte::generate(&mut row, input0, input1);
(row, None)
}
*/
}
}
Loading

0 comments on commit 5192107

Please sign in to comment.