Implement FFI types. Add placeholder for native types.

This commit is contained in:
Alejandro Cabeza Romero 2026-04-22 18:25:15 +02:00
parent 0683b26c7c
commit acf9d82841
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
12 changed files with 127 additions and 0 deletions

3
.gitignore vendored
View File

@ -7,3 +7,6 @@ result
*.r1cs
**/input.json
**/*-input.json
# Rust Codebase
rust/**/target/

View File

@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "libc"
version = "0.2.185"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ff2c0fe9bc6cb6b14a0592c2ff4fa9ceb83eea9db979b0487cd054946a2b8f"
[[package]]
name = "logos-blockchain-circuits-types"
version = "0.1.0"
dependencies = [
"libc",
]

View File

@ -0,0 +1,7 @@
[package]
name = "logos-blockchain-circuits-types"
version = "0.1.0"
edition = "2024"
[dependencies]
libc = "0.2.185"

View File

@ -0,0 +1,38 @@
use libc::free;
mod inner {
#[repr(C)]
pub struct Buffer<T> {
pub data: T,
pub size: usize,
}
}
pub type Bytes = inner::Buffer<*mut u8>;
pub type ConstBytes = inner::Buffer<*const u8>;
/// Frees the data buffer inside a [`Bytes`] struct allocated by the C API.
///
/// Only the inner data buffer is freed, not the struct itself, since the latter is managed by the
/// caller.
///
/// # Arguments
///
/// - `bytes`: A pointer to a [`Bytes`] struct whose data buffer was allocated by the C API and
/// needs to be freed.
///
/// # Safety
///
/// Dereferences raw pointers. The caller must ensure that the pointer is valid.
pub unsafe fn free_bytes(bytes: *mut Bytes) {
if bytes.is_null() {
return;
}
let bytes = unsafe { &mut *bytes };
if !bytes.data.is_null() {
unsafe { free(bytes.data as *mut libc::c_void) };
}
bytes.data = std::ptr::null_mut();
bytes.size = 0;
}

View File

@ -0,0 +1,7 @@
pub mod status;
pub mod bytes;
pub mod witness_input;
pub use status::Status;
pub use bytes::{Bytes, ConstBytes, free_bytes};
pub use witness_input::WitnessInput;

View File

@ -0,0 +1,44 @@
use std::cmp::PartialEq;
#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum Code {
Ok = 0,
DynError = 1,
InvalidInput = 2,
OutOfMemory = 3,
}
impl Code {
pub fn is_ok(&self) -> bool {
self == &Code::Ok
}
pub fn is_error(&self) -> bool {
!self.is_ok()
}
}
#[repr(C)]
#[derive(Debug)]
pub struct Status {
pub code: Code,
pub message: String,
}
impl Status {
pub fn ok() -> Self {
Status {
code: Code::Ok,
message: String::new(),
}
}
pub fn is_ok(&self) -> bool {
self.code.is_ok()
}
pub fn is_error(&self) -> bool {
self.code.is_error()
}
}

View File

@ -0,0 +1,7 @@
use std::ffi::c_char;
use crate::ffi::ConstBytes;
pub struct WitnessInput {
pub dat: ConstBytes,
pub inputs_json: *const c_char,
}

View File

@ -0,0 +1,2 @@
pub mod ffi;
pub mod types;

View File

@ -0,0 +1,3 @@
pub mod bytes;
pub mod status;
pub mod witness_input;