Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
75 changes: 74 additions & 1 deletion src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use crate::{
},
native_assert, native_panic,
statistics::Statistics,
types::TypeBuilder,
types::{circuit::build_u384_struct_type, TypeBuilder},
utils::{generate_function_name, walk_ir::walk_mlir_block},
};
use bumpalo::Bump;
Expand Down Expand Up @@ -141,6 +141,8 @@ pub fn compile(
}
}

declare_u384_integer_to_struct_mlir_func(context, module, Location::unknown(context));

// Sierra programs have the following structure:
// 1. Type declarations, one per line.
// 2. Libfunc declarations, one per line.
Expand Down Expand Up @@ -171,6 +173,77 @@ pub fn compile(
Ok(())
}

fn declare_u384_integer_to_struct_mlir_func<'a>(
context: &'a Context,
module: &Module,
location: Location<'a>,
) {
let return_type = build_u384_struct_type(context);
let integer_type = IntegerType::new(context, 384);

// New block containing function. Receives the integer from where it will get the 4 limbs
let inner_block = Block::new(&[(integer_type.into(), location)]);

/////// FUNCTION IMPLEMENTATION ///////
let argument: Value<'_, '_> = inner_block.argument(0).unwrap().into();
let u96_type = IntegerType::new(context, 96).into();
let limb1 = inner_block
.trunci(argument, IntegerType::new(context, 96).into(), location)
.unwrap();
let limb2 = {
let k96 = inner_block.const_int(context, location, 96, 384).unwrap();
let limb = inner_block.shrui(argument, k96, location).unwrap();
inner_block.trunci(limb, u96_type, location).unwrap()
};
let limb3 = {
let k192 = inner_block
.const_int(context, location, 96 * 2, 384)
.unwrap();
let limb = inner_block.shrui(argument, k192, location).unwrap();
inner_block.trunci(limb, u96_type, location).unwrap()
};
let limb4 = {
let k288 = inner_block
.const_int(context, location, 96 * 3, 384)
.unwrap();
let limb = inner_block.shrui(argument, k288, location).unwrap();
inner_block.trunci(limb, u96_type, location).unwrap()
};
let struct_type = build_u384_struct_type(context);
let struct_value = inner_block
.append_op_result(llvm::undef(struct_type, location))
.unwrap();
let result = inner_block
.insert_values(
context,
location,
struct_value,
&[limb1, limb2, limb3, limb4],
)
.unwrap();

inner_block.append_operation(llvm::r#return(Some(result), location));
///////////////////////////////////////

let region = Region::new();
region.append_block(inner_block);
let func_name = StringAttribute::new(context, "cairo_native__u384_integer_to_struct");

// Append the function with the region that has the implementation to the block
module.body().append_operation(llvm::func(
context,
func_name,
TypeAttribute::new(llvm::r#type::function(
return_type,
&[integer_type.into()],
false,
)),
region,
&[],
location,
));
}

/// Compile a single Sierra function.
///
/// The function accepts a `Function` argument, which provides the function's entry point, signature
Expand Down
96 changes: 63 additions & 33 deletions src/libfuncs/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use melior::{
cf, llvm,
},
helpers::{ArithBlockExt, BuiltinBlockExt, GepIndex, LlvmBlockExt},
ir::{r#type::IntegerType, Block, BlockLike, Location, Type, Value, ValueLike},
ir::{
attribute::FlatSymbolRefAttribute, operation::OperationBuilder, r#type::IntegerType, Block,
BlockLike, Identifier, Location, Type, Value, ValueLike,
},
Context,
};
use num_traits::Signed;
Expand Down Expand Up @@ -391,7 +394,9 @@ fn build_eval<'ctx, 'this>(
ok_block.store(context, location, value_ptr, gate)?;
}

let modulus_struct = u384_integer_to_struct(context, ok_block, location, circuit_modulus)?;
// let modulus_struct = u384_integer_to_struct(context, ok_block, location, circuit_modulus)?;
let modulus_struct =
call_u384_integer_to_struct_mlir_func(ok_block, location, context, circuit_modulus)?;

