fix: revers of scalar dep

This commit is contained in:
Oleksandr Pravdyvyi 2025-09-17 08:59:14 +03:00
parent 931b8c7176
commit 85a16a2f04
No known key found for this signature in database
GPG Key ID: 9F8955C63C443871
8 changed files with 30 additions and 34 deletions

View File

@ -1,5 +1,3 @@
use elliptic_curve::PrimeField;
use k256::Scalar;
use log::info;
use nssa_core::{
NullifierPublicKey, SharedSecretKey,
@ -29,11 +27,11 @@ impl EphemeralKeyHolder {
let hash_recepient = hasher.finalize();
let mut hasher = sha2::Sha256::new();
hasher.update(sender_outgoing_viewing_secret_key.to_bytes());
hasher.update(sender_outgoing_viewing_secret_key);
hasher.update(hash_recepient);
Self {
ephemeral_secret_key: Scalar::from_repr(hasher.finalize()).unwrap(),
ephemeral_secret_key: hasher.finalize().into(),
}
}

View File

@ -98,8 +98,8 @@ impl KeyChain {
#[cfg(test)]
mod tests {
use aes_gcm::aead::OsRng;
use elliptic_curve::ff::Field;
use k256::{AffinePoint, Scalar};
use k256::AffinePoint;
use rand::RngCore;
use super::*;
@ -117,7 +117,8 @@ mod tests {
let address_key_holder = KeyChain::new_os_random();
// Generate a random ephemeral public key sender
let scalar = Scalar::random(&mut OsRng);
let mut scalar = [0; 32];
OsRng.fill_bytes(&mut scalar);
let ephemeral_public_key_sender = EphemeralPublicKey::from_scalar(scalar);
// Calculate shared secret

View File

@ -1,8 +1,9 @@
use bip39::Mnemonic;
use common::TreeHashType;
use elliptic_curve::PrimeField;
use k256::Scalar;
use nssa_core::{NullifierPublicKey, NullifierSecretKey, encryption::IncomingViewingPublicKey};
use nssa_core::{
NullifierPublicKey, NullifierSecretKey,
encryption::{IncomingViewingPublicKey, Scalar},
};
use rand::{RngCore, rngs::OsRng};
use serde::{Deserialize, Serialize};
use sha2::{Digest, digest::FixedOutput};
@ -79,9 +80,7 @@ impl SecretSpendingKey {
hasher.update([2u8]);
hasher.update([0u8; 22]);
let hash = <TreeHashType>::from(hasher.finalize_fixed());
IncomingViewingSecretKey::from_repr(hash.into()).unwrap()
<TreeHashType>::from(hasher.finalize_fixed())
}
pub fn generate_outgoing_viewing_secret_key(&self) -> OutgoingViewingSecretKey {
@ -92,9 +91,7 @@ impl SecretSpendingKey {
hasher.update([3u8]);
hasher.update([0u8; 22]);
let hash = <TreeHashType>::from(hasher.finalize_fixed());
OutgoingViewingSecretKey::from_repr(hash.into()).unwrap()
<TreeHashType>::from(hasher.finalize_fixed())
}
pub fn produce_private_key_holder(&self) -> PrivateKeyHolder {

View File

@ -13,6 +13,8 @@ pub use shared_key_derivation::{EphemeralPublicKey, EphemeralSecretKey, Incoming
use crate::{Commitment, account::Account};
pub type Scalar = [u8; 32];
#[derive(Serialize, Deserialize, Clone)]
pub struct SharedSecretKey([u8; 32]);

View File

@ -1,14 +1,14 @@
use serde::{Deserialize, Serialize};
use k256::{
AffinePoint, EncodedPoint, FieldBytes, ProjectivePoint, Scalar,
AffinePoint, EncodedPoint, FieldBytes, ProjectivePoint,
elliptic_curve::{
PrimeField,
sec1::{FromEncodedPoint, ToEncodedPoint},
},
};
use crate::SharedSecretKey;
use crate::{SharedSecretKey, encryption::Scalar};
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Secp256k1Point(pub(crate) Vec<u8>);
@ -16,7 +16,7 @@ pub struct Secp256k1Point(pub(crate) Vec<u8>);
impl Secp256k1Point {
pub fn from_scalar(value: Scalar) -> Secp256k1Point {
let x_bytes: FieldBytes = value.into();
let x = Scalar::from_repr(x_bytes).unwrap();
let x = k256::Scalar::from_repr(x_bytes).unwrap();
let p = ProjectivePoint::GENERATOR * x;
let q = AffinePoint::from(p);
@ -37,6 +37,7 @@ impl From<&EphemeralSecretKey> for EphemeralPublicKey {
impl SharedSecretKey {
pub fn new(scalar: &Scalar, point: &Secp256k1Point) -> Self {
let scalar = k256::Scalar::from_repr((*scalar).into()).unwrap();
let point: [u8; 33] = point.0.clone().try_into().unwrap();
let encoded = EncodedPoint::from_bytes(point).unwrap();

View File

@ -88,7 +88,6 @@ impl Proof {
#[cfg(test)]
mod tests {
use k256::{Scalar, elliptic_curve::PrimeField};
use nssa_core::{
Commitment, EncryptionScheme, Nullifier,
account::{Account, AccountWithMetadata},
@ -140,7 +139,7 @@ mod tests {
let expected_sender_pre = sender.clone();
let recipient_keys = test_private_account_keys_1();
let esk = Scalar::from_repr([3; 32].into()).unwrap();
let esk = [3; 32];
let shared_secret = SharedSecretKey::new(&esk, &recipient_keys.ivk());
let (output, proof) = execute_and_prove(
@ -221,10 +220,10 @@ mod tests {
Commitment::new(&recipient_keys.npk(), &expected_private_account_2),
];
let esk_1 = Scalar::from_repr([3; 32].into()).unwrap();
let esk_1 = [3; 32].into();
let shared_secret_1 = SharedSecretKey::new(&esk_1, &sender_keys.ivk());
let esk_2 = Scalar::from_repr([5; 32].into()).unwrap();
let esk_2 = [5; 32];
let shared_secret_2 = SharedSecretKey::new(&esk_2, &recipient_keys.ivk());
let (output, proof) = execute_and_prove(

View File

@ -90,7 +90,6 @@ impl Message {
#[cfg(test)]
pub mod tests {
use k256::{Scalar, elliptic_curve::PrimeField};
use std::io::Cursor;
use nssa_core::{
@ -152,10 +151,10 @@ pub mod tests {
#[test]
fn test_encrypted_account_data_constructor() {
let npk = NullifierPublicKey::from(&[1; 32]);
let ivk = IncomingViewingPublicKey::from(&Scalar::from_repr([2; 32].into()).unwrap());
let ivk = IncomingViewingPublicKey::from_scalar([2; 32]);
let account = Account::default();
let commitment = Commitment::new(&npk, &account);
let esk = Scalar::from_repr([3; 32].into()).unwrap();
let esk = [3; 32];
let shared_secret = SharedSecretKey::new(&esk, &ivk);
let epk = EphemeralPublicKey::from_scalar(esk);
let ciphertext = EncryptionScheme::encrypt(&account, &shared_secret, &commitment, 2);

View File

@ -227,11 +227,10 @@ pub mod tests {
signature::PrivateKey,
};
use k256::{Scalar, elliptic_curve::PrimeField};
use nssa_core::{
Commitment, Nullifier, NullifierPublicKey, NullifierSecretKey, SharedSecretKey,
account::{Account, AccountWithMetadata, Nonce},
encryption::{EphemeralPublicKey, IncomingViewingPublicKey},
encryption::{EphemeralPublicKey, IncomingViewingPublicKey, Scalar},
};
fn transfer_transaction(
@ -760,14 +759,14 @@ pub mod tests {
pub fn test_private_account_keys_1() -> TestPrivateKeys {
TestPrivateKeys {
nsk: [13; 32],
isk: Scalar::from_repr([31; 32].into()).unwrap(),
isk: [31; 32],
}
}
pub fn test_private_account_keys_2() -> TestPrivateKeys {
TestPrivateKeys {
nsk: [38; 32],
isk: Scalar::from_repr([83; 32].into()).unwrap(),
isk: [83; 32],
}
}
@ -789,7 +788,7 @@ pub mod tests {
is_authorized: false,
};
let esk = Scalar::from_repr([3; 32].into()).unwrap();
let esk = [3; 32];
let shared_secret = SharedSecretKey::new(&esk, &recipient_keys.ivk());
let epk = EphemeralPublicKey::from_scalar(esk);
@ -835,11 +834,11 @@ pub mod tests {
is_authorized: false,
};
let esk_1 = Scalar::from_repr([3; 32].into()).unwrap();
let esk_1 = [3; 32];
let shared_secret_1 = SharedSecretKey::new(&esk_1, &sender_keys.ivk());
let epk_1 = EphemeralPublicKey::from_scalar(esk_1);
let esk_2 = Scalar::from_repr([3; 32].into()).unwrap();
let esk_2 = [3; 32];
let shared_secret_2 = SharedSecretKey::new(&esk_2, &recipient_keys.ivk());
let epk_2 = EphemeralPublicKey::from_scalar(esk_2);
@ -895,7 +894,7 @@ pub mod tests {
is_authorized: false,
};
let esk = Scalar::from_repr([3; 32].into()).unwrap();
let esk = [3; 32];
let shared_secret = SharedSecretKey::new(&esk, &sender_keys.ivk());
let epk = EphemeralPublicKey::from_scalar(esk);