Skip to content
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

Implement hints on common/ec.cairo #888

Merged
merged 35 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fc82ee5
Initial progress
fmoletta Mar 9, 2023
8005605
Implement random_ec_point hint
fmoletta Mar 9, 2023
1045dd8
Add integration test
fmoletta Mar 9, 2023
a6f3c6e
Fix alpha & beta values
fmoletta Mar 9, 2023
94581dd
Remove commented code
fmoletta Mar 9, 2023
c762ff7
fix test program
fmoletta Mar 9, 2023
dc4d840
Pad seed bytes
fmoletta Mar 9, 2023
6bf8c08
Pad left
fmoletta Mar 10, 2023
493d399
Fix i padding
fmoletta Mar 10, 2023
46f7ddd
Fix x & y_coef
fmoletta Mar 10, 2023
79b7568
Copy sqrt implementation from PR #715
fmoletta Mar 10, 2023
fd922fb
Move sqrt to math_utils + cleanup
fmoletta Mar 10, 2023
a87e40b
Add proptests
fmoletta Mar 10, 2023
73ad0a9
use constants in proptests
fmoletta Mar 10, 2023
c6caa9b
Add some tests
fmoletta Mar 10, 2023
7feee19
Add test
fmoletta Mar 10, 2023
2b63c80
Add test
fmoletta Mar 10, 2023
12ecb36
Add implementation for hint on chained_ec_op
fmoletta Mar 10, 2023
783772e
Fix string format
fmoletta Mar 10, 2023
57d68d0
Fix tests
fmoletta Mar 10, 2023
cb35e04
Add test program
fmoletta Mar 10, 2023
7d88bf0
Add test program
fmoletta Mar 10, 2023
a2ed87e
Add impl for recover_y hint
fmoletta Mar 10, 2023
ac38803
Add test for hint
fmoletta Mar 10, 2023
f69ef85
Add integration tests
fmoletta Mar 10, 2023
f55e699
Clippy
fmoletta Mar 10, 2023
594cadf
Clippy
fmoletta Mar 10, 2023
4806251
Add newline at EOf
fmoletta Mar 10, 2023
f91f427
Remove unused trait impl
fmoletta Mar 10, 2023
026d629
Update src/hint_processor/builtin_hint_processor/ec_utils.rs
fmoletta Mar 17, 2023
60f23c7
Merge branch 'main' of github.com:lambdaclass/cairo-rs into add-ec-hints
fmoletta Mar 17, 2023
5c0ad0d
Use constant for Felt::max / 2
fmoletta Mar 17, 2023
b6bc7df
Add missing import
fmoletta Mar 17, 2023
861b2ef
Fix proptest dependency
fmoletta Mar 17, 2023
815b9a2
Try to fix wasm tests
fmoletta Mar 17, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ iai = "0.1"
rusty-hook = "0.11"
assert_matches = "1.5.0"
criterion = { version = "0.3", features = ["html_reports"] }
proptest = "1.0.0"

[[bench]]
path = "bench/iai_benchmark.rs"
Expand Down
38 changes: 38 additions & 0 deletions cairo_programs/chained_ec_op.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
%builtins ec_op

from starkware.cairo.common.cairo_builtins import EcOpBuiltin
from starkware.cairo.common.ec_point import EcPoint
from starkware.cairo.common.ec import chained_ec_op
from starkware.cairo.common.alloc import alloc


func main{ec_op_ptr: EcOpBuiltin*}() {
let p = EcPoint(
0x6a4beaef5a93425b973179cdba0c9d42f30e01a5f1e2db73da0884b8d6756fc,
0x72565ec81bc09ff53fbfad99324a92aa5b39fb58267e395e8abe36290ebf24f,
);
let q1 = EcPoint(
0x654fd7e67a123dd13868093b3b7777f1ffef596c2e324f25ceaf9146698482c,
0x4fad269cbf860980e38768fe9cb6b0b9ab03ee3fe84cfde2eccce597c874fd8,
);
let q2 = EcPoint(
0x654fd7e67a123dd13868093b3b7777f1ffef596c2e324f25ceaf9146698482c,
0x4fad269cbf860980e38768fe9cb6b0b9ab03ee3fe84cfde2eccce597c874fd8,
);
let q3 = EcPoint(
0x654fd7e67a123dd13868093b3b7777f1ffef596c2e324f25ceaf9146698482c,
0x4fad269cbf860980e38768fe9cb6b0b9ab03ee3fe84cfde2eccce597c874fd8,
);
let q : EcPoint* = alloc();
assert q[0] = q1;
assert q[1] = q2;
assert q[2] = q3;
let m : felt* = alloc();
assert m[0] = 34;
assert m[1] = 34;
assert m[2] = 34;
let (r) = chained_ec_op(p, m, q, 3);
assert r.x = 3384892298291437283292800194657711696590239153368187334668717989522828417221;
assert r.y = 1522177177154723444905194991592642153940491339266976531102714535684279750063;
return ();
}
22 changes: 22 additions & 0 deletions cairo_programs/ec_op.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
%builtins ec_op

