-
Notifications
You must be signed in to change notification settings - Fork 6
feat: CRP-2802 add sign_with_bls to utils
#102
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
802d146
...
altkdf 34eceb4
rm unused dep
altkdf e43cf5a
use identity instead of generator
altkdf fc95f18
sign_with_bls
altkdf 70d1898
mv file
altkdf f29a751
test public key consistency
altkdf 6841466
fix comments
altkdf ac6ce1a
Merge branch 'main' into alex/derive_public_vetkey
altkdf a432480
address comments
altkdf bb2db68
!= instead of <
altkdf a3fe424
address new comments
altkdf b6af7d8
Merge branch 'main' into alex/derive_public_vetkey
altkdf 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| [package] | ||
| name = "ic-vetkeys-canisters-tests" | ||
| authors.workspace = true | ||
| description.workspace = true | ||
| documentation.workspace = true | ||
| edition.workspace = true | ||
| version.workspace = true | ||
| license.workspace = true | ||
|
|
||
| [lib] | ||
| path = "src/lib.rs" | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies] | ||
| candid = { workspace = true } | ||
| ic-cdk = { workspace = true } | ||
| ic-cdk-macros = { workspace = true } | ||
| ic-dummy-getrandom-for-wasm = { workspace = true } | ||
| ic-vetkeys = { path = "../../ic_vetkeys" } | ||
| serde = { workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| ic-vetkeys-test-utils = { path = "../../ic_vetkeys_test_utils" } | ||
| pocket-ic = { workspace = true } | ||
| rand = { workspace = true } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| .PHONY: build | ||
| .SILENT: build | ||
| build: | ||
| cargo build --release --target wasm32-unknown-unknown | ||
|
|
||
| .PHONY: test | ||
| .SILENT: test | ||
| test: build | ||
| cargo test |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Canister tests | ||
|
|
||
| Currently, we only test `ic_vetkeys::management_canister::derive_public_vetkey`. | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use ic_cdk::update; | ||
| use ic_vetkeys::vetkd_api_types::{ | ||
| VetKDDeriveKeyReply, VetKDDeriveKeyRequest, VetKDKeyId, VetKDPublicKeyReply, | ||
| VetKDPublicKeyRequest, | ||
| }; | ||
|
|
||
| #[update] | ||
| async fn sign_with_bls(input: Vec<u8>, context: Vec<u8>, key_id: VetKDKeyId) -> Vec<u8> { | ||
| ic_vetkeys::management_canister::sign_with_bls(input, context, key_id) | ||
| .await | ||
| .expect("derive_public_vetkey call failed") | ||
|
altkdf marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| #[update] | ||
| async fn vetkd_derive_key( | ||
| input: Vec<u8>, | ||
| context: Vec<u8>, | ||
| key_id: VetKDKeyId, | ||
| transport_public_key: Vec<u8>, | ||
| ) -> Vec<u8> { | ||
| let request = VetKDDeriveKeyRequest { | ||
| input, | ||
| context, | ||
| key_id, | ||
| // Encryption with the G1 generator produces unencrypted vetKeys | ||
|
altkdf marked this conversation as resolved.
Outdated
|
||
| transport_public_key, | ||
| }; | ||
|
|
||
| let reply: (VetKDDeriveKeyReply,) = ic_cdk::api::call::call_with_payment128( | ||
| candid::Principal::management_canister(), | ||
| "vetkd_derive_key", | ||
| (request,), | ||
| 26_153_846_153, | ||
| ) | ||
| .await | ||
| .expect("vetkd_derive_key call failed"); | ||
|
|
||
| reply.0.encrypted_key | ||
| } | ||
|
|
||
| #[update] | ||
| async fn bls_public_key(context: Vec<u8>, key_id: VetKDKeyId) -> Vec<u8> { | ||
|
altkdf marked this conversation as resolved.
Outdated
|
||
| ic_vetkeys::management_canister::bls_public_key(None, context, key_id) | ||
| .await | ||
| .expect("bls_public_key call failed") | ||
| } | ||
|
|
||
| #[update] | ||
| async fn get_verification_key(context: Vec<u8>, key_id: VetKDKeyId) -> Vec<u8> { | ||
|
altkdf marked this conversation as resolved.
Outdated
|
||
| let request = VetKDPublicKeyRequest { | ||
| canister_id: None, | ||
| context, | ||
| key_id, | ||
| }; | ||
|
|
||
| let reply: (VetKDPublicKeyReply,) = ic_cdk::api::call::call( | ||
| candid::Principal::management_canister(), | ||
| "vetkd_public_key", | ||
| (request,), | ||
| ) | ||
| .await | ||
| .expect("vetkd_public_key call failed"); | ||
|
|
||
| reply.0.public_key | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| use candid::{decode_one, encode_args, CandidType, Principal}; | ||
| use ic_vetkeys::vetkd_api_types::{VetKDCurve, VetKDKeyId}; | ||
| use ic_vetkeys::{DerivedPublicKey, EncryptedVetKey, TransportSecretKey}; | ||
| use ic_vetkeys_test_utils::{git_root_dir, reproducible_rng}; | ||
| use pocket_ic::{PocketIc, PocketIcBuilder}; | ||
| use rand::{CryptoRng, Rng}; | ||
| use std::path::Path; | ||
|
|
||
| #[test] | ||
| fn bls_signature_should_be_equal_to_decrypted_vetkey() { | ||
| let rng = &mut reproducible_rng(); | ||
| let env = TestEnvironment::new(); | ||
| let input = random_bytes(rng, 10); | ||
| let context = random_bytes(rng, 10); | ||
| let key_id = VetKDKeyId { | ||
| curve: VetKDCurve::Bls12_381_G2, | ||
| name: "dfx_test_key".to_string(), | ||
| }; | ||
| let transport_secret_key = random_transport_key(rng); | ||
| let transport_public_key = transport_secret_key.public_key(); | ||
|
|
||
| let bls_signature: Vec<u8> = env.update( | ||
| Principal::anonymous(), | ||
| "sign_with_bls", | ||
| encode_args((input.clone(), context.clone(), key_id.clone())).unwrap(), | ||
| ); | ||
|
|
||
| let verification_key: Vec<u8> = env.update( | ||
| Principal::anonymous(), | ||
| "get_verification_key", | ||
| encode_args((context.clone(), key_id.clone())).unwrap(), | ||
| ); | ||
| let encrypted_vetkey_bytes: Vec<u8> = env.update( | ||
| Principal::anonymous(), | ||
| "vetkd_derive_key", | ||
| encode_args((input.clone(), context, key_id, transport_public_key)).unwrap(), | ||
| ); | ||
| let encrypted_vetkey = EncryptedVetKey::deserialize(encrypted_vetkey_bytes.as_ref()).unwrap(); | ||
| let derived_public_key = DerivedPublicKey::deserialize(verification_key.as_ref()).unwrap(); | ||
| let decrypted_vetkey = encrypted_vetkey | ||
| .decrypt_and_verify(&transport_secret_key, &derived_public_key, &input) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(bls_signature, decrypted_vetkey.signature_bytes().to_vec()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn bls_public_key_should_be_equal_to_verification_key() { | ||
| let rng = &mut reproducible_rng(); | ||
| let env = TestEnvironment::new(); | ||
| let context = random_bytes(rng, 10); | ||
| let key_id = VetKDKeyId { | ||
| curve: VetKDCurve::Bls12_381_G2, | ||
| name: "dfx_test_key".to_string(), | ||
| }; | ||
| let bls_public_key: Vec<u8> = env.update( | ||
| Principal::anonymous(), | ||
| "bls_public_key", | ||
| encode_args((context.clone(), key_id.clone())).unwrap(), | ||
| ); | ||
| let verification_key: Vec<u8> = env.update( | ||
| Principal::anonymous(), | ||
| "get_verification_key", | ||
| encode_args((context.clone(), key_id.clone())).unwrap(), | ||
| ); | ||
| assert_eq!(bls_public_key, verification_key); | ||
| } | ||
| struct TestEnvironment { | ||
| pic: PocketIc, | ||
| canister_id: Principal, | ||
| } | ||
|
|
||
| impl TestEnvironment { | ||
| fn new() -> Self { | ||
| let pic = PocketIcBuilder::new() | ||
| .with_application_subnet() | ||
| .with_ii_subnet() | ||
| .with_fiduciary_subnet() | ||
| .with_nonmainnet_features(true) | ||
| .build(); | ||
|
|
||
| let canister_id = pic.create_canister(); | ||
| pic.add_cycles(canister_id, 2_000_000_000_000); | ||
|
|
||
| let wasm_bytes = load_canister_wasm(); | ||
| pic.install_canister(canister_id, wasm_bytes, vec![], None); | ||
|
|
||
| // Make sure the canister is properly initialized | ||
| fast_forward(&pic, 5); | ||
|
|
||
| Self { pic, canister_id } | ||
| } | ||
|
|
||
| fn update<T: CandidType + for<'de> candid::Deserialize<'de>>( | ||
| &self, | ||
| caller: Principal, | ||
| method_name: &str, | ||
| args: Vec<u8>, | ||
| ) -> T { | ||
| let reply = self | ||
| .pic | ||
| .update_call(self.canister_id, caller, method_name, args); | ||
| match reply { | ||
| Ok(data) => decode_one(&data).expect("failed to decode reply"), | ||
| Err(user_error) => panic!("canister returned a user error: {user_error}"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn load_canister_wasm() -> Vec<u8> { | ||
| let wasm_path_string = match std::env::var("CUSTOM_WASM_PATH") { | ||
| Ok(path) if !path.is_empty() => path, | ||
| _ => format!( | ||
| "{}/target/wasm32-unknown-unknown/release/ic_vetkeys_canisters_tests.wasm", | ||
| git_root_dir() | ||
| ), | ||
| }; | ||
| let wasm_path = Path::new(&wasm_path_string); | ||
| std::fs::read(wasm_path) | ||
| .expect("wasm does not exist - run `cargo build --release --target wasm32-unknown-unknown`") | ||
| } | ||
|
|
||
| fn random_transport_key<R: Rng + CryptoRng>(rng: &mut R) -> TransportSecretKey { | ||
| let mut seed = vec![0u8; 32]; | ||
| rng.fill_bytes(&mut seed); | ||
| TransportSecretKey::from_seed(seed).unwrap() | ||
| } | ||
|
|
||
| fn random_bytes<R: Rng + CryptoRng>(rng: &mut R, max_length: usize) -> Vec<u8> { | ||
| let length = rng.gen_range(0..max_length); | ||
| let mut bytes = vec![0u8; length]; | ||
| rng.fill_bytes(&mut bytes); | ||
| bytes | ||
| } | ||
|
|
||
| fn fast_forward(ic: &PocketIc, ticks: u64) { | ||
| for _ in 0..ticks - 1 { | ||
| ic.tick(); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.