Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schnorr scale #80

Open
wants to merge 77 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
6a845bb
Simple implementation of schnorr
hacpy Aug 2, 2021
89817b4
Follow clippy
hacpy Aug 2, 2021
bb96a68
Remove redundant references
hacpy Aug 2, 2021
db0fe60
Fix clippy
hacpy Aug 2, 2021
b165763
Add verify func and Signature
hacpy Aug 4, 2021
98e994c
Fix ci lint
hacpy Aug 4, 2021
b3182fc
Test with the right example
hacpy Aug 4, 2021
b3f077f
Passed BIP340 test
hacpy Aug 5, 2021
3417974
Choose a less ambiguous method name
hacpy Aug 5, 2021
291cd72
Add error handling
hacpy Aug 6, 2021
6afe108
Add sign with null aux
hacpy Aug 6, 2021
020a214
Easier to use sign and verify
hacpy Aug 9, 2021
9474095
Implement From<T> instead of from and to methods
hacpy Aug 10, 2021
3e2eb36
Generate random key
hacpy Aug 12, 2021
94e6911
Add check in verify()
hacpy Aug 13, 2021
19e6a9b
Add simple and rough batch verification
hacpy Aug 16, 2021
960a28b
Signature aggregation
hacpy Aug 16, 2021
8bea0ae
Remove helper function
hacpy Aug 17, 2021
8d77c67
Remove unavailable functions
hacpy Aug 17, 2021
7df4d95
Fix warnings
hacpy Aug 17, 2021
b1d5064
Clearer conversion of XOnly to PublicKey
hacpy Aug 20, 2021
eb53b28
Verify schnorr signatures only
hacpy Aug 30, 2021
25e3106
Externally available XOnly and schnorr signature
hacpy Sep 1, 2021
412bed1
Make sure the public key is an even y
hacpy Oct 19, 2021
e109406
Delete alloc feature of hex
Oct 26, 2021
22fd819
Resolve dependency conflicts
Oct 26, 2021
015b67a
Wrap address type
hacpy Oct 26, 2021
bdfdffe
Add Type
Oct 27, 2021
f43bc54
Support witness
hacpy Oct 27, 2021
d80ee89
Judge witness network
hacpy Oct 27, 2021
088fc38
Remove unwrap for Display
hacpy Oct 27, 2021
271ecb1
Reset AddressTypes default
hacpy Oct 28, 2021
c213211
Use core instead of std
hacpy Oct 28, 2021
13b6161
Use no-std bech32
hacpy Oct 28, 2021
d035b6a
Add sighash
Oct 29, 2021
d7c8094
Fix test of sighash
Nov 1, 2021
346f190
Fix test
Nov 1, 2021
9eb83f0
Fix param
Nov 1, 2021
637a440
Add pub
Nov 1, 2021
fd678b1
Add pub
Nov 1, 2021
b1812d5
Add verify merkle root
Nov 1, 2021
f787363
Fix test of check taproot
Nov 1, 2021
4525f15
Support no-std
Nov 2, 2021
9bf9dbf
Fix dependency
Nov 2, 2021
432ffce
Add some error message for check-taproot
hacpy Nov 4, 2021
8d585f2
Fix test
Nov 4, 2021
6929d2d
Fix slice panic
Nov 4, 2021
5a77212
Fix unwrap
Nov 5, 2021
5939253
Add mast
hacpy Nov 5, 2021
fb69bac
Disable arrayref default feature
hacpy Nov 8, 2021
616c841
Use remote musig2
hacpy Nov 8, 2021
f324850
Fix Cargo.toml
Nov 8, 2021
d001a10
Enable pub of mast
Nov 8, 2021
cc54d22
Add Public to Pulickey
Nov 8, 2021
b30edbd
Add network to string
Nov 8, 2021
f2387a6
Allow single node of pmt
Nov 10, 2021
63afa63
Support parallel compute
Nov 10, 2021
6271987
Fix Cargo.toml
Nov 10, 2021
d42889f
Resolve rayon conflict
Nov 11, 2021
4ef10cc
Use criterion instead of nightly feature
hacpy Nov 11, 2021
6ad4264
Fix clippy
hacpy Nov 11, 2021
257adb1
Support scale-info
icodezjb Nov 12, 2021
b45c45e
Bump version to v0.3.0
icodezjb Nov 12, 2021
875db56
Update libsecp256k1 and hashbrown
icodezjb Nov 12, 2021
eceb852
Revert "Bump version to v0.3.0"
icodezjb Nov 12, 2021
f778727
Add miss files
icodezjb Nov 12, 2021
d8ceefc
Fix control block
Nov 12, 2021
7099a1d
Fix build address
Nov 12, 2021
7a978ea
Add schnorr sign with aux
hacpy Nov 15, 2021
e0bf2df
Fix test
Nov 15, 2021
3375d99
Remove pubkey param in sign
hacpy Nov 15, 2021
ffdfd81
Merge branch 'support-schnorr-test' of https://github.com/chainx-org/…
hacpy Nov 15, 2021
fde8663
Impl Into for SchnorrSignature
hacpy Nov 15, 2021
1f329df
Fix verify schnorr
Nov 15, 2021
91cc771
Merge branch 'schnorr-scale' into scale-info
icodezjb Nov 15, 2021
02c51c7
Merge pull request #79 from chainx-org/scale-info
icodezjb Nov 15, 2021
4e5546e
Run `cargo test --release --all`
icodezjb Nov 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ std = [
"light-bitcoin-primitives/std",
"light-bitcoin-script/std",
"light-bitcoin-serialization/std",
"light-bitcoin-mast/std",
]
derive = ["light-bitcoin-serialization/derive"]

