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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! This module contains a useful trait for recursion: [KimchiCurve],
//! which defines how a pair of curves interact.

use ark_ec::{short_weierstrass_jacobian::GroupAffine, AffineCurve, ModelParameters};
use mina_curves::pasta::curves::{
    pallas::{LegacyPallasParameters, PallasParameters},
    vesta::{LegacyVestaParameters, VestaParameters},
};
use mina_poseidon::poseidon::ArithmeticSpongeParams;
use once_cell::sync::Lazy;
use poly_commitment::{
    commitment::{CommitmentCurve, EndoCurve},
    srs::endos,
};

/// Represents additional information that a curve needs in order to be used with Kimchi
pub trait KimchiCurve: CommitmentCurve + EndoCurve {
    /// A human readable name.
    const NAME: &'static str;

    /// Provides the sponge params to be used with this curve.
    fn sponge_params() -> &'static ArithmeticSpongeParams<Self::ScalarField>;

    /// Provides the sponge params to be used with the other curve.
    fn other_curve_sponge_params() -> &'static ArithmeticSpongeParams<Self::BaseField>;

    /// Provides the coefficients for the curve endomorphism, called (q,r) in some places.
    fn endos() -> &'static (Self::BaseField, Self::ScalarField);

    /// Provides the coefficient for the curve endomorphism over the other field, called q in some
    /// places.
    fn other_curve_endo() -> &'static Self::ScalarField;

    /// Accessor for the other curve's prime subgroup generator, as coordinates
    // TODO: This leaked from snarky.rs. Stop the bleed.
    fn other_curve_prime_subgroup_generator() -> (Self::ScalarField, Self::ScalarField);
}

fn vesta_endos() -> &'static (
    <VestaParameters as ModelParameters>::BaseField,
    <VestaParameters as ModelParameters>::ScalarField,
) {
    static VESTA_ENDOS: Lazy<(
        <VestaParameters as ModelParameters>::BaseField,
        <VestaParameters as ModelParameters>::ScalarField,
    )> = Lazy::new(endos::<GroupAffine<VestaParameters>>);
    &VESTA_ENDOS
}

fn pallas_endos() -> &'static (
    <PallasParameters as ModelParameters>::BaseField,
    <PallasParameters as ModelParameters>::ScalarField,
) {
    static PALLAS_ENDOS: Lazy<(
        <PallasParameters as ModelParameters>::BaseField,
        <PallasParameters as ModelParameters>::ScalarField,
    )> = Lazy::new(endos::<GroupAffine<PallasParameters>>);
    &PALLAS_ENDOS
}

impl KimchiCurve for GroupAffine<VestaParameters> {
    const NAME: &'static str = "vesta";

    fn sponge_params() -> &'static ArithmeticSpongeParams<Self::ScalarField> {
        mina_poseidon::pasta::fp_kimchi::static_params()
    }

    fn other_curve_sponge_params() -> &'static ArithmeticSpongeParams<Self::BaseField> {
        mina_poseidon::pasta::fq_kimchi::static_params()
    }

    fn endos() -> &'static (Self::BaseField, Self::ScalarField) {
        vesta_endos()
    }

    fn other_curve_endo() -> &'static Self::ScalarField {
        &pallas_endos().0
    }

    fn other_curve_prime_subgroup_generator() -> (Self::ScalarField, Self::ScalarField) {
        GroupAffine::<PallasParameters>::prime_subgroup_generator()
            .to_coordinates()
            .unwrap()
    }
}

impl KimchiCurve for GroupAffine<PallasParameters> {
    const NAME: &'static str = "pallas";

