Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6f8c152
implement blake libfunc
FrancoGiachetta Mar 31, 2025
94729dc
doc
FrancoGiachetta Mar 31, 2025
df089c7
Merge branch 'main' into impl-blake-libfunc
FrancoGiachetta Apr 1, 2025
e44e96e
merge main
FrancoGiachetta Apr 3, 2025
520e8f5
Merge branch 'impl-blake-libfunc' of github.com:lambdaclass/cairo_nat…
FrancoGiachetta Apr 3, 2025
73d4a52
merge main
FrancoGiachetta Apr 3, 2025
95e6f19
Merge branch 'main' into impl-blake-libfunc
FrancoGiachetta Apr 7, 2025
abfe998
rename runtime function
FrancoGiachetta Apr 7, 2025
40fe8ac
Merge branch 'impl-blake-libfunc' of github.com:lambdaclass/cairo_nat…
FrancoGiachetta Apr 7, 2025
5c774a9
remove blake type and fix runtime blake symbol
FrancoGiachetta Apr 8, 2025
da14c5e
fmt
FrancoGiachetta Apr 8, 2025
ed82cfb
add blake libfuncs' firms to the comments
FrancoGiachetta Apr 15, 2025
3d509f7
Merge branch 'main' into impl-blake-libfunc
FrancoGiachetta Apr 21, 2025
2ed2110
merge main
FrancoGiachetta Apr 30, 2025
5d06af9
Merge branch 'impl-blake-libfunc' of github.com:lambdaclass/cairo_nat…
FrancoGiachetta Apr 30, 2025
b7713dc
Merge branch 'main' into impl-blake-libfunc
FrancoGiachetta Apr 30, 2025
f2cebe3
merge main
FrancoGiachetta Oct 17, 2025
1128ee8
Merge branch 'impl-blake-libfunc' of github.com:lambdaclass/cairo_nat…
FrancoGiachetta Oct 17, 2025
5d5d65c
fmt
FrancoGiachetta Oct 17, 2025
b36248c
post merge fix
FrancoGiachetta Oct 17, 2025
8d6b3f6
post merge fix
FrancoGiachetta Oct 17, 2025
6b7a5ab
add tests
FrancoGiachetta Oct 17, 2025
7ceb9ac
change test
FrancoGiachetta Oct 17, 2025
307d968
Merge branch 'main' into impl-blake-libfunc
FrancoGiachetta Oct 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion src/libfuncs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use num_bigint::BigInt;
use std::{cell::Cell, error::Error, ops::Deref};

