mirror of
https://github.com/logos-blockchain/logos-blockchain-circuits.git
synced 2026-05-18 15:29:26 +00:00
Replace native Bytes newtype with bytes crate and expose it as Witness.
This commit is contained in:
parent
c28a2cde4a
commit
d5404d9b5b
1
rust/Cargo.lock
generated
1
rust/Cargo.lock
generated
@ -207,6 +207,7 @@ dependencies = [
|
||||
name = "logos-blockchain-circuits-types"
|
||||
version = "0.4.2"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"libc",
|
||||
]
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@ use crate::ffi::{poc_generate_witness, poc_generate_witness_from_files};
|
||||
use lbc_common::string::path_as_null_terminated_string;
|
||||
use lbc_types::{
|
||||
ffi,
|
||||
native::{Bytes, Error},
|
||||
native::{Error, Witness},
|
||||
};
|
||||
use std::path::Path;
|
||||
|
||||
@ -16,7 +16,7 @@ impl<'dat> lbc_types::CircuitDat<'dat> for PocDat {
|
||||
|
||||
pub type PocWitnessInput<'dat> = lbc_types::CircuitWitnessInput<'dat, PocDat>;
|
||||
|
||||
pub fn generate_witness(input: &PocWitnessInput) -> Result<Bytes, Error> {
|
||||
pub fn generate_witness(input: &PocWitnessInput) -> Result<Witness, Error> {
|
||||
let ffi_input_guard = input.as_ffi();
|
||||
let ffi_input = ffi_input_guard.as_ref();
|
||||
|
||||
@ -26,7 +26,7 @@ pub fn generate_witness(input: &PocWitnessInput) -> Result<Bytes, Error> {
|
||||
let status =
|
||||
unsafe { poc_generate_witness(std::ptr::from_ref(ffi_input), &raw mut ffi_output_bytes) };
|
||||
|
||||
status.try_into().map(|()| Bytes::from(ffi_output_bytes))
|
||||
status.try_into().map(|()| Witness::from(ffi_output_bytes))
|
||||
}
|
||||
|
||||
pub fn generate_witness_from_files(dat: &Path, inputs: &Path, output: &Path) -> Result<(), Error> {
|
||||
@ -77,6 +77,6 @@ mod tests {
|
||||
witness_output_path.display()
|
||||
)
|
||||
});
|
||||
assert_eq!(output.as_slice(), expected.as_slice());
|
||||
assert_eq!(output.iter().as_slice(), expected.as_slice());
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ use crate::ffi::{pol_generate_witness, pol_generate_witness_from_files};
|
||||
use lbc_common::string::path_as_null_terminated_string;
|
||||
use lbc_types::{
|
||||
ffi,
|
||||
native::{Bytes, Error},
|
||||
native::{Error, Witness},
|
||||
};
|
||||
use std::path::Path;
|
||||
|
||||
@ -16,7 +16,7 @@ impl<'dat> lbc_types::CircuitDat<'dat> for PolDat {
|
||||
|
||||
pub type PolWitnessInput<'dat> = lbc_types::CircuitWitnessInput<'dat, PolDat>;
|
||||
|
||||
pub fn generate_witness(input: &PolWitnessInput) -> Result<Bytes, Error> {
|
||||
pub fn generate_witness(input: &PolWitnessInput) -> Result<Witness, Error> {
|
||||
let ffi_input_guard = input.as_ffi();
|
||||
let ffi_input = ffi_input_guard.as_ref();
|
||||
|
||||
@ -26,7 +26,7 @@ pub fn generate_witness(input: &PolWitnessInput) -> Result<Bytes, Error> {
|
||||
let status =
|
||||
unsafe { pol_generate_witness(std::ptr::from_ref(ffi_input), &raw mut ffi_output_bytes) };
|
||||
|
||||
status.try_into().map(|()| Bytes::from(ffi_output_bytes))
|
||||
status.try_into().map(|()| Witness::from(ffi_output_bytes))
|
||||
}
|
||||
|
||||
pub fn generate_witness_from_files(dat: &Path, inputs: &Path, output: &Path) -> Result<(), Error> {
|
||||
@ -77,6 +77,6 @@ mod tests {
|
||||
witness_output_path.display()
|
||||
)
|
||||
});
|
||||
assert_eq!(output.as_slice(), expected.as_slice());
|
||||
assert_eq!(output.iter().as_slice(), expected.as_slice());
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ use crate::ffi::{poq_generate_witness, poq_generate_witness_from_files};
|
||||
use lbc_common::string::path_as_null_terminated_string;
|
||||
use lbc_types::{
|
||||
ffi,
|
||||
native::{Bytes, Error},
|
||||
native::{Error, Witness},
|
||||
};
|
||||
use std::path::Path;
|
||||
|
||||
@ -16,7 +16,7 @@ impl<'dat> lbc_types::CircuitDat<'dat> for PoqDat {
|
||||
|
||||
pub type PoqWitnessInput<'dat> = lbc_types::CircuitWitnessInput<'dat, PoqDat>;
|
||||
|
||||
pub fn generate_witness(input: &PoqWitnessInput) -> Result<Bytes, Error> {
|
||||
pub fn generate_witness(input: &PoqWitnessInput) -> Result<Witness, Error> {
|
||||
let ffi_input_guard = input.as_ffi();
|
||||
let ffi_input = ffi_input_guard.as_ref();
|
||||
|
||||
@ -26,7 +26,7 @@ pub fn generate_witness(input: &PoqWitnessInput) -> Result<Bytes, Error> {
|
||||
let status =
|
||||
unsafe { poq_generate_witness(std::ptr::from_ref(ffi_input), &raw mut ffi_output_bytes) };
|
||||
|
||||
status.try_into().map(|()| Bytes::from(ffi_output_bytes))
|
||||
status.try_into().map(|()| Witness::from(ffi_output_bytes))
|
||||
}
|
||||
|
||||
pub fn generate_witness_from_files(dat: &Path, inputs: &Path, output: &Path) -> Result<(), Error> {
|
||||
@ -77,6 +77,6 @@ mod tests {
|
||||
witness_output_path.display()
|
||||
)
|
||||
});
|
||||
assert_eq!(output.as_slice(), expected.as_slice());
|
||||
assert_eq!(output.iter().as_slice(), expected.as_slice());
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ use crate::ffi::{signature_generate_witness, signature_generate_witness_from_fil
|
||||
use lbc_common::string::path_as_null_terminated_string;
|
||||
use lbc_types::{
|
||||
ffi,
|
||||
native::{Bytes, Error},
|
||||
native::{Error, Witness},
|
||||
};
|
||||
use std::path::Path;
|
||||
|
||||
@ -18,7 +18,7 @@ impl<'dat> lbc_types::CircuitDat<'dat> for SignatureDat {
|
||||
|
||||
pub type SignatureWitnessInput<'dat> = lbc_types::CircuitWitnessInput<'dat, SignatureDat>;
|
||||
|
||||
pub fn generate_witness(input: &SignatureWitnessInput) -> Result<Bytes, Error> {
|
||||
pub fn generate_witness(input: &SignatureWitnessInput) -> Result<Witness, Error> {
|
||||
let ffi_input_guard = input.as_ffi();
|
||||
let ffi_input = ffi_input_guard.as_ref();
|
||||
|
||||
@ -29,7 +29,7 @@ pub fn generate_witness(input: &SignatureWitnessInput) -> Result<Bytes, Error> {
|
||||
signature_generate_witness(std::ptr::from_ref(ffi_input), &raw mut ffi_output_bytes)
|
||||
};
|
||||
|
||||
status.try_into().map(|()| Bytes::from(ffi_output_bytes))
|
||||
status.try_into().map(|()| Witness::from(ffi_output_bytes))
|
||||
}
|
||||
|
||||
pub fn generate_witness_from_files(dat: &Path, inputs: &Path, output: &Path) -> Result<(), Error> {
|
||||
@ -82,6 +82,6 @@ mod tests {
|
||||
witness_output_path.display()
|
||||
)
|
||||
});
|
||||
assert_eq!(output.as_slice(), expected.as_slice());
|
||||
assert_eq!(output.iter().as_slice(), expected.as_slice());
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,4 +13,5 @@ version.workspace = true
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
bytes = { workspace = true }
|
||||
libc = { workspace = true }
|
||||
|
||||
@ -7,4 +7,4 @@
|
||||
pub mod ffi;
|
||||
pub mod native;
|
||||
|
||||
pub use native::{CircuitDat, CircuitWitnessInput};
|
||||
pub use native::{CircuitDat, CircuitWitnessInput, Error};
|
||||
|
||||
@ -2,12 +2,12 @@
|
||||
//!
|
||||
//! Use them in preference to the types in [`crate::ffi`].
|
||||
|
||||
pub mod bytes;
|
||||
pub mod circuit_witness_input;
|
||||
pub mod status;
|
||||
pub mod witness;
|
||||
pub mod witness_input;
|
||||
|
||||
pub use bytes::Bytes;
|
||||
pub use circuit_witness_input::{CircuitDat, CircuitWitnessInput};
|
||||
pub use status::{Error, Result};
|
||||
pub use witness::Witness;
|
||||
pub use witness_input::WitnessInput;
|
||||
|
||||
@ -4,39 +4,9 @@ use crate::ffi;
|
||||
///
|
||||
/// When constructing from [`From<ffi::Bytes>`], it takes ownership of the underlying value and
|
||||
/// frees it.
|
||||
pub struct Bytes(Vec<u8>);
|
||||
pub type Witness = bytes::Bytes;
|
||||
|
||||
impl Bytes {
|
||||
#[must_use]
|
||||
pub fn into_inner(self) -> Vec<u8> {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn as_slice(&self) -> &[u8] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for Bytes {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
self.as_slice()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<u8>> for Bytes {
|
||||
fn from(value: Vec<u8>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Bytes> for Vec<u8> {
|
||||
fn from(value: Bytes) -> Self {
|
||||
value.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ffi::Bytes> for Bytes {
|
||||
impl From<ffi::Bytes> for Witness {
|
||||
fn from(mut ffi_value: ffi::Bytes) -> Self {
|
||||
let vec = if ffi_value.size == 0 || ffi_value.data.is_null() {
|
||||
Vec::new()
|
||||
@ -47,6 +17,6 @@ impl From<ffi::Bytes> for Bytes {
|
||||
};
|
||||
// SAFETY: `ffi_value` is a local variable, so the raw pointer is valid for this call.
|
||||
unsafe { ffi::free_bytes(&raw mut ffi_value) };
|
||||
Self(vec)
|
||||
Self::from(vec)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user