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

Merged
merged 86 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
852f856
optimize add
Sep 18, 2024
0d68798
save changes. Add, sub and mul checked
Sep 18, 2024
5ebc30a
fix tests
ColoCarletti Sep 19, 2024
81439ae
add new inv
ColoCarletti Sep 19, 2024
47cf7da
add mult by powers of two
Sep 19, 2024
2acfdd7
Merge branch 'optimize_mersenne31' of github.com:lambdaclass/lambdawo…
Sep 19, 2024
cfba8bc
replace inverse
Sep 19, 2024
01eba0d
test new inv
ColoCarletti Sep 20, 2024
60fd981
modify old algorithm for inv
ColoCarletti Sep 20, 2024
996e224
fix tests extension
Sep 20, 2024
c41b977
Merge branch 'optimize_mersenne31' of github.com:lambdaclass/lambdawo…
Sep 20, 2024
28d8b0e
add mul for degree 4 extension
Sep 20, 2024
5e1f533
add fp4 isField and isSubField operations and benchmarks
Sep 23, 2024
fde7faa
new version for fp4 mul based on the paper
Sep 23, 2024
60c8197
add mul of a fp2e by non-residue
Sep 23, 2024
25b8869
change inv using mul_fp2_by_non_resiude
Sep 23, 2024
1187be8
save work
Sep 24, 2024
ecff11e
wip fp2 test
ColoCarletti Sep 24, 2024
a6169ea
Merge branch 'optimize_mersenne31' of github.com:lambdaclass/lambdawo…
ColoCarletti Sep 24, 2024
ae1446a
add fp2 tests
ColoCarletti Sep 24, 2024
fa12fc5
add 2 * a^2 - 1 function
Sep 24, 2024
461485b
use karatsuba in fp4 mul version 1
Sep 25, 2024
d4e3f40
clean up
ColoCarletti Sep 25, 2024
f0437c7
fix Fp as subfield of Fp2. Tests Fp plus Fp4 is now correct
Sep 25, 2024
2c5a301
fix inv
ColoCarletti Sep 26, 2024
4a42dbc
fix comments
ColoCarletti Sep 26, 2024
21d09c6
create crate
ColoCarletti Sep 26, 2024
f7efd90
Revert "create crate"
ColoCarletti Sep 26, 2024
db3ab30
Merge branch 'optimize_mersenne31' into circle-starks
ColoCarletti Sep 26, 2024
eddd9be
add circle point implementation
ColoCarletti Sep 26, 2024
ba738c7
add group order
ColoCarletti Sep 26, 2024
85d80c1
rm dependencie
ColoCarletti Sep 26, 2024
14a7beb
add cosets
ColoCarletti Sep 30, 2024
b56d212
add twiddle
ColoCarletti Oct 1, 2024
58aeb0c
init cfft
ColoCarletti Oct 1, 2024
a85eb0f
test cfft
ColoCarletti Oct 2, 2024
6bda70d
test 16 not working
Oct 2, 2024
7cbd173
fix n16 test
ColoCarletti Oct 2, 2024
4f60a48
refactor
ColoCarletti Oct 3, 2024
35a9d5a
Merge branch 'main' into circle-starks
ColoCarletti Oct 4, 2024
95e87d0
clippy
ColoCarletti Oct 4, 2024
10636f9
fmt
ColoCarletti Oct 4, 2024
c76793f
Merge branch 'main' into circle-starks
diegokingston Oct 4, 2024
876459d
clippy
ColoCarletti Oct 4, 2024
dcdd18a
Merge branch 'circle-starks' of github.com:lambdaclass/lambdaworks in…
ColoCarletti Oct 4, 2024
076b683
rm std
ColoCarletti Oct 4, 2024
0d7d89f
add alloc
ColoCarletti Oct 4, 2024
9685a74
fix
ColoCarletti Oct 4, 2024
ac842bf
wip
ColoCarletti Oct 7, 2024
52655b1
Merge branch 'main' into circle-starks
ColoCarletti Oct 8, 2024
5f97990
add hand iterpolation for 4 and 8
ColoCarletti Oct 14, 2024
dc4124e
wip
ColoCarletti Oct 15, 2024
56dae17
evaluation and interpolation working
Oct 16, 2024
2546ca2
add tests and comments
Oct 16, 2024
57735cf
Merge branch 'main' into circle-starks
ColoCarletti Oct 17, 2024
da5fae7
clippy
ColoCarletti Oct 17, 2024
1166a42
Merge branch 'circle-starks' of github.com:lambdaclass/lambdaworks in…
ColoCarletti Oct 17, 2024
28ab71f
fmt
ColoCarletti Oct 17, 2024
e0c666d
remove unused functions
ColoCarletti Oct 17, 2024
3073470
add comment
ColoCarletti Oct 17, 2024
37aae9e
Update math/src/circle/polynomial.rs
nicole-graus Oct 23, 2024
4436e28
change generator and order CirclePoint functions as constants
Oct 23, 2024
835a565
impl eq as PartialEq
Oct 23, 2024
2ea3171
implement scalar_mul as Mul
Oct 23, 2024
c9acb74
Change error name to a more descriptive one and move it to point.rs
Oct 23, 2024
93fd515
fix lint
Oct 23, 2024
7ae4990
fix lint
Oct 23, 2024
8801317
fix some comments
Oct 23, 2024
7fe6a7d
Merge branch 'main' into circle-starks
diegokingston Oct 24, 2024
8b2b3d2
add alloc::vec
ColoCarletti Oct 24, 2024
ebc5e9d
fix no-std
ColoCarletti Oct 24, 2024
f3028e5
cargo fmt
ColoCarletti Oct 24, 2024
b645a7b
fix no-std
ColoCarletti Oct 24, 2024
c7cfd8f
remove macro
ColoCarletti Oct 24, 2024
fc68bea
add comment
ColoCarletti Oct 25, 2024
0a50704
addition between referenced and non-referenced values
Oct 28, 2024
a045516
Update math/src/circle/point.rs
nicole-graus Oct 30, 2024
c00a223
explain why won't panic
Oct 30, 2024
9ff3233
Merge branch 'circle-starks' of github.com:lambdaclass/lambdaworks in…
Oct 30, 2024
e0fa390
add errors.rs
ColoCarletti Oct 30, 2024
3ed8ac1
fix vec
Oct 30, 2024
a7161f2
Evaluate and interpolate functions have non-mutable inputs
Oct 30, 2024
b88794f
fix clippy
Oct 30, 2024
b2e9b9d
MulAssign for points and double function takes a reference
Oct 30, 2024
26d6078
Revert "MulAssign for points and double function takes a reference"
Oct 31, 2024
a6738c7
MulAssign and AddAssign
Oct 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions math/src/circle/cosets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::circle::point::CirclePoint;
use crate::field::fields::mersenne31::field::Mersenne31Field;
use alloc::vec::Vec;