// Build output struct
let outputs_type_id = &info.branch_signatures()[0].vars[2].ty;
Expand Down Expand Up @@ -916,7 +921,9 @@ fn build_get_output<'ctx, 'this>(
u384_type,
)?;
let output_integer = entry.load(context, location, output_integer_ptr, u384_type)?;
let output_struct = u384_integer_to_struct(context, entry, location, output_integer)?;
// let output_struct = u384_integer_to_struct(context, entry, location, output_integer)?;
let output_struct =
call_u384_integer_to_struct_mlir_func(entry, location, context, output_integer)?;

let guarantee_type_id = &info.branch_signatures()[0].vars[1].ty;
let guarantee = build_struct_value(
Expand Down Expand Up @@ -1001,42 +1008,65 @@ fn u384_struct_to_integer<'a>(
Ok(value)
}

fn u384_integer_to_struct<'a>(
context: &'a Context,
fn call_u384_integer_to_struct_mlir_func<'a>(
block: &'a Block<'a>,
location: Location<'a>,
context: &'a Context,
integer: Value<'a, 'a>,
) -> Result<Value<'a, 'a>> {
let u96_type = IntegerType::new(context, 96).into();

let limb1 = block.trunci(integer, IntegerType::new(context, 96).into(), location)?;
let limb2 = {
let k96 = block.const_int(context, location, 96, 384)?;
let limb = block.shrui(integer, k96, location)?;
block.trunci(limb, u96_type, location)?
};
let limb3 = {
let k192 = block.const_int(context, location, 96 * 2, 384)?;
let limb = block.shrui(integer, k192, location)?;
block.trunci(limb, u96_type, location)?
};
let limb4 = {
let k288 = block.const_int(context, location, 96 * 3, 384)?;
let limb = block.shrui(integer, k288, location)?;
block.trunci(limb, u96_type, location)?
};

let struct_type = build_u384_struct_type(context);
let struct_value = block.append_op_result(llvm::undef(struct_type, location))?;

Ok(block.insert_values(
context,
location,
struct_value,
&[limb1, limb2, limb3, limb4],
)?)
let return_type = build_u384_struct_type(context);
Ok(block
.append_operation(
OperationBuilder::new("llvm.call", location)
.add_attributes(&[(
Identifier::new(context, "callee"),
FlatSymbolRefAttribute::new(context, "cairo_native__u384_integer_to_struct")
.into(),
)])
.add_operands(&[integer])
.add_results(&[return_type])
.build()?,
)
.result(0)?
.into())
}

// fn u384_integer_to_struct<'a>(
// context: &'a Context,
// block: &'a Block<'a>,
// location: Location<'a>,
// integer: Value<'a, 'a>,
// ) -> Result<Value<'a, 'a>> {
// let u96_type = IntegerType::new(context, 96).into();

// let limb1 = block.trunci(integer, IntegerType::new(context, 96).into(), location)?;
// let limb2 = {
// let k96 = block.const_int(context, location, 96, 384)?;
// let limb = block.shrui(integer, k96, location)?;
// block.trunci(limb, u96_type, location)?
// };
// let limb3 = {
// let k192 = block.const_int(context, location, 96 * 2, 384)?;
// let limb = block.shrui(integer, k192, location)?;
// block.trunci(limb, u96_type, location)?
// };
// let limb4 = {
// let k288 = block.const_int(context, location, 96 * 3, 384)?;
// let limb = block.shrui(integer, k288, location)?;
// block.trunci(limb, u96_type, location)?
// };

// let struct_type = build_u384_struct_type(context);
// let struct_value = block.append_op_result(llvm::undef(struct_type, location))?;

// Ok(block.insert_values(
// context,
// location,
// struct_value,
// &[limb1, limb2, limb3, limb4],
// )?)
// }

/// The extended euclidean algorithm calculates the greatest common divisor (gcd) of two integers a and b,
/// as well as the bezout coefficients x and y such that ax+by=gcd(a,b)
/// if gcd(a,b) = 1, then x is the modular multiplicative inverse of a modulo b.
Expand Down
Loading