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
//! Miscellaneous helpers and re-exports from `dock_crypto_utils`.

use alloc::{format, string::String, vec::Vec};

use ark_ff::PrimeField;

#[cfg(feature = "parallel")]
use rayon::prelude::*;

use ark_std::{cfg_into_iter, cfg_iter};

use schnorr_pok::error::SchnorrError;

pub mod with_schnorr_and_blindings;
pub mod with_schnorr_response;

pub use iter::*;
pub use try_iter::*;
pub use utils::{
    aliases::*,
    extend_some::*,
    iter::{self, *},
    misc::*,
    owned_pairs::*,
    pairs::*,
    try_iter::{self, *},
};
pub use with_schnorr_and_blindings::*;
pub use with_schnorr_response::*;

use utils::{impl_indexed_iter, impl_into_indexed_iter, join};

/// TODO remove when `SchnorrError` will derive `Eq`, `PartialEq`, `Clone`
pub fn schnorr_error(err: SchnorrError) -> String {
    format!("{:?}", err)
}

/// `l_{i ∈ S} = \prod_{j ∈ S, j != i}((0 - j) / (i - j))`
#[allow(non_snake_case)]
pub fn lagrange_basis_at_0<F: PrimeField>(
    S: impl_into_indexed_iter!(<Item = impl Into<F>>),
) -> impl_indexed_iter!(<Item = F>) {
    let items: Vec<_> = cfg_into_iter!(S).map(Into::into).collect();
    let all_i_prod: F = cfg_iter!(items).product();

    cfg_into_iter!(items.clone()).map(move |i| {
        let (num, den) = join!(all_i_prod / i, {
            let mut prod: F = cfg_iter!(items)
                .filter(|&j| &i != j)
                .map(|&j| j - i)
                .product();
            prod.inverse_in_place().unwrap();

            prod
        });

        num * den
    })
}