Skip to content

Commit

Permalink
fix the tests and the fmt/clippy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vivek-arte committed Nov 13, 2023
1 parent 6905340 commit 9ec7524
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/bundle/burn_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ mod tests {
pub fn get_burn_tuple(asset_desc: &str, value: i64) -> (AssetBase, i64) {
use crate::keys::{IssuanceAuthorizingKey, IssuanceValidatingKey};

let isk = IssuanceAuthorizingKey::from_bytes([0u8; 32]).unwrap();
let isk = IssuanceAuthorizingKey::from_bytes([1u8; 32]).unwrap();

(
AssetBase::derive(&IssuanceValidatingKey::from(&isk), asset_desc),
Expand Down
38 changes: 16 additions & 22 deletions src/issuance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use blake2b_simd::Hash as Blake2bHash;
use group::Group;
use k256::schnorr;
use k256::schnorr::CryptoRngCore;
use nonempty::NonEmpty;
use rand::RngCore;
use std::collections::HashSet;
Expand Down Expand Up @@ -407,10 +406,7 @@ impl IssueBundle<Unauthorized> {
impl IssueBundle<Prepared> {
/// Sign the `IssueBundle`.
/// The call makes sure that the provided `isk` matches the `ik` and the driven `asset` for each note in the bundle.
pub fn sign<R: CryptoRngCore>(
self,
isk: &IssuanceAuthorizingKey,
) -> Result<IssueBundle<Signed>, Error> {
pub fn sign(self, isk: &IssuanceAuthorizingKey) -> Result<IssueBundle<Signed>, Error> {
let expected_ik: IssuanceValidatingKey = (isk).into();

// Make sure the `expected_ik` matches the `asset` for all notes.
Expand Down Expand Up @@ -540,7 +536,7 @@ pub enum Error {

/// Verification errors:
/// Invalid signature.
IssueBundleInvalidSignature(k256::ecdsa::Error),
IssueBundleInvalidSignature(schnorr::Error),
/// The provided `AssetBase` has been previously finalized.
IssueActionPreviouslyFinalizedAssetBase(AssetBase),

Expand All @@ -559,7 +555,10 @@ impl PartialEq for Error {
(IssueActionWithoutNoteNotFinalized, IssueActionWithoutNoteNotFinalized) => true,
(AssetBaseCannotBeIdentityPoint, AssetBaseCannotBeIdentityPoint) => true,
(IssueBundleInvalidSignature(_), IssueBundleInvalidSignature(_)) => true, //might want to do better here?
(IssueActionPreviouslyFinalizedAssetBase(x), IssueActionPreviouslyFinalizedAssetBase(y)) => x == y,
(
IssueActionPreviouslyFinalizedAssetBase(x),
IssueActionPreviouslyFinalizedAssetBase(y),
) => x == y,
(ValueSumOverflow, ValueSumOverflow) => true,
_ => false,
}
Expand Down Expand Up @@ -627,11 +626,11 @@ mod tests {
use crate::value::{NoteValue, ValueSum};
use crate::{Address, Note};
use group::{Group, GroupEncoding};
use k256::schnorr;
use nonempty::NonEmpty;
use pasta_curves::pallas::{Point, Scalar};
use rand::rngs::OsRng;
use rand::RngCore;
use reddsa::Error::InvalidSignature;
use std::collections::HashSet;

fn setup_params() -> (
Expand Down Expand Up @@ -699,12 +698,7 @@ mod tests {
fn identity_point_test_params(
note1_value: u64,
note2_value: u64,
) -> (
OsRng,
IssuanceAuthorizingKey,
IssueBundle<Unauthorized>,
[u8; 32],
) {
) -> (IssuanceAuthorizingKey, IssueBundle<Unauthorized>, [u8; 32]) {
let (mut rng, isk, ik, recipient, sighash) = setup_params();

let note1 = Note::new(
Expand All @@ -728,7 +722,7 @@ mod tests {

let bundle = IssueBundle::from_parts(ik, NonEmpty::new(action), Unauthorized);

(rng, isk, bundle, sighash)
(isk, bundle, sighash)
}

#[test]
Expand All @@ -749,7 +743,7 @@ mod tests {

#[test]
fn verify_supply_invalid_for_asset_base_as_identity() {
let (_, _, bundle, _) = identity_point_test_params(10, 20);
let (_, bundle, _) = identity_point_test_params(10, 20);

assert_eq!(
bundle.actions.head.verify_supply(&bundle.ik),
Expand Down Expand Up @@ -1201,14 +1195,14 @@ mod tests {
let mut signed = bundle.prepare(sighash).sign(&isk).unwrap();

signed.set_authorization(Signed {
signature: wrong_isk.sign(&mut rng, &sighash),
signature: wrong_isk.sign(&sighash),
});

let prev_finalized = &HashSet::new();

assert_eq!(
verify_issue_bundle(&signed, sighash, prev_finalized).unwrap_err(),
IssueBundleInvalidSignature(InvalidSignature)
IssueBundleInvalidSignature(schnorr::Error::new())
);
}

Expand All @@ -1232,7 +1226,7 @@ mod tests {

assert_eq!(
verify_issue_bundle(&signed, random_sighash, prev_finalized).unwrap_err(),
IssueBundleInvalidSignature(InvalidSignature)
IssueBundleInvalidSignature(schnorr::Error::new())
);
}

Expand Down Expand Up @@ -1360,7 +1354,7 @@ mod tests {

#[test]
fn issue_bundle_cannot_be_signed_with_asset_base_identity_point() {
let (rng, isk, bundle, sighash) = identity_point_test_params(10, 20);
let (isk, bundle, sighash) = identity_point_test_params(10, 20);

assert_eq!(
bundle.prepare(sighash).sign(&isk).unwrap_err(),
Expand All @@ -1370,13 +1364,13 @@ mod tests {

#[test]
fn issue_bundle_verify_fail_asset_base_identity_point() {
let (mut rng, isk, bundle, sighash) = identity_point_test_params(10, 20);
let (isk, bundle, sighash) = identity_point_test_params(10, 20);

let signed = IssueBundle {
ik: bundle.ik,
actions: bundle.actions,
authorization: Signed {
signature: isk.sign(&mut rng, &sighash),
signature: isk.sign(&sighash),
},
};

Expand Down
4 changes: 2 additions & 2 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pub struct IssuanceValidatingKey(schnorr::VerifyingKey);
impl From<&IssuanceAuthorizingKey> for IssuanceValidatingKey {
fn from(isk: &IssuanceAuthorizingKey) -> Self {
let schnorr_sk = schnorr::SigningKey::from_bytes(&isk.0).unwrap();
IssuanceValidatingKey(schnorr_sk.verifying_key().clone())
IssuanceValidatingKey(*schnorr_sk.verifying_key())
}
}

Expand Down Expand Up @@ -1244,7 +1244,7 @@ mod tests {
assert_eq!(<[u8; 32]>::from(ak.0), tv.ak);

let ik: IssuanceValidatingKey = (&isk).into();
assert_eq!(<[u8; 32]>::from(ik.0), tv.ik);
assert_eq!(ik.to_bytes(), tv.ik);

let nk: NullifierDerivingKey = (&sk).into();
assert_eq!(nk.0.to_repr(), tv.nk);
Expand Down
6 changes: 4 additions & 2 deletions src/note/asset_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ pub mod testing {
for tv in test_vectors {
let description = std::str::from_utf8(&tv.description).unwrap();

let calculated_asset_base =
AssetBase::derive(&IssuanceValidatingKey::from_bytes().unwrap(), description);
let calculated_asset_base = AssetBase::derive(
&IssuanceValidatingKey::from_bytes(&tv.key).unwrap(),
description,
);
let test_vector_asset_base = AssetBase::from_bytes(&tv.asset_base).unwrap();

assert_eq!(calculated_asset_base, test_vector_asset_base);
Expand Down
2 changes: 1 addition & 1 deletion src/supply_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod tests {
fn create_test_asset(asset_desc: &str) -> AssetBase {
use crate::keys::{IssuanceAuthorizingKey, IssuanceValidatingKey};

let isk = IssuanceAuthorizingKey::from_bytes([0u8; 32]).unwrap();
let isk = IssuanceAuthorizingKey::from_bytes([1u8; 32]).unwrap();

AssetBase::derive(&IssuanceValidatingKey::from(&isk), asset_desc)
}
Expand Down
3 changes: 1 addition & 2 deletions tests/zsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ fn prepare_keys() -> Keychain {

fn sign_issue_bundle(
unauthorized: IssueBundle<Unauthorized>,
rng: OsRng,
isk: &IssuanceAuthorizingKey,
) -> IssueBundle<Signed> {
let sighash = unauthorized.commitment().into();
Expand Down Expand Up @@ -161,7 +160,7 @@ fn issue_zsa_notes(asset_descr: &str, keys: &Keychain) -> (Note, Note) {
)
.is_ok());

let issue_bundle = sign_issue_bundle(unauthorized, rng, keys.isk());
let issue_bundle = sign_issue_bundle(unauthorized, keys.isk());

// Take notes from first action
let notes = issue_bundle.get_all_notes();
Expand Down

0 comments on commit 9ec7524

Please sign in to comment.