Merge pull request #56 from vacp2p/serde_json_vs_group_encoding

`serde_json` instead of `GroupEncoding` serialization
This commit is contained in:
tyshko-rostyslav 2025-04-08 14:25:09 -04:00 committed by GitHub
commit bfe39185fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 57 additions and 61 deletions

View File

@ -1,5 +1,4 @@
use aes_gcm::{aead::Aead, AeadCore, Aes256Gcm, Key, KeyInit};
use elliptic_curve::group::GroupEncoding;
use elliptic_curve::PrimeField;
use k256::{AffinePoint, FieldBytes, Scalar};
use log::info;
@ -41,7 +40,7 @@ impl EphemeralKeyHolder {
data: &[u8],
) -> (CipherText, Nonce) {
let key_point = self.calculate_shared_secret_sender(viewing_public_key_receiver);
let binding = key_point.to_bytes();
let binding = serde_json::to_vec(&key_point).unwrap();
let key_raw = &binding.as_slice()[..32];
let key_raw_adjust: [u8; 32] = key_raw.try_into().unwrap();
@ -56,7 +55,7 @@ impl EphemeralKeyHolder {
pub fn log(&self) {
info!(
"Ephemeral private key is {:?}",
hex::encode(self.ephemeral_secret_key.to_bytes())
hex::encode(serde_json::to_vec(&self.ephemeral_secret_key).unwrap())
);
}
}

View File

@ -1,6 +1,5 @@
use aes_gcm::{aead::Aead, Aes256Gcm, Key, KeyInit};
use constants_types::{CipherText, Nonce};
use elliptic_curve::group::GroupEncoding;
use ephemeral_key_holder::EphemeralKeyHolder;
use k256::AffinePoint;
use log::info;
@ -65,7 +64,7 @@ impl AddressKeyHolder {
nonce: Nonce,
) -> Result<Vec<u8>, aes_gcm::Error> {
let key_point = self.calculate_shared_secret_receiver(ephemeral_public_key_sender);
let binding = key_point.to_bytes();
let binding = serde_json::to_vec(&key_point).unwrap();
let key_raw = &binding.as_slice()[..32];
let key_raw_adjust: [u8; 32] = key_raw.try_into().unwrap();
@ -79,23 +78,29 @@ impl AddressKeyHolder {
pub fn log(&self) {
info!(
"Secret spending key is {:?}",
hex::encode(self.top_secret_key_holder.secret_spending_key.to_bytes()),
hex::encode(
serde_json::to_vec(&self.top_secret_key_holder.secret_spending_key).unwrap()
),
);
info!(
"Nulifier secret key is {:?}",
hex::encode(self.utxo_secret_key_holder.nullifier_secret_key.to_bytes()),
hex::encode(
serde_json::to_vec(&self.utxo_secret_key_holder.nullifier_secret_key).unwrap()
),
);
info!(
"Viewing secret key is {:?}",
hex::encode(self.utxo_secret_key_holder.viewing_secret_key.to_bytes()),
hex::encode(
serde_json::to_vec(&self.utxo_secret_key_holder.viewing_secret_key).unwrap()
),
);
info!(
"Nullifier public key is {:?}",
hex::encode(self.nullifer_public_key.to_bytes()),
hex::encode(serde_json::to_vec(&self.nullifer_public_key).unwrap()),
);
info!(
"Viewing public key is {:?}",
hex::encode(self.viewing_public_key.to_bytes()),
hex::encode(serde_json::to_vec(&self.viewing_public_key).unwrap()),
);
}
}
@ -158,7 +163,7 @@ mod tests {
address_key_holder.calculate_shared_secret_receiver(ephemeral_public_key_sender);
// Prepare the encryption key from shared secret
let key_raw = shared_secret.to_bytes();
let key_raw = serde_json::to_vec(&shared_secret).unwrap();
let key_raw_adjust_pre = &key_raw.as_slice()[..32];
let key_raw_adjust: [u8; 32] = key_raw_adjust_pre.try_into().unwrap();
let key: Key<Aes256Gcm> = key_raw_adjust.into();
@ -226,7 +231,7 @@ mod tests {
address_key_holder.calculate_shared_secret_receiver(ephemeral_public_key_sender);
// Prepare the encryption key from shared secret
let key_raw = shared_secret.to_bytes();
let key_raw = serde_json::to_vec(&shared_secret).unwrap();
let key_raw_adjust_pre = &key_raw.as_slice()[..32];
let key_raw_adjust: [u8; 32] = key_raw_adjust_pre.try_into().unwrap();
let key: Key<Aes256Gcm> = key_raw_adjust.into();
@ -266,7 +271,7 @@ mod tests {
address_key_holder.calculate_shared_secret_receiver(ephemeral_public_key_sender);
// Prepare the encryption key from shared secret
let key_raw = shared_secret.to_bytes();
let key_raw = serde_json::to_vec(&shared_secret).unwrap();
let key_raw_adjust_pre = &key_raw.as_slice()[..32];
let key_raw_adjust: [u8; 32] = key_raw_adjust_pre.try_into().unwrap();
let key: Key<Aes256Gcm> = key_raw_adjust.into();
@ -312,7 +317,7 @@ mod tests {
let shared_secret =
address_key_holder.calculate_shared_secret_receiver(ephemeral_public_key_sender);
// Prepare the encryption key from shared secret
let key_raw = shared_secret.to_bytes();
let key_raw = serde_json::to_vec(&shared_secret).unwrap();
let key_raw_adjust_pre = &key_raw.as_slice()[..32];
let key_raw_adjust: [u8; 32] = key_raw_adjust_pre.try_into().unwrap();
let key: Key<Aes256Gcm> = key_raw_adjust.into();
@ -351,7 +356,7 @@ mod tests {
println!(
"Group generator {:?}",
hex::encode(AffinePoint::GENERATOR.to_bytes())
hex::encode(serde_json::to_vec(&AffinePoint::GENERATOR).unwrap())
);
println!(
"Nullifier constant {:?}",
@ -373,11 +378,11 @@ mod tests {
println!("Address{:?}", hex::encode(address));
println!(
"Nulifier public key {:?}",
hex::encode(nullifer_public_key.to_bytes())
hex::encode(serde_json::to_vec(&nullifer_public_key).unwrap())
);
println!(
"Viewing public key {:?}",
hex::encode(viewing_public_key.to_bytes())
hex::encode(serde_json::to_vec(&viewing_public_key).unwrap())
);
}
}

View File

@ -1,4 +1,3 @@
use elliptic_curve::group::GroupEncoding;
use elliptic_curve::PrimeField;
use k256::{AffinePoint, FieldBytes, Scalar};
use rand::{rngs::OsRng, RngCore};
@ -105,8 +104,8 @@ impl UTXOSecretKeyHolder {
let mut hasher = sha2::Sha256::new();
hasher.update(npk.to_bytes());
hasher.update(vpk.to_bytes());
hasher.update(serde_json::to_vec(&npk).unwrap());
hasher.update(serde_json::to_vec(&vpk).unwrap());
<TreeHashType>::from(hasher.finalize_fixed())
}

View File

@ -4,7 +4,6 @@ use std::sync::{
};
use common::ExecutionFailureKind;
use k256::elliptic_curve::group::GroupEncoding;
use ::storage::transaction::{Transaction, TransactionPayload, TxKind};
use accounts::account_core::{Account, AccountAddress};
@ -181,7 +180,8 @@ impl NodeCore {
let ephm_key_holder = &accout.produce_ephemeral_key_holder();
ephm_key_holder.log();
let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes();
let eph_pub_key =
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
let encoded_data = Account::encrypt_data(
&ephm_key_holder,
@ -232,7 +232,8 @@ impl NodeCore {
let ephm_key_holder = &accout.produce_ephemeral_key_holder();
ephm_key_holder.log();
let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes();
let eph_pub_key =
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
let encoded_data = utxos
.iter()
@ -308,7 +309,8 @@ impl NodeCore {
let ephm_key_holder = &accout.produce_ephemeral_key_holder();
ephm_key_holder.log();
let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes();
let eph_pub_key =
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
let encoded_data: Vec<(Vec<u8>, Vec<u8>, u8)> = utxos
.iter()
@ -392,7 +394,8 @@ impl NodeCore {
let ephm_key_holder = &accout.produce_ephemeral_key_holder();
ephm_key_holder.log();
let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes();
let eph_pub_key =
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
let mut encoded_data: Vec<(Vec<u8>, Vec<u8>, u8)> = resulting_utxos_receiver
.iter()
@ -498,7 +501,8 @@ impl NodeCore {
let ephm_key_holder = &account.produce_ephemeral_key_holder();
ephm_key_holder.log();
let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes();
let eph_pub_key =
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
let encoded_data: Vec<(Vec<u8>, Vec<u8>, u8)> = utxos
.iter()
@ -1124,7 +1128,8 @@ impl NodeCore {
let ephm_key_holder = &accout.produce_ephemeral_key_holder();
ephm_key_holder.log();
let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes();
let eph_pub_key =
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
let encoded_data: Vec<(Vec<u8>, Vec<u8>, u8)> = utxos
.iter()

View File

@ -6,7 +6,6 @@ use std::{
use accounts::account_core::{Account, AccountAddress};
use anyhow::Result;
use block_store::NodeBlockStore;
use elliptic_curve::group::GroupEncoding;
use k256::AffinePoint;
use public_context::PublicSCContext;
use storage::{
@ -106,39 +105,29 @@ impl NodeChainStore {
.collect(),
)?;
let slice_try: Result<[u8; 33], _> = tx.ephemeral_pub_key.clone().try_into();
let eph_key_compressed =
slice_try.and_then(|inner| Ok(<AffinePoint as GroupEncoding>::Repr::from(inner)));
let eph_key_compressed = serde_json::to_vec(&tx.ephemeral_pub_key);
if let Ok(eph_key_compressed) = eph_key_compressed {
let ephemeral_public_key_sender = AffinePoint::from_bytes(&eph_key_compressed);
let ephemeral_public_key_sender =
serde_json::from_slice::<AffinePoint>(&eph_key_compressed)?;
if ephemeral_public_key_sender.is_some().into() {
let ephemeral_public_key_sender = ephemeral_public_key_sender.unwrap();
for (ciphertext, nonce, tag) in tx.encoded_data.clone() {
let slice = nonce.as_slice();
let nonce =
accounts::key_management::constants_types::Nonce::clone_from_slice(
slice,
for (ciphertext, nonce, tag) in tx.encoded_data.clone() {
let slice = nonce.as_slice();
let nonce =
accounts::key_management::constants_types::Nonce::clone_from_slice(slice);
for (acc_id, acc) in self.acc_map.iter_mut() {
if acc_id[0] == tag {
let decoded_data_curr_acc = acc.decrypt_data(
ephemeral_public_key_sender,
ciphertext.clone(),
nonce,
);
for (acc_id, acc) in self.acc_map.iter_mut() {
if acc_id[0] == tag {
let decoded_data_curr_acc = acc.decrypt_data(
ephemeral_public_key_sender,
ciphertext.clone(),
nonce,
);
if let Ok(decoded_data_curr_acc) = decoded_data_curr_acc {
let decoded_utxo_try =
serde_json::from_slice::<UTXO>(&decoded_data_curr_acc);
if let Ok(utxo) = decoded_utxo_try {
if &utxo.owner == acc_id {
acc.utxo_tree.insert_item(utxo)?;
}
if let Ok(decoded_data_curr_acc) = decoded_data_curr_acc {
let decoded_utxo_try =
serde_json::from_slice::<UTXO>(&decoded_data_curr_acc);
if let Ok(utxo) = decoded_utxo_try {
if &utxo.owner == acc_id {
acc.utxo_tree.insert_item(utxo)?;
}
}
}

View File

@ -1,5 +1,5 @@
use accounts::account_core::{AccountAddress, PublicKey};
use elliptic_curve::group::GroupEncoding;
use k256::AffinePoint;
use std::collections::HashMap;
#[derive(Debug, Clone)]
@ -16,10 +16,9 @@ impl AccountPublicData {
viewing_public_key: Vec<u8>,
) -> Self {
Self {
nullifier_public_key: PublicKey::from_bytes(nullifier_public_key.as_slice().into())
.unwrap(),
viewing_public_key: PublicKey::from_bytes(viewing_public_key.as_slice().into())
nullifier_public_key: serde_json::from_slice::<AffinePoint>(&nullifier_public_key)
.unwrap(),
viewing_public_key: serde_json::from_slice::<AffinePoint>(&viewing_public_key).unwrap(),
address,
}
}