Skip to content

Commit ed5d1a1

Browse files
add two implementations (circuits and felt252)
1 parent 6269ca2 commit ed5d1a1

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

.cargo/config.toml

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/libfuncs/circuit.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -601,15 +601,15 @@ fn build_gate_evaluation<'ctx, 'this>(
601601
let circuit_modulus_u768 = block.extui(circuit_modulus, u768_type, location)?;
602602

603603
// Apply egcd to find gcd and inverse
604-
let euclidean_result = runtime_bindings_meta.extended_euclidean_algorithm(
605-
context,
606-
helper.module,
607-
block,
608-
location,
609-
rhs_value,
610-
circuit_modulus_u768,
611-
u768_type
612-
)?;
604+
let euclidean_result = runtime_bindings_meta
605+
.circuit_extended_euclidean_algorithm(
606+
context,
607+
helper.module,
608+
block,
609+
location,
610+
rhs_value,
611+
circuit_modulus_u768,
612+
)?;
613613
// Extract the values from the result struct
614614
let gcd =
615615
block.extract_value(context, location, euclidean_result, u768_type, 0)?;

src/libfuncs/felt252.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,13 @@ pub fn build_binary_operation<'ctx, 'this>(
157157
let rhs = entry.extui(rhs, i512, location)?;
158158

159159
// Find 1 / rhs.
160-
let euclidean_result = runtime_bindings_meta.extended_euclidean_algorithm(
160+
let euclidean_result = runtime_bindings_meta.felt252_extended_euclidean_algorithm(
161161
context,
162162
helper.module,
163163
entry,
164164
location,
165165
rhs,
166166
prime,
167-
i512
168167
)?;
169168

170169
let inverse = entry.extract_value(context, location, euclidean_result, i512, 1)?;

src/metadata/runtime_bindings.rs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ enum RuntimeBinding {
4646
DictDup,
4747
GetCostsBuiltin,
4848
DebugPrint,
49-
ExtendedEuclideanAlgorithm,
49+
Felt252ExtendedEuclideanAlgorithm,
50+
CircuitExtendedEuclideanAlgorithm,
5051
CircuitArithOperation,
5152
#[cfg(feature = "with-cheatcode")]
5253
VtableCheatcode,
@@ -72,8 +73,11 @@ impl RuntimeBinding {
7273
RuntimeBinding::DictDrop => "cairo_native__dict_drop",
7374
RuntimeBinding::DictDup => "cairo_native__dict_dup",
7475
RuntimeBinding::GetCostsBuiltin => "cairo_native__get_costs_builtin",
75-
RuntimeBinding::ExtendedEuclideanAlgorithm => {
76-
"cairo_native__extended_euclidean_algorithm"
76+
RuntimeBinding::Felt252ExtendedEuclideanAlgorithm => {
77+
"cairo_native__felt252_extended_euclidean_algorithm"
78+
}
79+
RuntimeBinding::CircuitExtendedEuclideanAlgorithm => {
80+
"cairo_native__circuit_extended_euclidean_algorithm"
7781
}
7882
RuntimeBinding::CircuitArithOperation => "cairo_native__circuit_arith_operation",
7983
#[cfg(feature = "with-cheatcode")]
@@ -124,7 +128,8 @@ impl RuntimeBinding {
124128
RuntimeBinding::GetCostsBuiltin => {
125129
crate::runtime::cairo_native__get_costs_builtin as *const ()
126130
}
127-
RuntimeBinding::ExtendedEuclideanAlgorithm => return None,
131+
RuntimeBinding::Felt252ExtendedEuclideanAlgorithm
132+
| RuntimeBinding::CircuitExtendedEuclideanAlgorithm => return None,
128133
RuntimeBinding::CircuitArithOperation => return None,
129134
#[cfg(feature = "with-cheatcode")]
130135
RuntimeBinding::VtableCheatcode => {
@@ -202,23 +207,66 @@ impl RuntimeBindingsMeta {
202207
/// After checking, calls the MLIR function with arguments `a` and `b` which are the initial remainders
203208
/// used in the algorithm and returns a `Value` containing a struct where the first element is the
204209
/// greatest common divisor of `a` and `b` and the second element is the bezout coefficient x.
205-
pub fn extended_euclidean_algorithm<'c, 'a>(
210+
///
211+
/// This implementation is only for felt252, which uses u252 integers.
212+
pub fn felt252_extended_euclidean_algorithm<'c, 'a>(
213+
&mut self,
214+
context: &'c Context,
215+
module: &Module,
216+
block: &'a Block<'c>,
217+
location: Location<'c>,
218+
a: Value<'c, '_>,
219+
b: Value<'c, '_>,
220+
) -> Result<Value<'c, 'a>>
221+
where
222+
'c: 'a,
223+
{
224+
let integer_type = IntegerType::new(context, 512).into();
225+
let func_symbol = RuntimeBinding::Felt252ExtendedEuclideanAlgorithm.symbol();
226+
if self
227+
.active_map
228+
.insert(RuntimeBinding::Felt252ExtendedEuclideanAlgorithm)
229+
{
230+
build_egcd_function(module, context, location, func_symbol, integer_type)?;
231+
}
232+
// The struct returned by the function that contains both of the results
233+
let return_type = llvm::r#type::r#struct(context, &[integer_type, integer_type], false);
234+
Ok(block
235+
.append_operation(
236+
OperationBuilder::new("llvm.call", location)
237+
.add_attributes(&[(
238+
Identifier::new(context, "callee"),
239+
FlatSymbolRefAttribute::new(context, func_symbol).into(),
240+
)])
241+
.add_operands(&[a, b])
242+
.add_results(&[return_type])
243+
.build()?,
244+
)
245+
.result(0)?
246+
.into())
247+
}
248+
249+
/// Similar to [felt252_extended_euclidean_algorithm](Self::felt252_extended_euclidean_algorithm).
250+
///
251+
/// The difference with the other is that this function is meant to be used
252+
/// with circuits, which use u384 integers.
253+
pub fn circuit_extended_euclidean_algorithm<'c, 'a>(
206254
&mut self,
207255
context: &'c Context,
208256
module: &Module,
209257
block: &'a Block<'c>,
210258
location: Location<'c>,
211259
a: Value<'c, '_>,
212260
b: Value<'c, '_>,
213-
integer_type: Type<'c>
214261
) -> Result<Value<'c, 'a>>
215262
where
216263
'c: 'a,
217264
{
218-
let func_symbol = RuntimeBinding::ExtendedEuclideanAlgorithm.symbol();
265+
let integer_type = IntegerType::new(context, 512).into();
266+
let func_symbol = RuntimeBinding::CircuitExtendedEuclideanAlgorithm.symbol();
219267
if self
220268
.active_map
221-
.insert(RuntimeBinding::ExtendedEuclideanAlgorithm)
269+
.insert(RuntimeBinding::CircuitExtendedEuclideanAlgorithm)
222270
{
223271
build_egcd_function(module, context, location, func_symbol, integer_type)?;
224272
}

0 commit comments

Comments
 (0)