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
use core::ops::{AddAssign, Mul};
use ff::Field;
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
pub struct IsogenyCoefficients<F: Field + AddAssign + Mul<Output = F>> {
pub xnum: &'static [F],
pub xden: &'static [F],
pub ynum: &'static [F],
pub yden: &'static [F],
}
pub trait Isogeny: Field + AddAssign + Mul<Output = Self> {
type Degree: ArrayLength<Self>;
const COEFFICIENTS: IsogenyCoefficients<Self>;
fn isogeny(x: Self, y: Self) -> (Self, Self) {
let mut xs = GenericArray::<Self, Self::Degree>::default();
xs[0] = Self::one();
xs[1] = x;
xs[2] = x.square();
for i in 3..Self::Degree::to_usize() {
xs[i] = xs[i - 1] * x;
}
let x_num = Self::compute_iso(&xs, Self::COEFFICIENTS.xnum);
let x_den = Self::compute_iso(&xs, Self::COEFFICIENTS.xden)
.invert()
.unwrap();
let y_num = Self::compute_iso(&xs, Self::COEFFICIENTS.ynum) * y;
let y_den = Self::compute_iso(&xs, Self::COEFFICIENTS.yden)
.invert()
.unwrap();
(x_num * x_den, y_num * y_den)
}
fn compute_iso(xxs: &[Self], k: &[Self]) -> Self {
let mut xx = Self::zero();
for (xi, ki) in xxs.iter().zip(k.iter()) {
xx += *xi * ki;
}
xx
}
}