mod array;
mod blake;
mod r#bool;
mod bounded_int;
mod r#box;
Expand Down Expand Up @@ -179,7 +180,9 @@ impl LibfuncBuilder for CoreConcreteLibfunc {
Self::IntRange(selector) => self::int_range::build(
context, registry, entry, location, helper, metadata, selector,
),
Self::Blake(_) => native_panic!("Implement blake libfunc"),
Self::Blake(selector) => self::blake::build(
context, registry, entry, location, helper, metadata, selector,
),
Self::Mem(selector) => self::mem::build(
context, registry, entry, location, helper, metadata, selector,
),
Expand Down
120 changes: 120 additions & 0 deletions src/libfuncs/blake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use cairo_lang_sierra::{
extensions::{
blake::BlakeConcreteLibfunc,
core::{CoreLibfunc, CoreType},
lib_func::SignatureOnlyConcreteLibfunc,
},
program_registry::ProgramRegistry,
};
use melior::{
ir::{Block, BlockLike, Location},
Context,
};

use crate::{
error::{panic::ToNativeAssertError, Result},
metadata::{runtime_bindings::RuntimeBindingsMeta, MetadataStorage},
utils::BlockExt,
};

use super::LibfuncHelper;

pub fn build<'ctx, 'this>(
context: &'ctx Context,
registry: &ProgramRegistry<CoreType, CoreLibfunc>,
entry: &'this Block<'ctx>,
location: Location<'ctx>,
helper: &LibfuncHelper<'ctx, 'this>,
metadata: &mut MetadataStorage,
selector: &BlakeConcreteLibfunc,
) -> Result<()> {
match selector {
BlakeConcreteLibfunc::Blake2sCompress(info) => build_blake_operation(
context, registry, entry, location, helper, metadata, info, false,
),
BlakeConcreteLibfunc::Blake2sFinalize(info) => build_blake_operation(
context, registry, entry, location, helper, metadata, info, true,
),
}
}

/// Performs a blake2s compression.
///
/// `bytes_count` is the total amount of bytes hashed after hashing the message.
/// `finalize` is wether the libfunc call is a finalize or not.
#[allow(clippy::too_many_arguments)]
fn build_blake_operation<'ctx, 'this>(
context: &'ctx Context,
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
entry: &'this Block<'ctx>,
location: Location<'ctx>,
helper: &LibfuncHelper<'ctx, 'this>,
metadata: &mut MetadataStorage,
_info: &SignatureOnlyConcreteLibfunc,
finalize: bool,
) -> Result<()> {
let state_ptr = entry.arg(0)?;
let bytes_count = entry.arg(1)?;
let message = entry.arg(2)?;
let k_finalize = entry.const_int(context, location, finalize, 1)?;

let runtime_bindings = metadata
.get_mut::<RuntimeBindingsMeta>()
.to_native_assert_error("runtime library should be available")?;

runtime_bindings.libfunc_blake_compress(
context,
helper,
entry,
state_ptr,
message,
bytes_count,
k_finalize,
location,
)?;

entry.append_operation(helper.br(0, &[state_ptr], location));

Ok(())
}

#[cfg(test)]
mod tests {
use crate::{
utils::test::{jit_struct, load_cairo, run_program},
Value,
};

#[test]
fn test_blake() {
let program = load_cairo!(
use core::blake::{blake2s_compress, blake2s_finalize};

fn run_test() -> [u32; 8] nopanic {
let state = BoxTrait::new([0_u32; 8]);
let msg = BoxTrait::new([0_u32; 16]);
let byte_count = 64_u32;

let _res = blake2s_compress(state, byte_count, msg).unbox();

blake2s_finalize(state, byte_count, msg).unbox()
}
);

let result = run_program(&program, "run_test", &[]).return_value;

assert_eq!(
result,
jit_struct!(
Value::Uint32(128291589),
Value::Uint32(1454945417),
Value::Uint32(3191583614),
Value::Uint32(1491889056),
Value::Uint32(794023379),
Value::Uint32(651000200),
Value::Uint32(3725903680),
Value::Uint32(1044330286),
)
);
}
}
37 changes: 37 additions & 0 deletions src/metadata/runtime_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ enum RuntimeBinding {
DictDrop,
DictDup,
GetCostsBuiltin,
BlakeCompress,
DebugPrint,
#[cfg(feature = "with-cheatcode")]
VtableCheatcode,
Expand All @@ -65,6 +66,7 @@ impl RuntimeBinding {
RuntimeBinding::DictDrop => "cairo_native__dict_drop",
RuntimeBinding::DictDup => "cairo_native__dict_dup",
RuntimeBinding::GetCostsBuiltin => "cairo_native__get_costs_builtin",
RuntimeBinding::BlakeCompress => "cairo_native_libfunc_blake_compress",
#[cfg(feature = "with-cheatcode")]
RuntimeBinding::VtableCheatcode => "cairo_native__vtable_cheatcode",
}
Expand Down Expand Up @@ -109,6 +111,9 @@ impl RuntimeBinding {
RuntimeBinding::GetCostsBuiltin => {
crate::runtime::cairo_native__get_costs_builtin as *const ()
}
RuntimeBinding::BlakeCompress => {
crate::runtime::cairo_native_libfunc_blake_compress as *const ()
}
#[cfg(feature = "with-cheatcode")]
RuntimeBinding::VtableCheatcode => {
crate::starknet::cairo_native__vtable_cheatcode as *const ()
Expand Down Expand Up @@ -257,6 +262,37 @@ impl RuntimeBindingsMeta {
))
}

#[allow(clippy::too_many_arguments)]
pub fn libfunc_blake_compress<'c, 'a>(
&mut self,
context: &'c Context,
module: &Module,
block: &'a Block<'c>,
state: Value<'c, 'a>,
message: Value<'c, 'a>,
count_bytes: Value<'c, 'a>,
finalize: Value<'c, 'a>,
location: Location<'c>,
) -> Result<OperationRef<'c, 'a>>
where
'c: 'a,
{
let function = self.build_function(
context,
module,
block,
location,
RuntimeBinding::BlakeCompress,
)?;

Ok(block.append_operation(
OperationBuilder::new("llvm.call", location)
.add_operands(&[function])
.add_operands(&[state, message, count_bytes, finalize])
.build()?,
))
}

/// Register if necessary, then invoke the `ec_point_from_x_nz()` function.
pub fn libfunc_ec_point_from_x_nz<'c, 'a>(
&mut self,
Expand Down Expand Up @@ -681,6 +717,7 @@ pub fn setup_runtime(find_symbol_ptr: impl Fn(&str) -> Option<*mut c_void>) {
RuntimeBinding::DictDrop,
RuntimeBinding::DictDup,
RuntimeBinding::GetCostsBuiltin,
RuntimeBinding::BlakeCompress,
RuntimeBinding::DebugPrint,
#[cfg(feature = "with-cheatcode")]
RuntimeBinding::VtableCheatcode,
Expand Down
Loading
Loading