solana_curve25519/curve_syscall_traits.rs
1//! The traits representing the basic elliptic curve operations.
2//!
3//! These traits are instantiatable by all the commonly used elliptic curves and should help in
4//! organizing syscall support for other curves in the future. more complicated or curve-specific
5//! functions that are needed in cryptographic applications should be representable by combining
6//! the associated functions of these traits.
7//!
8//! NOTE: This module temporarily lives in zk_token_sdk/curve25519, but it is independent of
9//! zk-token-sdk or curve25519. It should be moved to a more general location in the future.
10//!
11
12// Functions are organized by the curve traits, which can be instantiated by multiple curve
13// representations. The functions take in a `curve_id` (e.g. `CURVE25519_EDWARDS`) and should run
14// the associated functions in the appropriate trait instantiation. The `curve_op` function
15// additionally takes in an `op_id` (e.g. `ADD`) that controls which associated functions to run in
16// `GroupOperations`.
17
18pub trait PointValidation {
19 type Point;
20
21 /// Verifies if a byte representation of a curve point lies in the curve.
22 fn validate_point(&self) -> bool;
23}
24
25pub trait GroupOperations {
26 type Point;
27 type Scalar;
28
29 /// Adds two curve points: P_0 + P_1.
30 fn add(left_point: &Self::Point, right_point: &Self::Point) -> Option<Self::Point>;
31
32 /// Subtracts two curve points: P_0 - P_1.
33 ///
34 /// NOTE: Altneratively, one can consider replacing this with a `negate` function that maps a
35 /// curve point P -> -P. Then subtraction can be computed by combining `negate` and `add`
36 /// syscalls. However, `subtract` is a much more widely used function than `negate`.
37 fn subtract(left_point: &Self::Point, right_point: &Self::Point) -> Option<Self::Point>;
38
39 /// Multiplies a scalar S with a curve point P: S*P
40 fn multiply(scalar: &Self::Scalar, point: &Self::Point) -> Option<Self::Point>;
41}
42
43pub trait MultiScalarMultiplication {
44 type Scalar;
45 type Point;
46
47 /// Given a vector of scalsrs S_1, ..., S_N, and curve points P_1, ..., P_N, computes the
48 /// "inner product": S_1*P_1 + ... + S_N*P_N.
49 ///
50 /// NOTE: This operation can be represented by combining `add` and `multiply` functions in
51 /// `GroupOperations`, but computing it in a single batch is significantly cheaper. Given how
52 /// commonly used the multiscalar multiplication (MSM) is, it seems to make sense to have a
53 /// designated trait for MSM support.
54 ///
55 /// NOTE: The inputs to the function is a non-fixed size vector and hence, there are some
56 /// complications in computing the cost for the syscall. The computational costs should only
57 /// depend on the length of the vectors (and the curve), so it would be ideal to support
58 /// variable length inputs and compute the syscall cost as is done in eip-197:
59 /// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-197.md#gas-costs>. If not, then we can
60 /// consider bounding the length of the input and assigning worst-case cost.
61 fn multiscalar_multiply(
62 scalars: &[Self::Scalar],
63 points: &[Self::Point],
64 ) -> Option<Self::Point>;
65}
66
67pub trait Pairing {
68 type G1Point;
69 type G2Point;
70 type GTPoint;
71
72 /// Applies the bilinear pairing operation to two curve points P1, P2 -> e(P1, P2). This trait
73 /// is only relevant for "pairing-friendly" curves such as BN254 and BLS12-381.
74 fn pairing_map(
75 left_point: &Self::G1Point,
76 right_point: &Self::G2Point,
77 ) -> Option<Self::GTPoint>;
78}
79
80pub const CURVE25519_EDWARDS: u64 = 0;
81pub const CURVE25519_RISTRETTO: u64 = 1;
82
83pub const ADD: u64 = 0;
84pub const SUB: u64 = 1;
85pub const MUL: u64 = 2;