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

🗼 Tower Extensions (Fq2, Fq6, and Fq12) and Torus Compression for BN254 precompiles #38

Draft
wants to merge 76 commits into
base: dl-precompiles
Choose a base branch
from

Conversation

ZamDimon
Copy link

@ZamDimon ZamDimon commented Mar 29, 2024

What ❔

This pull request primarily focuses on helper gadgets for EC pairing implementation under the constraint system.

To implement pairing, we introduce the following new functionality:

  • Tower extension $\mathbb{F}_{p^2} \mapsto \mathbb{F}_{p^6} \mapsto \mathbb{F}_{p^{12}}$ under the constraint system.
  • Twisted elliptic curve $E'(\mathbb{F}_{p^2})$ support.
  • Consequently, we implement the NonNativeField<F, T> trait over $\mathbb{F}_{p^k}$ extensions and basic arithmetic for all of the extensions.
  • Torus compression ($\mathbb{T}_2$) that makes final exponentiation step more R1CS-friendly.

Why ❔

  1. Currently, no EC pairing is implemented under the constraint system.
  2. Field extensions are implemented outside the constraint system (see here); however, no such functionality is available under the constraint system.

Checklist

  • PR title corresponds to the body of PR (we generate changelog entries from PRs).
  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • Code has been formatted via zk fmt and zk lint.

ZamDimon and others added 28 commits March 12, 2024 16:12
…-trait-impls`

🗼 Tower Extension Implementation
@ZamDimon ZamDimon marked this pull request as draft March 29, 2024 16:00
@NikitaMasych
Copy link

Hey, @jules, I have added fixes for the above notes from you, though since we moved BN254-specific functionality to era-zkevm_circuits, you can check the changes in conjugated PR

@ZamDimon ZamDimon changed the title 👫 BN254 EC Pairing implementation under Boojum constraint system 🗼 Tower Extensions (Fq2, Fq6, and Fq12) and Torus Compression for BN254 precompiles May 22, 2024
@ZamDimon
Copy link
Author

Since we moved the BN254-specific implementations outside this repo (namely, we moved them here), we rename this pull request to include information about gadgets only. We also close other pull requests not related to gadgets.

@jules jules changed the base branch from main to dl-precompiles May 23, 2024 13:26
Copy link
Member

@jules jules left a comment

Choose a reason for hiding this comment

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

looking great guys, just a few final notes :)

// We need this to ensure no conflicting implementations without negative impls

#[derive(Derivative)]
#[derivative(Clone, Copy, Debug, Hash)]
#[derive(Derivative, Serialize, PartialEq)]
Copy link
Member

Choose a reason for hiding this comment

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

what was the reason for adding serialize functionality to this type?

Choose a reason for hiding this comment

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

In circuits for ec_pairing we use internal FSM state with Fq12 as intermediate value element, thus it was needed to add it here because otherwise it wouldn't get compiled

Comment on lines +238 to +247
for i in BitIterator::new(exponent) {
if found_one {
result = result.square::<CS, SAFE>(cs);
} else {
found_one = i;
}

if i {
result = result.mul::<CS, SAFE>(cs, self);
}
Copy link
Member

Choose a reason for hiding this comment

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

this is non-homogenous and needs to be updated to use conditional selection gates

Comment on lines 255 to 257
if power % 2 == 0 {
return self.clone();
}
Copy link
Member

Choose a reason for hiding this comment

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

also non-homogenous in case the power can variate between circuits

Comment on lines +86 to +95
for i in BitIterator::new(exponent) {
if found_one {
result = result.square(cs);
} else {
found_one = i;
}

if i {
result = result.mul(cs, self);
}
Copy link
Member

Choose a reason for hiding this comment

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

same thing here, seems like this will cause divergent circuits based on input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants