Skip to content

Commit

Permalink
chore: make {Fixed,Var}LenBytes* constructor public
Browse files Browse the repository at this point in the history
It's convenient to be able to construct the structs from vectors of safe
bytes externally. Only unsafe-ness is `len <= max_len` is not checked.
  • Loading branch information
jonathanpwang committed Sep 26, 2023
1 parent 5a43f96 commit 3f84ec2
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions halo2-base/src/safe_types/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub struct VarLenBytes<F: ScalarField, const MAX_LEN: usize> {
}

impl<F: ScalarField, const MAX_LEN: usize> VarLenBytes<F, MAX_LEN> {
// VarLenBytes can be only created by SafeChip.
pub(super) fn new(bytes: [SafeByte<F>; MAX_LEN], len: AssignedValue<F>) -> Self {
/// Slightly unsafe constructor: it is not constrained that `len <= MAX_LEN`.
pub fn new(bytes: [SafeByte<F>; MAX_LEN], len: AssignedValue<F>) -> Self {
assert!(
len.value().le(&F::from(MAX_LEN as u64)),
"Invalid length which exceeds MAX_LEN {MAX_LEN}",
Expand Down Expand Up @@ -76,8 +76,8 @@ pub struct VarLenBytesVec<F: ScalarField> {
}

impl<F: ScalarField> VarLenBytesVec<F> {
// VarLenBytesVec can be only created by SafeChip.
pub(super) fn new(bytes: Vec<SafeByte<F>>, len: AssignedValue<F>, max_len: usize) -> Self {
/// Slightly unsafe constructor: it is not constrained that `len <= max_len`.
pub fn new(bytes: Vec<SafeByte<F>>, len: AssignedValue<F>, max_len: usize) -> Self {
assert!(
len.value().le(&F::from(max_len as u64)),
"Invalid length which exceeds MAX_LEN {}",
Expand Down Expand Up @@ -118,8 +118,8 @@ pub struct FixLenBytes<F: ScalarField, const LEN: usize> {
}

impl<F: ScalarField, const LEN: usize> FixLenBytes<F, LEN> {
// FixLenBytes can be only created by SafeChip.
pub(super) fn new(bytes: [SafeByte<F>; LEN]) -> Self {
/// Constructor
pub fn new(bytes: [SafeByte<F>; LEN]) -> Self {
Self { bytes }
}

Expand All @@ -143,8 +143,8 @@ pub struct FixLenBytesVec<F: ScalarField> {
}

impl<F: ScalarField> FixLenBytesVec<F> {
// FixLenBytes can be only created by SafeChip.
pub(super) fn new(bytes: Vec<SafeByte<F>>, len: usize) -> Self {
/// Constructor
pub fn new(bytes: Vec<SafeByte<F>>, len: usize) -> Self {
assert_eq!(bytes.len(), len, "bytes length doesn't match");
Self { bytes }
}
Expand Down

0 comments on commit 3f84ec2

Please sign in to comment.