Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keccak circuit implementation #268

Merged
merged 30 commits into from
Feb 14, 2022
Merged

Keccak circuit implementation #268

merged 30 commits into from
Feb 14, 2022

Conversation

CPerezz
Copy link
Member

@CPerezz CPerezz commented Jan 10, 2022

This PR implements the entire keccak permutation circuit (No padding) to be used as a gadget or a final circuit.

Resolves: #218
Resolves: #273

@github-actions github-actions bot added the crate-keccak Issues related to the keccak workspace member label Jan 10, 2022
@CPerezz CPerezz force-pushed the keccak_circuit branch 3 times, most recently from e9c2cec to 065c930 Compare January 26, 2022 13:47
CPerezz and others added 16 commits January 26, 2022 17:55
Since the state is always carried as an `AssignedCell`, this utility fn
helps to dettach the `Cell` from it's value `F`.

And this is implemented for N-lenght arrays.
This is the way on which al the configs are implemented.
And so, we cannot be using `region + offset` approach in any part of the
keccak_circuit.

Therefore Xi has been refactored to use a `Layouter` in order to
witness it's data.
This makes the implementation of the out-of-circuit primitives for
keccak much closer and similar to the inner circuit ones.

That makes it a lot more confortable to make tests or work within the
codebase.
Co-authored-by: Chih Cheng Liang <chihchengliang@gmail.com>
Adapts the configure fn of `KeccakPermutation` according to: #305
CPerezz and others added 2 commits January 26, 2022 18:27
Since the base conversion table is provided outside but the rho one is internal to the permutation, they both require to be loaded from different sources.

Co-authored-by: Chih Cheng Liang <chihchengliang@gmail.com>
@CPerezz CPerezz changed the title [WIP] Keccak circuit implementation Keccak circuit implementation Jan 27, 2022
@CPerezz CPerezz marked this pull request as ready for review January 27, 2022 11:56
Copy link
Collaborator

@therealyingtong therealyingtong left a comment

Choose a reason for hiding this comment

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

First-pass review.

keccak256/src/gates/absorb.rs Outdated Show resolved Hide resolved
circuit-benchmarks/src/keccak_permutation.rs Outdated Show resolved Hide resolved
circuit-benchmarks/src/keccak_permutation.rs Outdated Show resolved Hide resolved
circuit-benchmarks/src/keccak_permutation.rs Outdated Show resolved Hide resolved
keccak256/src/arith_helpers.rs Show resolved Hide resolved
keccak256/src/gates/absorb.rs Outdated Show resolved Hide resolved
@@ -148,7 +148,7 @@ impl<F: FieldExt> BaseConversionConfig<F> {
region.constrain_equal(input_acc_cell, input_coef_cell)?;
region.constrain_equal(output_acc_cell, output_coef_cell)?;
} else if offset == input_coefs.len() - 1 {
region.constrain_equal(input_acc_cell, input.0)?;
//region.constrain_equal(input_acc_cell, input.0)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this meant to be commented out?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is. We reviewed that with @ChihChengLiang and this leads to a Permutation err as we copy from different cells different rounds.

We should maybe look into this a bit more.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we need to enable that copy constraint to bind the output of the base conversion. I imagine we now have the flow like the following. The

                                                                             | ["Mixing result copies and constraints"]
flag(cell) ------(copy)--> computation_pathA ---> resultA(cell) ---(copy)--> | right_side(cell)
neg_flag(cell) --(copy)--> computation_pathB ---> resultB(cell) ---(copy)--> | left_side(cell)

Copy link
Collaborator

Choose a reason for hiding this comment

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

per offline discussion with @CPerezz, the copy constraint is required, but it might be difficult to enable in this PR. We can enable it in a future PR.

keccak256/src/gates/mixing.rs Outdated Show resolved Hide resolved
keccak256/src/gates/mixing.rs Outdated Show resolved Hide resolved
keccak256/src/gates/mixing.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@ChihChengLiang ChihChengLiang left a comment

Choose a reason for hiding this comment

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

First pass on gates

keccak256/src/gates/mixing.rs Outdated Show resolved Hide resolved
Add missing comments, explanations and spelling errors.

Co-authored-by: ying tong <yingtong@z.cash>
As said by CC:
We should split 3 polys here. Otherwise, the prover can witness values not satisfying individual polys but satisfying the summation poly.
Proof: let x=flag, y=negated_flag
flag_consistency: x + y - 1
bool_constraint for x: x(1-x)
bool_constraint for y: y(1-y)
summation: x + y - 1 + x(1-x) + y(1-y) = 0
falsifying example: x=2 and y=1

Co-authored-by: Chih Cheng Liang <chihchengliang@gmail.com>
@Brechtpd
Copy link
Collaborator

Brechtpd commented Feb 9, 2022

https://github.com/appliedzkp/zkevm-circuits/blob/4732717f959cb407a8b8bf6514dbc79c55b64291/keccak256/src/gates/rho_checks.rs#L459-L479

The step2_acc constraint (with final degree 14) and especially the step3_acc constraint (with final degree 171!!!) make the extended degree of the keccak circuit to be 256x the size of the normal domain, which has an enormous impact on performance and memory use. It will definitely be worth it to lower the degree of these constraints by either doing the checks differently in a way that has a lower degree, or use some extra cells so that the full expression can be split up into multiple sub expressions of a smaller degree.

Removing just these two constraints makes the circuit have a much more reasonable extended domain of just x8 bigger. The max degree of 7 is then caused by lookups (which have expressions up to degree 3 as input, but because they are used in the lookup an extra degree of 4 is added with current halo2 with zk), so probably still possible to get it down further with some additional tweaks, but of course will have much less impact than fixing the two constraints above and may end up not being worth it.

@ChihChengLiang
Copy link
Collaborator

@Brechtpd Nice catch for the performance bottleneck. Do you think using a lookup table check for step2_acc and step3_acc would improve the performance?

Copy link
Collaborator

@therealyingtong therealyingtong left a comment

Choose a reason for hiding this comment

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

LGTM modulo some small suggestions.

Another nitpick: I think we should avoid prefixing variable names with an underscore _, because that makes the compiler not warn us when these variables are unused.

keccak256/src/circuit.rs Outdated Show resolved Hide resolved
keccak256/src/gates/mixing.rs Outdated Show resolved Hide resolved
// start of the loop.
state = {
// TODO: That could be a Fixed column.
// Witness 1 for the activation flag.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I think we should make this a Selector.

Copy link
Collaborator

@ChihChengLiang ChihChengLiang left a comment

Choose a reason for hiding this comment

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

Found some undeleted comments

keccak256/src/keccak_arith.rs Outdated Show resolved Hide resolved
keccak256/src/keccak_arith.rs Outdated Show resolved Hide resolved
keccak256/src/keccak_arith.rs Outdated Show resolved Hide resolved
keccak256/src/keccak_arith.rs Outdated Show resolved Hide resolved
@github-actions github-actions bot added crate-bus-mapping Issues related to the bus-mapping workspace member T-opcode Type: opcode-related and focused PR/Issue labels Feb 14, 2022
@CPerezz
Copy link
Member Author

CPerezz commented Feb 14, 2022

Found some undeleted comments

Sorry @ChihChengLiang as mentioned in private, this was the result of pushing some debugging lines that I was not planning to push.

I've force-pushed so that the history is clean now.

@github-actions github-actions bot removed crate-bus-mapping Issues related to the bus-mapping workspace member T-opcode Type: opcode-related and focused PR/Issue labels Feb 14, 2022
@CPerezz CPerezz merged commit 898ff56 into main Feb 14, 2022
@CPerezz CPerezz deleted the keccak_circuit branch February 14, 2022 10:05
@CPerezz CPerezz mentioned this pull request Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate-keccak Issues related to the keccak workspace member
Projects
None yet
4 participants