-
Notifications
You must be signed in to change notification settings - Fork 47
[2.11.1] Implement blake libfunc #1160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FrancoGiachetta
wants to merge
16
commits into
main
Choose a base branch
from
impl-blake-libfunc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6f8c152
implement blake libfunc
FrancoGiachetta 94729dc
doc
FrancoGiachetta df089c7
Merge branch 'main' into impl-blake-libfunc
FrancoGiachetta e44e96e
merge main
FrancoGiachetta 520e8f5
Merge branch 'impl-blake-libfunc' of github.com:lambdaclass/cairo_nat…
FrancoGiachetta 73d4a52
merge main
FrancoGiachetta 95e6f19
Merge branch 'main' into impl-blake-libfunc
FrancoGiachetta abfe998
rename runtime function
FrancoGiachetta 40fe8ac
Merge branch 'impl-blake-libfunc' of github.com:lambdaclass/cairo_nat…
FrancoGiachetta 5c774a9
remove blake type and fix runtime blake symbol
FrancoGiachetta da14c5e
fmt
FrancoGiachetta ed82cfb
add blake libfuncs' firms to the comments
FrancoGiachetta 3d509f7
Merge branch 'main' into impl-blake-libfunc
FrancoGiachetta 2ed2110
merge main
FrancoGiachetta 5d06af9
Merge branch 'impl-blake-libfunc' of github.com:lambdaclass/cairo_nat…
FrancoGiachetta b7713dc
Merge branch 'main' into impl-blake-libfunc
FrancoGiachetta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
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. | ||
/// ```cairo | ||
/// pub extern fn blake2s_compress( | ||
/// state: Blake2sState, byte_count: u32, msg: Blake2sInput, | ||
/// ) -> Blake2sState nopanic; | ||
/// ``` | ||
/// | ||
/// Similar to `blake2s_compress`, but it marks the end of the compression | ||
/// ```cairo | ||
/// pub extern fn blake2s_finalize( | ||
/// state: Blake2sState, byte_count: u32, msg: Blake2sInput, | ||
/// ) -> Blake2sState nopanic; | ||
/// ``` | ||
#[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), | ||
JulianGCalderon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.