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

Circle starks #924

Open
wants to merge 75 commits into
base: main
Choose a base branch
from
Open

Circle starks #924

wants to merge 75 commits into from

Conversation

ColoCarletti
Copy link
Member

@ColoCarletti ColoCarletti commented Oct 4, 2024

Circle Starks

This PR introduces the foundational components necessary for the Circle Starks protocol implementation.

Key Features:

  • Mersenne-31 field: Incorporates the Mersenne-31 field, its quadratic and quartic extensions, along with all required operations.
  • Circle group: Defines the circle group and implements operations between its elements.
  • Sets and cosets: Generates sets and cosets to construct the necessary domains within the circle group.
  • CFFT: Implements the Circle Fast Fourier Transform (CFFT) for both interpolation and evaluation of the bivariate polynomial used in the Circle Starks protocol.

Type of change

  • New feature

Checklist

  • Linked to Github Issue
  • Unit tests added
  • This change requires new documentation.
    • Documentation has been added/updated.
  • This change is an Optimization
    • Benchmarks added/run

Copy link
Collaborator

@ilitteri ilitteri left a comment

Choose a reason for hiding this comment

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

The PR looks good, left some nits, comments, and suggestions. In general, I'd try to remove every change for the code to panic (even if we know for certain that indexing in some index will always be valid).

math/src/circle/polynomial.rs Outdated Show resolved Hide resolved
Comment on lines 72 to 74
pub fn zero() -> Self {
Self::new(FieldElement::one(), FieldElement::zero()).unwrap()
}
Copy link
Collaborator

@ilitteri ilitteri Oct 17, 2024

Choose a reason for hiding this comment

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

This suggestion is not formatted, I recommend not to commit this one.

Note

I'm not sure whether FieldElement::one() or FieldElement::zero() are const (they should though). If they're not, you'd need to hardcode the values to make this function const.

Suggested change
pub fn zero() -> Self {
Self::new(FieldElement::one(), FieldElement::zero()).unwrap()
}
pub const fn zero() -> Self {
CirclePoint { x: FieldElement::one(), y: FieldElement::zero() }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a const

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good catch! Just updated my suggestion

Copy link
Contributor

Choose a reason for hiding this comment

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

FieldElement::one() and FieldElement::zero() aren't const. I understand they should be, but to fix it we have to change it in every field of Lambdaworks. Maybe we can do it in another PR. If we wanted to hardcode the values, we should add them as parameters for every field we wanted to implement the Circle, I don't know if that is what you are suggesting.

Copy link
Collaborator

@ilitteri ilitteri Oct 17, 2024

Choose a reason for hiding this comment

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

CirclePoint UX and code's readability could be very much improved if we implement:

For CirclePoint addition (this would replace add implementation):

  • Add<&CirclePoint<F>> for &CirclePoint<F>.
  • Add<CirclePoint<F>> for CirclePoint<F>.
  • Add<CirclePoint<F>> for &CirclePoint<F>.
  • Add<&CirclePoint<F>> for CirclePoint<F>.
  • AddAssign<&CirclePoint<F>> for &CirclePoint<F>.
  • AddAssign<CirclePoint<F>> for CirclePoint<F>.
  • AddAssign<CirclePoint<F>> for &CirclePoint<F>.
  • AddAssign<&CirclePoint<F>> for CirclePoint<F>.

For CirclePoint scalar multiplication (this would replace scalar_mul implementation):

  • Mul<u128> for CirclePoint<F>.
  • Mul<u128> for &CirclePoint<F>.
  • MulAssign<u128> for CirclePoint<F>.
  • MulAssign<u128> for &CirclePoint<F>.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok! We replaced add for Add for &CirclePoint<F> and scalar_mul for Mul<u128> for CirclePoint<F>. Should we implement the other ones you mentioned here so we can add non-referenced values, multiply referenced values, etc.?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes!

math/src/circle/point.rs Outdated Show resolved Hide resolved
math/src/circle/point.rs Outdated Show resolved Hide resolved
math/src/circle/point.rs Outdated Show resolved Hide resolved
math/src/circle/point.rs Outdated Show resolved Hide resolved
math/src/circle/errors.rs Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Oct 24, 2024

Codecov Report

Attention: Patch coverage is 97.95597% with 13 lines in your changes missing coverage. Please review.

Project coverage is 71.66%. Comparing base (3bb1fa1) to head (fc68bea).

Files with missing lines Patch % Lines
math/src/circle/cosets.rs 88.00% 6 Missing ⚠️
math/src/field/fields/mersenne31/extensions.rs 0.00% 6 Missing ⚠️
math/src/circle/twiddles.rs 98.18% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #924      +/-   ##
==========================================
+ Coverage   71.12%   71.66%   +0.53%     
==========================================
  Files         144      149       +5     
  Lines       31880    32516     +636     
==========================================
+ Hits        22676    23302     +626     
- Misses       9204     9214      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

/// As a result we obtain the coefficients of the polynomial in the basis: {1, y, x, xy, 2xˆ2 -1, 2xˆ2y-y, 2xˆ3-x, 2xˆ3y-xy,...}
/// Note that eval has to be a vector of length a power of two 2^n.
#[cfg(feature = "alloc")]
pub fn interpolate_cfft(
Copy link
Contributor

Choose a reason for hiding this comment

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

As an optimization, we could just send the twiddles as input to the function. This is useful when we have to interpolate multiple columns

/// returns the evaluation of the polynomial on the points of the standard coset of size 2^n.
/// Note that coeff has to be a vector with length a power of two 2^n.
#[cfg(feature = "alloc")]
pub fn evaluate_cfft(
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar comment to the one after this one. Twiddles could be given as inputs

pub fn order_cfft_result_naive(
input: &mut [FieldElement<Mersenne31Field>],
) -> Vec<FieldElement<Mersenne31Field>> {
let mut result = Vec::new();
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we can use interleave

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.

5 participants