cl: pre-compute balance unit point outside stark

This commit is contained in:
David Rusu 2024-06-28 00:54:21 +00:00
parent a819123bc3
commit 69795b8296
6 changed files with 20 additions and 23 deletions

View File

@ -20,7 +20,7 @@ pub struct Balance(pub AffinePoint);
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct BalanceWitness {
pub value: u64,
pub unit: String,
pub unit: AffinePoint,
pub blinding: Scalar,
}
@ -34,7 +34,7 @@ impl BalanceWitness {
pub fn new(value: u64, unit: impl Into<String>, blinding: Scalar) -> Self {
Self {
value,
unit: unit.into(),
unit: unit_point(&unit.into()).into(),
blinding,
}
}
@ -44,11 +44,7 @@ impl BalanceWitness {
}
pub fn commit(&self) -> Balance {
Balance(balance(self.value, &self.unit, self.blinding).into())
}
pub fn unit_point(&self) -> ProjectivePoint {
unit_point(&self.unit)
Balance(balance(self.value, self.unit.into(), self.blinding).into())
}
}
@ -56,9 +52,9 @@ pub fn unit_point(unit: &str) -> ProjectivePoint {
crate::crypto::hash_to_curve(unit.as_bytes())
}
pub fn balance(value: u64, unit: &str, blinding: Scalar) -> ProjectivePoint {
pub fn balance(value: u64, unit: ProjectivePoint, blinding: Scalar) -> ProjectivePoint {
let value_scalar = Scalar::from(value);
unit_point(unit) * value_scalar + *PEDERSON_COMMITMENT_BLINDING_POINT * blinding
unit * value_scalar + *PEDERSON_COMMITMENT_BLINDING_POINT * blinding
}
// mod serde_scalar {

View File

@ -36,7 +36,7 @@ impl Bundle {
}
pub fn is_balanced(&self, balance_blinding_witness: Scalar) -> bool {
self.balance() == crate::balance::balance(0, "", balance_blinding_witness)
self.balance() == crate::balance::balance(0, ProjectivePoint::GENERATOR, balance_blinding_witness)
}
pub fn prove(
@ -65,7 +65,7 @@ impl Bundle {
return Err(Error::ProofFailed);
}
if self.balance() != crate::balance::balance(0, "", w.balance_blinding) {
if self.balance() != crate::balance::balance(0, ProjectivePoint::GENERATOR, w.balance_blinding) {
return Err(Error::ProofFailed);
}
@ -91,6 +91,7 @@ mod test {
use crate::{
input::InputWitness, note::NoteWitness, nullifier::NullifierSecret, output::OutputWitness,
partial_tx::PartialTxWitness, test_util::seed_rng,
crypto::hash_to_curve,
};
use super::*;
@ -127,9 +128,9 @@ mod test {
assert!(!bundle.is_balanced(bundle_witness.balance_blinding));
assert_eq!(
bundle.balance(),
crate::balance::balance(4840, "CRV", crv_4840_out.note.balance.blinding)
- (crate::balance::balance(10, "NMO", nmo_10_in.note.balance.blinding)
+ crate::balance::balance(23, "ETH", eth_23_in.note.balance.blinding))
crate::balance::balance(4840, hash_to_curve(b"CRV"), crv_4840_out.note.balance.blinding)
- (crate::balance::balance(10, hash_to_curve(b"NMO"), nmo_10_in.note.balance.blinding)
+ crate::balance::balance(23, hash_to_curve(b"ETH"), eth_23_in.note.balance.blinding))
);
let crv_4840_in =
@ -162,7 +163,7 @@ mod test {
assert_eq!(
bundle.balance(),
crate::balance::balance(0, "", witness.balance_blinding)
crate::balance::balance(0, ProjectivePoint::GENERATOR, witness.balance_blinding)
);
assert!(bundle.is_balanced(witness.balance_blinding));

View File

@ -10,7 +10,7 @@ use crate::{
partial_tx::PtxRoot,
};
use rand_core::RngCore;
use risc0_groth16::{PublicInputsJson, Verifier};
// use risc0_groth16::{PublicInputsJson, Verifier};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]

View File

@ -41,7 +41,7 @@ impl NoteWitness {
// COMMIT TO BALANCE
hasher.update(self.balance.value.to_le_bytes());
hasher.update(self.balance.unit_point().to_bytes());
hasher.update(self.balance.unit.to_bytes());
// Important! we don't commit to the balance blinding factor as that may make the notes linkable.
// COMMIT TO STATE

View File

@ -1,7 +1,7 @@
use std::collections::BTreeSet;
use rand_core::RngCore;
use risc0_groth16::ProofJson;
// use risc0_groth16::ProofJson;
use serde::{Deserialize, Serialize};
use k256::ProjectivePoint;
use k256::elliptic_curve::group::prime::PrimeCurveAffine;
@ -169,7 +169,7 @@ impl PartialTx {
#[cfg(test)]
mod test {
use crate::{note::NoteWitness, nullifier::NullifierSecret, test_util::seed_rng};
use crate::{note::NoteWitness, nullifier::NullifierSecret, test_util::seed_rng, crypto::hash_to_curve};
use super::*;
@ -222,9 +222,9 @@ mod test {
assert_eq!(
ptx.balance(),
crate::balance::balance(4840, "CRV", crv_4840.note.balance.blinding)
- (crate::balance::balance(10, "NMO", nmo_10.note.balance.blinding)
+ crate::balance::balance(23, "ETH", eth_23.note.balance.blinding))
crate::balance::balance(4840, hash_to_curve(b"CRV"), crv_4840.note.balance.blinding)
- (crate::balance::balance(10, hash_to_curve(b"NMO"), nmo_10.note.balance.blinding)
+ crate::balance::balance(23, hash_to_curve(b"ETH"), eth_23.note.balance.blinding))
);
}
}

View File

@ -13,6 +13,6 @@ lto = true
[patch.crates-io]
# Placing these patch statement in the workspace Cargo.toml will add RISC Zero SHA-256 and bigint
# multiplication accelerator support for all downstream usages of the following crates.
# sha2 = { git = "https://github.com/risc0/RustCrypto-hashes", tag = "sha2-v0.10.6-risczero.0" }
sha2 = { git = "https://github.com/risc0/RustCrypto-hashes", tag = "sha2-v0.10.8-risczero.0" }
k256 = { git = "https://github.com/risc0/RustCrypto-elliptic-curves", tag = "k256/v0.13.3-risczero.0" }
crypto-bigint = { git = "https://github.com/risc0/RustCrypto-crypto-bigint", tag = "v0.5.2-risczero.0" }