Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* chore: remove redundant Felt252<->bytes conversions and simplify [#2243](https://github.com/lambdaclass/cairo-vm/pull/2243)

* chore: Remove dead SegmentInfo struct from cairo_runner.rs [#2235](https://github.com/lambdaclass/cairo-vm/pull/2235)

* fix: Remove unused dependency getrandom for cairo1-run, and serde_json for wasm-demo-cairo1 [#2238](https://github.com/lambdaclass/cairo-vm/pull/2238)
Expand Down
48 changes: 11 additions & 37 deletions vm/src/vm/runners/builtin_runner/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
},
};
use lazy_static::lazy_static;
use num_bigint::{BigInt, Sign};
use num_bigint::BigInt;
use num_integer::div_ceil;
use num_traits::{Num, One};
use starknet_crypto::{verify, Signature};
Expand Down Expand Up @@ -61,17 +61,7 @@ impl SignatureBuiltinRunner {
relocatable: Relocatable,
(r, s): &(Felt252, Felt252),
) -> Result<(), MemoryError> {
let r_be_bytes = r.to_bytes_be();
let s_be_bytes = s.to_bytes_be();
let (r_felt, s_felt) = (
Felt252::from_bytes_be(&r_be_bytes),
Felt252::from_bytes_be(&s_be_bytes),
);

let signature = Signature {
r: r_felt,
s: s_felt,
};
let signature = Signature { r: *r, s: *s };

self.signatures
.borrow_mut()
Expand Down Expand Up @@ -131,9 +121,9 @@ impl SignatureBuiltinRunner {
.get(&pubkey_addr)
.ok_or_else(|| MemoryError::SignatureNotFound(Box::new(pubkey_addr)))?;

let public_key = Felt252::from_bytes_be(&pubkey.to_bytes_be());
let public_key = *pubkey;
let (r, s) = (signature.r, signature.s);
let message = Felt252::from_bytes_be(&msg.to_bytes_be());
let message = *msg;
match verify(&public_key, &message, &r, &s) {
Ok(true) => Ok(vec![]),
_ => Err(MemoryError::InvalidSignature(Box::new((
Expand Down Expand Up @@ -171,15 +161,7 @@ impl SignatureBuiltinRunner {
.signatures
.borrow()
.iter()
.map(|(k, v)| {
(
*k,
(
Felt252::from_bytes_be(&v.r.to_bytes_be()),
Felt252::from_bytes_be(&v.s.to_bytes_be()),
),
)
})
.map(|(k, v)| (*k, (v.r, v.s)))
.collect();
BuiltinAdditionalData::Signature(signatures)
}
Expand All @@ -197,13 +179,9 @@ impl SignatureBuiltinRunner {
if addr.segment_index != self.base as isize {
return Err(RunnerError::InvalidAdditionalData(BuiltinName::ecdsa));
}
self.signatures.borrow_mut().insert(
*addr,
Signature {
r: Felt252::from_bytes_be(&r.to_bytes_be()),
s: Felt252::from_bytes_be(&s.to_bytes_be()),
},
);
self.signatures
.borrow_mut()
.insert(*addr, Signature { r: *r, s: *s });
}
Ok(())
}
Expand Down Expand Up @@ -235,14 +213,10 @@ impl SignatureBuiltinRunner {
pubkey: *pubkey,
msg: *msg,
signature_input: SignatureInput {
r: Felt252::from_bytes_be(&signature.r.to_bytes_be()),
r: signature.r,
w: Felt252::from(
&div_mod(
&BigInt::one(),
&BigInt::from_bytes_be(Sign::Plus, &signature.s.to_bytes_be()),
&EC_ORDER,
)
.unwrap_or_default(),
&div_mod(&BigInt::one(), &signature.s.to_bigint(), &EC_ORDER)
.unwrap_or_default(),
),
},
}))
Expand Down
Loading