Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/encoding.nr
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ unconstrained fn __split_into_base4_5bit_slices(encoded: Field) -> [u32; 13] {
/// Split an input Field element `e` (known to be 64 bits) into slices that can be efficiently composed to produce inputs to `EncodedChoose`
unconstrained fn __decompose_e(e: Field) -> [u32; 9] {
let mut r: [u32; 9] = [0; 9];
let mut bytes = e.to_le_bytes::<8>();
let bytes = e.to_le_bytes::<8>();
let b8to14 = bytes[1] & 63;
let b14to16 = bytes[1] >> 6;
let b16to18 = bytes[2] & 3;
Expand Down Expand Up @@ -142,7 +142,7 @@ unconstrained fn __decompose_e(e: Field) -> [u32; 9] {
/// Split an input Field element `a` (known to be 64 bits) into slices that can be efficiently composed to produce inputs to `EncodedMajority`
unconstrained fn __decompose_a(a: Field) -> [u32; 9] {
let mut r: [u32; 9] = [0; 9];
let mut bytes = a.to_le_bytes::<8>();
let bytes = a.to_le_bytes::<8>();
let b24to28 = bytes[3] & 15;
let b28to32 = bytes[3] >> 4;
let b32to34 = bytes[4] & 3;
Expand Down Expand Up @@ -210,7 +210,7 @@ unconstrained fn __decompose_witness(w: Field) -> [u32; 12] {
/// # Cost
/// 40 gates
pub(crate) fn encode_message_extension(w: Field) -> EncodedWitness {
//Safety: split input into slices with the following bit sizes:
// Safety: split input into slices with the following bit sizes:
// 1, 5, 1, 1, 8, 3, 8, 8, 8, 9, 9, 3
// We need to validate the correctness of these slice claims by asserting their sum equals `w`,
// and we also need to validate the bit range of each slice
Expand Down
13 changes: 7 additions & 6 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn sha512_compression(_input: INT_BLOCK, _state: STATE) -> STATE {
/// Input is the message to be hashed, produces a 64-byte output
/// The `IS_SHA512` generic is used to toggle whether a SHA384 or SHA512 hash is desired
/// (the only diff between SHA384/SHA512 are different initial starting constants, and the SHA384 hash output is truncated)
fn digest<let N: u32, let IS_SHA512: u1>(msg: [u8; N]) -> [u8; 64] {
fn digest<let N: u32, let IS_SHA512: u8>(msg: [u8; N]) -> [u8; 64] {
// noir-fmt:ignore
let mut h: [u64; 8] = if IS_SHA512 == 1 {
SHA512_INITIAL_HASH_VALUES
Expand Down Expand Up @@ -121,7 +121,8 @@ fn digest<let N: u32, let IS_SHA512: u1>(msg: [u8; N]) -> [u8; 64] {
/// The `IS_SHA512` generic is used to toggle whether a SHA384 or SHA512 hash is desired
///
/// (the only diff between SHA384/SHA512 are different initial starting constants, and the SHA384 hash output is truncated)
fn digest_var<let N: u32, let IS_SHA512: u1>(msg: BoundedVec<u8, N>) -> [u8; 64] {
fn digest_var<let N: u32, let IS_SHA512: u8>(msg: BoundedVec<u8, N>) -> [u8; 64] {
std::static_assert(IS_SHA512 <= 1, "IS_SHA512 must be 0 or 1");
// noir-fmt:ignore
let mut h: [u64; 8] = if IS_SHA512 == 1 {
SHA512_INITIAL_HASH_VALUES
Expand Down Expand Up @@ -190,11 +191,11 @@ fn digest_var<let N: u32, let IS_SHA512: u1>(msg: BoundedVec<u8, N>) -> [u8; 64]
/// methods here are interface-compatible with sha256 blackbox fns
pub mod sha512 {
pub fn digest<let N: u32>(msg: [u8; N]) -> [u8; 64] {
crate::digest::<_, 1_u1>(msg)
crate::digest::<_, 1_u8>(msg)
}

pub fn sha512_var<let N: u32>(msg: BoundedVec<u8, N>) -> [u8; 64] {
crate::digest_var::<_, 1_u1>(msg)
crate::digest_var::<_, 1_u8>(msg)
}

// Fuzz testing the sha512 implementation
Expand Down Expand Up @@ -288,7 +289,7 @@ pub mod sha512 {

pub mod sha384 {
pub fn digest<let N: u32>(msg: [u8; N]) -> [u8; 48] {
let full_result = crate::digest::<_, 0_u1>(msg);
let full_result = crate::digest::<_, 0_u8>(msg);
let mut r: [u8; 48] = [0; 48];
for i in 0..48 {
r[i] = full_result[i];
Expand All @@ -297,7 +298,7 @@ pub mod sha384 {
}

pub fn sha384_var<let N: u32>(msg: BoundedVec<u8, N>) -> [u8; 48] {
let full_result = crate::digest_var::<_, 0_u1>(msg);
let full_result = crate::digest_var::<_, 0_u8>(msg);
let mut r: [u8; 48] = [0; 48];
for i in 0..48 {
r[i] = full_result[i];
Expand Down
Loading