Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Update schnorrkel to 0.8.5 (#25)
Browse files Browse the repository at this point in the history
* Upgrade sr25519 to version 0.2 and edit glue code for compatibility

* verify now returns pointer to error string

* Add binding to VRF

* Revert upgrade

* Fix tests

* And PT_NULL manually as it is absent on OS X

* Change error reporting to enum

* Make Sr25519SignatureResult derive traits required from it to work in Rust tests

* Update README

* Fixes from review

* Fixes from review

* Try comparing enums on Mac

* Fixes from review

* Fixes from review

* Add docs to vrf functions

* Fix macro in lib.rs(no macro no problems)

* Fix test failing on clang

* Upgrade schnorrkel to 0.8.5

* Reformat accidental changes in lib.rs
  • Loading branch information
Harrm authored and Warchant committed Aug 19, 2019
1 parent 43dda6f commit c3736c8
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ if (CCACHE_FOUND)
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
endif (CCACHE_FOUND)


option(TESTING "Enable tests" OFF)

if (TESTING)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name = "sr25519crust"
crate-type = ["cdylib", "staticlib"]

[dependencies]
schnorrkel = { version="0.1.1" }
schnorrkel = { version="0.8.5" }

[dev-dependencies]
hex-literal = "0.2.0"
Expand Down
28 changes: 14 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ extern crate schnorrkel;
// of the Apache-2.0 license. See the LICENSE file for details.

// Originally developed (as a fork) in https://github.com/polkadot-js/schnorrkel-js/
// which was adpated from the initial https://github.com/paritytech/schnorrkel-js/
// which was adopted from the initial https://github.com/paritytech/schnorrkel-js/
// forked at commit eff430ddc3090f56317c80654208b8298ef7ab3f

use schnorrkel::{
derive::{ChainCode, Derivation, CHAIN_CODE_LENGTH},
Keypair, MiniSecretKey, PublicKey, SecretKey, Signature,
context::signing_context, vrf::{VRFOutput, VRFProof}, SignatureError};
context::signing_context,
derive::{CHAIN_CODE_LENGTH, ChainCode, Derivation}, Keypair, MiniSecretKey, PublicKey, SecretKey,
Signature, SignatureError, vrf::{VRFOutput, VRFProof}, ExpansionMode};

use std::ptr;
use std::slice;
Expand Down Expand Up @@ -45,11 +45,12 @@ fn convert_error(err: &SignatureError) -> Sr25519SignatureResult {
SignatureError::EquationFalse => Sr25519SignatureResult::EquationFalse,
SignatureError::PointDecompressionError => Sr25519SignatureResult::PointDecompressionError,
SignatureError::ScalarFormatError => Sr25519SignatureResult::ScalarFormatError,
SignatureError::BytesLengthError {name: _, description: _, length: _}
=> Sr25519SignatureResult::BytesLengthError,
SignatureError::MuSigAbsent {musig_stage: _} => Sr25519SignatureResult::MuSigAbsent,
SignatureError::MuSigInconsistent {musig_stage: _, duplicate: _}
=> Sr25519SignatureResult::MuSigInconsistent,
SignatureError::BytesLengthError { name: _, description: _, length: _ }
=> Sr25519SignatureResult::BytesLengthError,
SignatureError::MuSigAbsent { musig_stage: _ } => Sr25519SignatureResult::MuSigAbsent,
SignatureError::MuSigInconsistent { musig_stage: _, duplicate: _ }
=> Sr25519SignatureResult::MuSigInconsistent,
SignatureError::NotMarkedSchnorrkel => Sr25519SignatureResult::NotMarkedSchnorrkel
}
}

