mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-09 01:03:08 +00:00
First try
This commit is contained in:
parent
71c392e935
commit
75ad055f40
@ -9,11 +9,75 @@ use crate::field::field_types::Field;
|
||||
use crate::gates::arithmetic::ArithmeticExtensionGate;
|
||||
use crate::iop::generator::{GeneratedValues, SimpleGenerator};
|
||||
use crate::iop::target::Target;
|
||||
use crate::iop::wire::Wire;
|
||||
use crate::iop::witness::PartialWitness;
|
||||
use crate::plonk::circuit_builder::CircuitBuilder;
|
||||
use crate::util::bits_u64;
|
||||
|
||||
impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
pub fn yo(
|
||||
&mut self,
|
||||
gate: usize,
|
||||
first_multiplicand_0: ExtensionTarget<D>,
|
||||
first_multiplicand_1: ExtensionTarget<D>,
|
||||
first_addend: ExtensionTarget<D>,
|
||||
second_multiplicand_0: ExtensionTarget<D>,
|
||||
second_multiplicand_1: ExtensionTarget<D>,
|
||||
second_addend: ExtensionTarget<D>,
|
||||
) -> (ExtensionTarget<D>, ExtensionTarget<D>) {
|
||||
// for et in &mut [
|
||||
// first_multiplicand_0,
|
||||
// first_multiplicand_1,
|
||||
// first_addend,
|
||||
// second_multiplicand_0,
|
||||
// second_multiplicand_1,
|
||||
// second_addend,
|
||||
// ] {
|
||||
// for t in &mut et.0 {
|
||||
// if let Target::Wire(Wire { gate: g, input }) = t {
|
||||
// if *g > gate {
|
||||
// *g = gate;
|
||||
// *input += 8 * D;
|
||||
// dbg!(*g, *t);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
let wire_third_multiplicand_0 = ExtensionTarget::from_range(
|
||||
gate,
|
||||
ArithmeticExtensionGate::<D>::wires_third_multiplicand_0(),
|
||||
);
|
||||
let wire_third_multiplicand_1 = ExtensionTarget::from_range(
|
||||
gate,
|
||||
ArithmeticExtensionGate::<D>::wires_third_multiplicand_1(),
|
||||
);
|
||||
let wire_third_addend =
|
||||
ExtensionTarget::from_range(gate, ArithmeticExtensionGate::<D>::wires_third_addend());
|
||||
let wire_fourth_multiplicand_0 = ExtensionTarget::from_range(
|
||||
gate,
|
||||
ArithmeticExtensionGate::<D>::wires_fourth_multiplicand_0(),
|
||||
);
|
||||
let wire_fourth_multiplicand_1 = ExtensionTarget::from_range(
|
||||
gate,
|
||||
ArithmeticExtensionGate::<D>::wires_fourth_multiplicand_1(),
|
||||
);
|
||||
let wire_fourth_addend =
|
||||
ExtensionTarget::from_range(gate, ArithmeticExtensionGate::<D>::wires_fourth_addend());
|
||||
let wire_third_output =
|
||||
ExtensionTarget::from_range(gate, ArithmeticExtensionGate::<D>::wires_third_output());
|
||||
let wire_fourth_output =
|
||||
ExtensionTarget::from_range(gate, ArithmeticExtensionGate::<D>::wires_fourth_output());
|
||||
|
||||
self.route_extension(first_multiplicand_0, wire_third_multiplicand_0);
|
||||
self.route_extension(first_multiplicand_1, wire_third_multiplicand_1);
|
||||
self.route_extension(first_addend, wire_third_addend);
|
||||
self.route_extension(second_multiplicand_0, wire_fourth_multiplicand_0);
|
||||
self.route_extension(second_multiplicand_1, wire_fourth_multiplicand_1);
|
||||
self.route_extension(second_addend, wire_fourth_addend);
|
||||
self.free_arithmetic = None;
|
||||
|
||||
(wire_third_output, wire_fourth_output)
|
||||
}
|
||||
pub fn double_arithmetic_extension(
|
||||
&mut self,
|
||||
const_0: F,
|
||||
@ -25,8 +89,22 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
second_multiplicand_1: ExtensionTarget<D>,
|
||||
second_addend: ExtensionTarget<D>,
|
||||
) -> (ExtensionTarget<D>, ExtensionTarget<D>) {
|
||||
let gate = self.add_gate(ArithmeticExtensionGate, vec![const_0, const_1]);
|
||||
if let Some((g, c_0, c_1)) = self.free_arithmetic {
|
||||
if g == self.num_gates() - 1 && c_0 == const_0 && c_1 == const_1 {
|
||||
return self.yo(
|
||||
g,
|
||||
first_multiplicand_0,
|
||||
first_multiplicand_1,
|
||||
first_addend,
|
||||
second_multiplicand_0,
|
||||
second_multiplicand_1,
|
||||
second_addend,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let gate = self.add_gate(ArithmeticExtensionGate, vec![const_0, const_1]);
|
||||
self.free_arithmetic = Some((gate, const_0, const_1));
|
||||
let wire_first_multiplicand_0 = ExtensionTarget::from_range(
|
||||
gate,
|
||||
ArithmeticExtensionGate::<D>::wires_first_multiplicand_0(),
|
||||
@ -175,11 +253,27 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
for chunk in pairs.chunks_exact(2) {
|
||||
let (a0, b0) = chunk[0];
|
||||
let (a1, b1) = chunk[1];
|
||||
let gate = self.num_gates();
|
||||
let first_out = ExtensionTarget::from_range(
|
||||
gate,
|
||||
ArithmeticExtensionGate::<D>::wires_first_output(),
|
||||
);
|
||||
let (gate, range) = if let Some((g, c_0, c_1)) = self.free_arithmetic {
|
||||
if g == self.num_gates() - 1 && c_0 == constant && c_1 == F::ONE {
|
||||
(g, ArithmeticExtensionGate::<D>::wires_third_output())
|
||||
} else {
|
||||
(
|
||||
self.num_gates(),
|
||||
ArithmeticExtensionGate::<D>::wires_first_output(),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
(
|
||||
self.num_gates(),
|
||||
ArithmeticExtensionGate::<D>::wires_first_output(),
|
||||
)
|
||||
};
|
||||
let first_out = ExtensionTarget::from_range(gate, range);
|
||||
// let gate = self.num_gates();
|
||||
// let first_out = ExtensionTarget::from_range(
|
||||
// gate,
|
||||
// ArithmeticExtensionGate::<D>::wires_first_output(),
|
||||
// );
|
||||
acc = self
|
||||
.double_arithmetic_extension(constant, F::ONE, a0, b0, acc, a1, b1, first_out)
|
||||
.1;
|
||||
@ -706,6 +800,35 @@ mod tests {
|
||||
verify(proof, &data.verifier_only, &data.common)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mul() -> Result<()> {
|
||||
type F = CrandallField;
|
||||
type FF = QuarticCrandallField;
|
||||
const D: usize = 4;
|
||||
|
||||
let config = CircuitConfig::large_config();
|
||||
|
||||
let pw = PartialWitness::new(config.num_wires);
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
|
||||
let x = FF::ONE;
|
||||
let y = FF::TWO;
|
||||
let z = x * y;
|
||||
let xt = builder.constant_extension(x);
|
||||
let yt = builder.constant_extension(y);
|
||||
let zt = builder.constant_extension(z);
|
||||
let comp_zt = builder.mul_extension(xt, yt);
|
||||
dbg!(comp_zt);
|
||||
builder.add_marked(zt.into(), "yo");
|
||||
builder.add_marked(comp_zt.into(), "ya");
|
||||
builder.assert_equal_extension(zt, comp_zt);
|
||||
|
||||
let data = builder.build();
|
||||
let proof = data.prove(pw)?;
|
||||
|
||||
verify(proof, &data.verifier_only, &data.common)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div_extension() -> Result<()> {
|
||||
type F = CrandallField;
|
||||
|
||||
@ -27,6 +27,7 @@ impl<const D: usize> ArithmeticExtensionGate<D> {
|
||||
pub fn wires_first_output() -> Range<usize> {
|
||||
3 * D..4 * D
|
||||
}
|
||||
|
||||
pub fn wires_second_multiplicand_0() -> Range<usize> {
|
||||
4 * D..5 * D
|
||||
}
|
||||
@ -39,6 +40,32 @@ impl<const D: usize> ArithmeticExtensionGate<D> {
|
||||
pub fn wires_second_output() -> Range<usize> {
|
||||
7 * D..8 * D
|
||||
}
|
||||
|
||||
pub fn wires_third_multiplicand_0() -> Range<usize> {
|
||||
8 * D..9 * D
|
||||
}
|
||||
pub fn wires_third_multiplicand_1() -> Range<usize> {
|
||||
9 * D..10 * D
|
||||
}
|
||||
pub fn wires_third_addend() -> Range<usize> {
|
||||
10 * D..11 * D
|
||||
}
|
||||
pub fn wires_third_output() -> Range<usize> {
|
||||
11 * D..12 * D
|
||||
}
|
||||
|
||||
pub fn wires_fourth_multiplicand_0() -> Range<usize> {
|
||||
12 * D..13 * D
|
||||
}
|
||||
pub fn wires_fourth_multiplicand_1() -> Range<usize> {
|
||||
13 * D..14 * D
|
||||
}
|
||||
pub fn wires_fourth_addend() -> Range<usize> {
|
||||
14 * D..15 * D
|
||||
}
|
||||
pub fn wires_fourth_output() -> Range<usize> {
|
||||
15 * D..16 * D
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Extendable<D>, const D: usize> Gate<F, D> for ArithmeticExtensionGate<D> {
|
||||
@ -56,18 +83,32 @@ impl<F: Extendable<D>, const D: usize> Gate<F, D> for ArithmeticExtensionGate<D>
|
||||
let second_multiplicand_0 = vars.get_local_ext_algebra(Self::wires_second_multiplicand_0());
|
||||
let second_multiplicand_1 = vars.get_local_ext_algebra(Self::wires_second_multiplicand_1());
|
||||
let second_addend = vars.get_local_ext_algebra(Self::wires_second_addend());
|
||||
let third_multiplicand_0 = vars.get_local_ext_algebra(Self::wires_third_multiplicand_0());
|
||||
let third_multiplicand_1 = vars.get_local_ext_algebra(Self::wires_third_multiplicand_1());
|
||||
let third_addend = vars.get_local_ext_algebra(Self::wires_third_addend());
|
||||
let fourth_multiplicand_0 = vars.get_local_ext_algebra(Self::wires_fourth_multiplicand_0());
|
||||
let fourth_multiplicand_1 = vars.get_local_ext_algebra(Self::wires_fourth_multiplicand_1());
|
||||
let fourth_addend = vars.get_local_ext_algebra(Self::wires_fourth_addend());
|
||||
let first_output = vars.get_local_ext_algebra(Self::wires_first_output());
|
||||
let second_output = vars.get_local_ext_algebra(Self::wires_second_output());
|
||||
let third_output = vars.get_local_ext_algebra(Self::wires_third_output());
|
||||
let fourth_output = vars.get_local_ext_algebra(Self::wires_fourth_output());
|
||||
|
||||
let first_computed_output = first_multiplicand_0 * first_multiplicand_1 * const_0.into()
|
||||
+ first_addend * const_1.into();
|
||||
let second_computed_output = second_multiplicand_0 * second_multiplicand_1 * const_0.into()
|
||||
+ second_addend * const_1.into();
|
||||
let third_computed_output = third_multiplicand_0 * third_multiplicand_1 * const_0.into()
|
||||
+ third_addend * const_1.into();
|
||||
let fourth_computed_output = fourth_multiplicand_0 * fourth_multiplicand_1 * const_0.into()
|
||||
+ fourth_addend * const_1.into();
|
||||
|
||||
let mut constraints = (first_output - first_computed_output)
|
||||
.to_basefield_array()
|
||||
.to_vec();
|
||||
constraints.extend((second_output - second_computed_output).to_basefield_array());
|
||||
constraints.extend((third_output - third_computed_output).to_basefield_array());
|
||||
constraints.extend((fourth_output - fourth_computed_output).to_basefield_array());
|
||||
constraints
|
||||
}
|
||||
|
||||
@ -81,18 +122,32 @@ impl<F: Extendable<D>, const D: usize> Gate<F, D> for ArithmeticExtensionGate<D>
|
||||
let second_multiplicand_0 = vars.get_local_ext(Self::wires_second_multiplicand_0());
|
||||
let second_multiplicand_1 = vars.get_local_ext(Self::wires_second_multiplicand_1());
|
||||
let second_addend = vars.get_local_ext(Self::wires_second_addend());
|
||||
let third_multiplicand_0 = vars.get_local_ext(Self::wires_third_multiplicand_0());
|
||||
let third_multiplicand_1 = vars.get_local_ext(Self::wires_third_multiplicand_1());
|
||||
let third_addend = vars.get_local_ext(Self::wires_third_addend());
|
||||
let fourth_multiplicand_0 = vars.get_local_ext(Self::wires_fourth_multiplicand_0());
|
||||
let fourth_multiplicand_1 = vars.get_local_ext(Self::wires_fourth_multiplicand_1());
|
||||
let fourth_addend = vars.get_local_ext(Self::wires_fourth_addend());
|
||||
let first_output = vars.get_local_ext(Self::wires_first_output());
|
||||
let second_output = vars.get_local_ext(Self::wires_second_output());
|
||||
let third_output = vars.get_local_ext(Self::wires_third_output());
|
||||
let fourth_output = vars.get_local_ext(Self::wires_fourth_output());
|
||||
|
||||
let first_computed_output = first_multiplicand_0 * first_multiplicand_1 * const_0.into()
|
||||
+ first_addend * const_1.into();
|
||||
let second_computed_output = second_multiplicand_0 * second_multiplicand_1 * const_0.into()
|
||||
+ second_addend * const_1.into();
|
||||
let third_computed_output = third_multiplicand_0 * third_multiplicand_1 * const_0.into()
|
||||
+ third_addend * const_1.into();
|
||||
let fourth_computed_output = fourth_multiplicand_0 * fourth_multiplicand_1 * const_0.into()
|
||||
+ fourth_addend * const_1.into();
|
||||
|
||||
let mut constraints = (first_output - first_computed_output)
|
||||
.to_basefield_array()
|
||||
.to_vec();
|
||||
constraints.extend((second_output - second_computed_output).to_basefield_array());
|
||||
constraints.extend((third_output - third_computed_output).to_basefield_array());
|
||||
constraints.extend((fourth_output - fourth_computed_output).to_basefield_array());
|
||||
constraints
|
||||
}
|
||||
|
||||
@ -110,8 +165,16 @@ impl<F: Extendable<D>, const D: usize> Gate<F, D> for ArithmeticExtensionGate<D>
|
||||
let second_multiplicand_0 = vars.get_local_ext_algebra(Self::wires_second_multiplicand_0());
|
||||
let second_multiplicand_1 = vars.get_local_ext_algebra(Self::wires_second_multiplicand_1());
|
||||
let second_addend = vars.get_local_ext_algebra(Self::wires_second_addend());
|
||||
let third_multiplicand_0 = vars.get_local_ext_algebra(Self::wires_third_multiplicand_0());
|
||||
let third_multiplicand_1 = vars.get_local_ext_algebra(Self::wires_third_multiplicand_1());
|
||||
let third_addend = vars.get_local_ext_algebra(Self::wires_third_addend());
|
||||
let fourth_multiplicand_0 = vars.get_local_ext_algebra(Self::wires_fourth_multiplicand_0());
|
||||
let fourth_multiplicand_1 = vars.get_local_ext_algebra(Self::wires_fourth_multiplicand_1());
|
||||
let fourth_addend = vars.get_local_ext_algebra(Self::wires_fourth_addend());
|
||||
let first_output = vars.get_local_ext_algebra(Self::wires_first_output());
|
||||
let second_output = vars.get_local_ext_algebra(Self::wires_second_output());
|
||||
let third_output = vars.get_local_ext_algebra(Self::wires_third_output());
|
||||
let fourth_output = vars.get_local_ext_algebra(Self::wires_fourth_output());
|
||||
|
||||
let first_computed_output =
|
||||
builder.mul_ext_algebra(first_multiplicand_0, first_multiplicand_1);
|
||||
@ -128,10 +191,29 @@ impl<F: Extendable<D>, const D: usize> Gate<F, D> for ArithmeticExtensionGate<D>
|
||||
let second_computed_output =
|
||||
builder.add_ext_algebra(second_computed_output, second_scaled_addend);
|
||||
|
||||
let third_computed_output =
|
||||
builder.mul_ext_algebra(third_multiplicand_0, third_multiplicand_1);
|
||||
let third_computed_output = builder.scalar_mul_ext_algebra(const_0, third_computed_output);
|
||||
let third_scaled_addend = builder.scalar_mul_ext_algebra(const_1, third_addend);
|
||||
let third_computed_output =
|
||||
builder.add_ext_algebra(third_computed_output, third_scaled_addend);
|
||||
|
||||
let fourth_computed_output =
|
||||
builder.mul_ext_algebra(fourth_multiplicand_0, fourth_multiplicand_1);
|
||||
let fourth_computed_output =
|
||||
builder.scalar_mul_ext_algebra(const_0, fourth_computed_output);
|
||||
let fourth_scaled_addend = builder.scalar_mul_ext_algebra(const_1, fourth_addend);
|
||||
let fourth_computed_output =
|
||||
builder.add_ext_algebra(fourth_computed_output, fourth_scaled_addend);
|
||||
|
||||
let diff_0 = builder.sub_ext_algebra(first_output, first_computed_output);
|
||||
let diff_1 = builder.sub_ext_algebra(second_output, second_computed_output);
|
||||
let diff_2 = builder.sub_ext_algebra(third_output, third_computed_output);
|
||||
let diff_3 = builder.sub_ext_algebra(fourth_output, fourth_computed_output);
|
||||
let mut constraints = diff_0.to_ext_target_array().to_vec();
|
||||
constraints.extend(diff_1.to_ext_target_array());
|
||||
constraints.extend(diff_2.to_ext_target_array());
|
||||
constraints.extend(diff_3.to_ext_target_array());
|
||||
constraints
|
||||
}
|
||||
|
||||
@ -140,21 +222,24 @@ impl<F: Extendable<D>, const D: usize> Gate<F, D> for ArithmeticExtensionGate<D>
|
||||
gate_index: usize,
|
||||
local_constants: &[F],
|
||||
) -> Vec<Box<dyn WitnessGenerator<F>>> {
|
||||
let gen0 = ArithmeticExtensionGenerator0 {
|
||||
gate_index,
|
||||
const_0: local_constants[0],
|
||||
const_1: local_constants[1],
|
||||
};
|
||||
let gen1 = ArithmeticExtensionGenerator1 {
|
||||
gate_index,
|
||||
const_0: local_constants[0],
|
||||
const_1: local_constants[1],
|
||||
};
|
||||
vec![Box::new(gen0), Box::new(gen1)]
|
||||
let gens = (0..4)
|
||||
.map(|i| ArithmeticExtensionGenerator {
|
||||
gate_index,
|
||||
const_0: local_constants[0],
|
||||
const_1: local_constants[1],
|
||||
i,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
vec![
|
||||
Box::new(gens[0].clone()),
|
||||
Box::new(gens[1].clone()),
|
||||
Box::new(gens[2].clone()),
|
||||
Box::new(gens[3].clone()),
|
||||
]
|
||||
}
|
||||
|
||||
fn num_wires(&self) -> usize {
|
||||
8 * D
|
||||
16 * D
|
||||
}
|
||||
|
||||
fn num_constants(&self) -> usize {
|
||||
@ -166,27 +251,21 @@ impl<F: Extendable<D>, const D: usize> Gate<F, D> for ArithmeticExtensionGate<D>
|
||||
}
|
||||
|
||||
fn num_constraints(&self) -> usize {
|
||||
2 * D
|
||||
4 * D
|
||||
}
|
||||
}
|
||||
|
||||
struct ArithmeticExtensionGenerator0<F: Extendable<D>, const D: usize> {
|
||||
#[derive(Clone)]
|
||||
struct ArithmeticExtensionGenerator<F: Extendable<D>, const D: usize> {
|
||||
gate_index: usize,
|
||||
const_0: F,
|
||||
const_1: F,
|
||||
i: usize,
|
||||
}
|
||||
|
||||
struct ArithmeticExtensionGenerator1<F: Extendable<D>, const D: usize> {
|
||||
gate_index: usize,
|
||||
const_0: F,
|
||||
const_1: F,
|
||||
}
|
||||
|
||||
impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for ArithmeticExtensionGenerator0<F, D> {
|
||||
impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for ArithmeticExtensionGenerator<F, D> {
|
||||
fn dependencies(&self) -> Vec<Target> {
|
||||
ArithmeticExtensionGate::<D>::wires_first_multiplicand_0()
|
||||
.chain(ArithmeticExtensionGate::<D>::wires_first_multiplicand_1())
|
||||
.chain(ArithmeticExtensionGate::<D>::wires_first_addend())
|
||||
(4 * self.i * D..(4 * self.i + 3) * D)
|
||||
.map(|i| Target::wire(self.gate_index, i))
|
||||
.collect()
|
||||
}
|
||||
@ -197,49 +276,13 @@ impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for ArithmeticExtensio
|
||||
witness.get_extension_target(t)
|
||||
};
|
||||
|
||||
let multiplicand_0 =
|
||||
extract_extension(ArithmeticExtensionGate::<D>::wires_first_multiplicand_0());
|
||||
let multiplicand_1 =
|
||||
extract_extension(ArithmeticExtensionGate::<D>::wires_first_multiplicand_1());
|
||||
let addend = extract_extension(ArithmeticExtensionGate::<D>::wires_first_addend());
|
||||
let start = 4 * self.i * D;
|
||||
let multiplicand_0 = extract_extension(start..start + D);
|
||||
let multiplicand_1 = extract_extension(start + D..start + 2 * D);
|
||||
let addend = extract_extension(start + 2 * D..start + 3 * D);
|
||||
|
||||
let output_target = ExtensionTarget::from_range(
|
||||
self.gate_index,
|
||||
ArithmeticExtensionGate::<D>::wires_first_output(),
|
||||
);
|
||||
|
||||
let computed_output =
|
||||
multiplicand_0 * multiplicand_1 * self.const_0.into() + addend * self.const_1.into();
|
||||
|
||||
out_buffer.set_extension_target(output_target, computed_output)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for ArithmeticExtensionGenerator1<F, D> {
|
||||
fn dependencies(&self) -> Vec<Target> {
|
||||
ArithmeticExtensionGate::<D>::wires_second_multiplicand_0()
|
||||
.chain(ArithmeticExtensionGate::<D>::wires_second_multiplicand_1())
|
||||
.chain(ArithmeticExtensionGate::<D>::wires_second_addend())
|
||||
.map(|i| Target::wire(self.gate_index, i))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn run_once(&self, witness: &PartialWitness<F>, out_buffer: &mut GeneratedValues<F>) {
|
||||
let extract_extension = |range: Range<usize>| -> F::Extension {
|
||||
let t = ExtensionTarget::from_range(self.gate_index, range);
|
||||
witness.get_extension_target(t)
|
||||
};
|
||||
|
||||
let multiplicand_0 =
|
||||
extract_extension(ArithmeticExtensionGate::<D>::wires_second_multiplicand_0());
|
||||
let multiplicand_1 =
|
||||
extract_extension(ArithmeticExtensionGate::<D>::wires_second_multiplicand_1());
|
||||
let addend = extract_extension(ArithmeticExtensionGate::<D>::wires_second_addend());
|
||||
|
||||
let output_target = ExtensionTarget::from_range(
|
||||
self.gate_index,
|
||||
ArithmeticExtensionGate::<D>::wires_second_output(),
|
||||
);
|
||||
let output_target =
|
||||
ExtensionTarget::from_range(self.gate_index, start + 3 * D..start + 4 * D);
|
||||
|
||||
let computed_output =
|
||||
multiplicand_0 * multiplicand_1 * self.const_0.into() + addend * self.const_1.into();
|
||||
|
||||
@ -76,10 +76,10 @@ pub(crate) fn generate_partial_witness<F: Field>(
|
||||
pending_generator_indices = next_pending_generator_indices;
|
||||
}
|
||||
|
||||
assert!(
|
||||
generator_is_expired.into_iter().all(identity),
|
||||
"Some generators weren't run."
|
||||
);
|
||||
// assert!(
|
||||
// generator_is_expired.into_iter().all(identity),
|
||||
// "Some generators weren't run."
|
||||
// );
|
||||
}
|
||||
|
||||
/// A generator participates in the generation of the witness.
|
||||
|
||||
@ -60,6 +60,8 @@ pub struct CircuitBuilder<F: Extendable<D>, const D: usize> {
|
||||
|
||||
constants_to_targets: HashMap<F, Target>,
|
||||
targets_to_constants: HashMap<Target, F>,
|
||||
|
||||
pub(crate) free_arithmetic: Option<(usize, F, F)>,
|
||||
}
|
||||
|
||||
impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
@ -76,6 +78,7 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
generators: Vec::new(),
|
||||
constants_to_targets: HashMap::new(),
|
||||
targets_to_constants: HashMap::new(),
|
||||
free_arithmetic: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -66,11 +66,11 @@ impl CircuitConfig {
|
||||
security_bits: 128,
|
||||
rate_bits: 3,
|
||||
num_challenges: 3,
|
||||
zero_knowledge: true,
|
||||
zero_knowledge: false,
|
||||
cap_height: 1,
|
||||
fri_config: FriConfig {
|
||||
proof_of_work_bits: 1,
|
||||
reduction_arity_bits: vec![1, 1, 1, 1],
|
||||
reduction_arity_bits: vec![1],
|
||||
num_query_rounds: 1,
|
||||
cap_height: 1,
|
||||
},
|
||||
|
||||
@ -97,8 +97,13 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
.chunks(inner_common_data.quotient_degree_factor)
|
||||
.enumerate()
|
||||
{
|
||||
dbg!(chunk.len());
|
||||
let recombined_quotient = scale.reduce(chunk, self);
|
||||
self.add_marked(z_h_zeta.into(), &format!("zhz {}", i));
|
||||
self.add_marked(recombined_quotient.into(), &format!("recomb {}", i));
|
||||
let computed_vanishing_poly = self.mul_extension(z_h_zeta, recombined_quotient);
|
||||
self.add_marked(vanishing_polys_zeta[i].into(), &format!("yo {}", i));
|
||||
self.add_marked(computed_vanishing_poly.into(), &format!("ya {}", i));
|
||||
self.named_assert_equal_extension(
|
||||
vanishing_polys_zeta[i],
|
||||
computed_vanishing_poly,
|
||||
@ -444,7 +449,7 @@ mod tests {
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config.clone());
|
||||
let _two = builder.two();
|
||||
let mut _two = builder.hash_n_to_hash(vec![_two], true).elements[0];
|
||||
for _ in 0..10000 {
|
||||
for _ in 0..20000 {
|
||||
_two = builder.mul(_two, _two);
|
||||
}
|
||||
let data = builder.build();
|
||||
|
||||
@ -382,6 +382,9 @@ pub(crate) fn eval_vanishing_poly_recursively<F: Extendable<D>, const D: usize>(
|
||||
]
|
||||
.concat();
|
||||
|
||||
for (i, &v) in vanishing_terms.iter().enumerate() {
|
||||
builder.add_marked(v.into(), &format!("v {}", i));
|
||||
}
|
||||
alphas
|
||||
.iter()
|
||||
.map(|&alpha| {
|
||||
|
||||
@ -176,16 +176,30 @@ impl<const D: usize> ReducingFactorTarget<D> {
|
||||
terms_vec.reverse();
|
||||
|
||||
let mut acc = zero;
|
||||
let mut countt = 0;
|
||||
dbg!(terms_vec.len());
|
||||
for pair in terms_vec.chunks(2) {
|
||||
// We will route the output of the first arithmetic operation to the multiplicand of the
|
||||
// second, i.e. we compute the following:
|
||||
// out_0 = alpha acc + pair[0]
|
||||
// acc' = out_1 = alpha out_0 + pair[1]
|
||||
let gate = builder.num_gates();
|
||||
let out_0 = ExtensionTarget::from_range(
|
||||
gate,
|
||||
ArithmeticExtensionGate::<D>::wires_first_output(),
|
||||
);
|
||||
|
||||
let (gate, range) = if let Some((g, c_0, c_1)) = builder.free_arithmetic {
|
||||
if g == builder.num_gates() - 1 && c_0 == F::ONE && c_1 == F::ONE {
|
||||
(g, ArithmeticExtensionGate::<D>::wires_third_output())
|
||||
} else {
|
||||
(
|
||||
builder.num_gates(),
|
||||
ArithmeticExtensionGate::<D>::wires_first_output(),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
(
|
||||
builder.num_gates(),
|
||||
ArithmeticExtensionGate::<D>::wires_first_output(),
|
||||
)
|
||||
};
|
||||
let out_0 = ExtensionTarget::from_range(gate, range);
|
||||
acc = builder
|
||||
.double_arithmetic_extension(
|
||||
F::ONE,
|
||||
@ -198,6 +212,9 @@ impl<const D: usize> ReducingFactorTarget<D> {
|
||||
pair[1],
|
||||
)
|
||||
.1;
|
||||
// dbg!(countt, acc);
|
||||
// builder.add_marked(acc.into(), &format!("acc {}", countt));
|
||||
countt += 1;
|
||||
}
|
||||
acc
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user