mirror of
https://github.com/logos-blockchain/logos-blockchain-specs.git
synced 2026-01-03 21:53:07 +00:00
cl: test partial transaction balance commitment
This commit is contained in:
parent
7ba69caccb
commit
2ea4a4d351
16
cl/src/balance.rs
Normal file
16
cl/src/balance.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
use jubjub::{ExtendedPoint, Scalar};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref PEDERSON_COMMITMENT_BLINDING_POINT: ExtendedPoint =
|
||||||
|
crate::crypto::hash_to_curve(b"NOMOS_CL_PEDERSON_COMMITMENT_BLINDING");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unit_point(unit: &str) -> ExtendedPoint {
|
||||||
|
crate::crypto::hash_to_curve(unit.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn balance(value: u64, unit: &str, blinding: Scalar) -> ExtendedPoint {
|
||||||
|
let value_scalar = Scalar::from(value);
|
||||||
|
unit_point(unit) * value_scalar + *PEDERSON_COMMITMENT_BLINDING_POINT * blinding
|
||||||
|
}
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
pub mod balance;
|
||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod input;
|
pub mod input;
|
||||||
|
|||||||
@ -1,17 +1,8 @@
|
|||||||
use blake2::{Blake2s256, Digest};
|
use blake2::{Blake2s256, Digest};
|
||||||
use group::GroupEncoding;
|
use group::GroupEncoding;
|
||||||
use jubjub::{ExtendedPoint, Scalar};
|
use jubjub::{ExtendedPoint, Scalar};
|
||||||
use lazy_static::lazy_static;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::nullifier::{NullifierCommitment, NullifierNonce};
|
||||||
crypto,
|
|
||||||
nullifier::{NullifierCommitment, NullifierNonce},
|
|
||||||
};
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref PEDERSON_COMMITMENT_BLINDING_POINT: ExtendedPoint =
|
|
||||||
crypto::hash_to_curve(b"NOMOS_CL_PEDERSON_COMMITMENT_BLINDING");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct NoteCommitment([u8; 32]);
|
pub struct NoteCommitment([u8; 32]);
|
||||||
@ -37,12 +28,11 @@ impl Note {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn unit_point(&self) -> ExtendedPoint {
|
pub fn unit_point(&self) -> ExtendedPoint {
|
||||||
crypto::hash_to_curve(self.unit.as_bytes())
|
crate::balance::unit_point(&self.unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn balance(&self, blinding: Scalar) -> ExtendedPoint {
|
pub fn balance(&self, blinding: Scalar) -> ExtendedPoint {
|
||||||
let value_scalar = Scalar::from(self.value);
|
crate::balance::balance(self.value, &self.unit, blinding)
|
||||||
self.unit_point() * value_scalar + *PEDERSON_COMMITMENT_BLINDING_POINT * blinding
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn commit(&self, nf_pk: NullifierCommitment, nonce: NullifierNonce) -> NoteCommitment {
|
pub fn commit(&self, nf_pk: NullifierCommitment, nonce: NullifierNonce) -> NoteCommitment {
|
||||||
|
|||||||
@ -142,4 +142,31 @@ mod test {
|
|||||||
|
|
||||||
assert!(ptx.verify(&ptx_proof));
|
assert!(ptx.verify(&ptx_proof));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_partial_tx_balance() {
|
||||||
|
let mut rng = seed_rng(0);
|
||||||
|
|
||||||
|
let nmo_10 = InputWitness::random(Note::new(10, "NMO"), &mut rng);
|
||||||
|
let eth_23 = InputWitness::random(Note::new(23, "ETH"), &mut rng);
|
||||||
|
let crv_4840 = OutputWitness::random(
|
||||||
|
Note::new(4840, "CRV"),
|
||||||
|
NullifierSecret::random(&mut rng).commit(), // transferring to a random owner
|
||||||
|
&mut rng,
|
||||||
|
);
|
||||||
|
|
||||||
|
let ptx_witness = PartialTxWitness {
|
||||||
|
inputs: vec![nmo_10.clone(), eth_23.clone()],
|
||||||
|
outputs: vec![crv_4840.clone()],
|
||||||
|
};
|
||||||
|
|
||||||
|
let ptx = PartialTx::from_witness(ptx_witness.clone());
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
ptx.balance(),
|
||||||
|
crate::balance::balance(10, "NMO", nmo_10.balance_blinding)
|
||||||
|
+ crate::balance::balance(23, "ETH", eth_23.balance_blinding)
|
||||||
|
- crate::balance::balance(4840, "CRV", crv_4840.balance_blinding)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user