Skip to content

Commit 8c56617

Browse files
add negative check the build_egcd_function
1 parent aaa1d7a commit 8c56617

File tree

3 files changed

+27
-51
lines changed

3 files changed

+27
-51
lines changed

src/libfuncs/circuit.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -637,26 +637,6 @@ fn build_gate_evaluation<'ctx, 'this>(
637637
));
638638
block = has_inverse_block;
639639

640-
// if the inverse is negative, then add modulus
641-
let zero = block.const_int_from_type(context, location, 0, u768_type)?;
642-
let is_negative = block
643-
.append_operation(arith::cmpi(
644-
context,
645-
CmpiPredicate::Slt,
646-
inverse,
647-
zero,
648-
location,
649-
))
650-
.result(0)?
651-
.into();
652-
let wrapped_inverse = block.addi(inverse, circuit_modulus_u768, location)?;
653-
let inverse = block.append_op_result(arith::select(
654-
is_negative,
655-
wrapped_inverse,
656-
inverse,
657-
location,
658-
))?;
659-
660640
// Truncate back
661641
let inverse = block.trunci(inverse, u384_type, location)?;
662642

src/libfuncs/felt252.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use cairo_lang_sierra::{
2121
use melior::{
2222
dialect::arith::{self, CmpiPredicate},
2323
helpers::{ArithBlockExt, BuiltinBlockExt, LlvmBlockExt},
24-
ir::{r#type::IntegerType, Block, BlockLike, Location, Value, ValueLike},
24+
ir::{r#type::IntegerType, Block, Location, Value, ValueLike},
2525
Context,
2626
};
2727
use num_bigint::{BigInt, Sign};
@@ -168,30 +168,6 @@ pub fn build_binary_operation<'ctx, 'this>(
168168

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

171-
// egcd sometimes returns a negative number for the inverse,
172-
// in such cases we must simply wrap it around back into [0, PRIME)
173-
// this suffices because |inv_i| <= divfloor(PRIME,2)
174-
let zero = entry.const_int_from_type(context, location, 0, i512)?;
175-
176-
let is_negative = entry
177-
.append_operation(arith::cmpi(
178-
context,
179-
CmpiPredicate::Slt,
180-
inverse,
181-
zero,
182-
location,
183-
))
184-
.result(0)?
185-
.into();
186-
// if the inverse is < 0, add PRIME
187-
let wrapped_inverse = entry.addi(inverse, prime, location)?;
188-
let inverse = entry.append_op_result(arith::select(
189-
is_negative,
190-
wrapped_inverse,
191-
inverse,
192-
location,
193-
))?;
194-
195171
// Peform lhs * (1 / rhs)
196172
let result = entry.muli(lhs, inverse, location)?;
197173
// Apply modulo and convert result to felt252

src/metadata/runtime_bindings.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -947,17 +947,37 @@ fn build_egcd_function<'ctx>(
947947
location,
948948
));
949949

950+
let gcd = end_block.arg(0)?;
951+
let inverse = end_block.arg(1)?;
952+
953+
// EGDC sometimes returns a negative number for the inverse,
954+
// in such cases we must simply wrap it around back into [0, MODULUS)
955+
// this suffices because |inv_i| <= divfloor(MODULUS,2)
956+
let zero = end_block.const_int_from_type(context, location, 0, integer_type)?;
957+
let is_negative = end_block
958+
.append_operation(arith::cmpi(
959+
context,
960+
CmpiPredicate::Slt,
961+
inverse,
962+
zero,
963+
location,
964+
))
965+
.result(0)?
966+
.into();
967+
let wrapped_inverse = end_block.addi(inverse, b, location)?;
968+
let inverse = end_block.append_op_result(arith::select(
969+
is_negative,
970+
wrapped_inverse,
971+
inverse,
972+
location,
973+
))?;
974+
950975
// Create the struct that will contain the results
951976
let results = end_block.append_op_result(llvm::undef(
952977
llvm::r#type::r#struct(context, &[integer_type, integer_type], false),
953978
location,
954979
))?;
955-
let results = end_block.insert_values(
956-
context,
957-
location,
958-
results,
959-
&[end_block.arg(0)?, end_block.arg(1)?],
960-
)?;
980+
let results = end_block.insert_values(context, location, results, &[gcd, inverse])?;
961981
end_block.append_operation(llvm::r#return(Some(results), location));
962982

963983
let func_name = StringAttribute::new(context, func_symbol);

0 commit comments

Comments
 (0)