-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a950fc
commit 494fd27
Showing
7 changed files
with
140 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use std::arch::x86_64::__m512i; | ||
|
||
use itertools::Itertools; | ||
|
||
use super::blake2s_avx::{compress16, set1, transpose_msgs, untranspose_states}; | ||
use super::{AVX512Backend, VECS_LOG_SIZE}; | ||
use crate::commitment_scheme::blake2_hash::{Blake2sHash, Blake2sHasher}; | ||
use crate::commitment_scheme::ops::MerkleOps; | ||
use crate::core::backend::{Col, ColumnOps}; | ||
use crate::core::fields::m31::BaseField; | ||
|
||
impl ColumnOps<Blake2sHash> for AVX512Backend { | ||
type Column = Vec<Blake2sHash>; | ||
|
||
fn bit_reverse_column(_column: &mut Self::Column) { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
impl MerkleOps<Blake2sHasher> for AVX512Backend { | ||
fn commit_on_layer( | ||
log_size: u32, | ||
prev_layer: Option<&Vec<Blake2sHash>>, | ||
columns: &[&Col<AVX512Backend, BaseField>], | ||
) -> Vec<Blake2sHash> { | ||
// Pad prev_layer if too small. | ||
let mut padded_buffer = vec![]; | ||
let prev_layer = if log_size < 4 { | ||
prev_layer.map(|prev_layer| { | ||
padded_buffer = prev_layer | ||
.iter() | ||
.copied() | ||
.chain(std::iter::repeat(Blake2sHash::default())) | ||
.collect_vec(); | ||
&padded_buffer | ||
}) | ||
} else { | ||
prev_layer | ||
}; | ||
|
||
// Commit to columns. | ||
let mut res = Vec::with_capacity(1 << log_size); | ||
for i in 0..(1 << (log_size - VECS_LOG_SIZE as u32)) { | ||
let mut state: [__m512i; 8] = unsafe { std::mem::zeroed() }; | ||
// Hash prev_layer. | ||
if let Some(prev_layer) = prev_layer { | ||
let ptr = prev_layer[(i << 5)..(i << 5) + 32].as_ptr() as *const __m512i; | ||
let msgs: [__m512i; 16] = std::array::from_fn(|j| unsafe { *ptr.add(j) }); | ||
state = unsafe { | ||
compress16( | ||
state, | ||
transpose_msgs(msgs), | ||
set1(0), | ||
set1(0), | ||
set1(0), | ||
set1(0), | ||
) | ||
}; | ||
} | ||
|
||
// Hash columns in chunks of 16. | ||
let mut col_chunk_iter = columns.array_chunks(); | ||
for col_chunk in &mut col_chunk_iter { | ||
let msgs = col_chunk.map(|column| column.data[i].0); | ||
state = unsafe { compress16(state, msgs, set1(0), set1(0), set1(0), set1(0)) }; | ||
} | ||
|
||
// Hash remaining columns. | ||
let remainder = col_chunk_iter.remainder(); | ||
if !remainder.is_empty() { | ||
let msgs = remainder | ||
.iter() | ||
.map(|column| column.data[i].0) | ||
.chain(std::iter::repeat(unsafe { set1(0) })) | ||
.take(16) | ||
.collect_vec() | ||
.try_into() | ||
.unwrap(); | ||
state = unsafe { compress16(state, msgs, set1(0), set1(0), set1(0), set1(0)) }; | ||
} | ||
let state: [Blake2sHash; 16] = | ||
unsafe { std::mem::transmute(untranspose_states(state)) }; | ||
res.extend_from_slice(&state); | ||
} | ||
res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use itertools::Itertools; | ||
|
||
use crate::commitment_scheme::blake2_hash::{Blake2sHash, Blake2sHasher}; | ||
use crate::commitment_scheme::ops::{MerkleHasher, MerkleOps}; | ||
use crate::core::backend::CPUBackend; | ||
use crate::core::fields::m31::BaseField; | ||
|
||
impl MerkleOps<Blake2sHasher> for CPUBackend { | ||
fn commit_on_layer( | ||
log_size: u32, | ||
prev_layer: Option<&Vec<Blake2sHash>>, | ||
columns: &[&Vec<BaseField>], | ||
) -> Vec<Blake2sHash> { | ||
(0..(1 << log_size)) | ||
.map(|i| { | ||
Blake2sHasher::hash_node( | ||
prev_layer.map(|prev_layer| (prev_layer[2 * i], prev_layer[2 * i + 1])), | ||
&columns.iter().map(|column| column[i]).collect_vec(), | ||
) | ||
}) | ||
.collect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod blake2s; | ||
mod circle; | ||
mod fri; | ||
pub mod quotients; | ||
|