This commit is contained in:
Jaco Greeff
2021-01-21 12:28:11 +01:00
committed by GitHub
parent 33a54be279
commit 16d1b28f77
5 changed files with 19 additions and 155 deletions

View File

@@ -1,11 +1,17 @@
# CHANGELOG
## 3.2.x
## 3.2.1 Jan 21, 2021
Contributed:
- Expose sr25519 `ext_vrf_{sign, verify}` methods (Thanks to https://github.com/stiiifff)
Changes:
- Add explicit `.editorconfig` for Rust sources
- Remove unused code bundles (`vrf_{sign,verify}_extra`, `secp256k1` with tests)
- Test run wrapper cleanups
## 3.1.1 Dec 19, 2020

View File

@@ -7,7 +7,6 @@ use hmac::Hmac;
use pbkdf2::pbkdf2;
use scrypt::{ScryptParams, scrypt};
use sha2::{Digest, Sha512};
// use secp256k1;
use tiny_keccak::{Hasher, Keccak};
use twox_hash::XxHash;
use wasm_bindgen::prelude::*;
@@ -95,58 +94,6 @@ pub fn ext_scrypt(password: &[u8], salt: &[u8], log2_n: u8, r: u32, p: u32) -> V
}
}
// /// Checks for the recoverability of a signature from message
// ///
// /// * message: The message
// /// * signature: The signature
// ///
// /// Returns 0 on success, or a specific error code
// #[wasm_bindgen]
// pub fn ext_secp256k1_is_recoverable(message: &[u8], signature: &[u8]) -> u32 {
// let rs = match secp256k1::Signature::parse_slice(&signature[0..64]) {
// Ok(rs) => rs,
// _ => return 1
// };
// let v = match secp256k1::RecoveryId::parse(if signature[64] > 26 { signature[64] - 27 } else { signature[64] } as u8) {
// Ok(v) => v,
// _ => return 2
// };
// let mut msg = [0u8; 32];
// msg.copy_from_slice(&message);
// match secp256k1::recover(&secp256k1::Message::parse(&msg), &rs, &v) {
// Ok(_) => 0,
// _ => 3
// }
// }
// /// Recovers the secp256k1 signature from the input
// ///
// /// * message: The message
// /// * signature: The signature
// ///
// /// Returns a vector with the recovered public key
// #[wasm_bindgen]
// pub fn ext_secp256k1_recover(message: &[u8], signature: &[u8]) -> Vec<u8> {
// let rs = match secp256k1::Signature::parse_slice(&signature[0..64]) {
// Ok(rs) => rs,
// _ => panic!("Unable to recover r,s")
// };
// let v = match secp256k1::RecoveryId::parse(if signature[64] > 26 { signature[64] - 27 } else { signature[64] } as u8) {
// Ok(v) => v,
// _ => panic!("Unable to recover v")
// };
// let mut msg = [0u8; 32];
// msg.copy_from_slice(&message);
// match secp256k1::recover(&secp256k1::Message::parse(&msg), &rs, &v) {
// Ok(pk) => return pk.serialize()[1..65].to_vec(),
// _ => panic!("Unable to recover publickey")
// }
// }
/// sha512 hash for the specified input
///
/// * data: Arbitrary data to be hashed
@@ -186,24 +133,6 @@ pub mod tests {
use hex_literal::hex;
use super::*;
// // Constructs the message that Ethereum RPCs `personal_sign` and `eth_sign` would sign.
// fn ethereum_signable_message(data: &[u8]) -> Vec<u8> {
// let prefix = b"Pay DOTs to the Polkadot account:";
// let mut l = prefix.len() + data.len();
// let mut rev = Vec::new();
// let mut v = b"\x19Ethereum Signed Message:\n".to_vec();
// while l > 0 {
// rev.push(b'0' + (l % 10) as u8);
// l /= 10;
// }
// v.extend(rev.into_iter().rev());
// v.extend_from_slice(&prefix[..]);
// v.extend_from_slice(data);
// v
// }
#[test]
fn can_blake2b() {
let data = b"abc";
@@ -245,17 +174,6 @@ pub mod tests {
assert_eq!(hash[..], expected[..]);
}
// #[test]
// fn can_secp256k1_real_eth_sig() {
// let sig = hex!("7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c");
// let pk = hex!("DF67EC7EAe23D2459694685257b6FC59d1BAA1FE");
// let data = [42, 0, 0, 0, 0, 0, 0, 0];
// let msg = ext_keccak256(&ethereum_signable_message(&data));
// let result = ext_keccak256(&ext_secp256k1_recover(&msg[..], &sig[..]));
// assert_eq!(result[12..], pk[..]);
// }
#[test]
fn can_sha512() {
let data = b"hello world";

View File

@@ -24,7 +24,7 @@ pub const VRF_RAW_OUTPUT_SIZE: usize = 16;
pub const VRF_THRESHOLD_SIZE: usize = 16;
/// Size of the final full output
pub const VRF_OUTPUT_FULL: usize = VRF_OUTPUT_SIZE + VRF_PROOF_SIZE;
pub const VRF_RESULT_SIZE: usize = VRF_OUTPUT_SIZE + VRF_PROOF_SIZE;
pub fn create_transcript(extra: &[u8]) -> Transcript {
let mut transcript = Transcript::new(b"VRF");
@@ -41,8 +41,6 @@ pub fn create_transcript(extra: &[u8]) -> Transcript {
// We don't use the non-extra vrf_sign and vrf_verify internally since in sr25519 they only wrap -
//
// https://github.com/w3f/schnorrkel/blob/8fa2ad3e9fbf0b652c724df6a87a4b3c5500f759/src/vrf.rs#L660
//
// The compat test (also commented) proofs this compatibility - vrf_extra_compat()
/// Run a Random Verifiable Function (VRF) on one single input
/// (message) transcript, and an extra message transcript,
@@ -59,7 +57,7 @@ pub fn ext_vrf_sign(secret: &[u8], context: &[u8], message: &[u8], extra: &[u8])
let keypair = SecretKey::from_ed25519_bytes(secret).unwrap().to_keypair();
let transcript = create_transcript(extra);
let (io, proof, _) = keypair.vrf_sign_extra(signing_context(context).bytes(message), transcript);
let mut result: [u8; VRF_OUTPUT_FULL] = [0u8; VRF_OUTPUT_FULL];
let mut result: [u8; VRF_RESULT_SIZE] = [0u8; VRF_RESULT_SIZE];
result[..VRF_OUTPUT_SIZE].copy_from_slice(io.as_output_bytes());
result[VRF_OUTPUT_SIZE..].copy_from_slice(&proof.to_bytes());
@@ -79,7 +77,7 @@ pub fn ext_vrf_verify(pubkey: &[u8], context: &[u8], message: &[u8], extra: &[u8
match (
PublicKey::from_bytes(pubkey),
VRFOutput::from_bytes(&out_and_proof[..VRF_OUTPUT_SIZE]),
VRFProof::from_bytes(&out_and_proof[VRF_OUTPUT_SIZE..(VRF_OUTPUT_FULL)])
VRFProof::from_bytes(&out_and_proof[VRF_OUTPUT_SIZE..VRF_RESULT_SIZE])
) {
(Ok(public), Ok(out), Ok(proof)) => {
public
@@ -123,8 +121,8 @@ pub mod tests {
let out_and_proof2 = ext_vrf_sign(private, context, message, extra);
// Basic size checks
assert!(out_and_proof1.len() == VRF_OUTPUT_FULL);
assert!(out_and_proof2.len() == VRF_OUTPUT_FULL);
assert!(out_and_proof1.len() == VRF_RESULT_SIZE);
assert!(out_and_proof2.len() == VRF_RESULT_SIZE);
// Given the same context, message & extra, output should be deterministic
let out1 = &out_and_proof1[..VRF_OUTPUT_SIZE];
@@ -152,49 +150,4 @@ pub mod tests {
&out_and_proof2
));
}
// #[test]
// fn vrf_extra_compat() {
// let seed = generate_random_seed();
// let keypair = ext_sr_from_seed(seed.as_slice());
// let private = &keypair[0..SECRET_KEY_LENGTH];
// let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH];
// let context = b"my VRF context";
// let message = b"this is a message";
// let extra = b"";
// // Perform multiple vrf_sign_extra calls w/ same context, message, extra args
// // (Here we refer to the current functions with an _extra postfix)
// let out_std = ext_vrf_sign(private, context, message);
// let out_ext = ext_vrf_sign_extra(private, context, message, extra);
// // Basic size checks
// assert!(out_std.len() == VRF_OUTPUT_FULL);
// assert!(out_ext.len() == VRF_OUTPUT_FULL);
// // Given the same context, message & extra, output should be deterministic
// let out1 = &out_std[..VRF_OUTPUT_SIZE];
// let out2 = &out_ext[..VRF_OUTPUT_SIZE];
// assert_eq!(out1, out2);
// // But proof is non-deterministic
// let proof1 = &out_std[VRF_OUTPUT_SIZE..];
// let proof2 = &out_ext[VRF_OUTPUT_SIZE..];
// assert_ne!(proof1, proof2);
// // Cross-verify
// assert!(ext_vrf_verify_extra(
// public,
// context,
// message,
// extra,
// &out_std
// ));
// assert!(ext_vrf_verify(
// public,
// context,
// message,
// &out_ext
// ));
// }
}

View File

@@ -29,17 +29,6 @@ function pbkdf2Hash (wasm) {
assert(hash === '0x5fcbe04f05300a3ecc5c35d18ea0b78f3f6853d2ae5f3fca374f69a7d1f78b5def5c60dae1a568026c7492511e0c53521e8bb6e03a650e1263265fee92722270', 'ERROR: pbkdf2Hash does not match');
}
// function secp256k1Recover (wasm) {
// const pk = u8aToHex(wasm.keccak256(wasm.secp256k1Recover(
// hexToU8a('0x2bbfa926ee947dfac384d981f8913b4e0992e52d7b6711e0ec3d9f8b0f752648'),
// hexToU8a('0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c')
// )));
// console.log('\tPUB', pk);
// assert(pk === '0x91d366769d44a7c7690428bfdf67ec7eae23d2459694685257b6fc59d1baa1fe', 'ERROR: secp256k1Recover does not match');
// }
function scryptHash (wasm) {
const hash = u8aToHex(wasm.scrypt(stringToU8a('password'), stringToU8a('salt'), 14, 8, 1));
@@ -71,7 +60,6 @@ module.exports = {
keccak256,
pbkdf2Hash,
scryptHash,
// secp256k1Recover,
sha512Hash,
twoxHash
};

View File

@@ -47,15 +47,14 @@ function runAll () {
}
function runUnassisted () {
(async () => {
await beforeAll();
beforeAll()
.then(() => runAll())
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
runAll();
})().catch((error) => {
console.error(error);
process.exit(-1);
});
process.exit(-1);
});
}
module.exports = {