|
| 1 | +// Copyright (C) 2019-2023 Aleo Systems Inc. |
| 2 | +// This file is part of the Aleo SDK library. |
| 3 | + |
| 4 | +// The Aleo SDK library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// The Aleo SDK library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use super::PrivateKey; |
| 18 | +use crate::{ |
| 19 | + types::{native::ComputeKeyNative, Group, Scalar}, |
| 20 | + Address, |
| 21 | +}; |
| 22 | + |
| 23 | +use core::{convert::TryFrom, ops::Deref}; |
| 24 | +use wasm_bindgen::prelude::*; |
| 25 | + |
| 26 | +#[wasm_bindgen] |
| 27 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 28 | +pub struct ComputeKey(ComputeKeyNative); |
| 29 | + |
| 30 | +#[wasm_bindgen] |
| 31 | +impl ComputeKey { |
| 32 | + /// Create a new compute key from a private key. |
| 33 | + /// |
| 34 | + /// @param {PrivateKey} private_key Private key |
| 35 | + /// |
| 36 | + /// @returns {ComputeKey} Compute key |
| 37 | + pub fn from_private_key(private_key: &PrivateKey) -> Self { |
| 38 | + Self(ComputeKeyNative::try_from(**private_key).unwrap()) |
| 39 | + } |
| 40 | + |
| 41 | + /// Get the address from the compute key. |
| 42 | + /// |
| 43 | + /// @returns {Address} |
| 44 | + pub fn address(&self) -> Address { |
| 45 | + Address::from(self.0.to_address()) |
| 46 | + } |
| 47 | + |
| 48 | + /// Get the sk_prf of the compute key. |
| 49 | + /// |
| 50 | + /// @returns {Scalar} sk_prf |
| 51 | + pub fn sk_prf(&self) -> Scalar { |
| 52 | + Scalar::from(self.0.sk_prf()) |
| 53 | + } |
| 54 | + |
| 55 | + /// Get the pr_tag of the compute key. |
| 56 | + /// |
| 57 | + /// @returns {Group} pr_tag |
| 58 | + pub fn pk_sig(&self) -> Group { |
| 59 | + Group::from(self.0.pk_sig()) |
| 60 | + } |
| 61 | + |
| 62 | + /// Get the pr_sig of the compute key. |
| 63 | + /// |
| 64 | + /// @returns {Group} pr_sig |
| 65 | + pub fn pr_sig(&self) -> Group { |
| 66 | + Group::from(self.0.pr_sig()) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Deref for ComputeKey { |
| 71 | + type Target = ComputeKeyNative; |
| 72 | + |
| 73 | + fn deref(&self) -> &Self::Target { |
| 74 | + &self.0 |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl From<ComputeKeyNative> for ComputeKey { |
| 79 | + fn from(compute_key: ComputeKeyNative) -> Self { |
| 80 | + Self(compute_key) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl From<ComputeKey> for ComputeKeyNative { |
| 85 | + fn from(compute_key: ComputeKey) -> Self { |
| 86 | + compute_key.0 |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl From<&ComputeKey> for ComputeKeyNative { |
| 91 | + fn from(compute_key: &ComputeKey) -> Self { |
| 92 | + compute_key.0.clone() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl From<&ComputeKeyNative> for ComputeKey { |
| 97 | + fn from(compute_key: &ComputeKeyNative) -> Self { |
| 98 | + Self(compute_key.clone()) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod tests { |
| 104 | + use super::*; |
| 105 | + use crate::{ |
| 106 | + types::native::{AddressNative, CurrentNetwork}, |
| 107 | + PrivateKey, |
| 108 | + }; |
| 109 | + |
| 110 | + use snarkvm_console::network::Network; |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_compute_key_conversions() { |
| 114 | + // Create a new private key. |
| 115 | + let private_key = PrivateKey::new(); |
| 116 | + let address = Address::from_private_key(&private_key); |
| 117 | + |
| 118 | + // Get the compute key from the private key. |
| 119 | + let compute_key = ComputeKey::from_private_key(&private_key); |
| 120 | + |
| 121 | + // Assert the address derivation can be computed from the compute key components. |
| 122 | + // Compute pk_prf := G^sk_prf. |
| 123 | + let pk_prf = Group::from(CurrentNetwork::g_scalar_multiply(&*compute_key.sk_prf())); |
| 124 | + // Compute the address := pk_sig + pr_sig + pk_prf. |
| 125 | + let group = compute_key.pk_sig().add(&compute_key.pr_sig()).add(&pk_prf); |
| 126 | + let address_native = AddressNative::new(*group); |
| 127 | + let derived_address = Address::from(address_native); |
| 128 | + |
| 129 | + // Assert an address can be derived from the compute key via it's official function. |
| 130 | + let compute_key_address = Address::from_compute_key(&compute_key); |
| 131 | + |
| 132 | + // Check the address matches all possible derivations from the compute key. |
| 133 | + assert_eq!(address, compute_key.address()); |
| 134 | + assert_eq!(address, derived_address); |
| 135 | + assert_eq!(address, compute_key_address); |
| 136 | + } |
| 137 | +} |
0 commit comments