-
Notifications
You must be signed in to change notification settings - Fork 95
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
Some additional test to verify ecrecovery with TxId
and coin witness
#495
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,141 @@ fn ecrecover() { | |
|
||
assert!(success); | ||
} | ||
|
||
#[test] | ||
fn ecrecover_tx_id() { | ||
let rng = &mut StdRng::seed_from_u64(2322u64); | ||
|
||
let mut client = MemoryClient::default(); | ||
|
||
let gas_price = 0; | ||
let gas_limit = 1_000_000; | ||
let maturity = Default::default(); | ||
let height = Default::default(); | ||
let params = ConsensusParameters::default(); | ||
let gas_costs = GasCosts::default(); | ||
|
||
let secret = SecretKey::random(rng); | ||
let public = secret.public_key(); | ||
|
||
#[rustfmt::skip] | ||
let script = vec![ | ||
// 0x21 is a address of the singer of the witness | ||
op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), | ||
op::move_(0x21, 0x20), | ||
// 0x22 is a witness - signature | ||
op::gtf_args(0x22, 0x00, GTFArgs::WitnessData), | ||
// TxId is stored in the first 32 bytes of the memory | ||
// Store it into register 0x23 | ||
op::movi(0x23, 0), | ||
// Allocate space for the recovered public key | ||
// 0x10 contains the size of the public key = PublicKey::LEN | ||
op::movi(0x10, PublicKey::LEN as Immediate18), | ||
op::aloc(0x10), | ||
op::move_(0x11, RegId::HP), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: assigning to a register is not needed, |
||
// Recover public key into `0x11` from `0x22` signature and TxId `0x23` | ||
op::ecr(0x11, 0x22, 0x23), | ||
// Compare address `0x21` from script data with with recovered `0x11` | ||
// for length `0x10` = PublicKey::LEN | ||
op::meq(0x12, 0x21, 0x11, 0x10), | ||
op::ret(0x12), | ||
].into_iter().collect(); | ||
|
||
let script_data = public.as_ref().to_vec(); | ||
|
||
let mut tx = TransactionBuilder::script(script, script_data) | ||
.gas_price(gas_price) | ||
.gas_limit(gas_limit) | ||
.maturity(maturity) | ||
.with_params(params) | ||
.add_random_fee_input() | ||
.finalize(); | ||
|
||
tx.sign_inputs(&secret, ¶ms.chain_id); | ||
let tx = tx.into_checked(height, ¶ms, &gas_costs).unwrap(); | ||
|
||
let receipts = client.transact(tx); | ||
let success = receipts | ||
.iter() | ||
.any(|r| matches!(r, Receipt::Return{ val, .. } if *val == 1)); | ||
|
||
assert!(success); | ||
} | ||
|
||
#[test] | ||
fn ecrecover_tx_id_predicate() { | ||
use crate::checked_transaction::EstimatePredicates; | ||
use rand::Rng; | ||
let rng = &mut StdRng::seed_from_u64(1234u64); | ||
|
||
let gas_price = 0; | ||
let gas_limit = 1_000_000; | ||
let maturity = Default::default(); | ||
let params = ConsensusParameters::default(); | ||
let gas_costs = GasCosts::default(); | ||
|
||
let secret = SecretKey::random(rng); | ||
let public = secret.public_key(); | ||
|
||
#[rustfmt::skip] | ||
let predicate = vec![ | ||
// 0x21 is a address of the singer of the witness | ||
op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), | ||
op::move_(0x21, 0x20), | ||
// 0x22 is a witness - signature | ||
op::gtf_args(0x22, 0x00, GTFArgs::WitnessData), | ||
// TxId is stored in the first 32 bytes of the memory | ||
// Store it into register 0x23 | ||
op::movi(0x23, 0), | ||
// Allocate space for the recovered public key | ||
// 0x10 contains the size of the public key = PublicKey::LEN | ||
op::movi(0x10, PublicKey::LEN as Immediate18), | ||
op::aloc(0x10), | ||
op::move_(0x11, RegId::HP), | ||
// Recover public key into `0x11` from `0x22` signature and TxId `0x23` | ||
op::ecr(0x11, 0x22, 0x23), | ||
// Compare address `0x21` from script data with with recovered `0x11` | ||
// for length `0x10` = PublicKey::LEN | ||
op::meq(0x12, 0x21, 0x11, 0x10), | ||
op::ret(0x12), | ||
].into_iter().collect(); | ||
|
||
let script_data = public.as_ref().to_vec(); | ||
|
||
let input = Input::coin_predicate( | ||
rng.gen(), | ||
Input::predicate_owner(&predicate, ¶ms.chain_id), | ||
1000, | ||
rng.gen(), | ||
Default::default(), | ||
rng.gen(), | ||
0, | ||
predicate, | ||
vec![], | ||
); | ||
|
||
let mut tx = TransactionBuilder::script(vec![], script_data) | ||
.gas_price(gas_price) | ||
.gas_limit(gas_limit) | ||
.maturity(maturity) | ||
.with_params(params) | ||
.add_input(input) | ||
.add_unsigned_coin_input( | ||
secret, | ||
rng.gen(), | ||
rng.gen(), | ||
rng.gen(), | ||
Default::default(), | ||
Default::default(), | ||
) | ||
.finalize(); | ||
|
||
tx.estimate_predicates(¶ms, &gas_costs) | ||
.expect("Should estimate predicate successfully"); | ||
tx.into_checked(maturity, ¶ms, &gas_costs) | ||
.expect("Should check predicate successfully"); | ||
} | ||
|
||
#[test] | ||
fn ecrecover_error() { | ||
let rng = &mut StdRng::seed_from_u64(2322u64); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: registers are zeroed already on initialization