/// Given g_n, a generator of the subgroup <g_n> of the circle of size n,
/// and given a shift, that is a another point of the cirvle,
/// Given g_n, a generator of the subgroup of size n of the circle, i.e. <g_n>,
/// and given a shift, that is a another point of the circle,
/// we define the coset shift + <g_n> which is the set of all the points in
/// <g_n> plus the shift.
/// For example, if <g_4> = {p1, p2, p3, p4}, then g_8 + <g_4> = {g_8 + p1, g_8 + p2, g_8 + p3, g_8 + p4}.
Expand Down
177 changes: 107 additions & 70 deletions math/src/circle/point.rs
nicole-graus marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,82 +1,24 @@
use super::errors::CircleError;
use crate::field::traits::IsField;
use crate::field::{
element::FieldElement,
fields::mersenne31::{extensions::Degree4ExtensionField, field::Mersenne31Field},
};
use core::ops::{Add, Mul};
use core::ops::{Add, AddAssign, Mul, MulAssign};

/// Given a Field F, we implement here the Group which consists of all the points (x, y) such as
/// x in F, y in F and x^2 + y^2 = 1, i.e. the Circle. The operation of the group will have
/// additive notation and is as follows:
/// (a, b) + (c, d) = (a * c - b * d, a * d + b * c)
/// (a, b) + (c, d) = (a * c - b * d, a * d + b * c).

#[derive(Debug, Clone)]
pub struct CirclePoint<F: IsField> {
pub x: FieldElement<F>,
pub y: FieldElement<F>,
}

