cl: test partial transaction balance commitment
This commit is contained in:
parent
7ba69caccb
commit
2ea4a4d351
|
@ -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 error;
|
||||
pub mod input;
|
||||
|
|
|
@ -1,17 +1,8 @@
|
|||
use blake2::{Blake2s256, Digest};
|
||||
use group::GroupEncoding;
|
||||
use jubjub::{ExtendedPoint, Scalar};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use crate::{
|
||||
crypto,
|
||||
nullifier::{NullifierCommitment, NullifierNonce},
|
||||
};
|
||||
|
||||
lazy_static! {
|
||||
static ref PEDERSON_COMMITMENT_BLINDING_POINT: ExtendedPoint =
|
||||
crypto::hash_to_curve(b"NOMOS_CL_PEDERSON_COMMITMENT_BLINDING");
|
||||
}
|
||||
use crate::nullifier::{NullifierCommitment, NullifierNonce};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct NoteCommitment([u8; 32]);
|
||||
|
@ -37,12 +28,11 @@ impl Note {
|
|||
}
|
||||
|
||||
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 {
|
||||
let value_scalar = Scalar::from(self.value);
|
||||
self.unit_point() * value_scalar + *PEDERSON_COMMITMENT_BLINDING_POINT * blinding
|
||||
crate::balance::balance(self.value, &self.unit, blinding)
|
||||
}
|
||||
|
||||
pub fn commit(&self, nf_pk: NullifierCommitment, nonce: NullifierNonce) -> NoteCommitment {
|
||||
|
|
|
@ -142,4 +142,31 @@ mod test {
|
|||
|
||||
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…
Reference in New Issue