mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-09 17:23:08 +00:00
103 lines
2.8 KiB
Rust
103 lines
2.8 KiB
Rust
use std::ops::Range;
|
|
|
|
use crate::field::extension_field::target::ExtensionTarget;
|
|
use crate::field::extension_field::Extendable;
|
|
use crate::gates::gate::Gate;
|
|
use crate::gates::util::StridedConstraintConsumer;
|
|
use crate::iop::generator::WitnessGenerator;
|
|
use crate::plonk::circuit_builder::CircuitBuilder;
|
|
use crate::plonk::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase};
|
|
|
|
/// A gate whose first four wires will be equal to a hash of public inputs.
|
|
pub struct PublicInputGate;
|
|
|
|
impl PublicInputGate {
|
|
pub fn wires_public_inputs_hash() -> Range<usize> {
|
|
0..4
|
|
}
|
|
}
|
|
|
|
impl<F: Extendable<D>, const D: usize> Gate<F, D> for PublicInputGate {
|
|
fn id(&self) -> String {
|
|
"PublicInputGate".into()
|
|
}
|
|
|
|
fn eval_unfiltered(&self, vars: EvaluationVars<F, D>) -> Vec<F::Extension> {
|
|
Self::wires_public_inputs_hash()
|
|
.zip(vars.public_inputs_hash.elements)
|
|
.map(|(wire, hash_part)| vars.local_wires[wire] - hash_part.into())
|
|
.collect()
|
|
}
|
|
|
|
fn eval_unfiltered_base_one(
|
|
&self,
|
|
vars: EvaluationVarsBase<F>,
|
|
mut yield_constr: StridedConstraintConsumer<F>,
|
|
) {
|
|
yield_constr.many(
|
|
Self::wires_public_inputs_hash()
|
|
.zip(vars.public_inputs_hash.elements)
|
|
.map(|(wire, hash_part)| vars.local_wires[wire] - hash_part),
|
|
)
|
|
}
|
|
|
|
fn eval_unfiltered_recursively(
|
|
&self,
|
|
builder: &mut CircuitBuilder<F, D>,
|
|
vars: EvaluationTargets<D>,
|
|
) -> Vec<ExtensionTarget<D>> {
|
|
Self::wires_public_inputs_hash()
|
|
.zip(vars.public_inputs_hash.elements)
|
|
.map(|(wire, hash_part)| {
|
|
let hash_part_ext = builder.convert_to_ext(hash_part);
|
|
builder.sub_extension(vars.local_wires[wire], hash_part_ext)
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn generators(
|
|
&self,
|
|
_gate_index: usize,
|
|
_local_constants: &[F],
|
|
) -> Vec<Box<dyn WitnessGenerator<F>>> {
|
|
Vec::new()
|
|
}
|
|
|
|
fn num_wires(&self) -> usize {
|
|
4
|
|
}
|
|
|
|
fn num_constants(&self) -> usize {
|
|
0
|
|
}
|
|
|
|
fn degree(&self) -> usize {
|
|
1
|
|
}
|
|
|
|
fn num_constraints(&self) -> usize {
|
|
4
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::field::goldilocks_field::GoldilocksField;
|
|
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
|
|
use crate::gates::public_input::PublicInputGate;
|
|
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
|
|
|
|
#[test]
|
|
fn low_degree() {
|
|
test_low_degree::<GoldilocksField, _, 4>(PublicInputGate)
|
|
}
|
|
|
|
#[test]
|
|
fn eval_fns() -> anyhow::Result<()> {
|
|
const D: usize = 2;
|
|
type C = PoseidonGoldilocksConfig;
|
|
type F = <C as GenericConfig<D>>::F;
|
|
test_eval_fns::<F, C, _, D>(PublicInputGate)
|
|
}
|
|
}
|