Expand All @@ -27,6 +28,7 @@ light-bitcoin-merkle = { path = "merkle", default-features = false }
light-bitcoin-primitives = { path = "primitives", default-features = false }
light-bitcoin-script = { path = "script", default-features = false }
light-bitcoin-serialization = { path = "serialization", default-features = false }
light-bitcoin-mast = { path = "mast", default-features = false }

[workspace]
members = [
Expand All @@ -38,4 +40,5 @@ members = [
"script",
"serialization",
"serialization-derive",
"mast",
]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ The library is largely based on the following:
* [parity-bitcoin](https://github.com/paritytech/parity-bitcoin/commit/e4cdea3b575574aac1ac20a4750ff263fa3e2e89) (GPL-v3)
* [rust-bitcoin](https://github.com/rust-bitcoin/rust-bitcoin) (Creative Commons CC0 1.0 Universal license)

## Run tests
`cargo test --all --release`

## License

[GPL-v3](./LICENSE)
4 changes: 3 additions & 1 deletion chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ std = [
"codec/std",
"hex/std",
"serde",
"scale-info/std",

"light-bitcoin-crypto/std",
"light-bitcoin-primitives/std",
"light-bitcoin-serialization/std",
]

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
hex = { version = "0.4", default-features = false }
serde = { version = "1.0", features = ["derive"], optional = true }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }

light-bitcoin-crypto = { path = "../crypto", default-features = false }
light-bitcoin-primitives = { path = "../primitives", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion chain/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::merkle_root::merkle_root;
use crate::transaction::Transaction;

/// A Bitcoin block, which is a collection of transactions with an attached proof of work.
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug, Default)]
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug, Default, scale_info::TypeInfo)]
#[derive(Serializable, Deserializable)]
pub struct Block {
/// The block header
Expand Down
4 changes: 2 additions & 2 deletions chain/src/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};

