-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add VarLenBytesVec and FixLenBytes Fix tests
- Loading branch information
1 parent
bfa3e63
commit 339bc40
Showing
5 changed files
with
224 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,91 @@ | ||
#![allow(clippy::len_without_is_empty)] | ||
use crate::AssignedValue; | ||
|
||
use super::{SafeByte, ScalarField}; | ||
|
||
use getset::Getters; | ||
|
||
/// Represents a variable length byte array in circuit. | ||
/// | ||
/// Each element is guaranteed to be a byte, given by type [`SafeByte`]. | ||
/// To represent a variable length array, we must know the maximum possible length `MAX_LEN` the array could be -- this is some additional context the user must provide. | ||
/// Then we right pad the array with 0s to the maximum length (we do **not** constrain that these paddings must be 0s). | ||
#[derive(Debug, Clone)] | ||
#[derive(Debug, Clone, Getters)] | ||
pub struct VarLenBytes<F: ScalarField, const MAX_LEN: usize> { | ||
/// The byte array, right padded with 0s | ||
pub bytes: [SafeByte<F>; MAX_LEN], | ||
/// The byte array, right padded | ||
#[getset(get = "pub")] | ||
bytes: [SafeByte<F>; MAX_LEN], | ||
/// Witness representing the actual length of the byte array. Upon construction, this is range checked to be at most `MAX_LEN` | ||
pub var_len: AssignedValue<F>, | ||
#[getset(get = "pub")] | ||
len: AssignedValue<F>, | ||
} | ||
|
||
impl<F: ScalarField, const MAX_LEN: usize> VarLenBytes<F, MAX_LEN> { | ||
fn new(bytes: [SafeByte<F>; MAX_LEN], var_len: AssignedValue<F>) -> Self { | ||
Self { bytes, var_len } | ||
// VarLenBytes can be only created by SafeChip. | ||
pub(super) fn new(bytes: [SafeByte<F>; MAX_LEN], len: AssignedValue<F>) -> Self { | ||
assert!( | ||
len.value().le(&F::from_u128(MAX_LEN as u128)), | ||
"Invalid length which exceeds MAX_LEN {}", | ||
MAX_LEN | ||
); | ||
Self { bytes, len } | ||
} | ||
|
||
/// Returns the maximum length of the byte array. | ||
pub fn max_len(&self) -> usize { | ||
MAX_LEN | ||
} | ||
} | ||
|
||
impl<F: ScalarField, const MAX_LEN: usize> AsRef<[SafeByte<F>]> for VarLenBytes<F, MAX_LEN> { | ||
fn as_ref(&self) -> &[SafeByte<F>] { | ||
&self.bytes | ||
} | ||
} | ||
|
||
impl<F: ScalarField, const MAX_LEN: usize> AsMut<[SafeByte<F>]> for VarLenBytes<F, MAX_LEN> { | ||
fn as_mut(&mut self) -> &mut [SafeByte<F>] { | ||
&mut self.bytes | ||
} | ||
} | ||
|
||
/// Represents a variable length byte array in circuit. | ||
/// Represents a variable length byte array in circuit. Not encourged to use because `MAX_LEN` cannot be verified at compile time. | ||
/// | ||
/// Each element is guaranteed to be a byte, given by type [`SafeByte`]. | ||
/// To represent a variable length array, we must know the maximum possible length `MAX_LEN` the array could be -- this is some additional context the user must provide. | ||
/// Then we right pad the array with 0s to the maximum length (we do **not** constrain that these paddings must be 0s). | ||
#[derive(Debug, Clone)] | ||
#[derive(Debug, Clone, Getters)] | ||
pub struct VarLenBytesVec<F: ScalarField> { | ||
/// The byte array, right padded with 0s | ||
/// The byte array, right padded | ||
#[getset(get = "pub")] | ||
bytes: Vec<SafeByte<F>>, | ||
/// Witness representing the actual length of the byte array. Upon construction, this is range checked to be at most `MAX_LEN` | ||
pub var_len: AssignedValue<F>, | ||
#[getset(get = "pub")] | ||
len: AssignedValue<F>, | ||
} | ||
|
||
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 { | ||
assert!( | ||
len.value().le(&F::from_u128(max_len as u128)), | ||
"Invalid length which exceeds MAX_LEN {}", | ||
max_len | ||
); | ||
assert!(bytes.len() == max_len, "bytes is not padded correctly"); | ||
Self { bytes, len } | ||
} | ||
|
||
/// Returns the maximum length of the byte array. | ||
pub fn max_len(&self) -> usize { | ||
self.bytes.len() | ||
} | ||
} | ||
|
||
/// Represents a fixed length byte array in circuit. | ||
#[derive(Debug, Clone, Getters)] | ||
pub struct FixLenBytes<F: ScalarField, const LEN: usize> { | ||
/// The byte array | ||
#[getset(get = "pub")] | ||
bytes: [SafeByte<F>; LEN], | ||
} | ||
|
||
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 { | ||
Self { bytes } | ||
} | ||
|
||
/// Returns the length of the byte array. | ||
pub fn len(&self) -> usize { | ||
LEN | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub (crate) mod var_byte_array; | ||
pub (crate) mod safe_type; | ||
pub(crate) mod safe_type; | ||
pub(crate) mod var_byte_array; |
Oops, something went wrong.