Skip to content

Commit

Permalink
Merge pull request #96 from availproject/miguel/halborn_audit/hal_02
Browse files Browse the repository at this point in the history
ChunkSize as Constant and some checks
  • Loading branch information
fmiguelgarcia authored Jun 10, 2024
2 parents a859ae0 + 6d5a6ec commit 95c7908
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 91 deletions.
19 changes: 17 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "avail-core"
version = "0.6.0"
version = "0.6.1"
authors = []
edition = "2021"
license = "Apache-2.0"
Expand Down Expand Up @@ -39,6 +39,7 @@ rand.workspace = true
serde_json.workspace = true
test-case.workspace = true
avail-core = { path = ".", features = ["runtime"] }
trybuild = "1.0.96"

[features]
default = [ "std" ]
Expand Down
59 changes: 59 additions & 0 deletions core/src/const_generic_asserts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use core::mem::size_of;

pub struct UsizeNonZero<const N: usize>;
impl<const N: usize> UsizeNonZero<N> {
#[allow(dead_code)]
pub const OK: () = assert!(N != 0, "must be non-zero");
}

pub struct UsizeEven<const N: usize>;
impl<const N: usize> UsizeEven<N> {
#[allow(dead_code)]
pub const OK: () = assert!(N % 2 == 0, "must be even");
}

pub struct USizeSafeCastToU32<const N: usize>;
impl<const N: usize> USizeSafeCastToU32<N> {
#[allow(dead_code)]
pub const OK: () = assert!(
size_of::<usize>() <= size_of::<u32>() || N <= u32::MAX as usize,
"must be safe to cast to u32"
);
}

pub struct USizeGreaterOrEq<const N: usize, const M: usize>;
impl<const N: usize, const M: usize> USizeGreaterOrEq<N, M> {
#[allow(dead_code)]
pub const OK: () = assert!(N >= M, "must be greater or equal");
}

#[cfg(test)]
mod tests {
#[test]
fn non_zero() {
let t = trybuild::TestCases::new();
t.pass("tests/const_generics/usize_non_zero.rs");
t.compile_fail("tests/const_generics/usize_non_zero_fail.rs");
}

#[test]
fn even() {
let t = trybuild::TestCases::new();
t.pass("tests/const_generics/usize_even.rs");
t.compile_fail("tests/const_generics/usize_even_fail.rs");
}

#[test]
fn safe_cast_to_u32() {
let t = trybuild::TestCases::new();
t.pass("tests/const_generics/usize_safe_cast_to_u32.rs");
t.compile_fail("tests/const_generics/usize_safe_cast_to_u32_fail.rs");
}

#[test]
fn greater_or_eq() {
let t = trybuild::TestCases::new();
t.pass("tests/const_generics/usize_greater_or_eq.rs");
t.compile_fail("tests/const_generics/usize_greater_or_eq_fail.rs");
}
}
2 changes: 2 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub use constants::*;
pub mod header_version;
pub use header_version::HeaderVersion;

pub mod const_generic_asserts;

#[cfg(feature = "runtime")]
pub mod bench_randomness;

Expand Down
10 changes: 10 additions & 0 deletions core/tests/const_generics/usize_even.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use avail_core::const_generic_asserts::UsizeEven;

fn const_generic_even<const N: usize>() {
let () = UsizeEven::<N>::OK;
}

fn main() {
const_generic_even::<2>();
const_generic_even::<4>();
}
9 changes: 9 additions & 0 deletions core/tests/const_generics/usize_even_fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use avail_core::const_generic_asserts::UsizeEven;

fn const_generic_even<const N: usize> () {
let () = UsizeEven::<N>::OK;
}

fn main () {
const_generic_even::<1>();
}
13 changes: 13 additions & 0 deletions core/tests/const_generics/usize_even_fail.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0080]: evaluation of `avail_core::const_generic_asserts::UsizeEven::<1>::OK` failed
--> src/const_generic_asserts.rs
|
| pub const OK: () = assert!(N % 2 == 0, "must be even");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'must be even', $DIR/src/const_generic_asserts.rs:12:24
|
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

note: the above error was encountered while instantiating `fn const_generic_even::<1>`
--> tests/const_generics/usize_even_fail.rs:8:2
|
8 | const_generic_even::<1>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^
15 changes: 15 additions & 0 deletions core/tests/const_generics/usize_greater_or_eq.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use avail_core::const_generic_asserts::USizeGreaterOrEq;

fn const_generic_ge<const N: usize, const M: usize>() {
let () = USizeGreaterOrEq::<N, M>::OK;
}

fn main() {
const_generic_ge::<0, 0>();
const_generic_ge::<1, 0>();
const_generic_ge::<1, 1>();
const_generic_ge::<100, 99>();
const_generic_ge::<100, 100>();
const_generic_ge::<{ usize::MAX }, { usize::MAX - 1 }>();
const_generic_ge::<{ usize::MAX }, { usize::MAX }>();
}
9 changes: 9 additions & 0 deletions core/tests/const_generics/usize_greater_or_eq_fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use avail_core::const_generic_asserts::USizeGreaterOrEq;

