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
pub(crate) mod decode_g1;
pub(crate) mod decode_g2;
pub(crate) mod decode_fp;
pub(crate) mod decode_utils;

#[macro_use]
pub(crate) mod api_specialization_macro;

mod g1_ops;
mod g2_ops;
mod pairing_ops;

pub mod sane_limits;
pub mod constants;

pub use pairing_ops::{PairingApi, PublicPairingApi};
pub use g1_ops::{G1Api, PublicG1Api};
pub use g2_ops::{G2Api, PublicG2Api};

mod unified_api;
pub use self::unified_api::{OperationType, perform_operation, PREALLOCATE_FOR_ERROR_BYTES, PREALLOCATE_FOR_RESULT_BYTES};
pub use crate::errors::ApiError;

#[cfg(feature = "c_api")]
mod c_api;
#[cfg(feature = "c_api")]
pub use self::c_api::{c_perform_operation};

#[cfg(feature = "eip_2537")]
pub mod eip2537;

pub struct API;

impl API {
    pub fn run(bytes: &[u8]) -> Result<Vec<u8>, ApiError> {
        use decode_utils::split;
        use constants::*;

        let (op_type, rest) = split(bytes, OPERATION_ENCODING_LENGTH , "Input should be longer than operation type encoding")?;

        match op_type[0] {
            OPERATION_G1_ADD => {
                PublicG1Api::add_points(&rest)
            },
            OPERATION_G1_MUL => {
                PublicG1Api::mul_point(&rest)
            },
            OPERATION_G1_MULTIEXP => {
                PublicG1Api::multiexp(&rest)
            },
            OPERATION_G2_ADD => {
                PublicG2Api::add_points(&rest)
            },
            OPERATION_G2_MUL => {
                PublicG2Api::mul_point(&rest)
            },
            OPERATION_G2_MULTIEXP => {
                PublicG2Api::multiexp(&rest)
            },
            OPERATION_PAIRING => {
                PublicPairingApi::pair(&rest)
            },
            _ => {
                return Err(ApiError::InputError("Unknown operation type".to_owned()));
            }
        }
    }
}