Skip to content

Commit 012a0d3

Browse files
authored
Add type aliases for KeyPairs (#3816)
commit-id:8a2fb548 --- **Stack**: - #3816 ⬅ - #3815 - #3814 ⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
1 parent df0c7ed commit 012a0d3

File tree

10 files changed

+60
-47
lines changed

10 files changed

+60
-47
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Forge
1111

12+
#### Added
13+
14+
- `Fuzzable` trait implementations for `bool` and `ContractAddress`
15+
- Type aliases for `StarkCurveKeyPair`, `Secp256k1CurveKeyPair`, `Secp256r1CurveKeyPair`
16+
1217
#### Changed
1318

1419
- Updated the error message returned when calling a nonexistent method on a contract to better align with the format used by the network

snforge_std/src/byte_array.cairo

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ pub fn byte_array_as_felt_array(self: @ByteArray) -> Array<felt252> {
1414
pub fn try_deserialize_bytearray_error(x: Span<felt252>) -> Result<ByteArray, ByteArray> {
1515
if x.len() > 0 && *x.at(0) == BYTE_ARRAY_MAGIC {
1616
let mut x_span = x.slice(1, x.len() - 1);
17-
return match Serde::<ByteArray>::deserialize(ref x_span) {
18-
Option::Some(x) => Result::Ok(x),
19-
Option::None => Result::Err("Malformed input provided"),
20-
};
17+
return Serde::<ByteArray>::deserialize(ref x_span).ok_or("Malformed input provided");
2118
}
2219
Result::Err("Input is not a ByteArray-formatted error")
2320
}

snforge_std/src/cheatcodes.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use starknet::{ClassHash, ContractAddress, contract_address_const};
1+
use starknet::{ClassHash, ContractAddress};
22
use super::cheatcode::execute_cheatcode_and_deserialize;
33
pub mod block_hash;
44
pub mod contract_class;
@@ -30,7 +30,7 @@ pub fn test_selector() -> felt252 {
3030
}
3131

3232
pub fn test_address() -> ContractAddress {
33-
contract_address_const::<469394814521890341860918960550914>()
33+
469394814521890341860918960550914.try_into().expect('Test address should be valid')
3434
}
3535

3636
/// Mocks contract call to a `function_selector` of a contract at the given address, for `n_times`

snforge_std/src/cheatcodes/contract_class.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn get_class_hash(contract_address: ContractAddress) -> ClassHash {
127127
fn _prepare_calldata(
128128
class_hash: @ClassHash, constructor_calldata: @Array::<felt252>,
129129
) -> Array<felt252> {
130-
let class_hash: felt252 = class_hash.clone().into();
130+
let class_hash: felt252 = (*class_hash).into();
131131
let mut inputs: Array<felt252> = array![class_hash];
132132
constructor_calldata.serialize(ref inputs);
133133
inputs

snforge_std/src/config_types.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub enum AvailableGasConfig {
1313

1414
#[derive(Drop, Serde)]
1515
pub enum BlockId {
16-
BlockTag: (),
16+
BlockTag,
1717
BlockHash: felt252,
1818
BlockNumber: felt252,
1919
}

snforge_std/src/fuzzable.cairo

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::fmt::Debug;
2+
use starknet::ContractAddress;
23
pub use super::cheatcodes::generate_arg::generate_arg;
34

45
const MAX_FELT: felt252 = 0x800000000000011000000000000000000000000000000000000000000000000;
@@ -83,3 +84,27 @@ pub impl FuzzableByteArray1000ASCII of Fuzzable<ByteArray> {
8384
ba
8485
}
8586
}
87+
88+
pub impl FuzzableBool of Fuzzable<bool> {
89+
fn blank() -> bool {
90+
false
91+
}
92+
93+
fn generate() -> bool {
94+
generate_arg(0, 1) == 1
95+
}
96+
}
97+
98+
pub impl FuzzableContractAddress of Fuzzable<ContractAddress> {
99+
fn blank() -> ContractAddress {
100+
0x1.try_into().expect('0x1 should be a valid address')
101+
}
102+
103+
fn generate() -> ContractAddress {
104+
// [0, 2 ** 251)
105+
let arg = generate_arg(
106+
0x0, 3618502788666131106986593281521497120414687020801267626233049500247285301247,
107+
);
108+
arg.try_into().expect('Should be a valid address')
109+
}
110+
}

snforge_std/src/signature/secp256k1_curve.cairo

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use starknet::secp256k1::Secp256k1Point;
77
use crate::cheatcode::execute_cheatcode_and_deserialize;
88
use super::SignError;
99

10+
pub type Secp256k1CurveKeyPair = KeyPair<u256, Secp256k1Point>;
11+
1012
pub impl Secp256k1CurveKeyPairImpl of KeyPairTrait<u256, Secp256k1Point> {
11-
fn generate() -> KeyPair<u256, Secp256k1Point> {
13+
fn generate() -> Secp256k1CurveKeyPair {
1214
let (secret_key, pk_x, pk_y) = execute_cheatcode_and_deserialize::<
1315
'generate_ecdsa_keys', (u256, u256, u256),
1416
>(array!['Secp256k1'].span());
@@ -18,7 +20,7 @@ pub impl Secp256k1CurveKeyPairImpl of KeyPairTrait<u256, Secp256k1Point> {
1820
KeyPair { secret_key, public_key }
1921
}
2022

21-
fn from_secret_key(secret_key: u256) -> KeyPair<u256, Secp256k1Point> {
23+
fn from_secret_key(secret_key: u256) -> Secp256k1CurveKeyPair {
2224
if (secret_key == 0_u256
2325
|| secret_key >= Secp256Trait::<Secp256k1Point>::get_curve_size()) {
2426
core::panic_with_felt252('invalid secret_key');
@@ -32,12 +34,8 @@ pub impl Secp256k1CurveKeyPairImpl of KeyPairTrait<u256, Secp256k1Point> {
3234
}
3335
}
3436

35-
pub impl Secp256k1CurveSignerImpl of SignerTrait<
36-
KeyPair<u256, Secp256k1Point>, u256, (u256, u256),
37-
> {
38-
fn sign(
39-
self: KeyPair<u256, Secp256k1Point>, message_hash: u256,
40-
) -> Result<(u256, u256), SignError> {
37+
pub impl Secp256k1CurveSignerImpl of SignerTrait<Secp256k1CurveKeyPair, u256, (u256, u256)> {
38+
fn sign(self: Secp256k1CurveKeyPair, message_hash: u256) -> Result<(u256, u256), SignError> {
4139
let mut input = array!['Secp256k1'];
4240
self.secret_key.serialize(ref input);
4341
message_hash.serialize(ref input);
@@ -46,12 +44,8 @@ pub impl Secp256k1CurveSignerImpl of SignerTrait<
4644
}
4745
}
4846

49-
pub impl Secp256k1CurveVerifierImpl of VerifierTrait<
50-
KeyPair<u256, Secp256k1Point>, u256, (u256, u256),
51-
> {
52-
fn verify(
53-
self: KeyPair<u256, Secp256k1Point>, message_hash: u256, signature: (u256, u256),
54-
) -> bool {
47+
pub impl Secp256k1CurveVerifierImpl of VerifierTrait<Secp256k1CurveKeyPair, u256, (u256, u256)> {
48+
fn verify(self: Secp256k1CurveKeyPair, message_hash: u256, signature: (u256, u256)) -> bool {
5549
let (r, s) = signature;
5650
is_valid_signature::<Secp256k1Point>(message_hash, r, s, self.public_key)
5751
}

snforge_std/src/signature/secp256r1_curve.cairo

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use starknet::secp256r1::Secp256r1Point;
55
use crate::cheatcode::execute_cheatcode_and_deserialize;
66
use super::SignError;
77

8+
pub type Secp256r1CurveKeyPair = KeyPair<u256, Secp256r1Point>;
9+
810
pub impl Secp256r1CurveKeyPairImpl of KeyPairTrait<u256, Secp256r1Point> {
9-
fn generate() -> KeyPair<u256, Secp256r1Point> {
11+
fn generate() -> Secp256r1CurveKeyPair {
1012
let (secret_key, pk_x, pk_y) = execute_cheatcode_and_deserialize::<
1113
'generate_ecdsa_keys', (u256, u256, u256),
1214
>(array!['Secp256r1'].span());
@@ -16,7 +18,7 @@ pub impl Secp256r1CurveKeyPairImpl of KeyPairTrait<u256, Secp256r1Point> {
1618
KeyPair { secret_key, public_key }
1719
}
1820

19-
fn from_secret_key(secret_key: u256) -> KeyPair<u256, Secp256r1Point> {
21+
fn from_secret_key(secret_key: u256) -> Secp256r1CurveKeyPair {
2022
if (secret_key == 0_u256
2123
|| secret_key >= Secp256Trait::<Secp256r1Point>::get_curve_size()) {
2224
core::panic_with_felt252('invalid secret_key');
@@ -30,12 +32,8 @@ pub impl Secp256r1CurveKeyPairImpl of KeyPairTrait<u256, Secp256r1Point> {
3032
}
3133
}
3234

33-
pub impl Secp256r1CurveSignerImpl of SignerTrait<
34-
KeyPair<u256, Secp256r1Point>, u256, (u256, u256),
35-
> {
36-
fn sign(
37-
self: KeyPair<u256, Secp256r1Point>, message_hash: u256,
38-
) -> Result<(u256, u256), SignError> {
35+
pub impl Secp256r1CurveSignerImpl of SignerTrait<Secp256r1CurveKeyPair, u256, (u256, u256)> {
36+
fn sign(self: Secp256r1CurveKeyPair, message_hash: u256) -> Result<(u256, u256), SignError> {
3937
let mut input = array!['Secp256r1'];
4038
self.secret_key.serialize(ref input);
4139
message_hash.serialize(ref input);
@@ -44,12 +42,8 @@ pub impl Secp256r1CurveSignerImpl of SignerTrait<
4442
}
4543
}
4644

47-
pub impl Secp256r1CurveVerifierImpl of VerifierTrait<
48-
KeyPair<u256, Secp256r1Point>, u256, (u256, u256),
49-
> {
50-
fn verify(
51-
self: KeyPair<u256, Secp256r1Point>, message_hash: u256, signature: (u256, u256),
52-
) -> bool {
45+
pub impl Secp256r1CurveVerifierImpl of VerifierTrait<Secp256r1CurveKeyPair, u256, (u256, u256)> {
46+
fn verify(self: Secp256r1CurveKeyPair, message_hash: u256, signature: (u256, u256)) -> bool {
5347
let (r, s) = signature;
5448
is_valid_signature::<Secp256r1Point>(message_hash, r, s, self.public_key)
5549
}

snforge_std/src/signature/stark_curve.cairo

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ use snforge_std::signature::{KeyPair, KeyPairTrait, SignerTrait, VerifierTrait};
44
use crate::cheatcode::execute_cheatcode_and_deserialize;
55
use super::SignError;
66

7+
pub type StarkCurveKeyPair = KeyPair<felt252, felt252>;
8+
79
pub impl StarkCurveKeyPairImpl of KeyPairTrait<felt252, felt252> {
8-
fn generate() -> KeyPair<felt252, felt252> {
10+
fn generate() -> StarkCurveKeyPair {
911
let (secret_key, public_key) = execute_cheatcode_and_deserialize::<
1012
'generate_stark_keys', (felt252, felt252),
1113
>(array![].span());
1214

1315
KeyPair { secret_key, public_key }
1416
}
1517

16-
fn from_secret_key(secret_key: felt252) -> KeyPair<felt252, felt252> {
18+
fn from_secret_key(secret_key: felt252) -> StarkCurveKeyPair {
1719
if (secret_key == 0) {
1820
core::panic_with_felt252('invalid secret_key');
1921
}
@@ -28,23 +30,19 @@ pub impl StarkCurveKeyPairImpl of KeyPairTrait<felt252, felt252> {
2830
}
2931
}
3032

31-
pub impl StarkCurveSignerImpl of SignerTrait<
32-
KeyPair<felt252, felt252>, felt252, (felt252, felt252),
33-
> {
33+
pub impl StarkCurveSignerImpl of SignerTrait<StarkCurveKeyPair, felt252, (felt252, felt252)> {
3434
fn sign(
35-
self: KeyPair<felt252, felt252>, message_hash: felt252,
35+
self: StarkCurveKeyPair, message_hash: felt252,
3636
) -> Result<(felt252, felt252), SignError> {
3737
execute_cheatcode_and_deserialize::<
3838
'stark_sign_message',
3939
>(array![self.secret_key, message_hash].span())
4040
}
4141
}
4242

43-
pub impl StarkCurveVerifierImpl of VerifierTrait<
44-
KeyPair<felt252, felt252>, felt252, (felt252, felt252),
45-
> {
43+
pub impl StarkCurveVerifierImpl of VerifierTrait<StarkCurveKeyPair, felt252, (felt252, felt252)> {
4644
fn verify(
47-
self: KeyPair<felt252, felt252>, message_hash: felt252, signature: (felt252, felt252),
45+
self: StarkCurveKeyPair, message_hash: felt252, signature: (felt252, felt252),
4846
) -> bool {
4947
let (r, s) = signature;
5048
check_ecdsa_signature(message_hash, self.public_key, r, s)

snforge_std/src/trace.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::starknet::ContractAddress;
1+
use starknet::ContractAddress;
22
use crate::cheatcode::execute_cheatcode_and_deserialize;
33

44
/// Tree-like structure which contains all of the starknet calls and sub-calls along with the

0 commit comments

Comments
 (0)