impl<F: IsField + HasCircleParams<F>> CirclePoint<F> {
pub fn new(x: FieldElement<F>, y: FieldElement<F>) -> Result<Self, CircleError> {
ilitteri marked this conversation as resolved.
Show resolved Hide resolved
if x.square() + y.square() == FieldElement::one() {
Ok(Self { x, y })
} else {
Err(CircleError::PointDoesntSatisfyCircleEquation)
}
}

/// Neutral element of the Circle group (with additive notation).
pub fn zero() -> Self {
Self::new(FieldElement::one(), FieldElement::zero()).unwrap()
}

/// Computes 2(x, y) = (2x^2 - 1, 2xy).
pub fn double(self) -> Self {
Self::new(
self.x.square().double() - FieldElement::one(),
self.x.double() * self.y,
)
.unwrap()
}

/// Computes 2^n * (x, y).
pub fn repeated_double(self, n: u32) -> Self {
let mut res = self;
for _ in 0..n {
res = res.double();
}
res
}

/// Computes the inverse of the point.
/// We are using -(x, y) = (x, -y), i.e. the inverse of the group opertion is conjugation
/// because the norm of every point in the circle is one.
pub fn conjugate(self) -> Self {
Self {
x: self.x,
y: -self.y,
}
}

pub fn antipode(self) -> Self {
Self {
x: -self.x,
y: -self.y,
}
}

pub const GENERATOR: Self = Self {
x: F::CIRCLE_GENERATOR_X,
y: F::CIRCLE_GENERATOR_Y,
};

/// Returns the generator of the subgroup of order n = 2^log_2_size.
/// We are using that 2^k * g is a generator of the subgroup of order 2^{31 - k}.
pub fn get_generator_of_subgroup(log_2_size: u32) -> Self {
Self::GENERATOR.repeated_double(31 - log_2_size)
}

pub const ORDER: u128 = F::ORDER;
#[derive(Debug)]
pub enum CircleError {
PointDoesntSatisfyCircleEquation,
}

/// Parameters of the base field that we'll need to define its Circle.
Expand Down Expand Up @@ -136,7 +78,7 @@ impl<F: IsField + HasCircleParams<F>> Add for &CirclePoint<F> {
CirclePoint { x, y }
}
}
impl<F: IsField + HasCircleParams<F>> Add<CirclePoint<F>> for CirclePoint<F> {
impl<F: IsField + HasCircleParams<F>> Add for CirclePoint<F> {
type Output = CirclePoint<F>;
fn add(self, rhs: CirclePoint<F>) -> Self::Output {
&self + &rhs
Expand All @@ -154,28 +96,123 @@ impl<F: IsField + HasCircleParams<F>> Add<&CirclePoint<F>> for CirclePoint<F> {
&self + rhs
}
}
impl<F: IsField + HasCircleParams<F>> AddAssign<&CirclePoint<F>> for CirclePoint<F> {
fn add_assign(&mut self, rhs: &CirclePoint<F>) {
*self = &*self + rhs;
}
}
impl<F: IsField + HasCircleParams<F>> AddAssign<CirclePoint<F>> for CirclePoint<F> {
fn add_assign(&mut self, rhs: CirclePoint<F>) {
*self += &rhs;
}
}

/// Multiplication between a point and a scalar (i.e. group operation repeatedly):
/// (x, y) * n = (x ,y) + ... + (x, y) n-times.
impl<F: IsField + HasCircleParams<F>> Mul<u128> for CirclePoint<F> {
impl<F: IsField + HasCircleParams<F>> Mul<u128> for &CirclePoint<F> {
type Output = CirclePoint<F>;

fn mul(self, scalar: u128) -> Self {
fn mul(self, scalar: u128) -> Self::Output {
let mut scalar = scalar;
let mut res = Self::zero();
let mut cur = self;
let mut res = CirclePoint::<F>::zero();
let mut cur = self.clone();
loop {
if scalar == 0 {
return res;
}
if scalar & 1 == 1 {
res = &res + &cur;
res += &cur;
}
cur = cur.double();
scalar >>= 1;
}
}
}
impl<F: IsField + HasCircleParams<F>> Mul<u128> for CirclePoint<F> {
type Output = CirclePoint<F>;
fn mul(self, scalar: u128) -> Self::Output {
&self * scalar
}
}
impl<F: IsField + HasCircleParams<F>> MulAssign<u128> for CirclePoint<F> {
fn mul_assign(&mut self, scalar: u128) {
let mut scalar = scalar;
let mut res = CirclePoint::<F>::zero();
loop {
if scalar == 0 {
*self = res.clone();
}
if scalar & 1 == 1 {
res += &*self;
}
*self = self.double();
scalar >>= 1;
}
}
}

impl<F: IsField + HasCircleParams<F>> CirclePoint<F> {
pub fn new(x: FieldElement<F>, y: FieldElement<F>) -> Result<Self, CircleError> {
ilitteri marked this conversation as resolved.
Show resolved Hide resolved
if x.square() + y.square() == FieldElement::one() {
Ok(Self { x, y })
} else {
Err(CircleError::PointDoesntSatisfyCircleEquation)
}
}

/// Neutral element of the Circle group (with additive notation).
pub fn zero() -> Self {
Self::new(FieldElement::one(), FieldElement::zero()).unwrap()
}

/// Computes 2(x, y) = (2x^2 - 1, 2xy).
pub fn double(&self) -> Self {
Self::new(
self.x.square().double() - FieldElement::one(),
self.x.double() * self.y.clone(),
)
.unwrap()
}

/// Computes 2^n * (x, y).
pub fn repeated_double(self, n: u32) -> Self {
let mut res = self;
for _ in 0..n {
res = res.double();
}
res
}

/// Computes the inverse of the point.
/// We are using -(x, y) = (x, -y), i.e. the inverse of the group opertion is conjugation
/// because the norm of every point in the circle is one.
pub fn conjugate(self) -> Self {
Self {
x: self.x,
y: -self.y,
}
}

pub fn antipode(self) -> Self {
Self {
x: -self.x,
y: -self.y,
}
}

pub const GENERATOR: Self = Self {
x: F::CIRCLE_GENERATOR_X,
y: F::CIRCLE_GENERATOR_Y,
};

/// Returns the generator of the subgroup of order n = 2^log_2_size.
/// We are using that 2^k * g is a generator of the subgroup of order 2^{31 - k}.
pub fn get_generator_of_subgroup(log_2_size: u32) -> Self {
Self::GENERATOR.repeated_double(31 - log_2_size)
}

pub const ORDER: u128 = F::ORDER;
}

#[cfg(test)]
mod tests {
Expand Down
Loading