Files
pezkuwi-sdk/bizinikiwi/primitives/crypto/ec-utils/src/ed_on_bls12_377.rs
T
pezkuwichain b6d35f6faf chore: add Dijital Kurdistan Tech Institute to copyright headers
Updated 4763 files with dual copyright:
- Parity Technologies (UK) Ltd.
- Dijital Kurdistan Tech Institute
2025-12-27 21:28:36 +03:00

96 lines
3.4 KiB
Rust

// This file is part of Bizinikiwi.
// Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! *Ed-on-BLS12-377* types and host functions.
use crate::utils;
use alloc::vec::Vec;
use ark_ec::CurveConfig;
use ark_ed_on_bls12_377_ext::CurveHooks;
use pezsp_runtime_interface::{
pass_by::{AllocateAndReturnByCodec, PassFatPointerAndRead},
runtime_interface,
};
/// Curve hooks jumping into [`host_calls`] host functions.
#[derive(Copy, Clone)]
pub struct HostHooks;
/// Group configuration.
pub type EdwardsConfig = ark_ed_on_bls12_377_ext::EdwardsConfig<HostHooks>;
/// Twisted Edwards form point affine representation.
pub type EdwardsAffine = ark_ed_on_bls12_377_ext::EdwardsAffine<HostHooks>;
/// Twisted Edwards form point projective representation.
pub type EdwardsProjective = ark_ed_on_bls12_377_ext::EdwardsProjective<HostHooks>;
impl CurveHooks for HostHooks {
fn msm(
bases: &[EdwardsAffine],
scalars: &[<EdwardsConfig as CurveConfig>::ScalarField],
) -> EdwardsProjective {
host_calls::ed_on_bls12_377_te_msm(utils::encode(bases), utils::encode(scalars))
.and_then(|res| utils::decode_proj_te(res))
.unwrap_or_default()
}
fn mul_projective(base: &EdwardsProjective, scalar: &[u64]) -> EdwardsProjective {
host_calls::ed_on_bls12_377_te_mul_projective(
utils::encode_proj_te(base),
utils::encode(scalar),
)
.and_then(|res| utils::decode_proj_te(res))
.unwrap_or_default()
}
}
/// Interfaces for working with *Arkworks* *Ed-on-BLS12-377* elliptic curve
/// related types from within the runtime.
///
/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
///
/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
/// and "not-compressed".
#[runtime_interface]
pub trait HostCalls {
/// Twisted Edwards multi scalar multiplication for *Ed-on-BLS12-377*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<EdwardsProjective>`.
/// - `scalars`: `ArkScale<Vec<EdwardsConfig::ScalarField>>`.
/// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
fn ed_on_bls12_377_te_msm(
bases: PassFatPointerAndRead<Vec<u8>>,
scalars: PassFatPointerAndRead<Vec<u8>>,
) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
utils::msm_te::<ark_ed_on_bls12_377::EdwardsConfig>(bases, scalars)
}
/// Twisted Edwards projective multiplication for *Ed-on-BLS12-377*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<EdwardsProjective>`.
/// - `scalar`: `ArkScale<Vec<u64>>`.
/// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
fn ed_on_bls12_377_te_mul_projective(
base: PassFatPointerAndRead<Vec<u8>>,
scalar: PassFatPointerAndRead<Vec<u8>>,
) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
utils::mul_projective_te::<ark_ed_on_bls12_377::EdwardsConfig>(base, scalar)
}
}