Skip to content

Commit

Permalink
Gen random circle point from channel
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmilson committed Nov 17, 2024
1 parent db85303 commit bdf91e2
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion stwo_cairo_verifier/src/circle.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use core::num::traits::one::One;
use core::num::traits::zero::Zero;
use core::num::traits::{WrappingAdd, WrappingSub, WrappingMul};
use stwo_cairo_verifier::channel::{Channel, ChannelImpl};
use stwo_cairo_verifier::fields::cm31::CM31;
use stwo_cairo_verifier::fields::m31::{M31, M31Impl};
use stwo_cairo_verifier::fields::qm31::{QM31Impl, QM31, QM31Trait};
use stwo_cairo_verifier::fields::qm31::{QM31Impl, QM31One, QM31, QM31Trait};
use super::utils::pow;

/// A generator for the circle group over [`M31`].
Expand Down Expand Up @@ -99,10 +100,38 @@ impl CirclePointAdd<F, +Add<F>, +Sub<F>, +Mul<F>, +Drop<F>, +Copy<F>> of Add<Cir
}
}

impl CirclePointNeg<F, +Neg<F>, +Drop<F>, +Copy<F>> of Neg<CirclePoint<F>> {
fn neg(a: CirclePoint<F>) -> CirclePoint<F> {
CirclePoint { x: a.x, y: -a.y }
}
}

pub impl CirclePointM31Impl of CirclePointTrait<M31> {}

#[generate_trait]
pub impl CirclePointQM31AddCirclePointM31Impl of CirclePointQM31AddCirclePointM31Trait {
fn add_circle_point_m31(self: CirclePoint<QM31>, rhs: CirclePoint<M31>) -> CirclePoint<QM31> {
CirclePoint {
x: self.x.mul_m31(rhs.x) - self.y.mul_m31(rhs.y),
y: self.x.mul_m31(rhs.y) + self.y.mul_m31(rhs.x)
}
}
}

pub impl CirclePointQM31Impl of CirclePointTrait<QM31> {}

#[generate_trait]
pub impl ChannelGetRandomCirclePointImpl of ChannelGetRandomCirclePointTrait {
fn get_random_point(ref self: Channel) -> CirclePoint<QM31> {
let t = self.draw_felt();
let t_squared = t * t;
let t_squared_plus_1_inv = (t_squared + QM31One::one()).inverse();
let x = (QM31One::one() - t_squared) * t_squared_plus_1_inv;
let y = (t + t) * t_squared_plus_1_inv;
CirclePoint { x, y }
}
}

impl CirclePointQM31PartialOrd of PartialOrd<CirclePoint<QM31>> {
fn lt(lhs: CirclePoint<QM31>, rhs: CirclePoint<QM31>) -> bool {
lhs.x < rhs.x || (lhs.x == rhs.x && lhs.y < rhs.y)
Expand Down

0 comments on commit bdf91e2

Please sign in to comment.