1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#![doc = include_str!("../README.md")]
#![no_std]
#![deny(missing_docs)]

pub use primeorder::{
    self,
    elliptic_curve::{
        self,
        bigint::{self, rand_core},
        ff,
        generic_array::{self, typenum},
    },
};

use bigint::U256;
use elliptic_curve::{
    scalar::{FromUintUnchecked, ScalarPrimitive},
    Curve, CurveArithmetic, PrimeCurve,
};
use primeorder::PrimeCurveParams;

use self::core::{field_element::FieldElementCore, scalar::ScalarCore, W};

pub mod constants;
pub mod core;

/// Field element (unsigned integer mod $p$)
pub type FieldElement = W<FieldElementCore>;
/// Scalar (unsigned integer mod $n$)
pub type Scalar = W<ScalarCore>;
/// Affine point on stark curve
pub type AffinePoint = primeorder::AffinePoint<StarkCurve>;
/// Projective point on stark curve
pub type ProjectivePoint = primeorder::ProjectivePoint<StarkCurve>;

/// Stark curve
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Eq, Ord)]
pub struct StarkCurve;

impl Curve for StarkCurve {
    type FieldBytesSize = typenum::U32;
    type Uint = U256;

    const ORDER: Self::Uint =
        U256::from_be_hex("0800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f");
}

impl PrimeCurve for StarkCurve {}

impl CurveArithmetic for StarkCurve {
    type Scalar = Scalar;
    type AffinePoint = AffinePoint;
    type ProjectivePoint = ProjectivePoint;
}

impl PrimeCurveParams for StarkCurve {
    type FieldElement = FieldElement;
    type PointArithmetic = primeorder::point_arithmetic::EquationAIsGeneric;

    const EQUATION_A: Self::FieldElement = constants::EQUATION_A;
    const EQUATION_B: Self::FieldElement = constants::EQUATION_B;

    const GENERATOR: (Self::FieldElement, Self::FieldElement) = constants::GENERATOR;
}

impl elliptic_curve::FieldBytesEncoding<StarkCurve> for U256 {}

impl From<Scalar> for ScalarPrimitive<StarkCurve> {
    fn from(s: Scalar) -> Self {
        ScalarPrimitive::from_uint_unchecked(s.to_uint())
    }
}

impl From<&Scalar> for ScalarPrimitive<StarkCurve> {
    fn from(s: &Scalar) -> Self {
        ScalarPrimitive::from_uint_unchecked(s.to_uint())
    }
}