Skip to content

Commit

Permalink
Merge pull request #1015 from dfinity/alex/reenable-tests-in-rust-tsc…
Browse files Browse the repository at this point in the history
…hnorr

reenable tests and adjust cycles in rust threshold Schnorr
  • Loading branch information
altkdf authored Oct 14, 2024
2 parents de5c69a + adfc19c commit 89ca125
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/motoko-threshold-schnorr-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
run: bash .github/workflows/provision-darwin.sh
- name: Motoko Threshold Schnorr Darwin
run: |
dfxvm default 0.22.0-beta.0
dfx start --background
npm install @noble/curves
pushd motoko/threshold-schnorr
Expand All @@ -34,7 +33,6 @@ jobs:
run: bash .github/workflows/provision-linux.sh
- name: Motoko Threshold Schnorr Linux
run: |
dfxvm default 0.22.0-beta.0
dfx start --background
npm install @noble/curves
pushd motoko/threshold-schnorr
Expand Down
7 changes: 6 additions & 1 deletion rust/threshold-schnorr/Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
.PHONY: all
all: build

.PHONY: build
.SILENT: build
build:
dfx build --check

.PHONY: deploy
.SILENT: deploy
deploy:
dfx deploy

.PHONY: test
.SILENT: test
test:
test: build
cargo test

.PHONY: clean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn public_key(algorithm: SchnorrAlgorithm) -> Result<PublicKeyReply, Strin
let request = ManagementCanisterSchnorrPublicKeyRequest {
canister_id: None,
derivation_path: vec![ic_cdk::api::caller().as_slice().to_vec()],
key_id: SchnorrKeyIds::TestKeyLocalDevelopment.to_key_id(algorithm),
key_id: SchnorrKeyIds::TestKey1.to_key_id(algorithm),
};

let (res,): (ManagementCanisterSchnorrPublicKeyReply,) = ic_cdk::call(
Expand All @@ -63,15 +63,15 @@ async fn sign(message: String, algorithm: SchnorrAlgorithm) -> Result<SignatureR
let internal_request = ManagementCanisterSignatureRequest {
message: message.as_bytes().to_vec(),
derivation_path: vec![ic_cdk::api::caller().as_slice().to_vec()],
key_id: SchnorrKeyIds::TestKeyLocalDevelopment.to_key_id(algorithm),
key_id: SchnorrKeyIds::TestKey1.to_key_id(algorithm),
};

let (internal_reply,): (ManagementCanisterSignatureReply,) =
ic_cdk::api::call::call_with_payment(
Principal::management_canister(),
"sign_with_schnorr",
(internal_request,),
25_000_000_000,
26_153_846_153,
)
.await
.map_err(|e| format!("sign_with_schnorr failed {e:?}"))?;
Expand Down
27 changes: 18 additions & 9 deletions rust/threshold-schnorr/src/schnorr_example_rust/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use candid::{decode_one, encode_args, encode_one, CandidType, Principal};
use pocket_ic::{PocketIc, WasmResult};
use pocket_ic::{PocketIc, PocketIcBuilder, WasmResult};
use schnorr_example_rust::{
PublicKeyReply, SchnorrAlgorithm, SignatureReply, SignatureVerificationReply,
};
Expand All @@ -11,7 +11,11 @@ fn signing_and_verification_should_work_correctly() {
const ALGORITHMS: [SchnorrAlgorithm; 2] =
[SchnorrAlgorithm::Bip340Secp256k1, SchnorrAlgorithm::Ed25519];

let pic = PocketIc::new();
let pic = PocketIcBuilder::new()
.with_application_subnet()
.with_ii_subnet()
.with_fiduciary_subnet()
.build();

for algorithm in ALGORITHMS {
for _trial in 0..5 {
Expand All @@ -33,14 +37,19 @@ fn test_impl(pic: &PocketIc, algorithm: SchnorrAlgorithm) {
// Make sure the canister is properly initialized
fast_forward(&pic, 5);

let message_hex = hex::encode("Test message");
// a message we can reverse to break the signature
// currently pocket IC only supports 32B messages for BIP340
let message: String = std::iter::repeat('a')
.take(16)
.chain(std::iter::repeat('b').take(16))
.collect();

let sig_reply: Result<SignatureReply, String> = update(
&pic,
my_principal,
example_canister_id,
"sign",
encode_args((message_hex.clone(), algorithm)).unwrap(),
encode_args((message.clone(), algorithm)).unwrap(),
);

let signature_hex = sig_reply.expect("failed to sign").signature_hex;
Expand All @@ -63,7 +72,7 @@ fn test_impl(pic: &PocketIc, algorithm: SchnorrAlgorithm) {
"verify",
encode_args((
signature_hex.clone(),
message_hex.clone(),
message.clone(),
public_key_hex.clone(),
algorithm,
))
Expand All @@ -81,7 +90,7 @@ fn test_impl(pic: &PocketIc, algorithm: SchnorrAlgorithm) {
"verify",
encode_args((
clone_and_reverse_chars(&signature_hex),
message_hex.clone(),
message.clone(),
public_key_hex.clone(),
algorithm,
))
Expand All @@ -99,7 +108,7 @@ fn test_impl(pic: &PocketIc, algorithm: SchnorrAlgorithm) {
"verify",
encode_args((
signature_hex.clone(),
clone_and_reverse_chars(&message_hex),
clone_and_reverse_chars(&message),
public_key_hex.clone(),
algorithm,
))
Expand All @@ -117,7 +126,7 @@ fn test_impl(pic: &PocketIc, algorithm: SchnorrAlgorithm) {
"verify",
encode_args((
signature_hex.clone(),
message_hex.clone(),
message.clone(),
clone_and_reverse_chars(&public_key_hex),
algorithm,
))
Expand All @@ -138,7 +147,7 @@ fn test_impl(pic: &PocketIc, algorithm: SchnorrAlgorithm) {
"verify",
encode_args((
signature_hex.clone(),
message_hex.clone(),
message.clone(),
public_key_hex.clone(),
other_algorithm(algorithm),
))
Expand Down

0 comments on commit 89ca125

Please sign in to comment.