Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 21 additions & 91 deletions syscall/bn254-syscall/src/addition.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use {
crate::{
swap_endianness, Endianness, PodG1, PodG2, ALT_BN128_FIELD_SIZE, ALT_BN128_FQ2_SIZE,
ALT_BN128_G1_POINT_SIZE, ALT_BN128_G2_POINT_SIZE, G1, G2,
},
ark_serialize::{CanonicalSerialize, Compress},
use crate::{
serialize_g1, serialize_g2, Endianness, PodG1, PodG2, ALT_BN128_G1_POINT_SIZE,
ALT_BN128_G2_POINT_SIZE,
};

/// Input size for the g1 add operation.
pub const ALT_BN128_G1_ADDITION_INPUT_SIZE: usize = ALT_BN128_G1_POINT_SIZE * 2; // 128
/// Input size for the g1 add operation (128 bytes).
pub const ALT_BN128_G1_ADDITION_INPUT_SIZE: usize = ALT_BN128_G1_POINT_SIZE * 2;

/// Input size for the g2 add operation.
pub const ALT_BN128_G2_ADDITION_INPUT_SIZE: usize = ALT_BN128_G2_POINT_SIZE * 2; // 256
/// Input size for the g2 add operation (256 bytes).
pub const ALT_BN128_G2_ADDITION_INPUT_SIZE: usize = ALT_BN128_G2_POINT_SIZE * 2;

/// The enum is used to version changes to the `alt_bn128_versioned_g1_addition` function.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VersionedG1Addition {
V0,
}

/// The enum is used to version changes to the `alt_bn128_versioned_g2_addition` function.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VersionedG2Addition {
V0,
}
Expand All @@ -37,53 +36,14 @@ pub enum VersionedG2Addition {
/// for strict guidelines on SIMD approvals and versioning.
pub fn alt_bn128_versioned_g1_addition(
_version: VersionedG1Addition,
input: &[u8],
p: &PodG1,
q: &PodG1,
endianness: Endianness,
) -> Option<[u8; ALT_BN128_G1_POINT_SIZE]> {
let is_valid_len = match endianness {
Endianness::BE => input.len() <= ALT_BN128_G1_ADDITION_INPUT_SIZE,
Endianness::LE => input.len() == ALT_BN128_G1_ADDITION_INPUT_SIZE,
};
) -> Option<PodG1> {
let p = p.deserialize_affine(endianness)?;
let q = q.deserialize_affine(endianness)?;

if !is_valid_len {
return None;
}

let mut padded_input = [0u8; ALT_BN128_G1_ADDITION_INPUT_SIZE];
padded_input[..input.len()].copy_from_slice(input);

let (p_bytes, q_bytes) = padded_input.split_at(ALT_BN128_G1_POINT_SIZE);

let (p, q) = match endianness {
Endianness::BE => (
PodG1::from_be_bytes(p_bytes)?.into_affine()?,
PodG1::from_be_bytes(q_bytes)?.into_affine()?,
),
Endianness::LE => (
PodG1::from_le_bytes(p_bytes)?.into_affine()?,
PodG1::from_le_bytes(q_bytes)?.into_affine()?,
),
};

let result_point_affine: G1 = (p + q).into();

let mut result_point_data = [0u8; ALT_BN128_G1_POINT_SIZE];
result_point_affine
.x
.serialize_with_mode(&mut result_point_data[..ALT_BN128_FIELD_SIZE], Compress::No)
.ok()?;
result_point_affine
.y
.serialize_with_mode(&mut result_point_data[ALT_BN128_FIELD_SIZE..], Compress::No)
.ok()?;

match endianness {
Endianness::BE => Some(swap_endianness::<
ALT_BN128_FIELD_SIZE,
ALT_BN128_G1_POINT_SIZE,
>(result_point_data)),
Endianness::LE => Some(result_point_data),
}
serialize_g1((p + q).into(), endianness)
}

/// The implementation of the `sol_alt_bn128_group_op` syscall G2 addition operation
Expand All @@ -102,42 +62,12 @@ pub fn alt_bn128_versioned_g1_addition(
/// guidelines on SIMD approvals and versioning.
pub fn alt_bn128_versioned_g2_addition(
_version: VersionedG2Addition,
input: &[u8],
p: &PodG2,
q: &PodG2,
endianness: Endianness,
) -> Option<[u8; ALT_BN128_G2_POINT_SIZE]> {
if input.len() != ALT_BN128_G2_ADDITION_INPUT_SIZE {
return None;
}

let (p_bytes, q_bytes) = input.split_at(ALT_BN128_G2_POINT_SIZE);

let (p, q) = match endianness {
Endianness::BE => (
PodG2::from_be_bytes(p_bytes)?.into_affine_unchecked()?,
PodG2::from_be_bytes(q_bytes)?.into_affine_unchecked()?,
),
Endianness::LE => (
PodG2::from_le_bytes(p_bytes)?.into_affine_unchecked()?,
PodG2::from_le_bytes(q_bytes)?.into_affine_unchecked()?,
),
};

let result_point_affine: G2 = (p + q).into();

let mut result_point_data = [0u8; ALT_BN128_G2_POINT_SIZE];
result_point_affine
.x
.serialize_with_mode(&mut result_point_data[..ALT_BN128_FQ2_SIZE], Compress::No)
.ok()?;
result_point_affine
.y
.serialize_with_mode(&mut result_point_data[ALT_BN128_FQ2_SIZE..], Compress::No)
.ok()?;
) -> Option<PodG2> {
let p = p.deserialize_affine_unchecked(endianness)?;
let q = q.deserialize_affine_unchecked(endianness)?;

match endianness {
Endianness::BE => {
Some(swap_endianness::<ALT_BN128_FQ2_SIZE, ALT_BN128_G2_POINT_SIZE>(result_point_data))
}
Endianness::LE => Some(result_point_data),
}
serialize_g2((p + q).into(), endianness)
}
Loading
Loading