from starkware.cairo.common.cairo_builtins import EcOpBuiltin
from starkware.cairo.common.ec_point import EcPoint
from starkware.cairo.common.ec import ec_op


func main{ec_op_ptr: EcOpBuiltin*}() {
let p = EcPoint(
0x6a4beaef5a93425b973179cdba0c9d42f30e01a5f1e2db73da0884b8d6756fc,
0x72565ec81bc09ff53fbfad99324a92aa5b39fb58267e395e8abe36290ebf24f,
);
let m = 34;
let q = EcPoint(
0x654fd7e67a123dd13868093b3b7777f1ffef596c2e324f25ceaf9146698482c,
0x4fad269cbf860980e38768fe9cb6b0b9ab03ee3fe84cfde2eccce597c874fd8,
);
let (r) = ec_op(p, m, q);
assert r.x = 108925483682366235368969256555281508851459278989259552980345066351008608800;
assert r.y = 1592365885972480102953613056006596671718206128324372995731808913669237079419;
return ();
}
14 changes: 14 additions & 0 deletions cairo_programs/recover_y.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%builtins ec_op

from starkware.cairo.common.cairo_builtins import EcOpBuiltin
from starkware.cairo.common.ec_point import EcPoint
from starkware.cairo.common.ec import recover_y


func main{ec_op_ptr: EcOpBuiltin*}() {
let x = 0x6a4beaef5a93425b973179cdba0c9d42f30e01a5f1e2db73da0884b8d6756fc;
let r: EcPoint = recover_y(x);
assert r.x = 0x6a4beaef5a93425b973179cdba0c9d42f30e01a5f1e2db73da0884b8d6756fc;
assert r.y = 0xda9a137e43f611ac0405266cdb56d55a4c604a7d981c6a17541c9d6f140db2;
return ();
}
9 changes: 9 additions & 0 deletions felt/src/bigint_felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,15 @@ impl<const PH: u128, const PL: u128> Sub<usize> for FeltBigInt<PH, PL> {
}
}

impl<'a, const PH: u128, const PL: u128> Pow<&'a FeltBigInt<PH, PL>> for &'a FeltBigInt<PH, PL> {
type Output = FeltBigInt<PH, PL>;
fn pow(self, rhs: Self) -> Self::Output {
FeltBigInt {
val: self.val.modpow(&rhs.val, &CAIRO_PRIME_BIGUINT),
}
}
}

impl<const PH: u128, const PL: u128> SubAssign for FeltBigInt<PH, PL> {
fn sub_assign(&mut self, rhs: Self) {
*self = &*self - &rhs;
Expand Down
91 changes: 60 additions & 31 deletions felt/src/lib.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ use felt::Felt252;
#[cfg(feature = "skip_next_instruction_hint")]
use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction;

use super::ec_utils::{chained_ec_op_random_ec_point_hint, random_ec_point_hint, recover_y_hint};

pub struct HintProcessorData {
pub code: String,
pub ap_tracking: ApTracking,
Expand Down Expand Up @@ -135,7 +137,6 @@ impl HintProcessor for BuiltinHintProcessor {
constants,
);
}

match &*hint_data.code {
hint_code::ADD_SEGMENT => add_segment(vm),
hint_code::IS_NN => is_nn(vm, &hint_data.ids_data, &hint_data.ap_tracking),
Expand Down Expand Up @@ -438,6 +439,13 @@ impl HintProcessor for BuiltinHintProcessor {
hint_code::VERIFY_ECDSA_SIGNATURE => {
verify_ecdsa_signature(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::RANDOM_EC_POINT => {
random_ec_point_hint(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::CHAINED_EC_OP_RANDOM_EC_POINT => {
chained_ec_op_random_ec_point_hint(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::RECOVER_Y => recover_y_hint(vm, &hint_data.ids_data, &hint_data.ap_tracking),
#[cfg(feature = "skip_next_instruction_hint")]
hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm),
code => Err(HintError::UnknownHint(code.to_string())),
Expand Down
Loading