plonky2/src/gates/constant.rs

93 lines
2.2 KiB
Rust
Raw Normal View History

use crate::circuit_builder::CircuitBuilder;
2021-02-26 13:18:41 -08:00
use crate::field::field::Field;
use crate::gates::gate::{Gate, GateRef};
use crate::generator::{SimpleGenerator, WitnessGenerator};
use crate::target::Target;
2021-04-21 22:31:45 +02:00
use crate::vars::{EvaluationTargets, EvaluationVars};
use crate::wire::Wire;
2021-03-30 20:16:20 -07:00
use crate::witness::PartialWitness;
2021-02-26 13:18:41 -08:00
/// A gate which takes a single constant parameter and outputs that value.
pub struct ConstantGate;
2021-02-26 13:18:41 -08:00
impl ConstantGate {
2021-02-26 13:18:41 -08:00
pub fn get<F: Field>() -> GateRef<F> {
GateRef::new(ConstantGate)
2021-02-26 13:18:41 -08:00
}
pub const CONST_INPUT: usize = 0;
pub const WIRE_OUTPUT: usize = 0;
}
impl<F: Field> Gate<F> for ConstantGate {
2021-02-26 13:18:41 -08:00
fn id(&self) -> String {
"ConstantGate".into()
}
fn eval_unfiltered(&self, vars: EvaluationVars<F>) -> Vec<F> {
let input = vars.local_constants[Self::CONST_INPUT];
let output = vars.local_wires[Self::WIRE_OUTPUT];
vec![output - input]
}
fn eval_unfiltered_recursively(
&self,
builder: &mut CircuitBuilder<F>,
vars: EvaluationTargets,
) -> Vec<Target> {
let input = vars.local_constants[Self::CONST_INPUT];
let output = vars.local_wires[Self::WIRE_OUTPUT];
vec![builder.sub(output, input)]
}
fn generators(
&self,
gate_index: usize,
local_constants: &[F],
2021-03-30 20:16:20 -07:00
_next_constants: &[F],
) -> Vec<Box<dyn WitnessGenerator<F>>> {
let gen = ConstantGenerator {
gate_index,
constant: local_constants[0],
};
vec![Box::new(gen)]
}
fn num_wires(&self) -> usize {
1
}
fn num_constants(&self) -> usize {
1
}
fn degree(&self) -> usize {
1
}
fn num_constraints(&self) -> usize {
1
}
}
#[derive(Debug)]
struct ConstantGenerator<F: Field> {
gate_index: usize,
constant: F,
}
impl<F: Field> SimpleGenerator<F> for ConstantGenerator<F> {
fn dependencies(&self) -> Vec<Target> {
Vec::new()
}
2021-03-30 20:16:20 -07:00
fn run_once(&self, _witness: &PartialWitness<F>) -> PartialWitness<F> {
2021-04-21 22:31:45 +02:00
let wire = Wire {
gate: self.gate_index,
input: ConstantGate::WIRE_OUTPUT,
};
2021-04-02 15:29:21 -07:00
PartialWitness::singleton_target(Target::Wire(wire), self.constant)
2021-02-26 13:18:41 -08:00
}
}