Skip to content

Commit 2eb4b9d

Browse files
authored
Devnet4 fix-avx512 (#194)
* poseidon avx2 avx512 (#192) Co-authored-by: Tom Wambsgans <TomWambsgans@users.noreply.github.com> * fix avx512 (panicked on small instances) (#193) * fix avx512 (panicked on small instances) * add `test_aggregation` --------- Co-authored-by: Tom Wambsgans <TomWambsgans@users.noreply.github.com> * fmt --------- Co-authored-by: Tom Wambsgans <TomWambsgans@users.noreply.github.com>
1 parent 2dc7867 commit 2eb4b9d

10 files changed

Lines changed: 239 additions & 151 deletions

File tree

crates/backend/koala-bear/src/monty_31/aarch64_neon/poseidon_helpers.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use core::mem::transmute;
88
use super::exp_small;
99
use crate::{FieldParameters, MontyParameters, PackedMontyField31Neon, PackedMontyParameters, RelativelyPrimePower};
1010

11+
// Convenience alias to match the naming used for the AVX2/AVX512 helpers.
12+
pub(crate) use convert_to_vec_neg_form_neon as convert_to_vec_neg_form;
13+
1114
/// A specialized representation of the Poseidon state for a width of 16.
1215
///
1316
/// Splits the state into `s0` (undergoes S-box) and `s_hi` (undergoes only linear transforms),
@@ -79,3 +82,16 @@ where
7982
*val = PackedMontyField31Neon::<PMP>::from_vector(output);
8083
}
8184
}
85+
86+
/// Applies the S-Box `x -> x^D` to a packed vector. Output is in canonical form.
87+
#[inline(always)]
88+
pub(crate) fn sbox<PMP, const D: u64>(val: PackedMontyField31Neon<PMP>) -> PackedMontyField31Neon<PMP>
89+
where
90+
PMP: PackedMontyParameters + FieldParameters + RelativelyPrimePower<D>,
91+
{
92+
unsafe {
93+
let signed = val.to_signed_vector();
94+
let out = exp_small::<PMP, D>(signed);
95+
PackedMontyField31Neon::<PMP>::from_vector(out)
96+
}
97+
}

crates/backend/koala-bear/src/monty_31/x86_64_avx2/poseidon_helpers.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
use core::arch::x86_64::{self, __m256i};
66
use core::mem::transmute;
77

8-
use crate::{
9-
MontyParameters, PackedMontyField31AVX2, PackedMontyParameters, apply_func_to_even_odd, packed_exp_3, packed_exp_5,
10-
packed_exp_7,
11-
};
8+
use super::{apply_func_to_even_odd, packed_exp_3, packed_exp_5, packed_exp_7};
9+
use crate::{MontyParameters, PackedMontyField31AVX2, PackedMontyParameters};
1210

1311
/// A specialized representation of the Poseidon state for a width of 16.
1412
///
@@ -34,29 +32,6 @@ impl<PMP: PackedMontyParameters> InternalLayer16<PMP> {
3432
}
3533
}
3634

37-
/// A specialized representation of the Poseidon state for a width of 24.
38-
///
39-
/// Same split as `InternalLayer16` but for width 24.
40-
#[derive(Clone, Copy)]
41-
#[repr(C)]
42-
pub struct InternalLayer24<PMP: PackedMontyParameters> {
43-
pub(crate) s0: PackedMontyField31AVX2<PMP>,
44-
pub(crate) s_hi: [__m256i; 23],
45-
}
46-
47-
impl<PMP: PackedMontyParameters> InternalLayer24<PMP> {
48-
#[inline]
49-
pub(crate) unsafe fn to_packed_field_array(self) -> [PackedMontyField31AVX2<PMP>; 24] {
50-
unsafe { transmute(self) }
51-
}
52-
53-
#[inline]
54-
#[must_use]
55-
pub(crate) fn from_packed_field_array(vector: [PackedMontyField31AVX2<PMP>; 24]) -> Self {
56-
unsafe { transmute(vector) }
57-
}
58-
}
59-
6035
/// Use hard coded methods to compute `x -> x^D` for the even index entries and small `D`.
6136
/// Inputs should be signed 32-bit integers in `[-P, ..., P]`.
6237
/// Outputs will also be signed integers in `(-P, ..., P)` stored in the odd indices.
@@ -94,3 +69,16 @@ pub(crate) fn add_rc_and_sbox<PMP: PackedMontyParameters, const D: u64>(
9469
*val = PackedMontyField31AVX2::<PMP>::from_vector(output);
9570
}
9671
}
72+
73+
/// Applies the S-Box `x -> x^D` to a packed vector. Output is in canonical form.
74+
#[inline(always)]
75+
#[must_use]
76+
pub(crate) fn sbox<PMP: PackedMontyParameters, const D: u64>(
77+
val: PackedMontyField31AVX2<PMP>,
78+
) -> PackedMontyField31AVX2<PMP> {
79+
unsafe {
80+
let vec = val.to_vector();
81+
let out = apply_func_to_even_odd::<PMP>(vec, exp_small::<PMP, D>);
82+
PackedMontyField31AVX2::<PMP>::from_vector(out)
83+
}
84+
}