    fn sponge_params() -> &'static ArithmeticSpongeParams<Self::ScalarField> {
        mina_poseidon::pasta::fq_kimchi::static_params()
    }

    fn other_curve_sponge_params() -> &'static ArithmeticSpongeParams<Self::BaseField> {
        mina_poseidon::pasta::fp_kimchi::static_params()
    }

    fn endos() -> &'static (Self::BaseField, Self::ScalarField) {
        pallas_endos()
    }

    fn other_curve_endo() -> &'static Self::ScalarField {
        &vesta_endos().0
    }

    fn other_curve_prime_subgroup_generator() -> (Self::ScalarField, Self::ScalarField) {
        GroupAffine::<VestaParameters>::prime_subgroup_generator()
            .to_coordinates()
            .unwrap()
    }
}

//
// Legacy curves
//

impl KimchiCurve for GroupAffine<LegacyVestaParameters> {
    const NAME: &'static str = "legacy_vesta";

    fn sponge_params() -> &'static ArithmeticSpongeParams<Self::ScalarField> {
        mina_poseidon::pasta::fp_legacy::static_params()
    }

    fn other_curve_sponge_params() -> &'static ArithmeticSpongeParams<Self::BaseField> {
        mina_poseidon::pasta::fq_legacy::static_params()
    }

    fn endos() -> &'static (Self::BaseField, Self::ScalarField) {
        vesta_endos()
    }

    fn other_curve_endo() -> &'static Self::ScalarField {
        &pallas_endos().0
    }

    fn other_curve_prime_subgroup_generator() -> (Self::ScalarField, Self::ScalarField) {
        GroupAffine::<PallasParameters>::prime_subgroup_generator()
            .to_coordinates()
            .unwrap()
    }
}

impl KimchiCurve for GroupAffine<LegacyPallasParameters> {
    const NAME: &'static str = "legacy_pallas";

    fn sponge_params() -> &'static ArithmeticSpongeParams<Self::ScalarField> {
        mina_poseidon::pasta::fq_legacy::static_params()
    }

    fn other_curve_sponge_params() -> &'static ArithmeticSpongeParams<Self::BaseField> {
        mina_poseidon::pasta::fp_legacy::static_params()
    }

    fn endos() -> &'static (Self::BaseField, Self::ScalarField) {
        pallas_endos()
    }

    fn other_curve_endo() -> &'static Self::ScalarField {
        &vesta_endos().0
    }

    fn other_curve_prime_subgroup_generator() -> (Self::ScalarField, Self::ScalarField) {
        GroupAffine::<VestaParameters>::prime_subgroup_generator()
            .to_coordinates()
            .unwrap()
    }
}

#[cfg(feature = "bn254")]
use mina_poseidon::dummy_values::kimchi_dummy;

#[cfg(feature = "bn254")]
impl KimchiCurve for GroupAffine<ark_bn254::g1::Parameters> {
    const NAME: &'static str = "bn254";

    fn sponge_params() -> &'static ArithmeticSpongeParams<Self::ScalarField> {
        // TODO: Generate some params
        static PARAMS: Lazy<ArithmeticSpongeParams<ark_bn254::Fr>> = Lazy::new(kimchi_dummy);
        &PARAMS
    }

    fn other_curve_sponge_params() -> &'static ArithmeticSpongeParams<Self::BaseField> {
        // TODO: Generate some params
        static PARAMS: Lazy<ArithmeticSpongeParams<ark_bn254::Fq>> = Lazy::new(kimchi_dummy);
        &PARAMS
    }

    fn endos() -> &'static (Self::BaseField, Self::ScalarField) {
        static ENDOS: Lazy<(ark_bn254::Fq, ark_bn254::Fr)> =
            Lazy::new(endos::<ark_bn254::G1Affine>);
        &ENDOS
    }

    fn other_curve_endo() -> &'static Self::ScalarField {
        // TODO: Dummy value, this is definitely not right
        static ENDO: Lazy<ark_bn254::Fr> = Lazy::new(|| 13u64.into());
        &ENDO
    }

    fn other_curve_prime_subgroup_generator() -> (Self::ScalarField, Self::ScalarField) {
        // TODO: Dummy value, this is definitely not right
        (44u64.into(), 88u64.into())
    }
}