mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-01-02 13:23:10 +00:00
refactor. add tests
This commit is contained in:
parent
b2f79e2965
commit
0dcdb2188a
@ -76,87 +76,3 @@ impl Program {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test utils
|
||||
#[cfg(test)]
|
||||
impl Program {
|
||||
/// A program that changes the nonce of an account
|
||||
pub fn nonce_changer_program() -> Self {
|
||||
use test_program_methods::{NONCE_CHANGER_ELF, NONCE_CHANGER_ID};
|
||||
|
||||
Program {
|
||||
id: NONCE_CHANGER_ID,
|
||||
elf: NONCE_CHANGER_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that produces more output accounts than the inputs it received
|
||||
pub fn extra_output_program() -> Self {
|
||||
use test_program_methods::{EXTRA_OUTPUT_ELF, EXTRA_OUTPUT_ID};
|
||||
|
||||
Program {
|
||||
id: EXTRA_OUTPUT_ID,
|
||||
elf: EXTRA_OUTPUT_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that produces less output accounts than the inputs it received
|
||||
pub fn missing_output_program() -> Self {
|
||||
use test_program_methods::{MISSING_OUTPUT_ELF, MISSING_OUTPUT_ID};
|
||||
|
||||
Program {
|
||||
id: MISSING_OUTPUT_ID,
|
||||
elf: MISSING_OUTPUT_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that changes the program owner of an account to [0, 1, 2, 3, 4, 5, 6, 7]
|
||||
pub fn program_owner_changer() -> Self {
|
||||
use test_program_methods::{PROGRAM_OWNER_CHANGER_ELF, PROGRAM_OWNER_CHANGER_ID};
|
||||
|
||||
Program {
|
||||
id: PROGRAM_OWNER_CHANGER_ID,
|
||||
elf: PROGRAM_OWNER_CHANGER_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that transfers balance without caring about authorizations
|
||||
pub fn simple_balance_transfer() -> Self {
|
||||
use test_program_methods::{SIMPLE_BALANCE_TRANSFER_ELF, SIMPLE_BALANCE_TRANSFER_ID};
|
||||
|
||||
Program {
|
||||
id: SIMPLE_BALANCE_TRANSFER_ID,
|
||||
elf: SIMPLE_BALANCE_TRANSFER_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that modifies the data of an account
|
||||
pub fn data_changer() -> Self {
|
||||
use test_program_methods::{DATA_CHANGER_ELF, DATA_CHANGER_ID};
|
||||
|
||||
Program {
|
||||
id: DATA_CHANGER_ID,
|
||||
elf: DATA_CHANGER_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that mints balance
|
||||
pub fn minter() -> Self {
|
||||
use test_program_methods::{MINTER_ELF, MINTER_ID};
|
||||
|
||||
Program {
|
||||
id: MINTER_ID,
|
||||
elf: MINTER_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that burns balance
|
||||
pub fn burner() -> Self {
|
||||
use test_program_methods::{BURNER_ELF, BURNER_ID};
|
||||
|
||||
Program {
|
||||
id: BURNER_ID,
|
||||
elf: BURNER_ELF,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,383 +6,3 @@ mod signature;
|
||||
pub use private_key::PrivateKey;
|
||||
pub use public_key::PublicKey;
|
||||
pub use signature::Signature;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{PrivateKey, PublicKey, Signature};
|
||||
|
||||
fn hex_to_bytes<const N: usize>(hex: &str) -> [u8; N] {
|
||||
hex::decode(hex).unwrap().try_into().unwrap()
|
||||
}
|
||||
|
||||
pub struct TestVector {
|
||||
pub seckey: Option<PrivateKey>,
|
||||
pub pubkey: PublicKey,
|
||||
pub aux_rand: Option<[u8; 32]>,
|
||||
pub message: Option<Vec<u8>>,
|
||||
pub signature: Signature,
|
||||
pub verification_result: bool,
|
||||
}
|
||||
|
||||
/// Test vectors from
|
||||
/// https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.csv
|
||||
//
|
||||
pub fn test_vectors() -> Vec<TestVector> {
|
||||
vec![
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0000000000000000000000000000000000000000000000000000000000000003",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)),
|
||||
message: Some( hex::decode("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000001",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"C90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B14E5C9",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DD308AFEC5777E13121FA72B9CC1B7CC0139715309B086C960E18FD969774EB8",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"C87AA53824B4D7AE2EB035A2B5BBBCCC080E76CDC6D1692C4B0B62D798E6D906",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("7E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"5831AAEED7B44BB74E5EAB94BA9D4294C49BCF2A60728D8B4C200F50DD313C1BAB745879A5AD954A72C45A91C3A51D3C7ADEA98D82F8481E0E1E03674A6F3FB7",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0B432B2677937381AEF05BB02A66ECD012773062CF3FA2549E44F58ED2401710",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"25D1DFF95105F5253C4022F628A996AD3A0D95FBF21D468A1B33F8C160D8F517",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"7EB0509757E246F19449885651611CB965ECC1A187DD51B64FDA1EDC9637D5EC97582B9CB13DB3933705B32BA982AF5AF25FD78881EBB32771FC5922EFC66EA3",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"D69C3509BB99E412E68B0FE8544E72837DFA30746D8BE2AA65975F29D22DC7B9",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6376AFB1548AF603B3EB45C9F8207DEE1060CB71C04E80F593060B07D28308D7F4",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
|
||||
// Test with invalid public key
|
||||
// TestVector {
|
||||
// seckey: None,
|
||||
// pubkey: PublicKey(hex_to_bytes(
|
||||
// "EEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34",
|
||||
// )),
|
||||
// aux_rand: None,
|
||||
// message: Some(
|
||||
// hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
// ),
|
||||
// signature: Signature {
|
||||
// value: hex_to_bytes(
|
||||
// "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E17776969E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B",
|
||||
// ),
|
||||
// },
|
||||
// verification_result: false,
|
||||
// },
|
||||
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"FFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A14602975563CC27944640AC607CD107AE10923D9EF7A73C643E166BE5EBEAFA34B1AC553E2",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"1FA62E331EDBC21C394792D2AB1100A7B432B013DF3F6FF4F99FCB33E0E1515F28890B3EDB6E7189B630448B515CE4F8622A954CFE545735AAEA5134FCCDB2BD",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E177769961764B3AA9B2FFCB6EF947B6887A226E8D7C93E00C5ED0C1834FF0D0C2E6DA6",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000123DDA8328AF9C23A94C1FEECFD123BA4FB73476F0D594DCB65C6425BD186051",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"00000000000000000000000000000000000000000000000000000000000000017615FBAF5AE28864013C099742DEADB4DBA87F11AC6754F93780D5A1837CF197",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"4A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D69E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F69E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E177769FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
|
||||
// Test with invalid public key
|
||||
// TestVector {
|
||||
// seckey: None,
|
||||
// pubkey: PublicKey(hex_to_bytes(
|
||||
// "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC30",
|
||||
// )),
|
||||
// aux_rand: None,
|
||||
// message: Some(
|
||||
// hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
// ),
|
||||
// signature: Signature {
|
||||
// value: hex_to_bytes(
|
||||
// "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E17776969E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B",
|
||||
// ),
|
||||
// },
|
||||
// verification_result: false,
|
||||
// },
|
||||
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0340034003400340034003400340034003400340034003400340034003400340",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)),
|
||||
message: None,
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"71535DB165ECD9FBBC046E5FFAEA61186BB6AD436732FCCC25291A55895464CF6069CE26BF03466228F19A3A62DB8A649F2D560FAC652827D1AF0574E427AB63",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0340034003400340034003400340034003400340034003400340034003400340",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("11").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"08A20A0AFEF64124649232E0693C583AB1B9934AE63B4C3511F3AE1134C6A303EA3173BFEA6683BD101FA5AA5DBC1996FE7CACFC5A577D33EC14564CEC2BACBF",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0340034003400340034003400340034003400340034003400340034003400340",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("0102030405060708090A0B0C0D0E0F1011").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"5130F39A4059B43BC7CAC09A19ECE52B5D8699D1A71E3C52DA9AFDB6B50AC370C4A482B77BF960F8681540E25B6771ECE1E5A37FD80E5A51897C5566A97EA5A5",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0340034003400340034003400340034003400340034003400340034003400340",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"403B12B0D8555A344175EA7EC746566303321E5DBFA8BE6F091635163ECA79A8585ED3E3170807E7C03B720FC54C7B23897FCBA0E9D0B4A06894CFD249F22367",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,23 +16,3 @@ impl PublicKey {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{PublicKey, signature::tests::test_vectors};
|
||||
|
||||
#[test]
|
||||
fn test_public_key_generation_from_bip340_test_vectors() {
|
||||
for (i, test_vector) in test_vectors().iter().enumerate() {
|
||||
let Some(private_key) = &test_vector.seckey else {
|
||||
continue;
|
||||
};
|
||||
let public_key = PublicKey::new(private_key);
|
||||
let expected_public_key = &test_vector.pubkey;
|
||||
assert_eq!(
|
||||
&public_key, expected_public_key,
|
||||
"Failed test vector at index {i}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,13 +8,17 @@ pub struct Signature {
|
||||
}
|
||||
|
||||
impl Signature {
|
||||
pub(crate) fn new(key: &PrivateKey, message: &[u8]) -> Self {
|
||||
pub fn new(key: &PrivateKey, message: &[u8]) -> Self {
|
||||
let mut aux_random = [0u8; 32];
|
||||
OsRng.fill_bytes(&mut aux_random);
|
||||
Self::new_with_aux_random(key, message, aux_random)
|
||||
}
|
||||
|
||||
fn new_with_aux_random(key: &PrivateKey, message: &[u8], aux_random: [u8; 32]) -> Self {
|
||||
pub(crate) fn new_with_aux_random(
|
||||
key: &PrivateKey,
|
||||
message: &[u8],
|
||||
aux_random: [u8; 32],
|
||||
) -> Self {
|
||||
let value = {
|
||||
let secp = secp256k1::Secp256k1::new();
|
||||
let secret_key = secp256k1::SecretKey::from_byte_array(key.0).unwrap();
|
||||
@ -32,45 +36,3 @@ impl Signature {
|
||||
secp.verify_schnorr(&sig, bytes, &pk).is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{Signature, signature::tests::test_vectors};
|
||||
|
||||
#[test]
|
||||
fn test_signature_generation_from_bip340_test_vectors() {
|
||||
for (i, test_vector) in test_vectors().into_iter().enumerate() {
|
||||
let Some(private_key) = test_vector.seckey else {
|
||||
continue;
|
||||
};
|
||||
let Some(aux_random) = test_vector.aux_rand else {
|
||||
continue;
|
||||
};
|
||||
let Some(message) = test_vector.message else {
|
||||
continue;
|
||||
};
|
||||
if !test_vector.verification_result {
|
||||
continue;
|
||||
}
|
||||
let expected_signature = &test_vector.signature;
|
||||
|
||||
let signature = Signature::new_with_aux_random(&private_key, &message, aux_random);
|
||||
|
||||
assert_eq!(&signature, expected_signature, "Failed test vector {i}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_signature_verification_from_bip340_test_vectors() {
|
||||
for (i, test_vector) in test_vectors().into_iter().enumerate() {
|
||||
let message = test_vector.message.unwrap_or(vec![]);
|
||||
let expected_result = test_vector.verification_result;
|
||||
|
||||
let result = test_vector
|
||||
.signature
|
||||
.is_valid_for(&message, &test_vector.pubkey);
|
||||
|
||||
assert_eq!(result, expected_result, "Failed test vector {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@ impl V01State {
|
||||
this
|
||||
}
|
||||
|
||||
fn insert_program(&mut self, program: Program) {
|
||||
pub(crate) fn insert_program(&mut self, program: Program) {
|
||||
self.builtin_programs.insert(program.id(), program);
|
||||
}
|
||||
|
||||
@ -73,59 +73,9 @@ impl V01State {
|
||||
pub(crate) fn builtin_programs(&self) -> &HashMap<ProgramId, Program> {
|
||||
&self.builtin_programs
|
||||
}
|
||||
}
|
||||
|
||||
// Test utils
|
||||
#[cfg(test)]
|
||||
impl V01State {
|
||||
/// Include test programs in the builtin programs map
|
||||
pub fn with_test_programs(mut self) -> Self {
|
||||
self.insert_program(Program::nonce_changer_program());
|
||||
self.insert_program(Program::extra_output_program());
|
||||
self.insert_program(Program::missing_output_program());
|
||||
self.insert_program(Program::program_owner_changer());
|
||||
self.insert_program(Program::simple_balance_transfer());
|
||||
self.insert_program(Program::data_changer());
|
||||
self.insert_program(Program::minter());
|
||||
self.insert_program(Program::burner());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_non_default_accounts_but_default_program_owners(mut self) -> Self {
|
||||
let account_with_default_values_except_balance = Account {
|
||||
balance: 100,
|
||||
..Account::default()
|
||||
};
|
||||
let account_with_default_values_except_nonce = Account {
|
||||
nonce: 37,
|
||||
..Account::default()
|
||||
};
|
||||
let account_with_default_values_except_data = Account {
|
||||
data: vec![0xca, 0xfe],
|
||||
..Account::default()
|
||||
};
|
||||
self.public_state.insert(
|
||||
Address::new([255; 32]),
|
||||
account_with_default_values_except_balance,
|
||||
);
|
||||
self.public_state.insert(
|
||||
Address::new([254; 32]),
|
||||
account_with_default_values_except_nonce,
|
||||
);
|
||||
self.public_state.insert(
|
||||
Address::new([253; 32]),
|
||||
account_with_default_values_except_data,
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_account_owned_by_burner_program(mut self) -> Self {
|
||||
let account = Account {
|
||||
program_owner: Program::burner().id(),
|
||||
balance: 100,
|
||||
..Default::default()
|
||||
};
|
||||
self.public_state.insert(Address::new([252; 32]), account);
|
||||
self
|
||||
#[cfg(test)]
|
||||
pub fn force_insert_account(&mut self, address: Address, account: Account) {
|
||||
self.public_state.insert(address, account);
|
||||
}
|
||||
}
|
||||
|
||||
367
nssa/src/tests/bip340_test_vectors.rs
Normal file
367
nssa/src/tests/bip340_test_vectors.rs
Normal file
@ -0,0 +1,367 @@
|
||||
use crate::{PrivateKey, PublicKey, Signature};
|
||||
|
||||
fn hex_to_bytes<const N: usize>(hex: &str) -> [u8; N] {
|
||||
hex::decode(hex).unwrap().try_into().unwrap()
|
||||
}
|
||||
|
||||
pub struct TestVector {
|
||||
pub seckey: Option<PrivateKey>,
|
||||
pub pubkey: PublicKey,
|
||||
pub aux_rand: Option<[u8; 32]>,
|
||||
pub message: Option<Vec<u8>>,
|
||||
pub signature: Signature,
|
||||
pub verification_result: bool,
|
||||
}
|
||||
|
||||
/// Test vectors from
|
||||
/// https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.csv
|
||||
//
|
||||
pub fn test_vectors() -> Vec<TestVector> {
|
||||
vec![
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0000000000000000000000000000000000000000000000000000000000000003",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("0000000000000000000000000000000000000000000000000000000000000000")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000001",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"C90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B14E5C9",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DD308AFEC5777E13121FA72B9CC1B7CC0139715309B086C960E18FD969774EB8",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"C87AA53824B4D7AE2EB035A2B5BBBCCC080E76CDC6D1692C4B0B62D798E6D906",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("7E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"5831AAEED7B44BB74E5EAB94BA9D4294C49BCF2A60728D8B4C200F50DD313C1BAB745879A5AD954A72C45A91C3A51D3C7ADEA98D82F8481E0E1E03674A6F3FB7",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0B432B2677937381AEF05BB02A66ECD012773062CF3FA2549E44F58ED2401710",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"25D1DFF95105F5253C4022F628A996AD3A0D95FBF21D468A1B33F8C160D8F517",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"7EB0509757E246F19449885651611CB965ECC1A187DD51B64FDA1EDC9637D5EC97582B9CB13DB3933705B32BA982AF5AF25FD78881EBB32771FC5922EFC66EA3",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"D69C3509BB99E412E68B0FE8544E72837DFA30746D8BE2AA65975F29D22DC7B9",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6376AFB1548AF603B3EB45C9F8207DEE1060CB71C04E80F593060B07D28308D7F4",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
// Test with invalid public key
|
||||
// TestVector {
|
||||
// seckey: None,
|
||||
// pubkey: PublicKey(hex_to_bytes(
|
||||
// "EEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34",
|
||||
// )),
|
||||
// aux_rand: None,
|
||||
// message: Some(
|
||||
// hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
// ),
|
||||
// signature: Signature {
|
||||
// value: hex_to_bytes(
|
||||
// "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E17776969E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B",
|
||||
// ),
|
||||
// },
|
||||
// verification_result: false,
|
||||
// },
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"FFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A14602975563CC27944640AC607CD107AE10923D9EF7A73C643E166BE5EBEAFA34B1AC553E2",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"1FA62E331EDBC21C394792D2AB1100A7B432B013DF3F6FF4F99FCB33E0E1515F28890B3EDB6E7189B630448B515CE4F8622A954CFE545735AAEA5134FCCDB2BD",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E177769961764B3AA9B2FFCB6EF947B6887A226E8D7C93E00C5ED0C1834FF0D0C2E6DA6",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000123DDA8328AF9C23A94C1FEECFD123BA4FB73476F0D594DCB65C6425BD186051",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"00000000000000000000000000000000000000000000000000000000000000017615FBAF5AE28864013C099742DEADB4DBA87F11AC6754F93780D5A1837CF197",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"4A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D69E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F69E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
TestVector {
|
||||
seckey: None,
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
|
||||
)),
|
||||
aux_rand: None,
|
||||
message: Some(
|
||||
hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
|
||||
.unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E177769FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",
|
||||
),
|
||||
},
|
||||
verification_result: false,
|
||||
},
|
||||
// Test with invalid public key
|
||||
// TestVector {
|
||||
// seckey: None,
|
||||
// pubkey: PublicKey(hex_to_bytes(
|
||||
// "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC30",
|
||||
// )),
|
||||
// aux_rand: None,
|
||||
// message: Some(
|
||||
// hex::decode("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89").unwrap(),
|
||||
// ),
|
||||
// signature: Signature {
|
||||
// value: hex_to_bytes(
|
||||
// "6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E17776969E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B",
|
||||
// ),
|
||||
// },
|
||||
// verification_result: false,
|
||||
// },
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0340034003400340034003400340034003400340034003400340034003400340",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)),
|
||||
message: None,
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"71535DB165ECD9FBBC046E5FFAEA61186BB6AD436732FCCC25291A55895464CF6069CE26BF03466228F19A3A62DB8A649F2D560FAC652827D1AF0574E427AB63",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0340034003400340034003400340034003400340034003400340034003400340",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)),
|
||||
message: Some(hex::decode("11").unwrap()),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"08A20A0AFEF64124649232E0693C583AB1B9934AE63B4C3511F3AE1134C6A303EA3173BFEA6683BD101FA5AA5DBC1996FE7CACFC5A577D33EC14564CEC2BACBF",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0340034003400340034003400340034003400340034003400340034003400340",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)),
|
||||
message: Some(hex::decode("0102030405060708090A0B0C0D0E0F1011").unwrap()),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"5130F39A4059B43BC7CAC09A19ECE52B5D8699D1A71E3C52DA9AFDB6B50AC370C4A482B77BF960F8681540E25B6771ECE1E5A37FD80E5A51897C5566A97EA5A5",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
TestVector {
|
||||
seckey: Some(PrivateKey(hex_to_bytes(
|
||||
"0340034003400340034003400340034003400340034003400340034003400340",
|
||||
))),
|
||||
pubkey: PublicKey(hex_to_bytes(
|
||||
"778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117",
|
||||
)),
|
||||
aux_rand: Some(hex_to_bytes::<32>(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)),
|
||||
message: Some(
|
||||
hex::decode("99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999").unwrap(),
|
||||
),
|
||||
signature: Signature {
|
||||
value: hex_to_bytes(
|
||||
"403B12B0D8555A344175EA7EC746566303321E5DBFA8BE6F091635163ECA79A8585ED3E3170807E7C03B720FC54C7B23897FCBA0E9D0B4A06894CFD249F22367",
|
||||
),
|
||||
},
|
||||
verification_result: true,
|
||||
},
|
||||
]
|
||||
}
|
||||
@ -1,2 +1,5 @@
|
||||
mod bip340_test_vectors;
|
||||
mod program_tests;
|
||||
mod signature_tests;
|
||||
mod state_tests;
|
||||
mod valid_execution_tests;
|
||||
|
||||
123
nssa/src/tests/program_tests.rs
Normal file
123
nssa/src/tests/program_tests.rs
Normal file
@ -0,0 +1,123 @@
|
||||
use nssa_core::account::{Account, AccountWithMetadata};
|
||||
|
||||
use crate::program::Program;
|
||||
|
||||
impl Program {
|
||||
/// A program that changes the nonce of an account
|
||||
pub fn nonce_changer_program() -> Self {
|
||||
use test_program_methods::{NONCE_CHANGER_ELF, NONCE_CHANGER_ID};
|
||||
|
||||
Program {
|
||||
id: NONCE_CHANGER_ID,
|
||||
elf: NONCE_CHANGER_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that produces more output accounts than the inputs it received
|
||||
pub fn extra_output_program() -> Self {
|
||||
use test_program_methods::{EXTRA_OUTPUT_ELF, EXTRA_OUTPUT_ID};
|
||||
|
||||
Program {
|
||||
id: EXTRA_OUTPUT_ID,
|
||||
elf: EXTRA_OUTPUT_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that produces less output accounts than the inputs it received
|
||||
pub fn missing_output_program() -> Self {
|
||||
use test_program_methods::{MISSING_OUTPUT_ELF, MISSING_OUTPUT_ID};
|
||||
|
||||
Program {
|
||||
id: MISSING_OUTPUT_ID,
|
||||
elf: MISSING_OUTPUT_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that changes the program owner of an account to [0, 1, 2, 3, 4, 5, 6, 7]
|
||||
pub fn program_owner_changer() -> Self {
|
||||
use test_program_methods::{PROGRAM_OWNER_CHANGER_ELF, PROGRAM_OWNER_CHANGER_ID};
|
||||
|
||||
Program {
|
||||
id: PROGRAM_OWNER_CHANGER_ID,
|
||||
elf: PROGRAM_OWNER_CHANGER_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that transfers balance without caring about authorizations
|
||||
pub fn simple_balance_transfer() -> Self {
|
||||
use test_program_methods::{SIMPLE_BALANCE_TRANSFER_ELF, SIMPLE_BALANCE_TRANSFER_ID};
|
||||
|
||||
Program {
|
||||
id: SIMPLE_BALANCE_TRANSFER_ID,
|
||||
elf: SIMPLE_BALANCE_TRANSFER_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that modifies the data of an account
|
||||
pub fn data_changer() -> Self {
|
||||
use test_program_methods::{DATA_CHANGER_ELF, DATA_CHANGER_ID};
|
||||
|
||||
Program {
|
||||
id: DATA_CHANGER_ID,
|
||||
elf: DATA_CHANGER_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that mints balance
|
||||
pub fn minter() -> Self {
|
||||
use test_program_methods::{MINTER_ELF, MINTER_ID};
|
||||
|
||||
Program {
|
||||
id: MINTER_ID,
|
||||
elf: MINTER_ELF,
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that burns balance
|
||||
pub fn burner() -> Self {
|
||||
use test_program_methods::{BURNER_ELF, BURNER_ID};
|
||||
|
||||
Program {
|
||||
id: BURNER_ID,
|
||||
elf: BURNER_ELF,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_program_execution() {
|
||||
let program = Program::simple_balance_transfer();
|
||||
let balance_to_move: u128 = 11223344556677;
|
||||
let instruction_data = Program::serialize_instruction(balance_to_move).unwrap();
|
||||
let sender = AccountWithMetadata {
|
||||
account: Account {
|
||||
balance: 77665544332211,
|
||||
..Account::default()
|
||||
},
|
||||
is_authorized: false,
|
||||
};
|
||||
let recipient = AccountWithMetadata {
|
||||
account: Account::default(),
|
||||
is_authorized: false,
|
||||
};
|
||||
|
||||
let expected_sender_post = Account {
|
||||
balance: 77665544332211 - balance_to_move,
|
||||
program_owner: program.id(),
|
||||
..Account::default()
|
||||
};
|
||||
let expected_recipient_post = Account {
|
||||
balance: balance_to_move,
|
||||
// Program claims the account since the pre_state has default prorgam owner
|
||||
program_owner: program.id(),
|
||||
..Account::default()
|
||||
};
|
||||
let [sender_post, recipient_post] = program
|
||||
.execute(&[sender, recipient], &instruction_data)
|
||||
.unwrap()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(sender_post, expected_sender_post);
|
||||
assert_eq!(recipient_post, expected_recipient_post);
|
||||
}
|
||||
53
nssa/src/tests/signature_tests.rs
Normal file
53
nssa/src/tests/signature_tests.rs
Normal file
@ -0,0 +1,53 @@
|
||||
use crate::{PublicKey, Signature, tests::bip340_test_vectors};
|
||||
|
||||
#[test]
|
||||
fn test_signature_generation_from_bip340_test_vectors() {
|
||||
for (i, test_vector) in bip340_test_vectors::test_vectors().into_iter().enumerate() {
|
||||
let Some(private_key) = test_vector.seckey else {
|
||||
continue;
|
||||
};
|
||||
let Some(aux_random) = test_vector.aux_rand else {
|
||||
continue;
|
||||
};
|
||||
let Some(message) = test_vector.message else {
|
||||
continue;
|
||||
};
|
||||
if !test_vector.verification_result {
|
||||
continue;
|
||||
}
|
||||
let expected_signature = &test_vector.signature;
|
||||
|
||||
let signature = Signature::new_with_aux_random(&private_key, &message, aux_random);
|
||||
|
||||
assert_eq!(&signature, expected_signature, "Failed test vector {i}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_signature_verification_from_bip340_test_vectors() {
|
||||
for (i, test_vector) in bip340_test_vectors::test_vectors().into_iter().enumerate() {
|
||||
let message = test_vector.message.unwrap_or(vec![]);
|
||||
let expected_result = test_vector.verification_result;
|
||||
|
||||
let result = test_vector
|
||||
.signature
|
||||
.is_valid_for(&message, &test_vector.pubkey);
|
||||
|
||||
assert_eq!(result, expected_result, "Failed test vector {i}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_public_key_generation_from_bip340_test_vectors() {
|
||||
for (i, test_vector) in bip340_test_vectors::test_vectors().into_iter().enumerate() {
|
||||
let Some(private_key) = &test_vector.seckey else {
|
||||
continue;
|
||||
};
|
||||
let public_key = PublicKey::new(private_key);
|
||||
let expected_public_key = &test_vector.pubkey;
|
||||
assert_eq!(
|
||||
&public_key, expected_public_key,
|
||||
"Failed test vector at index {i}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,59 @@ use crate::{
|
||||
Address, PublicTransaction, V01State, error::NssaError, program::Program, public_transaction,
|
||||
};
|
||||
|
||||
impl V01State {
|
||||
/// Include test programs in the builtin programs map
|
||||
pub fn with_test_programs(mut self) -> Self {
|
||||
self.insert_program(Program::nonce_changer_program());
|
||||
self.insert_program(Program::extra_output_program());
|
||||
self.insert_program(Program::missing_output_program());
|
||||
self.insert_program(Program::program_owner_changer());
|
||||
self.insert_program(Program::simple_balance_transfer());
|
||||
self.insert_program(Program::data_changer());
|
||||
self.insert_program(Program::minter());
|
||||
self.insert_program(Program::burner());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_non_default_accounts_but_default_program_owners(mut self) -> Self {
|
||||
let account_with_default_values_except_balance = Account {
|
||||
balance: 100,
|
||||
..Account::default()
|
||||
};
|
||||
let account_with_default_values_except_nonce = Account {
|
||||
nonce: 37,
|
||||
..Account::default()
|
||||
};
|
||||
let account_with_default_values_except_data = Account {
|
||||
data: vec![0xca, 0xfe],
|
||||
..Account::default()
|
||||
};
|
||||
self.force_insert_account(
|
||||
Address::new([255; 32]),
|
||||
account_with_default_values_except_balance,
|
||||
);
|
||||
self.force_insert_account(
|
||||
Address::new([254; 32]),
|
||||
account_with_default_values_except_nonce,
|
||||
);
|
||||
self.force_insert_account(
|
||||
Address::new([253; 32]),
|
||||
account_with_default_values_except_data,
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_account_owned_by_burner_program(mut self) -> Self {
|
||||
let account = Account {
|
||||
program_owner: Program::burner().id(),
|
||||
balance: 100,
|
||||
..Default::default()
|
||||
};
|
||||
self.force_insert_account(Address::new([252; 32]), account);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_program_should_fail_if_modifies_nonces() {
|
||||
let initial_data = [([1; 32], 100)];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user