use crate::{
syscall_bn254_add, syscall_bn254_double,
utils::{AffinePoint, WeierstrassAffinePoint, WeierstrassPoint},
};
pub const N: usize = 16;
#[derive(Copy, Clone)]
#[repr(align(4))]
pub struct Bn254Point(pub WeierstrassPoint<N>);
impl WeierstrassAffinePoint<N> for Bn254Point {
fn infinity() -> Self {
Self(WeierstrassPoint::Infinity)
}
fn is_infinity(&self) -> bool {
matches!(self.0, WeierstrassPoint::Infinity)
}
}
impl AffinePoint<N> for Bn254Point {
const GENERATOR: [u32; N] = [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0];
fn new(limbs: [u32; N]) -> Self {
Self(WeierstrassPoint::Affine(limbs))
}
fn limbs_ref(&self) -> &[u32; N] {
match &self.0 {
WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"),
WeierstrassPoint::Affine(limbs) => limbs,
}
}
fn limbs_mut(&mut self) -> &mut [u32; N] {
match &mut self.0 {
WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"),
WeierstrassPoint::Affine(limbs) => limbs,
}
}
fn complete_add_assign(&mut self, other: &Self) {
self.weierstrass_add_assign(other);
}
fn add_assign(&mut self, other: &Self) {
let a = self.limbs_mut();
let b = other.limbs_ref();
unsafe {
syscall_bn254_add(a, b);
}
}
fn double(&mut self) {
let a = self.limbs_mut();
unsafe {
syscall_bn254_double(a);
}
}
}