mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-01-10 15:09:44 -05:00
Make hash_to_scalar a macro so we can pass different types
This commit is contained in:
parent
53916aab6b
commit
cf32828739
@ -45,11 +45,7 @@ pub fn sign(
|
|||||||
pseudo_output_commitment,
|
pseudo_output_commitment,
|
||||||
msg,
|
msg,
|
||||||
);
|
);
|
||||||
let h_0 = hash_to_scalar(&[
|
let h_0 = hash_to_scalar!(prefix, L_0.compress(), R_0.compress());
|
||||||
&prefix,
|
|
||||||
L_0.compress().as_bytes(),
|
|
||||||
R_0.compress().as_bytes(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
let h_last = fake_responses
|
let h_last = fake_responses
|
||||||
.iter()
|
.iter()
|
||||||
@ -61,11 +57,7 @@ pub fn sign(
|
|||||||
let L_i = compute_L(h_prev, &mus, *s_i, pk_i, adjusted_commitment_i);
|
let L_i = compute_L(h_prev, &mus, *s_i, pk_i, adjusted_commitment_i);
|
||||||
let R_i = compute_R(h_prev, &mus, pk_i, *s_i, I, D_inv_8);
|
let R_i = compute_R(h_prev, &mus, pk_i, *s_i, I, D_inv_8);
|
||||||
|
|
||||||
hash_to_scalar(&[
|
hash_to_scalar!(prefix, L_i.compress(), R_i.compress())
|
||||||
&prefix,
|
|
||||||
L_i.compress().as_bytes().as_ref(),
|
|
||||||
R_i.compress().as_bytes().as_ref(),
|
|
||||||
])
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let s_last = alpha - h_last * ((mus.mu_P * signing_key) + (mus.mu_C * z));
|
let s_last = alpha - h_last * ((mus.mu_P * signing_key) + (mus.mu_C * z));
|
||||||
@ -125,11 +117,7 @@ pub fn verify(
|
|||||||
let L_i = compute_L(h, &mus, *s_i, pk_i, adjusted_commitment_i);
|
let L_i = compute_L(h, &mus, *s_i, pk_i, adjusted_commitment_i);
|
||||||
let R_i = compute_R(h, &mus, pk_i, *s_i, sig.I, sig.D);
|
let R_i = compute_R(h, &mus, pk_i, *s_i, sig.I, sig.D);
|
||||||
|
|
||||||
h = hash_to_scalar(&[
|
h = hash_to_scalar!(prefix, L_i.compress(), R_i.compress())
|
||||||
&prefix,
|
|
||||||
L_i.compress().as_bytes().as_ref(),
|
|
||||||
R_i.compress().as_bytes().as_ref(),
|
|
||||||
])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
h == sig.h_0
|
h == sig.h_0
|
||||||
@ -219,28 +207,22 @@ impl AggregationHashes {
|
|||||||
pseudo_output_commitment: CompressedEdwardsY,
|
pseudo_output_commitment: CompressedEdwardsY,
|
||||||
D: CompressedEdwardsY,
|
D: CompressedEdwardsY,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let ring = ring.as_ref();
|
let mu_P = hash_to_scalar!(
|
||||||
let commitment_ring = commitment_ring.as_ref();
|
|
||||||
let I = I.as_bytes().as_ref();
|
|
||||||
let D = D.as_bytes().as_ref();
|
|
||||||
let pseudo_output_commitment = pseudo_output_commitment.as_bytes().as_ref();
|
|
||||||
|
|
||||||
let mu_P = hash_to_scalar(&[
|
|
||||||
b"CLSAG_agg_0",
|
b"CLSAG_agg_0",
|
||||||
ring,
|
ring,
|
||||||
commitment_ring,
|
commitment_ring,
|
||||||
I,
|
I,
|
||||||
D,
|
D,
|
||||||
pseudo_output_commitment,
|
pseudo_output_commitment
|
||||||
]);
|
);
|
||||||
let mu_C = hash_to_scalar(&[
|
let mu_C = hash_to_scalar!(
|
||||||
b"CLSAG_agg_1",
|
b"CLSAG_agg_1",
|
||||||
ring,
|
ring,
|
||||||
commitment_ring,
|
commitment_ring,
|
||||||
I,
|
I,
|
||||||
D,
|
D,
|
||||||
pseudo_output_commitment,
|
pseudo_output_commitment
|
||||||
]);
|
);
|
||||||
|
|
||||||
Self { mu_P, mu_C }
|
Self { mu_P, mu_C }
|
||||||
}
|
}
|
||||||
@ -264,19 +246,6 @@ impl From<Signature> for monero::util::ringct::Clsag {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash_to_scalar(elements: &[&[u8]]) -> Scalar {
|
|
||||||
let mut hasher = Keccak::v256();
|
|
||||||
|
|
||||||
for element in elements {
|
|
||||||
hasher.update(element);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut hash = [0u8; 32];
|
|
||||||
hasher.finalize(&mut hash);
|
|
||||||
|
|
||||||
Scalar::from_bytes_mod_order(hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -15,6 +15,8 @@ use tiny_keccak::{Hasher, Keccak};
|
|||||||
|
|
||||||
use clsag::{Signature, RING_SIZE};
|
use clsag::{Signature, RING_SIZE};
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod macros;
|
||||||
mod clsag;
|
mod clsag;
|
||||||
mod ring;
|
mod ring;
|
||||||
|
|
||||||
|
49
monero-adaptor/src/macros.rs
Normal file
49
monero-adaptor/src/macros.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
use crate::ring::Ring;
|
||||||
|
use curve25519_dalek::edwards::CompressedEdwardsY;
|
||||||
|
|
||||||
|
macro_rules! hash_to_scalar {
|
||||||
|
($($e:expr),+) => {
|
||||||
|
{
|
||||||
|
use crate::macros::AsByteSlice as _;
|
||||||
|
|
||||||
|
let mut hasher = Keccak::v256();
|
||||||
|
|
||||||
|
$(
|
||||||
|
hasher.update($e.as_byte_slice());
|
||||||
|
)+
|
||||||
|
|
||||||
|
let mut hash = [0u8; 32];
|
||||||
|
hasher.finalize(&mut hash);
|
||||||
|
|
||||||
|
Scalar::from_bytes_mod_order(hash)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) trait AsByteSlice {
|
||||||
|
fn as_byte_slice(&self) -> &[u8];
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsByteSlice for CompressedEdwardsY {
|
||||||
|
fn as_byte_slice(&self) -> &[u8] {
|
||||||
|
self.0.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsByteSlice for Vec<u8> {
|
||||||
|
fn as_byte_slice(&self) -> &[u8] {
|
||||||
|
self.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> AsByteSlice for [u8; N] {
|
||||||
|
fn as_byte_slice(&self) -> &[u8] {
|
||||||
|
self.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> AsByteSlice for Ring<'a> {
|
||||||
|
fn as_byte_slice(&self) -> &[u8] {
|
||||||
|
self.as_ref()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user