fn const_generic_ge<const N: usize, const M: usize> () {
let () = USizeGreaterOrEq::<N,M>::OK;
}

fn main () {
const_generic_ge::<1, 2>();
}
13 changes: 13 additions & 0 deletions core/tests/const_generics/usize_greater_or_eq_fail.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0080]: evaluation of `avail_core::const_generic_asserts::USizeGreaterOrEq::<1, 2>::OK` failed
--> src/const_generic_asserts.rs
|
| pub const OK: () = assert!(N >= M, "must be greater or equal");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'must be greater or equal', $DIR/src/const_generic_asserts.rs:27:24
|
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

note: the above error was encountered while instantiating `fn const_generic_ge::<1, 2>`
--> tests/const_generics/usize_greater_or_eq_fail.rs:8:2
|
8 | const_generic_ge::<1, 2>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
10 changes: 10 additions & 0 deletions core/tests/const_generics/usize_non_zero.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use avail_core::const_generic_asserts::UsizeNonZero;

fn const_generic_non_zero<const N: usize>() {
let () = UsizeNonZero::<N>::OK;
}

fn main() {
const_generic_non_zero::<1>();
const_generic_non_zero::<2>();
}
9 changes: 9 additions & 0 deletions core/tests/const_generics/usize_non_zero_fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use avail_core::const_generic_asserts::UsizeNonZero;

fn const_generic_non_zero<const N: usize> () {
let () = UsizeNonZero::<N>::OK;
}

fn main () {
const_generic_non_zero::<0>();
}
13 changes: 13 additions & 0 deletions core/tests/const_generics/usize_non_zero_fail.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0080]: evaluation of `avail_core::const_generic_asserts::UsizeNonZero::<0>::OK` failed
--> src/const_generic_asserts.rs
|
| pub const OK: () = assert!(N != 0, "must be non-zero");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'must be non-zero', $DIR/src/const_generic_asserts.rs:6:24
|
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

note: the above error was encountered while instantiating `fn const_generic_non_zero::<0>`
--> tests/const_generics/usize_non_zero_fail.rs:8:2
|
8 | const_generic_non_zero::<0>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11 changes: 11 additions & 0 deletions core/tests/const_generics/usize_safe_cast_to_u32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use avail_core::const_generic_asserts::USizeSafeCastToU32;

fn const_generic_safe_cast_to_u32<const N: usize>() {
let () = USizeSafeCastToU32::<N>::OK;
}

fn main() {
const_generic_safe_cast_to_u32::<0>();
const_generic_safe_cast_to_u32::<{ (u32::MAX - 1) as usize }>();
const_generic_safe_cast_to_u32::<{ u32::MAX as usize }>();
}
9 changes: 9 additions & 0 deletions core/tests/const_generics/usize_safe_cast_to_u32_fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use avail_core::const_generic_asserts::USizeSafeCastToU32;

fn const_generic_safe_cast_to_u32<const N: usize>() {
let () = USizeSafeCastToU32::<N>::OK;
}

fn main() {
const_generic_safe_cast_to_u32::<{ u32::MAX as usize + 1 }>();
}
17 changes: 17 additions & 0 deletions core/tests/const_generics/usize_safe_cast_to_u32_fail.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0080]: evaluation of `avail_core::const_generic_asserts::USizeSafeCastToU32::<4294967296>::OK` failed
--> src/const_generic_asserts.rs
|
| pub const OK: () = assert!(
| ________________________^
| | size_of::<usize>() <= size_of::<u32>() || N <= u32::MAX as usize,
| | "must be safe to cast to u32"
| | );
| |_____^ the evaluated program panicked at 'must be safe to cast to u32', $DIR/src/const_generic_asserts.rs:18:24
|
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

note: the above error was encountered while instantiating `fn const_generic_safe_cast_to_u32::<4294967296>`
--> tests/const_generics/usize_safe_cast_to_u32_fail.rs:8:2
|
8 | const_generic_safe_cast_to_u32::<{ u32::MAX as usize + 1 }>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 changes: 1 addition & 1 deletion kate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kate"
version = "0.8.2"
version = "0.9.0"
authors = ["Denis Ermolin <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand Down
4 changes: 1 addition & 3 deletions kate/benches/reconstruct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use avail_core::{AppExtrinsic, AppId, BlockLengthColumns, BlockLengthRows, DataLookup};
use core::num::NonZeroU32;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use dusk_plonk::prelude::BlsScalar;
use kate::{
Expand Down Expand Up @@ -128,10 +127,9 @@ fn bench_reconstruct(c: &mut Criterion) {

fn reconstruct(xts: &[AppExtrinsic]) {
let metrics = IgnoreMetrics {};
let (layout, commitments, dims, matrix) = par_build_commitments(
let (layout, commitments, dims, matrix) = par_build_commitments::<32, _>(
BlockLengthRows(64),
BlockLengthColumns(16),
unsafe { NonZeroU32::new_unchecked(32) },
xts,
Seed::default(),
&metrics,
Expand Down
Loading

0 comments on commit 95c7908

Please sign in to comment.