Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqvq committed Feb 14, 2024
1 parent 5a20e0e commit 736d47c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 66 deletions.
16 changes: 3 additions & 13 deletions crates/sui-core/src/unit_tests/transaction_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,6 @@ async fn test_zklogin_transfer_with_bad_ephemeral_sig() {
.await;
}

// fn zklogin_key_pair_and_inputs() -> Vec<(Ed25519KeyPair, ZkLoginInputs)> {
// let key1 = Ed25519KeyPair::generate(&mut StdRng::from_seed([1; 32]));
// let key2 = Ed25519KeyPair::generate(&mut StdRng::from_seed([2; 32]));

// let inputs1 = ZkLoginInputs::from_json("{\"proofPoints\":{\"a\":[\"7351610957585487046328875967050889651854514987235893782501043846344306437586\",\"15901581830174345085102528605366245320934422564305327249129736514949843983391\",\"1\"],\"b\":[[\"8511334686125322419369086121569737536249817670014553268281989325333085952301\",\"4879445774811020644521006463993914729416121646921376735430388611804034116132\"],[\"17435652898871739253945717312312680537810513841582909477368887889905134847157\",\"14885460127400879557124294989610467103783286587437961743305395373299049315863\"],[\"1\",\"0\"]],\"c\":[\"18935582624804960299209074901817240117999581542763303721451852621662183299378\",\"5367019427921492326304024952457820199970536888356564030410757345854117465786\",\"1\"]},\"issBase64Details\":{\"value\":\"wiaXNzIjoiaHR0cHM6Ly9pZC50d2l0Y2gudHYvb2F1dGgyIiw\",\"indexMod4\":2},\"headerBase64\":\"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEifQ\"}", "20794788559620669596206457022966176986688727876128223628113916380927502737911").unwrap();
// let inputs2 = ZkLoginInputs::from_json("{\"proofPoints\":{\"a\":[\"7351610957585487046328875967050889651854514987235893782501043846344306437586\",\"15901581830174345085102528605366245320934422564305327249129736514949843983391\",\"1\"],\"b\":[[\"8511334686125322419369086121569737536249817670014553268281989325333085952301\",\"4879445774811020644521006463993914729416121646921376735430388611804034116132\"],[\"17435652898871739253945717312312680537810513841582909477368887889905134847157\",\"14885460127400879557124294989610467103783286587437961743305395373299049315863\"],[\"1\",\"0\"]],\"c\":[\"18935582624804960299209074901817240117999581542763303721451852621662183299378\",\"5367019427921492326304024952457820199970536888356564030410757345854117465786\",\"1\"]},\"issBase64Details\":{\"value\":\"wiaXNzIjoiaHR0cHM6Ly9pZC50d2l0Y2gudHYvb2F1dGgyIiw\",\"indexMod4\":2},\"headerBase64\":\"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEifQ\"}", "20794788559620669596206457022966176986688727876128223628113916380927502737911").unwrap();

// vec![(key1, inputs1), (key2, inputs2)]
// }