/// A block header, which contains all the block's information except
/// the actual transactions
#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Default)]
#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Default, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Serializable, Deserializable)]
pub struct BlockHeader {
Expand Down Expand Up @@ -69,7 +69,7 @@ impl BlockHeader {

impl codec::Encode for BlockHeader {
fn encode(&self) -> Vec<u8> {
let value = serialize::<BlockHeader>(&self);
let value = serialize::<BlockHeader>(self);
value.encode()
}
}
Expand Down
2 changes: 1 addition & 1 deletion chain/src/indexed_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::indexed_transaction::IndexedTransaction;
use crate::merkle_root::merkle_root;
use crate::transaction::Transaction;

#[derive(Ord, PartialOrd, Eq, Clone, Debug, Default, Deserializable)]
#[derive(Ord, PartialOrd, Eq, Clone, Debug, Default, Deserializable, scale_info::TypeInfo)]
pub struct IndexedBlock {
pub header: IndexedBlockHeader,
pub transactions: Vec<IndexedTransaction>,
Expand Down
2 changes: 1 addition & 1 deletion chain/src/indexed_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use light_bitcoin_serialization::{Deserializable, Reader};
use crate::block_header::BlockHeader;
use crate::read_and_hash::ReadAndHash;

#[derive(Ord, PartialOrd, Eq, Copy, Clone, Default)]
#[derive(Ord, PartialOrd, Eq, Copy, Clone, Default, scale_info::TypeInfo)]
pub struct IndexedBlockHeader {
pub hash: H256,
pub raw: BlockHeader,
Expand Down
2 changes: 1 addition & 1 deletion chain/src/indexed_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use light_bitcoin_serialization::{Deserializable, Reader};
use crate::read_and_hash::ReadAndHash;
use crate::transaction::Transaction;

#[derive(Ord, PartialOrd, Eq, Clone, Default)]
#[derive(Ord, PartialOrd, Eq, Clone, Default, scale_info::TypeInfo)]
pub struct IndexedTransaction {
pub hash: H256,
pub raw: Transaction,
Expand Down
4 changes: 3 additions & 1 deletion chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ pub use light_bitcoin_primitives::*;
pub use self::block::Block;
pub use self::block_header::BlockHeader;
pub use self::merkle_root::{merkle_node_hash, merkle_root};
pub use self::transaction::{OutPoint, Transaction, TransactionInput, TransactionOutput};
pub use self::transaction::{
OutPoint, Transaction, TransactionInput, TransactionOutput, TransactionOutputArray,
};

pub use self::indexed_block::IndexedBlock;
pub use self::indexed_header::IndexedBlockHeader;
Expand Down
80 changes: 46 additions & 34 deletions chain/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const WITNESS_MARKER: u8 = 0;
const WITNESS_FLAG: u8 = 1;

/// A reference to a transaction output
#[derive(Ord, PartialOrd, PartialEq, Eq, Copy, Clone)]
#[derive(Ord, PartialOrd, PartialEq, Eq, Copy, Clone, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Serializable, Deserializable)]
pub struct OutPoint {
Expand Down Expand Up @@ -74,7 +74,7 @@ impl OutPoint {
}

/// A transaction input, which defines old coins to be consumed
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug, Default)]
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug, Default, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct TransactionInput {
/// The reference to the previous output that is being used an an input
Expand Down Expand Up @@ -139,7 +139,7 @@ impl Deserializable for TransactionInput {
}

/// A transaction output, which defines new coins to be created from old ones.
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug)]
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Serializable, Deserializable)]
pub struct TransactionOutput {
Expand All @@ -149,6 +149,13 @@ pub struct TransactionOutput {
pub script_pubkey: Bytes,
}

#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Serializable, Deserializable)]
pub struct TransactionOutputArray {
pub outputs: Vec<TransactionOutput>,
}

impl Default for TransactionOutput {
fn default() -> Self {
TransactionOutput {
Expand All @@ -159,7 +166,7 @@ impl Default for TransactionOutput {
}

/// A Bitcoin transaction, which describes an authenticated movement of coins.
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug, Default)]
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug, Default, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Transaction {
/// The protocol version, is currently expected to be 1 or 2 (BIP 68).
Expand Down Expand Up @@ -329,7 +336,7 @@ impl Deserializable for Transaction {

impl codec::Encode for Transaction {
fn encode(&self) -> Vec<u8> {
let value = serialize::<Transaction>(&self);
let value = serialize::<Transaction>(self);
value.encode()
}
}
Expand Down Expand Up @@ -392,36 +399,36 @@ mod tests {
fn test_transaction_reader_with_witness() {
let actual: Transaction = "01000000000102fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f00000000494830450221008b9d1dc26ba6a9cb62127b02742fa9d754cd3bebf337f7a55d114c8e5cdd30be022040529b194ba3f9281a99f2b1c0a19c0489bc22ede944ccf4ecbab4cc618ef3ed01eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac000247304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee0121025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee635711000000".parse().unwrap();
let expected = Transaction {
version: 1,
inputs: vec![TransactionInput {
previous_output: OutPoint {
txid: h256("fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f"),
index: 0,
},
script_sig: "4830450221008b9d1dc26ba6a9cb62127b02742fa9d754cd3bebf337f7a55d114c8e5cdd30be022040529b194ba3f9281a99f2b1c0a19c0489bc22ede944ccf4ecbab4cc618ef3ed01".parse().unwrap(),
sequence: 0xffffffee,
script_witness: vec![],
}, TransactionInput {
previous_output: OutPoint {
version: 1,
inputs: vec![TransactionInput {
previous_output: OutPoint {
txid: h256("fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f"),
index: 0,
},
script_sig: "4830450221008b9d1dc26ba6a9cb62127b02742fa9d754cd3bebf337f7a55d114c8e5cdd30be022040529b194ba3f9281a99f2b1c0a19c0489bc22ede944ccf4ecbab4cc618ef3ed01".parse().unwrap(),
sequence: 0xffffffee,
script_witness: vec![],
}, TransactionInput {
previous_output: OutPoint {
txid: h256("ef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a"),
index: 1,
},
script_sig: "".parse().unwrap(),
sequence: 0xffffffff,
script_witness: vec![
"304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee01".parse().unwrap(),
"025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357".parse().unwrap(),
],
}],
outputs: vec![TransactionOutput {
value: 0x0000000006b22c20,
script_pubkey: "76a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac".parse().unwrap(),
}, TransactionOutput {
value: 0x000000000d519390,
script_pubkey: "76a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac".parse().unwrap(),
}],
lock_time: 0x00000011,
};
index: 1,
},
script_sig: "".parse().unwrap(),
sequence: 0xffffffff,
script_witness: vec![
"304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee01".parse().unwrap(),
"025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357".parse().unwrap(),
],
}],
outputs: vec![TransactionOutput {
value: 0x0000000006b22c20,
script_pubkey: "76a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac".parse().unwrap(),
}, TransactionOutput {
value: 0x000000000d519390,
script_pubkey: "76a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac".parse().unwrap(),
}],
lock_time: 0x00000011,
};
assert_eq!(actual, expected);
}

Expand All @@ -438,6 +445,11 @@ mod tests {
serialize_with_flags(&transaction_with_witness, 0),
serialize_with_flags(&transaction_with_witness, SERIALIZE_TRANSACTION_WITNESS)
);
let tx : Transaction = "020000000001015dce8efe6cbd845587aa230a0b3667d4b52a45d3965d1607ab187de1f9d9d82b00000000000000000002a086010000000000225120dc82a9c33d787242d80fb4535bcc8d90bb13843fea52c9e78bb43c541dd607b900350c0000000000225120c9929543dfa1e0bb84891acd47bfa6546b05e26b7a04af8eb6765fcc969d565f0140708f206174a9e2963dd87d3afbb9f390fb320e2e9d4fdfc7b8bd7bc71a29c252026aa505ae71d4155ee3c13ce189ccba1fc0a26cfbcaa5f8b91bab377c2124eb00000000".parse().unwrap();
let transaction_output = TransactionOutputArray {
outputs: vec![tx.outputs[0].clone()],
};
assert_eq!( hex::encode(&serialize(&transaction_output)), "01a086010000000000225120dc82a9c33d787242d80fb4535bcc8d90bb13843fea52c9e78bb43c541dd607b9")
}

#[test]
Expand Down
14 changes: 12 additions & 2 deletions keys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,32 @@ default = ["std"]
std = [
"bs58/std",
"codec/std",
"digest/std",
"hex/std",
"libsecp256k1/std",
"musig2/std",
"serde",
"scale-info/std",
"sha2/std",

"light-bitcoin-crypto/std",
"light-bitcoin-primitives/std",
"light-bitcoin-serialization/std",
]

[dependencies]
arrayref = { version = "0.3.6" }
bs58 = { version = "0.4", default-features = false, features = ["alloc"] }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
hex = { version = "0.4", default-features = false }
digest = { version = "0.9.0", default-features = false }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
libsecp256k1 = { version = "0.3.5", default-features = false, features = ["hmac"] }
musig2 = { git = "https://github.com/chainx-org/Musig2", branch = "lib", default-features = false }
serde = { version = "1.0", features = ["derive"], optional = true }

scale-info = { version = "1.0", default-features = false, features = ["derive"] }
sha2 = { version = "0.9.5", default-features = false }
# for no-std
bitcoin-bech32 = { git = "https://github.com/chainx-org/rust-bech32-bitcoin", branch = "master", default-features = false }
light-bitcoin-crypto = { path = "../crypto", default-features = false }
light-bitcoin-primitives = { path = "../primitives", default-features = false }
light-bitcoin-serialization = { path = "../serialization", default-features = false, features = ["derive"] }
Expand Down
Loading