diff --git a/examples/empty.wit b/examples/empty.wit deleted file mode 100644 index 0967ef4..0000000 --- a/examples/empty.wit +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/examples/escrow_with_delay.timeout.wit b/examples/escrow_with_delay.timeout.wit index cb942b4..8e282ee 100644 --- a/examples/escrow_with_delay.timeout.wit +++ b/examples/escrow_with_delay.timeout.wit @@ -1,6 +1,6 @@ { "transfer_or_timeout": { - "value": "Right([0xed, 0xb6, 0x86, 0x50, 0x94, 0x26, 0x0f, 0x85, 0x58, 0x72, 0x82, 0x33, 0xaa, 0xe0, 0x17, 0xdd, 0x09, 0x69, 0xa2, 0xaf, 0xe5, 0xf0, 0x8c, 0x28, 0x2e, 0x1a, 0xb6, 0x59, 0xbf, 0x24, 0x62, 0x68, 0x4c, 0x99, 0xa6, 0x4a, 0x2a, 0x57, 0x24, 0x63, 0x58, 0xa0, 0xd6, 0x32, 0x67, 0x17, 0x78, 0xd0, 0x16, 0xe6, 0xdf, 0x73, 0x81, 0x29, 0x3d, 0xd5, 0xbb, 0x9f, 0x09, 0x99, 0xd3, 0x86, 0x40, 0xd4])", - "type": "Either<[Option<[u8; 64]>; 3], [u8; 64]>" + "value": "Right(0xedb6865094260f8558728233aae017dd0969a2afe5f08c282e1ab659bf2462684c99a64a2a57246358a0d632671778d016e6df7381293dd5bb9f0999d38640d4)", + "type": "Either<[Option; 3], Signature>" } } diff --git a/examples/hodl_vault.wit b/examples/hodl_vault.wit index 747cfa9..a954e75 100644 --- a/examples/hodl_vault.wit +++ b/examples/hodl_vault.wit @@ -8,11 +8,11 @@ "type": "u32" }, "oracle_sig": { - "value": "[0x90, 0x23, 0x1b, 0x8d, 0xe9, 0x6a, 0x1f, 0x94, 0x0d, 0xdc, 0xf4, 0x06, 0xfe, 0x83, 0x89, 0x41, 0x7c, 0xa8, 0xfb, 0x0b, 0x03, 0x15, 0x16, 0x08, 0xe2, 0xf9, 0x4b, 0x31, 0xb4, 0x43, 0xa7, 0xe0, 0xd2, 0x6a, 0x12, 0xe4, 0x37, 0xdf, 0x69, 0x02, 0x8f, 0x09, 0x02, 0x7c, 0x37, 0xd5, 0xf6, 0x74, 0x2a, 0x10, 0xc1, 0xe8, 0x86, 0x40, 0x61, 0xd1, 0x19, 0xb8, 0xbb, 0xce, 0x96, 0x2d, 0x26, 0xd3]", - "type": "[u8; 64]" + "value": "0x90231b8de96a1f940ddcf406fe8389417ca8fb0b03151608e2f94b31b443a7e0d26a12e437df69028f09027c37d5f6742a10c1e8864061d119b8bbce962d26d3", + "type": "Signature" }, "owner_sig": { - "value": "[0xf2, 0x34, 0x1f, 0x57, 0x1f, 0x06, 0x92, 0x16, 0xed, 0xfc, 0x72, 0x82, 0x2f, 0x60, 0x94, 0xb8, 0xec, 0x33, 0x9c, 0x2f, 0x72, 0xdc, 0x64, 0xae, 0xa0, 0xee, 0xd1, 0xe3, 0xd6, 0x0a, 0xbf, 0x45, 0x72, 0xfd, 0xd0, 0x46, 0x18, 0xe5, 0xb5, 0xbc, 0x67, 0x2c, 0xcd, 0x71, 0xcf, 0xaf, 0x12, 0x5b, 0x6c, 0x1b, 0x10, 0x1a, 0xec, 0xa3, 0xa7, 0xb9, 0x38, 0xfe, 0x83, 0x93, 0x2a, 0xb3, 0x87, 0x43]", - "type": "[u8; 64]" + "value": "0xf2341f571f069216edfc72822f6094b8ec339c2f72dc64aea0eed1e3d60abf4572fdd04618e5b5bc672ccd71cfaf125b6c1b101aeca3a7b938fe83932ab38743", + "type": "Signature" } } diff --git a/examples/htlc.complete.wit b/examples/htlc.complete.wit index e8151de..950c6d4 100644 --- a/examples/htlc.complete.wit +++ b/examples/htlc.complete.wit @@ -1,6 +1,6 @@ { "complete_or_cancel": { - "value": "Left((0x0000000000000000000000000000000000000000000000000000000000000000, [0xf7, 0x4b, 0x3c, 0xa5, 0x74, 0x64, 0x7f, 0x85, 0x95, 0x62, 0x4b, 0x12, 0x93, 0x24, 0xaf, 0xa2, 0xf3, 0x8b, 0x59, 0x8a, 0x9c, 0x1c, 0x7c, 0xfc, 0x5f, 0x08, 0xa9, 0xc0, 0x36, 0xec, 0x5a, 0xcd, 0x3c, 0x0f, 0xbb, 0x9e, 0xd3, 0xda, 0xe5, 0xca, 0x23, 0xa0, 0xa6, 0x5a, 0x34, 0xb5, 0xd6, 0xcc, 0xcd, 0xd6, 0xba, 0x24, 0x89, 0x85, 0xd6, 0x04, 0x1f, 0x7b, 0x21, 0x26, 0x2b, 0x17, 0xaf, 0x6f]))", - "type": "Either<(u256, [u8; 64]), [u8; 64]>" + "value": "Left((0x0000000000000000000000000000000000000000000000000000000000000000, 0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f))", + "type": "Either<(u256, Signature), Signature>" } } diff --git a/examples/htlc.simf b/examples/htlc.simf index 491c6af..3ea7d7e 100644 --- a/examples/htlc.simf +++ b/examples/htlc.simf @@ -15,7 +15,7 @@ fn sha2(string: u256) -> u256 { jet::sha_256_ctx_8_finalize(hasher) } -fn checksig(pk: u256, sig: Signature) { +fn checksig(pk: Pubkey, sig: Signature) { let msg: u256 = jet::sig_all_hash(); jet::bip_0340_verify((pk, msg), sig); } diff --git a/examples/last_will.inherit.wit b/examples/last_will.inherit.wit index 72f9650..f9d592c 100644 --- a/examples/last_will.inherit.wit +++ b/examples/last_will.inherit.wit @@ -1,6 +1,6 @@ { "inherit_or_not": { - "value": "Left([0x75, 0x52, 0x01, 0xbb, 0x62, 0xb0, 0xa8, 0xb8, 0xd1, 0x8f, 0xd1, 0x2f, 0xc0, 0x29, 0x51, 0xea, 0x39, 0x98, 0xba, 0x42, 0xbf, 0xc6, 0x66, 0x4d, 0xaa, 0xf8, 0xa0, 0xd2, 0x29, 0x8c, 0xad, 0x43, 0xcd, 0xc2, 0x13, 0x58, 0xc7, 0xc8, 0x2f, 0x37, 0x65, 0x42, 0x75, 0xdc, 0x2f, 0xea, 0x8c, 0x85, 0x8a, 0xdb, 0xe9, 0x7b, 0xac, 0x92, 0x82, 0x8b, 0x49, 0x8a, 0x5a, 0x23, 0x70, 0x04, 0xdb, 0x6f])", - "type": "Either<[u8; 64], Either<[u8; 64], [u8; 64]>>" + "value": "Left(0x755201bb62b0a8b8d18fd12fc02951ea3998ba42bfc6664daaf8a0d2298cad43cdc21358c7c82f37654275dc2fea8c858adbe97bac92828b498a5a237004db6f)", + "type": "Either>" } } diff --git a/examples/p2ms.wit b/examples/p2ms.wit index ebccacf..fa3b64f 100644 --- a/examples/p2ms.wit +++ b/examples/p2ms.wit @@ -1,6 +1,6 @@ { "maybe_sigs": { - "value": "[Some([0xf7, 0x4b, 0x3c, 0xa5, 0x74, 0x64, 0x7f, 0x85, 0x95, 0x62, 0x4b, 0x12, 0x93, 0x24, 0xaf, 0xa2, 0xf3, 0x8b, 0x59, 0x8a, 0x9c, 0x1c, 0x7c, 0xfc, 0x5f, 0x08, 0xa9, 0xc0, 0x36, 0xec, 0x5a, 0xcd, 0x3c, 0x0f, 0xbb, 0x9e, 0xd3, 0xda, 0xe5, 0xca, 0x23, 0xa0, 0xa6, 0x5a, 0x34, 0xb5, 0xd6, 0xcc, 0xcd, 0xd6, 0xba, 0x24, 0x89, 0x85, 0xd6, 0x04, 0x1f, 0x7b, 0x21, 0x26, 0x2b, 0x17, 0xaf, 0x6f]), None, Some([0x29, 0xdb, 0xea, 0xb5, 0x62, 0x8a, 0xe4, 0x72, 0xbc, 0xe3, 0xe0, 0x87, 0x28, 0xea, 0xd1, 0x99, 0x7e, 0xf7, 0x89, 0xd4, 0xf0, 0x4b, 0x5b, 0xe3, 0x9c, 0xc0, 0x8b, 0x36, 0x2d, 0xc2, 0x29, 0xf5, 0x53, 0xfd, 0x35, 0x3f, 0x8a, 0x0a, 0xcf, 0xfd, 0xfb, 0xdd, 0xd4, 0x71, 0xd1, 0x5a, 0x0d, 0xda, 0x3b, 0x30, 0x68, 0x42, 0x41, 0x6f, 0xf2, 0x46, 0xbc, 0x07, 0x46, 0x2e, 0x56, 0x67, 0xeb, 0x89])]", - "type": "[Option<[u8; 64]>; 3]" + "value": "[Some(0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f), None, Some(0x29dbeab5628ae472bce3e08728ead1997ef789d4f04b5be39cc08b362dc229f553fd353f8a0acffdfbddd471d15a0dda3b306842416ff246bc07462e5667eb89)]", + "type": "[Option; 3]" } } diff --git a/examples/p2pk.simf b/examples/p2pk.simf index fb91bed..767fbf2 100644 --- a/examples/p2pk.simf +++ b/examples/p2pk.simf @@ -6,8 +6,8 @@ * https://docs.ivylang.org/bitcoin/language/ExampleContracts.html#lockwithpublickey */ fn main() { - let pk: u256 = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; // 1 * G + let pk: Pubkey = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; // 1 * G let msg: u256 = jet::sig_all_hash(); - let sig: [u8; 64] = witness("sig"); + let sig: Signature = witness("sig"); jet::bip_0340_verify((pk, msg), sig) } diff --git a/examples/p2pk.wit b/examples/p2pk.wit index 54c4f63..d7e1751 100644 --- a/examples/p2pk.wit +++ b/examples/p2pk.wit @@ -1,6 +1,6 @@ { "sig": { - "value": "[0xf7, 0x4b, 0x3c, 0xa5, 0x74, 0x64, 0x7f, 0x85, 0x95, 0x62, 0x4b, 0x12, 0x93, 0x24, 0xaf, 0xa2, 0xf3, 0x8b, 0x59, 0x8a, 0x9c, 0x1c, 0x7c, 0xfc, 0x5f, 0x08, 0xa9, 0xc0, 0x36, 0xec, 0x5a, 0xcd, 0x3c, 0x0f, 0xbb, 0x9e, 0xd3, 0xda, 0xe5, 0xca, 0x23, 0xa0, 0xa6, 0x5a, 0x34, 0xb5, 0xd6, 0xcc, 0xcd, 0xd6, 0xba, 0x24, 0x89, 0x85, 0xd6, 0x04, 0x1f, 0x7b, 0x21, 0x26, 0x2b, 0x17, 0xaf, 0x6f]", - "type": "[u8; 64]" + "value": "0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f", + "type": "Signature" } } diff --git a/examples/p2pkh.simf b/examples/p2pkh.simf index c8be316..09b5ece 100644 --- a/examples/p2pkh.simf +++ b/examples/p2pkh.simf @@ -13,12 +13,12 @@ fn sha2(string: u256) -> u256 { } fn main() { - let pk: u256 = witness("pk"); + let pk: Pubkey = witness("pk"); let expected_pk_hash: u256 = 0x132f39a98c31baaddba6525f5d43f2954472097fa15265f45130bfdb70e51def; // sha2(1 * G) let pk_hash: u256 = sha2(pk); assert!(jet::eq_256(pk_hash, expected_pk_hash)); let msg: u256 = jet::sig_all_hash(); - let sig: [u8; 64] = witness("sig"); + let sig: Signature = witness("sig"); jet::bip_0340_verify((pk, msg), sig) } diff --git a/examples/p2pkh.wit b/examples/p2pkh.wit index 06f75fb..01767cd 100644 --- a/examples/p2pkh.wit +++ b/examples/p2pkh.wit @@ -1,10 +1,10 @@ { "pk": { "value": "0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", - "type": "u256" + "type": "Pubkey" }, "sig": { - "value": "[0xf7, 0x4b, 0x3c, 0xa5, 0x74, 0x64, 0x7f, 0x85, 0x95, 0x62, 0x4b, 0x12, 0x93, 0x24, 0xaf, 0xa2, 0xf3, 0x8b, 0x59, 0x8a, 0x9c, 0x1c, 0x7c, 0xfc, 0x5f, 0x08, 0xa9, 0xc0, 0x36, 0xec, 0x5a, 0xcd, 0x3c, 0x0f, 0xbb, 0x9e, 0xd3, 0xda, 0xe5, 0xca, 0x23, 0xa0, 0xa6, 0x5a, 0x34, 0xb5, 0xd6, 0xcc, 0xcd, 0xd6, 0xba, 0x24, 0x89, 0x85, 0xd6, 0x04, 0x1f, 0x7b, 0x21, 0x26, 0x2b, 0x17, 0xaf, 0x6f]", - "type": "[u8; 64]" + "value": "0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f", + "type": "Signature" } } diff --git a/examples/presigned_vault.complete.wit b/examples/presigned_vault.complete.wit index 7d803ae..574e3f5 100644 --- a/examples/presigned_vault.complete.wit +++ b/examples/presigned_vault.complete.wit @@ -1,6 +1,6 @@ { "hot_or_cold": { - "value": "Left([0xed, 0xb6, 0x86, 0x50, 0x94, 0x26, 0x0f, 0x85, 0x58, 0x72, 0x82, 0x33, 0xaa, 0xe0, 0x17, 0xdd, 0x09, 0x69, 0xa2, 0xaf, 0xe5, 0xf0, 0x8c, 0x28, 0x2e, 0x1a, 0xb6, 0x59, 0xbf, 0x24, 0x62, 0x68, 0x4c, 0x99, 0xa6, 0x4a, 0x2a, 0x57, 0x24, 0x63, 0x58, 0xa0, 0xd6, 0x32, 0x67, 0x17, 0x78, 0xd0, 0x16, 0xe6, 0xdf, 0x73, 0x81, 0x29, 0x3d, 0xd5, 0xbb, 0x9f, 0x09, 0x99, 0xd3, 0x86, 0x40, 0xd4])", - "type": "Either<[u8; 64], [u8; 64]>" + "value": "Left(0xedb6865094260f8558728233aae017dd0969a2afe5f08c282e1ab659bf2462684c99a64a2a57246358a0d632671778d016e6df7381293dd5bb9f0999d38640d4)", + "type": "Either" } } diff --git a/examples/presigned_vault.simf b/examples/presigned_vault.simf index 1aafb76..952d500 100644 --- a/examples/presigned_vault.simf +++ b/examples/presigned_vault.simf @@ -16,7 +16,7 @@ * * https://docs.ivylang.org/bitcoin/language/ExampleContracts.html#vaultspend */ -fn checksig(pk: u256, sig: Signature) { +fn checksig(pk: Pubkey, sig: Signature) { let msg: u256 = jet::sig_all_hash(); jet::bip_0340_verify((pk, msg), sig); } @@ -24,12 +24,12 @@ fn checksig(pk: u256, sig: Signature) { fn complete_spend(hot_sig: Signature) { let timeout: Distance = 1000; jet::check_lock_distance(timeout); - let hot_pk: u256 = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; // 1 * G + let hot_pk: Pubkey = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; // 1 * G checksig(hot_pk, hot_sig); } fn cancel_spend(cold_sig: Signature) { - let cold_pk: u256 = 0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5; // 2 * G + let cold_pk: Pubkey = 0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5; // 2 * G checksig(cold_pk, cold_sig) } diff --git a/examples/sighash_all_anyprevoutanyscript.simf b/examples/sighash_all_anyprevoutanyscript.simf index 6a0f608..69f3f26 100644 --- a/examples/sighash_all_anyprevoutanyscript.simf +++ b/examples/sighash_all_anyprevoutanyscript.simf @@ -22,7 +22,7 @@ fn main() { // Message let msg: u256 = jet::sha_256_ctx_8_finalize(ctx); - let pk: u256 = 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9; - let sig: [u8; 64] = witness("sig"); + let pk: Pubkey = 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9; + let sig: Signature = witness("sig"); jet::bip_0340_verify((pk, msg), sig); } diff --git a/examples/sighash_all_anyprevoutanyscript.wit b/examples/sighash_all_anyprevoutanyscript.wit index 9ae5c21..e50b9c8 100644 --- a/examples/sighash_all_anyprevoutanyscript.wit +++ b/examples/sighash_all_anyprevoutanyscript.wit @@ -1,6 +1,6 @@ { "sig": { - "value": "[0xc9, 0x39, 0xe3, 0x23, 0x44, 0x64, 0x2e, 0x3c, 0x22, 0x8b, 0x01, 0x62, 0x04, 0x18, 0x24, 0xaf, 0xe1, 0x94, 0x29, 0x74, 0x77, 0x18, 0x82, 0x55, 0xd2, 0x63, 0x79, 0x08, 0xa0, 0xd0, 0x0d, 0x1f, 0xdf, 0x59, 0x21, 0x4a, 0x30, 0x4d, 0xdf, 0x18, 0x0b, 0x1a, 0x07, 0x01, 0x9a, 0x60, 0xd8, 0x08, 0xd1, 0x81, 0x70, 0xfb, 0x77, 0xfd, 0x3b, 0x00, 0xec, 0xa7, 0x66, 0x67, 0xd7, 0xaa, 0x26, 0x8e]", - "type": "[u8; 64]" + "value": "0xc939e32344642e3c228b0162041824afe194297477188255d2637908a0d00d1fdf59214a304ddf180b1a07019a60d808d18170fb77fd3b00eca76667d7aa268e", + "type": "Signature" } } diff --git a/examples/sighash_none.simf b/examples/sighash_none.simf index 2661251..4a45f83 100644 --- a/examples/sighash_none.simf +++ b/examples/sighash_none.simf @@ -20,7 +20,7 @@ fn main() { // Message let msg: u256 = jet::sha_256_ctx_8_finalize(ctx); - let pk: u256 = 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9; - let sig: [u8; 64] = witness("sig"); + let pk: Pubkey = 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9; + let sig: Signature = witness("sig"); jet::bip_0340_verify((pk, msg), sig); } diff --git a/examples/sighash_none.wit b/examples/sighash_none.wit index 5ab3394..c171754 100644 --- a/examples/sighash_none.wit +++ b/examples/sighash_none.wit @@ -1,6 +1,6 @@ { "sig": { - "value": "[0x34, 0x61, 0x52, 0x58, 0x3d, 0x5b, 0x60, 0xb9, 0x72, 0xbb, 0x4c, 0x03, 0xab, 0x67, 0x2e, 0x33, 0x94, 0x31, 0x06, 0x0e, 0x2b, 0x09, 0xc4, 0x47, 0xab, 0x98, 0x3c, 0x65, 0xda, 0xbc, 0x70, 0xa4, 0x59, 0xf3, 0xbe, 0xca, 0x77, 0x88, 0xbf, 0xa5, 0xda, 0x22, 0x1c, 0xf9, 0x92, 0x27, 0xb6, 0x5b, 0x4a, 0xd3, 0x82, 0x1a, 0x20, 0x45, 0xc8, 0x47, 0xee, 0x56, 0xd4, 0x8d, 0xf2, 0x6a, 0xee, 0x9c]", - "type": "[u8; 64]" + "value": "0x346152583d5b60b972bb4c03ab672e339431060e2b09c447ab983c65dabc70a459f3beca7788bfa5da221cf99227b65b4ad3821a2045c847ee56d48df26aee9c", + "type": "Signature" } } diff --git a/examples/transfer_with_timeout.transfer.wit b/examples/transfer_with_timeout.transfer.wit index 85c8941..3c5339a 100644 --- a/examples/transfer_with_timeout.transfer.wit +++ b/examples/transfer_with_timeout.transfer.wit @@ -1,10 +1,10 @@ { "sender_sig": { - "value": "[0xf7, 0x4b, 0x3c, 0xa5, 0x74, 0x64, 0x7f, 0x85, 0x95, 0x62, 0x4b, 0x12, 0x93, 0x24, 0xaf, 0xa2, 0xf3, 0x8b, 0x59, 0x8a, 0x9c, 0x1c, 0x7c, 0xfc, 0x5f, 0x08, 0xa9, 0xc0, 0x36, 0xec, 0x5a, 0xcd, 0x3c, 0x0f, 0xbb, 0x9e, 0xd3, 0xda, 0xe5, 0xca, 0x23, 0xa0, 0xa6, 0x5a, 0x34, 0xb5, 0xd6, 0xcc, 0xcd, 0xd6, 0xba, 0x24, 0x89, 0x85, 0xd6, 0x04, 0x1f, 0x7b, 0x21, 0x26, 0x2b, 0x17, 0xaf, 0x6f]", - "type": "[u8; 64]" + "value": "0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f", + "type": "Signature" }, "transfer_or_timeout": { - "value": "Some([0xf9, 0x14, 0xc8, 0x8a, 0x1e, 0xe8, 0x8b, 0x4b, 0x7b, 0x8e, 0xb5, 0x60, 0x3b, 0x83, 0x5f, 0xa8, 0x79, 0x38, 0x6a, 0xf7, 0x96, 0x28, 0xaa, 0x94, 0x35, 0x22, 0x6f, 0x64, 0xbd, 0xd5, 0x43, 0x13, 0x79, 0x4b, 0x18, 0xe3, 0x04, 0x37, 0x23, 0x18, 0x97, 0xda, 0xb3, 0x86, 0x1d, 0x92, 0x43, 0x7e, 0x0e, 0xfe, 0x03, 0xa4, 0xbb, 0x17, 0xe6, 0x0e, 0x43, 0x69, 0xd1, 0x92, 0xb0, 0xc6, 0x1e, 0xcf])", - "type": "Option<[u8; 64]>" + "value": "Some(0xf914c88a1ee88b4b7b8eb5603b835fa879386af79628aa9435226f64bdd54313794b18e30437231897dab3861d92437e0efe03a4bb17e60e4369d192b0c61ecf)", + "type": "Option" } } diff --git a/src/ast.rs b/src/ast.rs index 041b7ba..d6009fd 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -741,7 +741,7 @@ impl AbstractSyntaxTree for SingleExpression { parse::SingleExpressionInner::Decimal(decimal) => { let ty = ty .as_integer() - .ok_or(Error::TypeValueMismatch(ty.clone())) + .ok_or(Error::ExpressionUnexpectedType(ty.clone())) .with_span(from)?; UIntValue::parse_decimal(decimal, ty) .with_span(from) @@ -751,18 +751,14 @@ impl AbstractSyntaxTree for SingleExpression { parse::SingleExpressionInner::Binary(bits) => { let ty = ty .as_integer() - .ok_or(Error::TypeValueMismatch(ty.clone())) + .ok_or(Error::ExpressionUnexpectedType(ty.clone())) .with_span(from)?; let value = UIntValue::parse_binary(bits, ty).with_span(from)?; SingleExpressionInner::Constant(Value::from(value)) } parse::SingleExpressionInner::Hexadecimal(bytes) => { - let ty = ty - .as_integer() - .ok_or(Error::TypeValueMismatch(ty.clone())) - .with_span(from)?; - let value = UIntValue::parse_hexadecimal(bytes, ty).with_span(from)?; - SingleExpressionInner::Constant(Value::from(value)) + let value = Value::parse_hexadecimal(bytes, ty).with_span(from)?; + SingleExpressionInner::Constant(value) } parse::SingleExpressionInner::Witness(name) => { scope @@ -790,10 +786,10 @@ impl AbstractSyntaxTree for SingleExpression { parse::SingleExpressionInner::Tuple(tuple) => { let types = ty .as_tuple() - .ok_or(Error::TypeValueMismatch(ty.clone())) + .ok_or(Error::ExpressionUnexpectedType(ty.clone())) .with_span(from)?; if tuple.len() != types.len() { - return Err(Error::TypeValueMismatch(ty.clone())).with_span(from); + return Err(Error::ExpressionUnexpectedType(ty.clone())).with_span(from); } tuple .iter() @@ -805,10 +801,10 @@ impl AbstractSyntaxTree for SingleExpression { parse::SingleExpressionInner::Array(array) => { let (el_ty, size) = ty .as_array() - .ok_or(Error::TypeValueMismatch(ty.clone())) + .ok_or(Error::ExpressionUnexpectedType(ty.clone())) .with_span(from)?; if array.len() != size { - return Err(Error::TypeValueMismatch(ty.clone())).with_span(from); + return Err(Error::ExpressionUnexpectedType(ty.clone())).with_span(from); } array .iter() @@ -819,10 +815,10 @@ impl AbstractSyntaxTree for SingleExpression { parse::SingleExpressionInner::List(list) => { let (el_ty, bound) = ty .as_list() - .ok_or(Error::TypeValueMismatch(ty.clone())) + .ok_or(Error::ExpressionUnexpectedType(ty.clone())) .with_span(from)?; if bound.get() <= list.len() { - return Err(Error::TypeValueMismatch(ty.clone())).with_span(from); + return Err(Error::ExpressionUnexpectedType(ty.clone())).with_span(from); } list.iter() .map(|e| Expression::analyze(e, el_ty, scope)) @@ -832,7 +828,7 @@ impl AbstractSyntaxTree for SingleExpression { parse::SingleExpressionInner::Either(either) => { let (ty_l, ty_r) = ty .as_either() - .ok_or(Error::TypeValueMismatch(ty.clone())) + .ok_or(Error::ExpressionUnexpectedType(ty.clone())) .with_span(from)?; match either { Either::Left(parse_l) => Expression::analyze(parse_l, ty_l, scope) @@ -847,7 +843,7 @@ impl AbstractSyntaxTree for SingleExpression { parse::SingleExpressionInner::Option(maybe_parse) => { let ty = ty .as_option() - .ok_or(Error::TypeValueMismatch(ty.clone())) + .ok_or(Error::ExpressionUnexpectedType(ty.clone())) .with_span(from)?; match maybe_parse { Some(parse) => { diff --git a/src/error.rs b/src/error.rs index af48f85..aaec590 100644 --- a/src/error.rs +++ b/src/error.rs @@ -259,7 +259,7 @@ impl From> for RichError { pub enum Error { ListBoundPow2(usize), BitStringPow2(usize), - HexStringPow2(usize), + HexStringLen(usize), ForWhileWidthPow2(usize), CannotParse(String), Grammar(String), @@ -269,7 +269,6 @@ pub enum Error { // The compiler can only be this precise if it knows a type system at least as expressive as Simplicity's CannotCompile(String), JetDoesNotExist(JetName), - TypeValueMismatch(ResolvedType), InvalidCast(ResolvedType, ResolvedType), MainNoInputs, MainNoOutput, @@ -279,6 +278,7 @@ pub enum Error { InvalidNumberOfArguments(usize, usize), FunctionNotFoldable(FunctionName), FunctionNotLoopable(FunctionName), + ExpressionUnexpectedType(ResolvedType), ExpressionTypeMismatch(ResolvedType, ResolvedType), ExpressionNotConstant, IntegerOutOfBounds(UIntType), @@ -303,9 +303,9 @@ impl fmt::Display for Error { f, "Expected a valid bit string length (1, 2, 4, 8, 16, 32, 64, 128, 256), found {len}" ), - Error::HexStringPow2(len) => write!( + Error::HexStringLen(len) => write!( f, - "Expected a valid hex string length (2, 4, 8, 16, 32, 64), found {len}" + "Expected an even hex string length (0, 2, 4, 6, 8, ...), found {len}" ), Error::ForWhileWidthPow2(bit_width) => write!( f, @@ -331,10 +331,6 @@ impl fmt::Display for Error { f, "Jet `{name}` does not exist" ), - Error::TypeValueMismatch(ty) => write!( - f, - "Value does not match the assigned type `{ty}`" - ), Error::InvalidCast(source, target) => write!( f, "Cannot cast values of type `{source}` as values of type `{target}`" @@ -371,6 +367,10 @@ impl fmt::Display for Error { f, "Expected a signature like `fn {name}(accumulator: A, context: C, counter u{{1,2,4,8,16}}) -> Either` for a for-while loop" ), + Error::ExpressionUnexpectedType(ty) => write!( + f, + "Expected expression of type `{ty}`; found something else" + ), Error::ExpressionTypeMismatch(expected, found) => write!( f, "Expected expression of type `{expected}`, found type `{found}`" diff --git a/src/main.rs b/src/main.rs index 660df99..be702c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,9 +19,11 @@ fn run() -> Result<(), String> { let args: Vec = env::args().collect(); if args.len() < 2 { - println!("Usage: {} [sig.wit (optional)]", args[0]); - println!("If no witness file is provided, the program will be compiled and printed."); - println!("If a witness file is provided, the program will be satisfied and printed."); + println!("Usage: {} PROGRAM_FILE [WITNESS_FILE]", args[0]); + println!( + "Compile the given Simfony program and print the resulting Simplicity base64 string." + ); + println!("If a Simfony witness is provided, then use it to satisfy the program."); return Ok(()); } diff --git a/src/pattern.rs b/src/pattern.rs index 30f39d3..043f36e 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -65,7 +65,7 @@ impl Pattern { (Pattern::Array(pats), TypeInner::Array(ty, size)) if pats.len() == *size => { stack.extend(pats.iter().zip(std::iter::repeat(ty.as_ref()))); } - _ => return Err(Error::TypeValueMismatch(ty.clone())), + _ => return Err(Error::ExpressionUnexpectedType(ty.clone())), } } Ok(output) diff --git a/src/types.rs b/src/types.rs index bc6a0d5..26f091b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -162,6 +162,13 @@ impl UIntType { _ => None, } } + + /// Return the byte width of values of this type. + /// + /// Return 0 for types that take less than an entire byte: `u1`, `u2`, `u4`. + pub const fn byte_width(self) -> usize { + self.bit_width().get() / 8 + } } impl fmt::Debug for UIntType { @@ -216,6 +223,17 @@ impl<'a> TryFrom<&'a ResolvedType> for UIntType { } } +macro_rules! construct_int { + ($name: ident, $ty: ident, $text: expr) => { + #[doc = "Create the type of"] + #[doc = $text] + #[doc = "integers."] + fn $name() -> Self { + Self::from(UIntType::$ty) + } + }; +} + /// Various type constructors. pub trait TypeConstructible: Sized + From { /// Create a sum of the given `left` and `right` types. @@ -248,6 +266,16 @@ pub trait TypeConstructible: Sized + From { /// Create a list with less than `bound` many values of the `element` type. fn list(element: Self, bound: NonZeroPow2Usize) -> Self; + + construct_int!(u1, U1, "1-bit"); + construct_int!(u2, U2, "2-bit"); + construct_int!(u4, U4, "4-bit"); + construct_int!(u8, U8, "8-bit"); + construct_int!(u16, U16, "16-bit"); + construct_int!(u32, U32, "32-bit"); + construct_int!(u64, U64, "64-bit"); + construct_int!(u128, U128, "128-bit"); + construct_int!(u256, U256, "256-bit"); } /// Various type destructors for types that maintain the structure in which they were created. @@ -992,18 +1020,12 @@ mod tests { fn display_type() { let unit = ResolvedType::unit(); assert_eq!("()", &unit.to_string()); - let singleton = ResolvedType::tuple([ResolvedType::from(UIntType::U1)]); + let singleton = ResolvedType::tuple([ResolvedType::u1()]); assert_eq!("(u1,)", &singleton.to_string()); - let pair = ResolvedType::tuple([ - ResolvedType::from(UIntType::U1), - ResolvedType::from(UIntType::U8), - ]); + let pair = ResolvedType::tuple([ResolvedType::u1(), ResolvedType::u8()]); assert_eq!("(u1, u8)", &pair.to_string()); - let triple = ResolvedType::tuple([ - ResolvedType::from(UIntType::U1), - ResolvedType::from(UIntType::U8), - ResolvedType::from(UIntType::U16), - ]); + let triple = + ResolvedType::tuple([ResolvedType::u1(), ResolvedType::u8(), ResolvedType::u16()]); assert_eq!("(u1, u8, u16)", &triple.to_string()); let empty_array = ResolvedType::array(ResolvedType::unit(), 0); assert_eq!("[(); 0]", &empty_array.to_string()); diff --git a/src/value.rs b/src/value.rs index 0fcef2f..b3921ba 100644 --- a/src/value.rs +++ b/src/value.rs @@ -174,22 +174,6 @@ impl UIntValue { _ => Ok(Self::try_from(bytes.as_ref()).expect("Enough bytes")), } } - - /// Create an integer from a `hexadecimal` string and type. - pub fn parse_hexadecimal(hexadecimal: &Hexadecimal, ty: UIntType) -> Result { - use simplicity::elements::hex::FromHex; - - let s = hexadecimal.as_inner(); - let nibble_len = Pow2Usize::new(s.len()).ok_or(Error::HexStringPow2(s.len()))?; - let bit_len = nibble_len.mul2().mul2(); - let byte_ty = UIntType::from_bit_width(bit_len).ok_or(Error::HexStringPow2(s.len()))?; - if ty != byte_ty { - return Err(Error::ExpressionTypeMismatch(ty.into(), byte_ty.into())); - } - - let bytes = Vec::::from_hex(s).map_err(Error::from)?; - Ok(Self::try_from(bytes.as_ref()).expect("Enough bytes")) - } } impl From for UIntValue { @@ -244,10 +228,34 @@ impl TryFrom<&[u8]> for UIntValue { } } +macro_rules! construct_int_fallible { + ($name: ident, $ty: ty, $text: expr) => { + #[doc = "Create"] + #[doc = $text] + #[doc = "integer.\n\n"] + #[doc = "## Panics\n"] + #[doc = "The value is ouf of range."] + fn $name(value: $ty) -> Self { + Self::from(UIntValue::$name(value).expect("The value is out of range")) + } + }; +} + +macro_rules! construct_int { + ($name: ident, $ty: ty, $text: expr) => { + #[doc = "Create"] + #[doc = $text] + #[doc = "integer."] + fn $name(value: $ty) -> Self { + Self::from(UIntValue::from(value)) + } + }; +} + /// Various value constructors. pub trait ValueConstructible: Sized + From + From { /// The type of the constructed value. - type Type; + type Type: TypeConstructible; /// Create the unit value. fn unit() -> Self { @@ -292,6 +300,12 @@ pub trait ValueConstructible: Sized + From + From { /// The given `elements` are not of the given type. fn array>(elements: I, ty: Self::Type) -> Self; + /// Create an array from the given `bytes`. + fn byte_array>(bytes: I) -> Self { + let converted = bytes.into_iter().map(UIntValue::U8).map(Self::from); + Self::array(converted, Self::Type::u8()) + } + /// Create `bound`ed list from the given `elements`. /// /// The type of the value is `List`. @@ -310,6 +324,16 @@ pub trait ValueConstructible: Sized + From + From { ty: Self::Type, bound: NonZeroPow2Usize, ) -> Self; + + construct_int_fallible!(u1, u8, "a 1-bit"); + construct_int_fallible!(u2, u8, "a 2-bit"); + construct_int_fallible!(u4, u8, "a 4-bit"); + construct_int!(u8, u8, "an 8-bit"); + construct_int!(u16, u16, "a 16-bit"); + construct_int!(u32, u32, "a 32-bit"); + construct_int!(u64, u64, "a 64-bit"); + construct_int!(u128, u128, "a 128-bit"); + construct_int!(u256, U256, "a 256-bit"); } /// The structure of a Simfony value. @@ -537,6 +561,30 @@ impl Value { pub fn is_of_type(&self, ty: &ResolvedType) -> bool { self.ty() == ty } + + /// Create a value from the given `hexadecimal` string and type. + pub fn parse_hexadecimal(hexadecimal: &Hexadecimal, ty: &ResolvedType) -> Result { + use hex_conservative::FromHex; + + let expected_byte_len = match ty.as_inner() { + TypeInner::UInt(int) => int.byte_width(), + TypeInner::Array(inner, len) if inner.as_integer() == Some(UIntType::U8) => *len, + _ => return Err(Error::ExpressionUnexpectedType(ty.clone())), + }; + let s = hexadecimal.as_inner(); + if s.len() % 2 != 0 || s.len() != expected_byte_len * 2 { + return Err(Error::ExpressionUnexpectedType(ty.clone())); + } + let bytes = Vec::::from_hex(s).expect("valid chars and valid length"); + let ret = match ty.as_inner() { + TypeInner::UInt(..) => { + Self::from(UIntValue::try_from(bytes.as_ref()).expect("valid length")) + } + TypeInner::Array(..) => Self::byte_array(bytes), + _ => unreachable!(), + }; + Ok(ret) + } } impl Value { @@ -595,9 +643,9 @@ impl Value { let value = UIntValue::parse_binary(binary, *ty)?; output.push(Value::from(value)); } - (SingleExpressionInner::Hexadecimal(hexadecimal), TypeInner::UInt(ty)) => { - let value = UIntValue::parse_hexadecimal(hexadecimal, *ty)?; - output.push(Value::from(value)); + (SingleExpressionInner::Hexadecimal(hexadecimal), _) => { + let value = Value::parse_hexadecimal(hexadecimal, ty)?; + output.push(value); } ( SingleExpressionInner::Either(Either::Left(expr_l)), @@ -668,7 +716,7 @@ impl Value { ) => { return Err(Error::ExpressionNotConstant); } - _ => return Err(Error::TypeValueMismatch(ty.clone())), + _ => return Err(Error::ExpressionUnexpectedType(ty.clone())), } } Task::MakeLeft(right) => { @@ -913,18 +961,11 @@ mod tests { fn display_value() { let unit = Value::unit(); assert_eq!("()", &unit.to_string()); - let singleton = Value::tuple([Value::from(UIntValue::U1(1))]); + let singleton = Value::tuple([Value::u1(1)]); assert_eq!("(1, )", &singleton.to_string()); - let pair = Value::tuple([ - Value::from(UIntValue::U1(1)), - Value::from(UIntValue::U8(42)), - ]); + let pair = Value::tuple([Value::u8(1), Value::u8(42)]); assert_eq!("(1, 42)", &pair.to_string()); - let triple = Value::tuple([ - Value::from(UIntValue::U1(1)), - Value::from(UIntValue::U8(42)), - Value::from(UIntValue::U16(1337)), - ]); + let triple = Value::tuple([Value::u1(1), Value::u8(42), Value::u16(1337)]); assert_eq!("(1, 42, 1337)", &triple.to_string()); let empty_array = Value::array([], ResolvedType::unit()); assert_eq!("[]", &empty_array.to_string()); @@ -955,11 +996,7 @@ mod tests { let bound4 = NonZeroPow2Usize::new(4).unwrap(); let string_ty_value = [ ("false", ResolvedType::boolean(), Value::from(false)), - ( - "42", - ResolvedType::from(UIntType::U8), - Value::from(UIntValue::U8(42)), - ), + ("42", ResolvedType::u8(), Value::u8(42)), ( "Left(false)", ResolvedType::either(ResolvedType::boolean(), ResolvedType::unit()), @@ -977,21 +1014,13 @@ mod tests { UIntType::U2.into(), UIntType::U4.into(), ]), - Value::tuple([ - UIntValue::U1(1).into(), - UIntValue::U2(2).into(), - UIntValue::U4(3).into(), - ]), + Value::tuple([Value::u1(1), Value::u2(2), Value::u4(3)]), ), ( "[1, 2, 3]", ResolvedType::array(UIntType::U4.into(), 3), Value::array( - [ - UIntValue::U4(1).into(), - UIntValue::U4(2).into(), - UIntValue::U4(3).into(), - ], + [Value::u4(1), Value::u4(2), Value::u4(3)], UIntType::U4.into(), ), ), @@ -999,11 +1028,7 @@ mod tests { "list![1, 2, 3]", ResolvedType::list(UIntType::U4.into(), bound4), Value::list( - [ - UIntValue::U4(1).into(), - UIntValue::U4(2).into(), - UIntValue::U4(3).into(), - ], + [Value::u4(1), Value::u4(2), Value::u4(3)], UIntType::U4.into(), bound4, ), diff --git a/src/witness.rs b/src/witness.rs index da7c6eb..564cf2d 100644 --- a/src/witness.rs +++ b/src/witness.rs @@ -200,7 +200,7 @@ impl<'de> Deserialize<'de> for WitnessName { #[cfg(test)] mod tests { use super::*; - use crate::value::UIntValue; + use crate::value::ValueConstructible; #[test] fn witness_reuse() { @@ -226,7 +226,7 @@ mod tests { let mut witness = WitnessValues::empty(); let a = WitnessName::parse_from_str("a").unwrap(); - witness.insert(a, Value::from(UIntValue::U16(42))).unwrap(); + witness.insert(a, Value::u16(42)).unwrap(); match crate::satisfy(s, &witness) { Ok(_) => panic!("Ill-typed witness assignment was falsely accepted"), @@ -241,10 +241,8 @@ mod tests { fn witness_duplicate_assignment() { let mut witness = WitnessValues::empty(); let a = WitnessName::parse_from_str("a").unwrap(); - witness - .insert(a.clone(), Value::from(UIntValue::U32(42))) - .unwrap(); - match witness.insert(a, Value::from(UIntValue::U32(43))) { + witness.insert(a.clone(), Value::u32(42)).unwrap(); + match witness.insert(a, Value::u32(43)) { Ok(_) => panic!("Duplicate witness assignment was falsely accepted"), Err(Error::WitnessReassigned(..)) => {} Err(error) => panic!("Unexpected error: {error}"),