Skip to content
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

Update vetKD examples with latest API names #1087

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,12 @@ shared ({ caller = initializer }) actor class () {
vetkd_public_key : ({
canister_id : ?Principal;
derivation_path : [Blob];
key_id : { curve : { #bls12_381 }; name : Text };
key_id : { curve : { #bls12_381_g2 }; name : Text };
}) -> async ({ public_key : Blob });
vetkd_encrypted_key : ({
public_key_derivation_path : [Blob];
vetkd_derive_encrypted_key : ({
derivation_path : [Blob];
derivation_id : Blob;
key_id : { curve : { #bls12_381 }; name : Text };
key_id : { curve : { #bls12_381_g2 }; name : Text };
encryption_public_key : Blob;
}) -> async ({ encrypted_key : Blob });
};
Expand All @@ -331,7 +331,7 @@ shared ({ caller = initializer }) actor class () {
let { public_key } = await vetkd_system_api.vetkd_public_key({
canister_id = null;
derivation_path = Array.make(Text.encodeUtf8("note_symmetric_key"));
key_id = { curve = #bls12_381; name = "test_key_1" };
key_id = { curve = #bls12_381_g2; name = "test_key_1" };
});
Hex.encode(Blob.toArray(public_key));
};
Expand All @@ -348,10 +348,10 @@ shared ({ caller = initializer }) actor class () {
buf.append(Buffer.fromArray(Blob.toArray(Text.encodeUtf8(note.owner))));
let derivation_id = Blob.fromArray(Buffer.toArray(buf)); // prefix-free

let { encrypted_key } = await vetkd_system_api.vetkd_encrypted_key({
let { encrypted_key } = await vetkd_system_api.vetkd_derive_encrypted_key({
derivation_id;
public_key_derivation_path = Array.make(Text.encodeUtf8("note_symmetric_key"));
key_id = { curve = #bls12_381; name = "test_key_1" };
derivation_path = Array.make(Text.encodeUtf8("note_symmetric_key"));
key_id = { curve = #bls12_381_g2; name = "test_key_1" };
encryption_public_key;
});
Hex.encode(Blob.toArray(encrypted_key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ async fn symmetric_key_verification_key_for_note() -> String {
let request = VetKDPublicKeyRequest {
canister_id: None,
derivation_path: vec![b"note_symmetric_key".to_vec()],
key_id: bls12_381_test_key_1(),
key_id: bls12_381_g2_test_key_1(),
};

let (response,): (VetKDPublicKeyReply,) = ic_cdk::call(
Expand Down Expand Up @@ -396,8 +396,8 @@ async fn encrypted_symmetric_key_for_note(
buf.extend_from_slice(note.owner.as_bytes());
buf // prefix-free
},
public_key_derivation_path: vec![b"note_symmetric_key".to_vec()],
key_id: bls12_381_test_key_1(),
derivation_path: vec![b"note_symmetric_key".to_vec()],
key_id: bls12_381_g2_test_key_1(),
encryption_public_key,
}
} else {
Expand All @@ -407,18 +407,18 @@ async fn encrypted_symmetric_key_for_note(

let (response,): (VetKDEncryptedKeyReply,) = ic_cdk::call(
vetkd_system_api_canister_id(),
"vetkd_encrypted_key",
"vetkd_derive_encrypted_key",
(request,),
)
.await
.expect("call to vetkd_encrypted_key failed");
.expect("call to vetkd_derive_encrypted_key failed");

hex::encode(response.encrypted_key)
}

fn bls12_381_test_key_1() -> VetKDKeyId {
fn bls12_381_g2_test_key_1() -> VetKDKeyId {
VetKDKeyId {
curve: VetKDCurve::Bls12_381,
curve: VetKDCurve::Bls12_381_G2,
name: "test_key_1".to_string(),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ pub type CanisterId = Principal;

#[derive(CandidType, Deserialize)]
pub enum VetKDCurve {
#[serde(rename = "bls12_381")]
Bls12_381,
#[serde(rename = "bls12_381_g2")]
#[allow(non_camel_case_types)]
Bls12_381_G2,
}

#[derive(CandidType, Deserialize)]
Expand All @@ -30,7 +31,7 @@ pub struct VetKDPublicKeyReply {

#[derive(CandidType, Deserialize)]
pub struct VetKDEncryptedKeyRequest {
pub public_key_derivation_path: Vec<Vec<u8>>,
pub derivation_path: Vec<Vec<u8>>,
pub derivation_id: Vec<u8>,
pub key_id: VetKDKeyId,
pub encryption_public_key: Vec<u8>,
Expand Down
6 changes: 3 additions & 3 deletions motoko/encrypted-notes-dapp-vetkd/vetkd_system_api.did
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type canister_id = principal;
type vetkd_curve = variant { bls12_381 };
type vetkd_curve = variant { bls12_381_g2 };

service : {
vetkd_public_key : (
Expand All @@ -9,9 +9,9 @@ service : {
key_id : record { curve : vetkd_curve; name : text };
}
) -> (record { public_key : blob });
vetkd_encrypted_key : (
vetkd_derive_encrypted_key : (
record {
public_key_derivation_path : vec blob;
derivation_path : vec blob;
derivation_id : blob;
key_id : record { curve : vetkd_curve; name : text };
encryption_public_key : blob;
Expand Down
Binary file modified motoko/encrypted-notes-dapp-vetkd/vetkd_system_api.wasm
Binary file not shown.
26 changes: 13 additions & 13 deletions motoko/vetkd/src/app_backend/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ actor {
vetkd_public_key : ({
canister_id : ?Principal;
derivation_path : [Blob];
key_id : { curve : { #bls12_381 }; name : Text };
key_id : { curve : { #bls12_381_g2 }; name : Text };
}) -> async ({ public_key : Blob });
vetkd_encrypted_key : ({
public_key_derivation_path : [Blob];
vetkd_derive_encrypted_key : ({
derivation_path : [Blob];
derivation_id : Blob;
key_id : { curve : { #bls12_381 }; name : Text };
key_id : { curve : { #bls12_381_g2 }; name : Text };
encryption_public_key : Blob;
}) -> async ({ encrypted_key : Blob });
};
Expand All @@ -26,7 +26,7 @@ actor {
let { public_key } = await vetkd_system_api.vetkd_public_key({
canister_id = null;
derivation_path;
key_id = { curve = #bls12_381; name = "test_key_1" };
key_id = { curve = #bls12_381_g2; name = "test_key_1" };
});
Hex.encode(Blob.toArray(public_key));
};
Expand All @@ -35,18 +35,18 @@ actor {
let { public_key } = await vetkd_system_api.vetkd_public_key({
canister_id = null;
derivation_path = Array.make(Text.encodeUtf8("symmetric_key"));
key_id = { curve = #bls12_381; name = "test_key_1" };
key_id = { curve = #bls12_381_g2; name = "test_key_1" };
});
Hex.encode(Blob.toArray(public_key));
};

public shared ({ caller }) func encrypted_symmetric_key_for_caller(encryption_public_key : Blob) : async Text {
Debug.print("encrypted_symmetric_key_for_caller: caller: " # debug_show (caller));

let { encrypted_key } = await vetkd_system_api.vetkd_encrypted_key({
let { encrypted_key } = await vetkd_system_api.vetkd_derive_encrypted_key({
derivation_id = Principal.toBlob(caller);
public_key_derivation_path = Array.make(Text.encodeUtf8("symmetric_key"));
key_id = { curve = #bls12_381; name = "test_key_1" };
derivation_path = Array.make(Text.encodeUtf8("symmetric_key"));
key_id = { curve = #bls12_381_g2; name = "test_key_1" };
encryption_public_key;
});
Hex.encode(Blob.toArray(encrypted_key));
Expand All @@ -56,18 +56,18 @@ actor {
let { public_key } = await vetkd_system_api.vetkd_public_key({
canister_id = null;
derivation_path = Array.make(Text.encodeUtf8("ibe_encryption"));
key_id = { curve = #bls12_381; name = "test_key_1" };
key_id = { curve = #bls12_381_g2; name = "test_key_1" };
});
Hex.encode(Blob.toArray(public_key));
};

public shared ({ caller }) func encrypted_ibe_decryption_key_for_caller(encryption_public_key : Blob) : async Text {
Debug.print("encrypted_ibe_decryption_key_for_caller: caller: " # debug_show (caller));

let { encrypted_key } = await vetkd_system_api.vetkd_encrypted_key({
let { encrypted_key } = await vetkd_system_api.vetkd_derive_encrypted_key({
derivation_id = Principal.toBlob(caller);
public_key_derivation_path = Array.make(Text.encodeUtf8("ibe_encryption"));
key_id = { curve = #bls12_381; name = "test_key_1" };
derivation_path = Array.make(Text.encodeUtf8("ibe_encryption"));
key_id = { curve = #bls12_381_g2; name = "test_key_1" };
encryption_public_key;
});
Hex.encode(Blob.toArray(encrypted_key));
Expand Down
18 changes: 9 additions & 9 deletions motoko/vetkd/src/system_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ lazy_static::lazy_static! {
}

thread_local! {
static RNG: RefCell<Option<ChaCha20Rng>> = RefCell::new(None);
static RNG: RefCell<Option<ChaCha20Rng>> = const { RefCell::new(None) }
}

#[update]
async fn vetkd_public_key(request: VetKDPublicKeyRequest) -> VetKDPublicKeyReply {
ensure_bls12_381_test_key_1(request.key_id);
ensure_bls12_381_g2_test_key_1(request.key_id);
ensure_derivation_path_is_valid(&request.derivation_path);
let derivation_path = {
let canister_id = request.canister_id.unwrap_or_else(ic_cdk::caller);
Expand All @@ -51,13 +51,13 @@ async fn vetkd_public_key(request: VetKDPublicKeyRequest) -> VetKDPublicKeyReply
}

#[update]
async fn vetkd_encrypted_key(request: VetKDEncryptedKeyRequest) -> VetKDEncryptedKeyReply {
async fn vetkd_derive_encrypted_key(request: VetKDEncryptedKeyRequest) -> VetKDEncryptedKeyReply {
ensure_call_is_paid(ENCRYPTED_KEY_CYCLE_COSTS);
ensure_bls12_381_test_key_1(request.key_id);
ensure_derivation_path_is_valid(&request.public_key_derivation_path);
ensure_bls12_381_g2_test_key_1(request.key_id);
ensure_derivation_path_is_valid(&request.derivation_path);
let derivation_path = DerivationPath::new(
ic_cdk::caller().as_slice(),
&request.public_key_derivation_path,
&request.derivation_path,
);
let tpk =
TransportPublicKey::deserialize(&request.encryption_public_key).unwrap_or_else(
Expand Down Expand Up @@ -93,16 +93,16 @@ async fn vetkd_encrypted_key(request: VetKDEncryptedKeyRequest) -> VetKDEncrypte
}
}

fn ensure_bls12_381_test_key_1(key_id: VetKDKeyId) {
if key_id.curve != VetKDCurve::Bls12_381 {
fn ensure_bls12_381_g2_test_key_1(key_id: VetKDKeyId) {
if key_id.curve != VetKDCurve::Bls12_381_G2 {
ic_cdk::trap("unsupported key ID curve");
}
if key_id.name.as_str() != "test_key_1" {
ic_cdk::trap("unsupported key ID name");
}
}

fn ensure_derivation_path_is_valid(derivation_path: &Vec<Vec<u8>>) {
fn ensure_derivation_path_is_valid(derivation_path: &[Vec<u8>]) {
if derivation_path.len() > 255 {
ic_cdk::trap("derivation path too long")
}
Expand Down
7 changes: 4 additions & 3 deletions motoko/vetkd/src/system_api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ pub type CanisterId = Principal;

#[derive(CandidType, Deserialize, Eq, PartialEq)]
pub enum VetKDCurve {
#[serde(rename = "bls12_381")]
Bls12_381,
#[serde(rename = "bls12_381_g2")]
#[allow(non_camel_case_types)]
Bls12_381_G2,
}

#[derive(CandidType, Deserialize, Eq, PartialEq)]
Expand All @@ -30,7 +31,7 @@ pub struct VetKDPublicKeyReply {

#[derive(CandidType, Deserialize)]
pub struct VetKDEncryptedKeyRequest {
pub public_key_derivation_path: Vec<Vec<u8>>,
pub derivation_path: Vec<Vec<u8>>,
pub derivation_id: Vec<u8>,
pub key_id: VetKDKeyId,
pub encryption_public_key: Vec<u8>,
Expand Down
6 changes: 3 additions & 3 deletions motoko/vetkd/src/system_api/vetkd_system_api.did
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type canister_id = principal;
type vetkd_curve = variant { bls12_381 };
type vetkd_curve = variant { bls12_381_g2 };

service : {
vetkd_public_key : (
Expand All @@ -9,9 +9,9 @@ service : {
key_id : record { curve : vetkd_curve; name : text };
}
) -> (record { public_key : blob });
vetkd_encrypted_key : (
vetkd_derive_encrypted_key : (
record {
public_key_derivation_path : vec blob;
derivation_path : vec blob;
derivation_id : blob;
key_id : record { curve : vetkd_curve; name : text };
encryption_public_key : blob;
Expand Down
24 changes: 12 additions & 12 deletions rust/vetkd/src/app_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn symmetric_key_verification_key() -> String {
let request = VetKDPublicKeyRequest {
canister_id: None,
derivation_path: vec![b"symmetric_key".to_vec()],
key_id: bls12_381_test_key_1(),
key_id: bls12_381_g2_test_key_1(),
};

let (response,): (VetKDPublicKeyReply,) = ic_cdk::api::call::call(
Expand All @@ -34,18 +34,18 @@ async fn encrypted_symmetric_key_for_caller(encryption_public_key: Vec<u8>) -> S

let request = VetKDEncryptedKeyRequest {
derivation_id: ic_cdk::caller().as_slice().to_vec(),
public_key_derivation_path: vec![b"symmetric_key".to_vec()],
key_id: bls12_381_test_key_1(),
derivation_path: vec![b"symmetric_key".to_vec()],
key_id: bls12_381_g2_test_key_1(),
encryption_public_key,
};

let (response,): (VetKDEncryptedKeyReply,) = ic_cdk::api::call::call(
vetkd_system_api_canister_id(),
"vetkd_encrypted_key",
"vetkd_derive_encrypted_key",
(request,),
)
.await
.expect("call to vetkd_encrypted_key failed");
.expect("call to vetkd_derive_encrypted_key failed");

hex::encode(response.encrypted_key)
}
Expand All @@ -55,7 +55,7 @@ async fn ibe_encryption_key() -> String {
let request = VetKDPublicKeyRequest {
canister_id: None,
derivation_path: vec![b"ibe_encryption".to_vec()],
key_id: bls12_381_test_key_1(),
key_id: bls12_381_g2_test_key_1(),
};

let (response,): (VetKDPublicKeyReply,) = ic_cdk::api::call::call(
Expand All @@ -75,25 +75,25 @@ async fn encrypted_ibe_decryption_key_for_caller(encryption_public_key: Vec<u8>)

let request = VetKDEncryptedKeyRequest {
derivation_id: ic_cdk::caller().as_slice().to_vec(),
public_key_derivation_path: vec![b"ibe_encryption".to_vec()],
key_id: bls12_381_test_key_1(),
derivation_path: vec![b"ibe_encryption".to_vec()],
key_id: bls12_381_g2_test_key_1(),
encryption_public_key,
};

let (response,): (VetKDEncryptedKeyReply,) = ic_cdk::api::call::call(
vetkd_system_api_canister_id(),
"vetkd_encrypted_key",
"vetkd_derive_encrypted_key",
(request,),
)
.await
.expect("call to vetkd_encrypted_key failed");
.expect("call to vetkd_derive_encrypted_key failed");

hex::encode(response.encrypted_key)
}

fn bls12_381_test_key_1() -> VetKDKeyId {
fn bls12_381_g2_test_key_1() -> VetKDKeyId {
VetKDKeyId {
curve: VetKDCurve::Bls12_381,
curve: VetKDCurve::Bls12_381_G2,
name: "test_key_1".to_string(),
}
}
Expand Down
7 changes: 4 additions & 3 deletions rust/vetkd/src/app_backend/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ pub type CanisterId = Principal;

#[derive(CandidType, Deserialize)]
pub enum VetKDCurve {
#[serde(rename = "bls12_381")]
Bls12_381,
#[serde(rename = "bls12_381_g2")]
#[allow(non_camel_case_types)]
Bls12_381_G2,
}

#[derive(CandidType, Deserialize)]
Expand All @@ -30,7 +31,7 @@ pub struct VetKDPublicKeyReply {

#[derive(CandidType, Deserialize)]
pub struct VetKDEncryptedKeyRequest {
pub public_key_derivation_path: Vec<Vec<u8>>,
pub derivation_path: Vec<Vec<u8>>,
pub derivation_id: Vec<u8>,
pub key_id: VetKDKeyId,
pub encryption_public_key: Vec<u8>,
Expand Down
Loading
Loading