Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Refactor pi gate #223

Closed
wants to merge 3 commits into from
Closed

Refactor pi gate #223

wants to merge 3 commits into from

Conversation

ChihChengLiang
Copy link
Collaborator

@github-actions github-actions bot added the crate-keccak Issues related to the keccak workspace member label Dec 13, 2021
state: [Column<Advice>; 25],
_marker: PhantomData<F>,
}

impl<F: FieldExt> PiConfig<F> {
pub const OFFSET: usize = 2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please. Don't remove the OFFSET as it's required later for assembliyng the entire Keccak circuit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'll return them back.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I realize it's part of the layouter refactor.

Currently the circuit looks like this

  state = {
                // Apply pi outside circuit
                let out_state = KeccakFArith::pi(&state_to_biguint(state));
                let out_state = state_bigint_to_pallas(out_state);
                // assignment
                self.pi_config
                    .assign_state(region, offset, state, out_state)?
            };

            offset += PiConfig::OFFSET;

With the new interface, we can do

  state = self.pi_config.assign_state(mut layouter, state, out_state)?;

No need to track the region and the offset. The layouter find a new region for it.
What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm.

So the proposal is to have each gate/config application within a different region then?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it makes sense to let layouter decide where to put the state row. Counting offsets is like doing the layouter's job.

Comment on lines -27 to -41
meta.create_gate("pi", |meta| {
let q_enable = meta.query_selector(q_enable);
(0..5)
.cartesian_product(0..5)
.map(|(x, y)| {
let lane = meta.query_advice(
state[5 * ((x + 3 * y) % 5) + x],
Rotation::cur(),
);
let new_lane =
meta.query_advice(state[5 * x + y], Rotation::next());

q_enable.clone() * (new_lane - lane)
})
.collect::<Vec<Expression<F>>>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you removing the gate from Pi??
It does not apply any constraints now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that using purely copy constraints can achieve pi gate. There's no need to add polys for the gate.
What do you think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. But copy constraints can only enforce that the provided input is indeed the one that has to be used.

Not sure how you still enforce with constraints:

 let lane = meta.query_advice(
                        state[5 * ((x + 3 * y) % 5) + x],
                        Rotation::cur(),
                    );
                    let new_lane =
                        meta.query_advice(state[5 * x + y], Rotation::next());

                    q_enable.clone() * (new_lane - lane)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIU the halo2 code correctly, copy constraints are applicable as long as we know what cells to copy. We don't have to create custom gates for them.
I did realize I forgot to add enable equality in this case. Will add now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh I see! That sounds correct! Thanks!

Copy link
Member

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Depending on the Layouter instead of Region + offset seems like a really nice idea!

@ChihChengLiang
Copy link
Collaborator Author

Close in favor of #234

@CPerezz CPerezz deleted the refactor-pi-gate branch November 30, 2022 11:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
crate-keccak Issues related to the keccak workspace member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants