plonky2/src/gadgets/hash.rs

57 lines
1.8 KiB
Rust
Raw Normal View History

2021-04-02 15:29:21 -07:00
use std::convert::TryInto;
use crate::circuit_builder::CircuitBuilder;
use crate::field::extension_field::Extendable;
2021-04-02 15:29:21 -07:00
use crate::gates::gmimc::GMiMCGate;
use crate::hash::GMIMC_ROUNDS;
use crate::target::Target;
use crate::wire::Wire;
2021-04-12 10:38:07 +02:00
// TODO: Move to be next to native `permute`?
impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
2021-04-02 15:29:21 -07:00
pub fn permute(&mut self, inputs: [Target; 12]) -> [Target; 12] {
let zero = self.zero();
2021-04-21 22:31:45 +02:00
let gate =
self.add_gate_no_constants(GMiMCGate::<F, D, GMIMC_ROUNDS>::with_automatic_constants());
2021-04-02 15:29:21 -07:00
2021-04-12 10:38:07 +02:00
// We don't want to swap any inputs, so set that wire to 0.
let swap_wire = GMiMCGate::<F, D, GMIMC_ROUNDS>::WIRE_SWAP;
2021-04-21 22:31:45 +02:00
let swap_wire = Target::Wire(Wire {
gate,
input: swap_wire,
});
2021-04-12 10:38:07 +02:00
self.route(zero, swap_wire);
2021-04-02 15:29:21 -07:00
2021-04-12 10:38:07 +02:00
// The old accumulator wire doesn't matter, since we won't read the new accumulator wire.
// We do have to set it to something though, so we'll arbitrary pick 0.
let old_acc_wire = GMiMCGate::<F, D, GMIMC_ROUNDS>::WIRE_INDEX_ACCUMULATOR_OLD;
2021-04-21 22:31:45 +02:00
let old_acc_wire = Target::Wire(Wire {
gate,
input: old_acc_wire,
});
2021-04-12 10:38:07 +02:00
self.route(zero, old_acc_wire);
// Route input wires.
2021-04-02 15:29:21 -07:00
for i in 0..12 {
let in_wire = GMiMCGate::<F, D, GMIMC_ROUNDS>::wire_input(i);
2021-04-21 22:31:45 +02:00
let in_wire = Target::Wire(Wire {
gate,
input: in_wire,
});
2021-04-02 15:29:21 -07:00
self.route(inputs[i], in_wire);
}
2021-04-12 10:38:07 +02:00
// Collect output wires.
2021-04-02 15:29:21 -07:00
(0..12)
2021-04-21 22:31:45 +02:00
.map(|i| {
Target::Wire(Wire {
gate,
input: GMiMCGate::<F, D, GMIMC_ROUNDS>::wire_output(i),
2021-04-21 22:31:45 +02:00
})
})
2021-04-02 15:29:21 -07:00
.collect::<Vec<_>>()
.try_into()
.unwrap()
}
}