From 57205cb43c3f909ba7bcf96a9b829418bdb4c696 Mon Sep 17 00:00:00 2001 From: Nick Furfaro Date: Mon, 10 Jan 2022 17:30:46 -0700 Subject: [PATCH] ec-recover (#5) * Add ecr lib * Add todo comment * Add recover_pubkey and refactor ecr * Fixup * Update comments * Add comments to ec-recover asm block * Modify asm * Clean up asm * Fix B512 field assignment * Fix ordering of lib deps * Remove ec_recover, to be added in separate PR * Clean up comments --- src/ecr.sw | 19 +++++++++++++++++++ src/lib.sw | 1 + 2 files changed, 20 insertions(+) create mode 100644 src/ecr.sw diff --git a/src/ecr.sw b/src/ecr.sw new file mode 100644 index 00000000000..125f886274c --- /dev/null +++ b/src/ecr.sw @@ -0,0 +1,19 @@ +library ecr; + +use ::b512::B512; +use ::address::Address; + +/// Recover the address derived from the private key used to sign a message +pub fn ec_recover_address(signature: B512, msg_hash: b256) -> Address { + let address = asm(pub_key_buffer, sig_ptr: signature.hi, hash: msg_hash, addr_buffer, sixty_four: 64) { + move pub_key_buffer sp; // mv sp to pub_key result buffer. + cfei i64; + ecr pub_key_buffer sig_ptr hash; // recover public_key from sig & hash + move addr_buffer sp; // mv sp to addr result buffer. + cfei i32; + s256 addr_buffer pub_key_buffer sixty_four; // hash 64 bytes to the addr_buffer + addr_buffer: b256 + }; + + ~Address::from(address) +} diff --git a/src/lib.sw b/src/lib.sw index e017ac57edd..b60766a2ddb 100644 --- a/src/lib.sw +++ b/src/lib.sw @@ -10,5 +10,6 @@ dep context; dep address; dep block; dep result; +dep ecr; use core::*;