Skip to content

Commit f6a056d

Browse files
heliuchuanJoshLind
authored andcommitted
set limit to num multikey to 32 (aptos-labs#17006)
(cherry picked from commit d4aa16d)
1 parent e493319 commit f6a056d

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

api/types/src/transaction.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,27 @@ pub struct MultiKeySignature {
18151815

18161816
impl VerifyInput for MultiKeySignature {
18171817
fn verify(&self) -> anyhow::Result<()> {
1818+
if self.public_keys.is_empty() {
1819+
bail!("MultiKey signature has no public keys")
1820+
} else if self.signatures.is_empty() {
1821+
bail!("MultiKey signature has no signatures")
1822+
} else if self.public_keys.len() > MAX_NUM_OF_KEYS {
1823+
bail!(
1824+
"MultiKey signature has over the maximum number of public keys {}",
1825+
MAX_NUM_OF_KEYS
1826+
)
1827+
} else if self.signatures.len() > MAX_NUM_OF_SIGS {
1828+
bail!(
1829+
"MultiKey signature has over the maximum number of signatures {}",
1830+
MAX_NUM_OF_SIGS
1831+
)
1832+
} else if self.signatures.len() != self.signatures_required as usize {
1833+
bail!("MultiKey signature does not the number of signatures required")
1834+
} else if self.signatures_required == 0 {
1835+
bail!("MultiKey signature threshold must be greater than 0")
1836+
} else if self.signatures_required > MAX_NUM_OF_SIGS as u8 {
1837+
bail!("MultiKey signature threshold is greater than the maximum number of signatures")
1838+
}
18181839
let _: AccountAuthenticator = self.try_into()?;
18191840
Ok(())
18201841
}

types/src/transaction/authenticator.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,12 @@ impl MultiKey {
10471047
"The number of required signatures is 0."
10481048
);
10491049

1050+
ensure!(
1051+
public_keys.len() <= MAX_NUM_OF_SIGS, // This max number of signatures is also the max number of public keys.
1052+
"The number of public keys is greater than {}.",
1053+
MAX_NUM_OF_SIGS
1054+
);
1055+
10501056
ensure!(
10511057
public_keys.len() >= signatures_required as usize,
10521058
"The number of public keys is smaller than the number of required signatures, {} < {}",
@@ -2089,4 +2095,21 @@ mod tests {
20892095

20902096
assert!(signed_txn.verify_signature().is_err());
20912097
}
2098+
2099+
#[test]
2100+
fn test_multi_key_with_33_keys_fails() {
2101+
let mut keys = Vec::new();
2102+
for _ in 0..33 {
2103+
let private_key = Ed25519PrivateKey::generate(&mut rand::thread_rng());
2104+
let public_key = private_key.public_key();
2105+
keys.push(AnyPublicKey::ed25519(public_key));
2106+
}
2107+
2108+
let result = MultiKey::new(keys, 3);
2109+
assert!(result.is_err());
2110+
assert_eq!(
2111+
result.unwrap_err().to_string(),
2112+
"The number of public keys is greater than 32."
2113+
);
2114+
}
20922115
}

0 commit comments

Comments
 (0)