#[sim_test]
async fn zklogin_test_cached_proof_wrong_key() {
telemetry_subscribers::init_for_testing();
Expand Down Expand Up @@ -492,7 +482,7 @@ async fn zklogin_test_cached_proof_wrong_key() {
*/

let (skp, _eph_pk, zklogin) =
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[0];
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[1];
let ephemeral_key = match skp {
SuiKeyPair::Ed25519(kp) => kp,
_ => panic!(),
Expand Down Expand Up @@ -621,7 +611,7 @@ async fn setup_zklogin_network(
NetworkAuthorityClient,
) {
let (skp, _eph_pk, zklogin) =
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[0];
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[1];
let ephemeral_key = match skp {
SuiKeyPair::Ed25519(kp) => kp,
_ => panic!(),
Expand Down Expand Up @@ -747,7 +737,7 @@ async fn zklogin_txn_fail_if_missing_jwk() {

// Initialize an authorty state with some objects under a zklogin address.
let (skp, _eph_pk, zklogin) =
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[0];
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[1];
let ephemeral_key = match skp {
SuiKeyPair::Ed25519(kp) => kp,
_ => panic!(),
Expand Down
12 changes: 6 additions & 6 deletions crates/sui-e2e-tests/tests/multisig_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ async fn test_multisig_with_zklogin_scenerios() {

// construct a multisig address with 4 pks (ed25519, secp256k1, secp256r1, zklogin) with threshold = 1.
let (eph_kp, _eph_pk, zklogin_inputs) =
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[0];
let (eph_kp_1, _, _) =
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[1];
let (eph_kp_1, _, _) =
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[2];
let zklogin_pk = PublicKey::ZkLogin(
ZkLoginPublicIdentifier::new(zklogin_inputs.get_iss(), zklogin_inputs.get_address_seed())
.unwrap(),
Expand Down Expand Up @@ -614,14 +614,14 @@ async fn test_expired_epoch_zklogin_in_multisig() {

#[sim_test]
async fn test_random_zklogin_in_multisig() {
let test_vectors = &load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json");

let test_vectors =
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[1..11];
let test_cluster = TestClusterBuilder::new().with_default_jwks().build().await;
test_cluster.wait_for_authenticator_state_update().await;
let rgp = test_cluster.get_reference_gas_price().await;
let context = &test_cluster.wallet;

// create a multisig with x out of 10 zklogin authenticator where x is 1..10.
// create a multisig with 10 zklogin pks.
let pks = test_vectors.iter().map(|(_, pk, _)| pk.clone()).collect();
let multisig_pk = MultiSigPublicKey::new(pks, vec![1; 10], 10).unwrap();
let multisig_addr = SuiAddress::from(&multisig_pk);
Expand Down Expand Up @@ -712,7 +712,7 @@ async fn construct_simple_zklogin_multisig_tx(test_cluster: &TestCluster) -> Tra
let context = &test_cluster.wallet;
// construct a multisig address with 1 zklogin pk with threshold = 1.
let (eph_kp, _eph_pk, zklogin_inputs) =
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[0];
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[1];
let zklogin_pk = PublicKey::ZkLogin(
ZkLoginPublicIdentifier::new(zklogin_inputs.get_iss(), zklogin_inputs.get_address_seed())
.unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-e2e-tests/tests/zklogin_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async fn zklogin_end_to_end_test() {
let context = &mut test_cluster.wallet;

let (eph_kp, pk_zklogin, zklogin_inputs) =
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[0];
&load_test_vectors("../sui-types/src/unit_tests/zklogin_test_vectors.json")[1];
let zklogin_addr = (pk_zklogin).into();

let gas = fund_address_and_return_gas(context, rgp, zklogin_addr).await;
Expand Down
71 changes: 25 additions & 46 deletions crates/sui-types/src/unit_tests/multisig_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ fn test_combine_sigs() {
message: "Hello".as_bytes().to_vec(),
},
);
let sig1 = Signature::new_secure(&msg, &kp1).into();
let sig1: GenericSignature = Signature::new_secure(&msg, &kp1).into();
let sig2 = Signature::new_secure(&msg, &kp2).into();
let sig3 = Signature::new_secure(&msg, &kp3).into();

// MultiSigPublicKey contains only 2 public key but 3 signatures are passed, fails to combine.
assert!(MultiSig::combine(vec![sig1, sig2, sig3], multisig_pk.clone()).is_err());
assert!(MultiSig::combine(vec![sig1.clone(), sig2, sig3], multisig_pk.clone()).is_err());

// Cannot create malformed MultiSig.
assert!(MultiSig::combine(vec![], multisig_pk).is_err());
assert!(MultiSig::combine(vec![], multisig_pk.clone()).is_err());
assert!(MultiSig::combine(vec![sig1.clone(), sig1], multisig_pk).is_err());
}
#[test]
fn test_serde_roundtrip() {
Expand Down Expand Up @@ -136,23 +137,7 @@ fn test_serde_roundtrip() {
}

#[test]
fn single_sig_port_works() {
let kp: SuiKeyPair = SuiKeyPair::Ed25519(get_key_pair().1);
let addr = SuiAddress::from(&kp.public());
let msg = IntentMessage::new(
Intent::sui_transaction(),
PersonalMessage {
message: "Hello".as_bytes().to_vec(),
},
);
let sig = Signature::new_secure(&msg, &kp);
assert!(sig
.verify_authenticator(&msg, addr, None, &VerifyParams::default())
.is_ok());
}

#[test]
fn test_multisig_pk_failure() {
fn test_multisig_pk_new() {
let keys = keys();
let pk1 = keys[0].public();
let pk2 = keys[1].public();
Expand All @@ -169,7 +154,7 @@ fn test_multisig_pk_failure() {
// Fails on threshold 0.
assert!(MultiSigPublicKey::new(
vec![pk1.clone(), pk2.clone(), pk3.clone()],
vec![0, 1, 1],
vec![1, 1, 1],
0
)
.is_err());
Expand Down Expand Up @@ -222,12 +207,10 @@ fn test_max_sig() {
);
let mut seed = StdRng::from_seed([0; 32]);
let mut keys = Vec::new();
let mut sigs = Vec::new();
let mut pks = Vec::new();

for _ in 0..11 {
let k = SuiKeyPair::Ed25519(get_key_pair_from_rng(&mut seed).1);
sigs.push(Signature::new_secure(&msg, &k).into());
pks.push(k.public());
keys.push(k);
}
Expand All @@ -244,20 +227,20 @@ fn test_max_sig() {
assert!(MultiSigPublicKey::new(pks.clone()[..5].to_vec(), vec![3; 5], 16).is_err());

// multisig_pk with max weights for each pk and max reachable threshold is ok.
let high_threshold_pk = MultiSigPublicKey::new(
let res = MultiSigPublicKey::new(
pks.clone()[..10].to_vec(),
vec![WeightUnit::MAX; MAX_SIGNER_IN_MULTISIG],
(WeightUnit::MAX as ThresholdUnit) * (MAX_SIGNER_IN_MULTISIG as ThresholdUnit),
)
.unwrap();
);
assert!(res.is_ok());

// But max threshold cannot be met, fails to validate.
sigs.remove(10);
sigs.remove(0);
assert!(MultiSig::combine(sigs, high_threshold_pk)
.unwrap()
.init_and_validate()
.is_err());
// multisig_pk with unreachable threshold fails.
let res = MultiSigPublicKey::new(
pks.clone()[..10].to_vec(),
vec![WeightUnit::MAX; MAX_SIGNER_IN_MULTISIG],
(WeightUnit::MAX as ThresholdUnit) * (MAX_SIGNER_IN_MULTISIG as ThresholdUnit) + 1,
);
assert!(res.is_err());

// multisig_pk with max weights for each pk with threshold is 1x max weight validates ok.
let low_threshold_pk = MultiSigPublicKey::new(
Expand Down Expand Up @@ -359,12 +342,14 @@ fn multisig_zklogin_scenarios() {
let skp: SuiKeyPair = SuiKeyPair::Ed25519(kp);
let pk1 = skp.public();

let (eph_kp, _pk, inputs) = &load_test_vectors("./src/unit_tests/zklogin_test_vectors.json")[0];

let (_, _, inputs) = &load_test_vectors("./src/unit_tests/zklogin_test_vectors.json")[0];
// pk consistent with the one in make_zklogin_tx
let pk2 = PublicKey::ZkLogin(
ZkLoginPublicIdentifier::new(&OIDCProvider::Twitch.get_config().iss, DEFAULT_ADDRESS_SEED)
.unwrap(),
ZkLoginPublicIdentifier::new(
&OIDCProvider::Twitch.get_config().iss,
inputs.get_address_seed(),
)
.unwrap(),
);

// set up 1-out-of-2 multisig with one zklogin public identifier and one traditional public key.
Expand All @@ -381,18 +366,12 @@ fn multisig_zklogin_scenarios() {
let tx = binding.transaction_data();
assert_eq!(Base64::encode(bcs::to_bytes(tx).unwrap()), "AAABACACAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgEBAQABAAC5wHgKOUPN4TokCb8abwauYLDf8rLzcyYM9ieqT0OliAGbB4FfBEl+LgXSLKw6oGFBCyCGjMYZFUxCocYb6ZAnFwEAAAAAAAAAIJZw7UpW1XHubORIOaY8d2+WyBNwoJ+FEAxlsa7h7JHrucB4CjlDzeE6JAm/Gm8GrmCw3/Ky83MmDPYnqk9DpYgBAAAAAAAAABAnAAAAAAAAAA==".to_string());

let intent_msg: &IntentMessage<crate::transaction::TransactionData> =
&IntentMessage::new(Intent::sui_transaction(), tx.clone());
let intent_msg = &IntentMessage::new(Intent::sui_transaction(), tx.clone());
assert_eq!(Base64::encode(zklogin_sig.as_ref()), "BQNNMTczMTgwODkxMjU5NTI0MjE3MzYzNDIyNjM3MTc5MzI3MTk0Mzc3MTc4NDQyODI0MTAxODc5NTc5ODQ3NTE5Mzk5NDI4OTgyNT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62B6ALVOxMq6nsc0y3JlY8Gf1ZoPA976dom6y3JGBUTsry6axfqHcVrtRAy5xu4WMO8+cRFEpkjbBruyKE9ydM++5T/87lA8waSSAA==".to_string());

let eph_sig = Signature::new_secure(intent_msg, eph_kp);
let zklogin_sig = GenericSignature::ZkLoginAuthenticator(ZkLoginAuthenticator::new(
inputs.clone(),
10,
eph_sig,
));
let single_sig = GenericSignature::Signature(Signature::new_secure(intent_msg, &skp));
let multisig = GenericSignature::MultiSig(
MultiSig::combine(vec![zklogin_sig], multisig_pk.clone()).unwrap(),
MultiSig::combine(vec![single_sig, zklogin_sig], multisig_pk.clone()).unwrap(),
);
assert_eq!(Base64::encode(multisig.as_ref()), "AwIAcAEsWrZtlsE3AdGUKJAPag8Tu6HPfMW7gEemeneO9fmNGiJP/rDZu/tL75lr8A22eFDx9K2G1DL4v8XlmuTtCgOaBwUDTTE3MzE4MDg5MTI1OTUyNDIxNzM2MzQyMjYzNzE3OTMyNzE5NDM3NzE3ODQ0MjgyNDEwMTg3OTU3OTg0NzUxOTM5OTQyODk4MjUxMjUwTTExMzczOTY2NjQ1NDY5MTIyNTgyMDc0MDgyMjk1OTg1Mzg4MjU4ODQwNjgxNjE4MjY4NTkzOTc2Njk3MzI1ODkyMjgwOTE1NjgxMjA3ATEDAkw1OTM5ODcxMTQ3MzQ4ODM0OTk3MzYxNzIwMTIyMjM4OTgwMTc3MTUyMzAzMjc0MzExMDQ3MjQ5OTA1OTQyMzg0OTE1NzY4NjkwODk1TDQ1MzM1NjgyNzExMzQ3ODUyNzg3MzEyMzQ1NzAzNjE0ODI2NTE5OTY3NDA3OTE4ODgyODU4NjQ5NjY4ODQwMzI3MTcwNDk4MTE3MDgCTTEwNTY0Mzg3Mjg1MDcxNTU1NDY5NzUzOTkwNjYxNDEwODQwMTE4NjM1OTI1NDY2NTk3MDM3MDE4MDU4NzcwMDQxMzQ3NTE4NDYxMzY4TTEyNTk3MzIzNTQ3Mjc3NTc5MTQ0Njk4NDk2MzcyMjQyNjE1MzY4MDg1ODAxMzEzMzQzMTU1NzM1NTExMzMwMDAzODg0NzY3OTU3ODU0AgExATADTTE1NzkxNTg5NDcyNTU2ODI2MjYzMjMxNjQ0NzI4ODczMzM3NjI5MDE1MjY5OTg0Njk5NDA0MDczNjIzNjAzMzUyNTM3Njc4ODEzMTcxTDQ1NDc4NjY0OTkyNDg4ODE0NDk2NzYxNjExNTgwMjQ3NDgwNjA0ODUzNzMyNTAwMjk0MjM5MDQxMTMwMTc0MjI1MzkwMzcxNjI1MjcBMTF3aWFYTnpJam9pYUhSMGNITTZMeTlwWkM1MGQybDBZMmd1ZEhZdmIyRjFkR2d5SWl3AjJleUpoYkdjaU9pSlNVekkxTmlJc0luUjVjQ0k2SWtwWFZDSXNJbXRwWkNJNklqRWlmUU0yMDc5NDc4ODU1OTYyMDY2OTU5NjIwNjQ1NzAyMjk2NjE3Njk4NjY4ODcyNzg3NjEyODIyMzYyODExMzkxNjM4MDkyNzUwMjczNzkxMQoAAAAAAAAAYQAR6ZEOSb8am6giraofTNFOXN6N5etgegC1TsTKup7HNMtyZWPBn9WaDwPe+naJustyRgVE7K8umsX6h3Fa7UQMucbuFjDvPnERRKZI2wa7sihPcnTPvuU//O5QPMGkkgADAAIADX2rNYyNrapO+gBJp1sHQ2VVsQo2ghm7aA9wVxNJ13UBAzwbaHR0cHM6Ly9pZC50d2l0Y2gudHYvb2F1dGgyLflu6Eag/zG3tLd5CtZRYx9p1t34RovVSn/+uHFiYfcBAQA=".to_string());
}
Expand Down
7 changes: 7 additions & 0 deletions crates/sui-types/src/unit_tests/zklogin_test_vectors.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
[
{
"zklogin_inputs": "{\"proofPoints\":{\"a\":[\"17318089125952421736342263717932719437717844282410187957984751939942898251250\",\"11373966645469122582074082295985388258840681618268593976697325892280915681207\",\"1\"],\"b\":[[\"5939871147348834997361720122238980177152303274311047249905942384915768690895\",\"4533568271134785278731234570361482651996740791888285864966884032717049811708\"],[\"10564387285071555469753990661410840118635925466597037018058770041347518461368\",\"12597323547277579144698496372242615368085801313343155735511330003884767957854\"],[\"1\",\"0\"]],\"c\":[\"15791589472556826263231644728873337629015269984699404073623603352537678813171\",\"4547866499248881449676161158024748060485373250029423904113017422539037162527\",\"1\"]},\"issBase64Details\":{\"value\":\"wiaXNzIjoiaHR0cHM6Ly9pZC50d2l0Y2gudHYvb2F1dGgyIiw\",\"indexMod4\":2},\"headerBase64\":\"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEifQ\"}",
"kp": "suiprivkey1qplrnsltlrzxj3rn5syd6e863xs0227u4rayja6lz0exg623wftxjx4mql0",
"pk_bigint": "84029355920633174015103288781128426107680789454168570548782290541079926444544",
"randomness": "100681567828351849884072155819400689117",
"address_seed": "20794788559620669596206457022966176986688727876128223628113916380927502737911"
},
{
"zklogin_inputs": "{\"proofPoints\":{\"a\":[\"6273992944282656241643249967546436415683538330073309038295090020437032734417\",\"13925131915383843099385555626272032961923726580226697015319197132189118605257\",\"1\"],\"b\":[[\"7704007614120244428203234032774689081078638869317243551253010726338510436127\",\"4733691566008191204012705799062275962647301524570212343732751171422663135752\"],[\"9632116537411472981464147640393917418420287809518621999342206768832847695896\",\"8436287150321216550921548884226038273194202440574729038196401403024384113898\"],[\"1\",\"0\"]],\"c\":[\"5172184205489148831058189142062767553723109372209872440130518436384175126524\",\"5151432066138747209120687522208554041564827080797050716927769643447422123270\",\"1\"]},\"issBase64Details\":{\"value\":\"wiaXNzIjoiaHR0cHM6Ly9pZC50d2l0Y2gudHYvb2F1dGgyIiw\",\"indexMod4\":2},\"headerBase64\":\"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEifQ\"}",
"kp": "suiprivkey1qzdlfxn2qa2lj5uprl8pyhexs02sg2wrhdy7qaq50cqgnffw4c2477kg9h3",
Expand Down

0 comments on commit 736d47c

Please sign in to comment.