Expand All @@ -68,7 +69,7 @@ fn create_cc(data: &[u8]) -> ChainCode {
/// Keypair helper function.
fn create_from_seed(seed: &[u8]) -> Keypair {
match MiniSecretKey::from_bytes(seed) {
Ok(mini) => return mini.expand_to_keypair(),
Ok(mini) => return mini.expand_to_keypair(ExpansionMode::Ed25519),
Err(_) => panic!("Provided seed is invalid."),
}
}
Expand Down Expand Up @@ -141,7 +142,7 @@ pub unsafe extern "C" fn sr25519_derive_keypair_hard(
.secret
.hard_derive_mini_secret_key(Some(create_cc(cc)), &[])
.0
.expand_to_keypair();
.expand_to_keypair(ExpansionMode::Ed25519);

ptr::copy(kp.to_bytes().as_ptr(), keypair_out, SR25519_KEYPAIR_SIZE as usize);
}
Expand Down Expand Up @@ -259,7 +260,7 @@ pub unsafe extern "C" fn sr25519_verify(
Err(_) => return false,
};

create_public(public).verify_simple(SIGNING_CTX, message, &signature)
create_public(public).verify_simple(SIGNING_CTX, message, &signature).is_ok()
}

#[repr(C)]
Expand Down Expand Up @@ -289,7 +290,7 @@ pub unsafe extern "C" fn sr25519_vrf_sign_if_less(
let message = slice::from_raw_parts(message_ptr, message_length as usize);
let limit = slice::from_raw_parts(limit_ptr, SR25519_VRF_OUTPUT_SIZE as usize);
let res =
keypair.vrf_sign_n_check(
keypair.vrf_sign_after_check(
signing_context(SIGNING_CTX).bytes(message),
|x| x.as_output_bytes().as_ref().lt(&limit));
if let Some((io, proof, _)) = res {
Expand Down Expand Up @@ -350,7 +351,6 @@ pub unsafe extern "C" fn sr25519_vrf_verify(
pub mod tests {
extern crate rand;
extern crate schnorrkel;

use super::*;
use hex_literal::hex;
use schnorrkel::{KEYPAIR_LENGTH, SECRET_KEY_LENGTH, SIGNATURE_LENGTH};
Expand Down
8 changes: 4 additions & 4 deletions test/derive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern "C" {

TEST(sr25519, DeriveHardKnown) {
auto known_kp =
"28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a"_unhex;
"4c1250e05afcd79e74f6c035aee10248841090e009b6fd7ba6a98d5dc743250cafa4b32c608e3ee2ba624850b3f14c75841af84b16798bf1ee4a3875aa37a2cee661e416406384fe1ca091980958576d2bff7c461636e9f22c895f444905ea1f"_unhex;

auto cc =
"14416c6963650000000000000000000000000000000000000000000000000000"_unhex;
Expand All @@ -24,14 +24,14 @@ TEST(sr25519, DeriveHardKnown) {
auto actual_pubkey =
hex(std::vector<uint8_t>{derived.begin() + 64, derived.end()});
auto expected_pubkey =
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"s;
"d8db757f04521a940f0237c8a1e44dfbe0b3e39af929eb2e9e257ba61b9a0a1a"s;

ASSERT_EQ(actual_pubkey, expected_pubkey);
}

TEST(sr25519, DeriveSoftKnown) {
auto known_kp =
"28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a"_unhex;
"4c1250e05afcd79e74f6c035aee10248841090e009b6fd7ba6a98d5dc743250cafa4b32c608e3ee2ba624850b3f14c75841af84b16798bf1ee4a3875aa37a2cee661e416406384fe1ca091980958576d2bff7c461636e9f22c895f444905ea1f"_unhex;

auto cc =
"0c666f6f00000000000000000000000000000000000000000000000000000000"_unhex;
Expand All @@ -43,7 +43,7 @@ TEST(sr25519, DeriveSoftKnown) {
auto actual_pubkey =
hex(std::vector<uint8_t>{derived.begin() + 64, derived.end()});
auto expected_pubkey =
"40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a"s;
"b21e5aabeeb35d6a1bf76226a6c65cd897016df09ef208243e59eed2401f5357"s;

ASSERT_EQ(actual_pubkey, expected_pubkey);
}
6 changes: 4 additions & 2 deletions test/ds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ TEST(sr25519, SignAndVerifyInvalid) {

TEST(sr25519, VerifyExisting) {
auto pub =
"741c08a06f41c596608f6774259bd9043304adfa5d3eea62760bd9be97634d63"_unhex;
"46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a"_unhex;
auto secret = "05d65584630d16cd4af6d0bec10f34bb504a5dcb62dba2122d49f5a663763d0a"_unhex;

auto msg = "this is a message"_v;
auto sig =
"decef12cf20443e7c7a9d406c237e90bcfcf145860722622f92ebfd5eb4b5b3990b6443934b5cba8f925a0ae75b3a77d35b8490cbb358dd850806e58eaf72904"_unhex;
"4e172314444b8f820bb54c22e95076f220ed25373e5c178234aa6c211d29271244b947e3ff3418ff6b45fd1df1140c8cbff69fc58ee6dc96df70936a2bb74b82"_unhex;

ASSERT_EQ(pub.size(), SR25519_PUBLIC_SIZE);
ASSERT_EQ(sig.size(), SR25519_SIGNATURE_SIZE);
Expand Down
10 changes: 5 additions & 5 deletions test/keypair_from_seed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ struct KeypairFromSeed : public ::testing::TestWithParam<Case1> {

std::vector<Case1> KeypairFromSeed::cases = {
{std::vector<uint8_t>(32, 0),
"5046adc1dba838867b2bbbfdd0c3423e58b57970b5267a90f57960924a87f1560a6a85eaa642dac835424b5d7c8d637c00408c7a73da672b7f498521420b6dd3def12e42f3e487e9b14095aa8d5cc16a33491f1b50dadcf8811d1480f3fa8627"s},
"caa835781b15c7706f65b71f7a58c807ab360faed6440fb23e0f4c52e930de0a0a6a85eaa642dac835424b5d7c8d637c00408c7a73da672b7f498521420b6dd3def12e42f3e487e9b14095aa8d5cc16a33491f1b50dadcf8811d1480f3fa8627"s},
{"12345678901234567890123456789012"_v,
"f0106660c3dda23f16daa9ac5b811b963077f5bc0af89f85804f0de8e424f050f98d66f39442506ff947fd911f18c7a7a5da639a63e8d3b4e233f74143d951c1741c08a06f41c596608f6774259bd9043304adfa5d3eea62760bd9be97634d63"s},
"1ec20c6cb85bf4c7423b95752b70c312e6ae9e5701ffb310f0a9019d9c041e0af98d66f39442506ff947fd911f18c7a7a5da639a63e8d3b4e233f74143d951c1741c08a06f41c596608f6774259bd9043304adfa5d3eea62760bd9be97634d63"s},
{"fac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"_unhex,
"28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a"s}};
"05d65584630d16cd4af6d0bec10f34bb504a5dcb62dba2122d49f5a663763d0afd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a"s}};

TEST_P(KeypairFromSeed, ValidKeypairGenerated) {
auto [seed, expected] = GetParam();
TEST_P(KeypairFromSeed, ValidKeypairGenerated) {
auto [seed, expected] = GetParam();

std::vector<uint8_t> kp(SR25519_KEYPAIR_SIZE, 0);
sr25519_keypair_from_seed(kp.data(), seed.data());
Expand Down
3 changes: 1 addition & 2 deletions test/vrf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ TEST(VrfTest, Verify) {
}

TEST(VrfTest, ResultNotLess) {
auto keypair =
"e07a5da9575743a1993a9f2b9e39991c8d0c2ee4137347e450a39561f419fa7e84912092ffd5746b98a628c57eeafcfa60eb7e5c07438e00b3d0d528f42666813223ce3b7e32de1f876d5cbf44619dd5d1fa91d8c87b63af4a068f4fee24ad3a"_unhex;
auto keypair = "915bb406968655c3412df5773c3de3dee9f6da84668b5de8d2f34d0304d20b0bac5ea3a293dfd93859ee64a5b825937753864c19be857f045758dcae10259ba1049b21bb9cb88471b9dadb50b925135cfb291a463043635b58599a2d01b1fd18"_unhex;
std::array<uint8_t, SR25519_VRF_OUTPUT_SIZE + SR25519_VRF_PROOF_SIZE>
out_and_proof;

Expand Down

0 comments on commit c3736c8

Please sign in to comment.