Skip to content

Commit

Permalink
add errors.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ColoCarletti committed Oct 30, 2024
1 parent 9ff3233 commit e0fa390
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 67 deletions.
4 changes: 4 additions & 0 deletions math/src/circle/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[derive(Debug)]
pub enum CircleError {
PointDoesntSatisfyCircleEquation,
}
1 change: 1 addition & 0 deletions math/src/circle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod cfft;
pub mod cosets;
pub mod errors;
pub mod point;
pub mod polynomial;
pub mod twiddles;
129 changes: 62 additions & 67 deletions math/src/circle/point.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::errors::CircleError;
use crate::field::traits::IsField;
use crate::field::{
element::FieldElement,
Expand All @@ -9,16 +10,73 @@ use core::ops::{Add, Mul};
/// 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)

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

#[derive(Debug)]
pub enum CircleError {
PointDoesntSatisfyCircleEquation,
impl<F: IsField + HasCircleParams<F>> CirclePoint<F> {
pub fn new(x: FieldElement<F>, y: FieldElement<F>) -> Result<Self, CircleError> {
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;
}

/// Parameters of the base field that we'll need to define its Circle.
Expand Down Expand Up @@ -119,69 +177,6 @@ impl<F: IsField + HasCircleParams<F>> Mul<u128> for CirclePoint<F> {
}
}

impl<F: IsField + HasCircleParams<F>> CirclePoint<F> {
pub fn new(x: FieldElement<F>, y: FieldElement<F>) -> Result<Self, CircleError> {
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;
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit e0fa390

Please sign in to comment.