acvm_blackbox_solver/
curve_specific_solver.rs

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
use acir::BlackBoxFunc;

use crate::BlackBoxResolutionError;

/// This component will generate outputs for Blackbox function calls where the underlying [`acir::BlackBoxFunc`]
/// doesn't have a canonical Rust implementation.
///
/// Returns an [`BlackBoxResolutionError`] if the backend does not support the given [`acir::BlackBoxFunc`].
pub trait BlackBoxFunctionSolver<F> {
    fn pedantic_solving(&self) -> bool;
    fn multi_scalar_mul(
        &self,
        points: &[F],
        scalars_lo: &[F],
        scalars_hi: &[F],
    ) -> Result<(F, F, F), BlackBoxResolutionError>;
    fn ec_add(
        &self,
        input1_x: &F,
        input1_y: &F,
        input1_infinite: &F,
        input2_x: &F,
        input2_y: &F,
        input2_infinite: &F,
    ) -> Result<(F, F, F), BlackBoxResolutionError>;
    fn poseidon2_permutation(
        &self,
        inputs: &[F],
        len: u32,
    ) -> Result<Vec<F>, BlackBoxResolutionError>;
}

// pedantic_solving: bool
pub struct StubbedBlackBoxSolver(pub bool);

// pedantic_solving enabled by default
impl Default for StubbedBlackBoxSolver {
    fn default() -> StubbedBlackBoxSolver {
        let pedantic_solving = true;
        StubbedBlackBoxSolver(pedantic_solving)
    }
}

impl StubbedBlackBoxSolver {
    fn fail(black_box_function: BlackBoxFunc) -> BlackBoxResolutionError {
        BlackBoxResolutionError::Failed(
            black_box_function,
            format!("{} is not supported", black_box_function.name()),
        )
    }
}

impl<F> BlackBoxFunctionSolver<F> for StubbedBlackBoxSolver {
    fn pedantic_solving(&self) -> bool {
        self.0
    }
    fn multi_scalar_mul(
        &self,
        _points: &[F],
        _scalars_lo: &[F],
        _scalars_hi: &[F],
    ) -> Result<(F, F, F), BlackBoxResolutionError> {
        Err(Self::fail(BlackBoxFunc::MultiScalarMul))
    }
    fn ec_add(
        &self,
        _input1_x: &F,
        _input1_y: &F,
        _input1_infinite: &F,
        _input2_x: &F,
        _input2_y: &F,
        _input2_infinite: &F,
    ) -> Result<(F, F, F), BlackBoxResolutionError> {
        Err(Self::fail(BlackBoxFunc::EmbeddedCurveAdd))
    }
    fn poseidon2_permutation(
        &self,
        _inputs: &[F],
        _len: u32,
    ) -> Result<Vec<F>, BlackBoxResolutionError> {
        Err(Self::fail(BlackBoxFunc::Poseidon2Permutation))
    }
}