sp_crypto_ec_utils/
pallas.rs1use crate::utils::{self, HostcallResult, FAIL_MSG};
21use alloc::vec::Vec;
22use ark_ec::{AffineRepr, CurveConfig, CurveGroup};
23use ark_pallas_ext::CurveHooks;
24use sp_runtime_interface::{
25 pass_by::{PassFatPointerAndRead, PassFatPointerAndWrite},
26 runtime_interface,
27};
28
29pub type PallasConfig = ark_pallas_ext::PallasConfig<HostHooks>;
31pub type Affine = ark_pallas_ext::Affine<HostHooks>;
33pub type Projective = ark_pallas_ext::Projective<HostHooks>;
35
36pub type ScalarField = <PallasConfig as CurveConfig>::ScalarField;
38
39#[derive(Copy, Clone)]
41pub struct HostHooks;
42
43impl CurveHooks for HostHooks {
44 fn msm(bases: &[Affine], scalars: &[ScalarField]) -> Projective {
45 let mut out = utils::buffer_for::<Affine>();
46 host_calls::pallas_msm(&utils::encode(bases), &utils::encode(scalars), &mut out)
47 .and_then(|_| utils::decode::<Affine>(&out))
48 .expect(FAIL_MSG)
49 .into_group()
50 }
51
52 fn mul_projective(base: &Projective, scalar: &[u64]) -> Projective {
53 let mut out = utils::buffer_for::<Affine>();
54 host_calls::pallas_mul(&utils::encode(base.into_affine()), &utils::encode(scalar), &mut out)
55 .and_then(|_| utils::decode::<Affine>(&out))
56 .expect(FAIL_MSG)
57 .into_group()
58 }
59}
60
61#[runtime_interface]
69pub trait HostCalls {
70 fn pallas_msm(
77 bases: PassFatPointerAndRead<&[u8]>,
78 scalars: PassFatPointerAndRead<&[u8]>,
79 out: PassFatPointerAndWrite<&mut [u8]>,
80 ) -> HostcallResult {
81 utils::msm_sw::<ark_pallas::PallasConfig>(bases, scalars, out)
82 }
83
84 fn pallas_mul(
91 base: PassFatPointerAndRead<&[u8]>,
92 scalar: PassFatPointerAndRead<&[u8]>,
93 out: PassFatPointerAndWrite<&mut [u8]>,
94 ) -> HostcallResult {
95 utils::mul_sw::<ark_pallas::PallasConfig>(base, scalar, out)
96 }
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102 use crate::utils::testing::*;
103
104 #[test]
105 fn mul_works() {
106 mul_test::<Affine, ark_pallas::Affine>();
107 }
108
109 #[test]
110 fn msm_works() {
111 msm_test::<Affine, ark_pallas::Affine>();
112 }
113}