ziskos 1.1.0-alpha

Guest runtime and entrypoint for programs targeting the ZisK zkVM
//! syscall_bls12_381_curve_add system call interception

#[cfg(zisk_guest)]
use crate::ziskos_syscall;

#[cfg(zisk_guest)]
use core::arch::asm;

use super::point::SyscallPoint384;

#[derive(Debug)]
#[repr(C)]
pub struct SyscallBls12_381CurveAddParams<'a> {
    pub p1: &'a mut SyscallPoint384,
    pub p2: &'a SyscallPoint384,
}

/// Performs the addition of two points on the BLS12-381 curve, storing the result in the first point.
///
/// `Bls12_381CurveAdd` operates on two points, each with two coordinates of 384 bits.
/// Each coordinate is represented as an array of six `u64` elements.
/// The syscall takes as a parameter the address of a structure containing points `p1` and `p2`.
/// The result of the addition is stored in `p1`.
///
/// ### Safety
///
/// The caller must ensure that the data is aligned to a 64-bit boundary.
///
/// The caller must ensure that both `p1` and `p2` are finite points on the BLS12-381 curve.
/// The point at infinity is not allowed.
///
/// The caller must ensure that `p1` is neither equal to `p2` nor the negation of `p2`
/// (i.e. `p1 ≠ p2` and `p1 ≠ -p2`).
///
/// The caller must ensure that the coordinates of `p1` and `p2` are canonical representatives
/// of elements in the BLS12-381 base field.
///
/// The resulting point will have both coordinates reduced to canonical representatives
/// in the range of the BLS12-381 base field.
#[allow(unused_variables)]
#[cfg_attr(not(feature = "hints"), no_mangle)]
#[cfg_attr(feature = "hints", export_name = "hints_syscall_bls12_381_curve_add")]
pub extern "C" fn syscall_bls12_381_curve_add(
    params: &mut SyscallBls12_381CurveAddParams,
    #[cfg(feature = "hints")] hints: &mut Vec<u64>,
) {
    #[cfg(zisk_guest)]
    ziskos_syscall!(zisk_definitions::SYSCALL_BLS12_381_CURVE_ADD_ID, params);
    #[cfg(not(zisk_guest))]
    {
        let p1 = [params.p1.x, params.p1.y].concat().try_into().unwrap();
        let p2 = [params.p2.x, params.p2.y].concat().try_into().unwrap();
        let mut p3: [u64; 12] = [0; 12];
        zisk_precomp_helpers::bls12_381_curve_add(&p1, &p2, &mut p3);
        params.p1.x.copy_from_slice(&p3[0..6]);
        params.p1.y.copy_from_slice(&p3[6..12]);
        #[cfg(feature = "hints")]
        {
            hints.extend_from_slice(&p3);
        }
    }
}