crates/backend/koala-bear/src/monty_31/x86_64_avx512/poseidon_helpers.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
use core::arch::x86_64::{self, __m512i};
66
use core::mem::transmute;
77

8-
use crate::{
9-
MontyParameters, PackedMontyField31AVX512, PackedMontyParameters, apply_func_to_even_odd, packed_exp_3,
10-
packed_exp_5, packed_exp_7,
11-
};
8+
use super::{apply_func_to_even_odd, packed_exp_3, packed_exp_5, packed_exp_7};
9+
use crate::{MontyParameters, PackedMontyField31AVX512, PackedMontyParameters};
1210

1311
/// A specialized representation of the Poseidon state for a width of 16.
1412
///
@@ -34,29 +32,6 @@ impl<PMP: PackedMontyParameters> InternalLayer16<PMP> {
3432
}
3533
}
3634

37-
/// A specialized representation of the Poseidon state for a width of 24.
38-
///
39-
/// Same split as `InternalLayer16` but for width 24.
40-
#[derive(Clone, Copy)]
41-
#[repr(C)]
42-
pub struct InternalLayer24<PMP: PackedMontyParameters> {
43-
pub(crate) s0: PackedMontyField31AVX512<PMP>,
44-
pub(crate) s_hi: [__m512i; 23],
45-
}
46-
47-
impl<PMP: PackedMontyParameters> InternalLayer24<PMP> {
48-
#[inline]
49-
pub(crate) unsafe fn to_packed_field_array(self) -> [PackedMontyField31AVX512<PMP>; 24] {
50-
unsafe { transmute(self) }
51-
}
52-
53-
#[inline]
54-
#[must_use]
55-
pub(crate) fn from_packed_field_array(vector: [PackedMontyField31AVX512<PMP>; 24]) -> Self {
56-
unsafe { transmute(vector) }
57-
}
58-
}
59-
6035
/// Use hard coded methods to compute `x -> x^D` for the even index entries and small `D`.
6136
/// Inputs should be signed 32-bit integers in `[-P, ..., P]`.
6237
/// Outputs will also be signed integers in `(-P, ..., P)` stored in the odd indices.
@@ -94,3 +69,16 @@ pub(crate) fn add_rc_and_sbox<PMP: PackedMontyParameters, const D: u64>(
9469
*val = PackedMontyField31AVX512::<PMP>::from_vector(output);
9570
}
9671
}
72+
73+
/// Applies the S-Box `x -> x^D` to a packed vector. Output is in canonical form.
74+
#[inline(always)]
75+
#[must_use]
76+
pub(crate) fn sbox<PMP: PackedMontyParameters, const D: u64>(
77+
val: PackedMontyField31AVX512<PMP>,
78+
) -> PackedMontyField31AVX512<PMP> {
79+
unsafe {
80+
let vec = val.to_vector();
81+
let out = apply_func_to_even_odd::<PMP>(vec, exp_small::<PMP, D>);
82+
PackedMontyField31AVX512::<PMP>::from_vector(out)
83+
}
84+
}

0 commit comments

Comments
 (0)