Skip to content

Commit

Permalink
Merge pull request #58 from SiaFoundation/nate/v2-transactions
Browse files Browse the repository at this point in the history
Add V2 Types
  • Loading branch information
n8maninger authored Nov 9, 2024
2 parents e15237f + 073591a commit fc39e05
Show file tree
Hide file tree
Showing 6 changed files with 827 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
newline_style="Unix"
reorder_imports=true
# only supported in nightly
imports_granularity="Module"
imports_granularity="Module"
24 changes: 13 additions & 11 deletions sia/src/merkle.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use blake2b_simd::Params;

use crate::Hash256;

pub const LEAF_HASH_PREFIX: &[u8; 1] = &[0];
pub const NODE_HASH_PREFIX: &[u8; 1] = &[1];

// A generic Merkle tree accumulator.
pub struct Accumulator {
trees: [[u8; 32]; 64],
trees: [Hash256; 64],
num_leaves: u64,
params: Params,
}
Expand All @@ -15,7 +17,7 @@ impl Accumulator {
let mut params = Params::new();
params.hash_length(32);
Self {
trees: [[0; 32]; 64],
trees: [Hash256::default(); 64],
num_leaves: 0,
params,
}
Expand All @@ -25,7 +27,7 @@ impl Accumulator {
self.num_leaves & (1 << height) != 0
}

pub fn add_leaf(&mut self, h: &[u8; 32]) {
pub fn add_leaf(&mut self, h: &Hash256) {
let mut i = 0;
let mut node = *h;
while self.has_tree_at_height(i) {
Expand All @@ -36,10 +38,10 @@ impl Accumulator {
self.num_leaves += 1;
}

pub fn root(&self) -> [u8; 32] {
pub fn root(&self) -> Hash256 {
let mut i = self.num_leaves.trailing_zeros() as usize;
if i == 64 {
return [0; 32];
return Hash256::default();
}
let mut root = self.trees[i];
i += 1;
Expand All @@ -54,23 +56,23 @@ impl Accumulator {
}

#[allow(dead_code)]
pub fn sum_leaf(params: &Params, leaf: &[u8]) -> [u8; 32] {
pub fn sum_leaf(params: &Params, leaf: &[u8]) -> Hash256 {
let h = params
.to_state()
.update(LEAF_HASH_PREFIX)
.update(leaf)
.finalize();

h.as_bytes().try_into().unwrap()
h.into()
}

pub fn sum_node(params: &Params, left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] {
pub fn sum_node(params: &Params, left: &Hash256, right: &Hash256) -> Hash256 {
let h = params
.to_state()
.update(NODE_HASH_PREFIX)
.update(left)
.update(right)
.update(left.as_ref())
.update(right.as_ref())
.finalize();

h.as_bytes().try_into().unwrap()
h.into()
}
16 changes: 8 additions & 8 deletions sia/src/rhp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn sector_root(sector: &[u8]) -> Hash256 {
let mut params = Params::new();
params.hash_length(32);

let mut tree_hashes = vec![[0; 32]; SECTOR_SIZE / SEGMENT_SIZE];
let mut tree_hashes = vec![Hash256::default(); SECTOR_SIZE / SEGMENT_SIZE];
tree_hashes
.par_chunks_exact_mut(4)
.enumerate()
Expand All @@ -37,7 +37,7 @@ pub fn sector_root(sector: &[u8]) -> Hash256 {

// collect results
for j in 0..4 {
chunk[j] = jobs[j].to_hash().as_bytes().try_into().unwrap();
chunk[j] = jobs[j].to_hash().into();
}
});

Expand All @@ -49,8 +49,8 @@ pub fn sector_root(sector: &[u8]) -> Hash256 {
for (j, input) in inputs.iter_mut().enumerate() {
input[0] = NODE_HASH_PREFIX[0];
let step = j * chunk_size / 2;
input[1..33].copy_from_slice(&nodes[step]);
input[33..65].copy_from_slice(&nodes[step + chunk_size / 4]);
input[1..33].copy_from_slice(nodes[step].as_ref());
input[33..65].copy_from_slice(nodes[step + chunk_size / 4].as_ref());
}

// hash them
Expand All @@ -61,17 +61,17 @@ pub fn sector_root(sector: &[u8]) -> Hash256 {
hash_many(&mut jobs);

// collect results
nodes[0] = jobs[0].to_hash().as_bytes().try_into().unwrap();
nodes[nodes.len() / 2] = jobs[1].to_hash().as_bytes().try_into().unwrap();
nodes[0] = jobs[0].to_hash().into();
nodes[nodes.len() / 2] = jobs[1].to_hash().into();
});
chunk_size *= 2;
}
// hash last two nodes into roots
Hash256::from(sum_node(
sum_node(
&params,
&tree_hashes[0],
&tree_hashes[tree_hashes.len() / 2],
))
)
}

#[cfg(test)]
Expand Down
21 changes: 15 additions & 6 deletions sia/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ impl PublicKey {
PublicKey(buf)
}

pub fn verify(&self, msg: &[u8], signature: &Signature) -> bool {
pub fn verify(&self, sig_hash: &Hash256, signature: &Signature) -> bool {
let pk = VerifyingKey::from_bytes(&self.0).unwrap();
pk.verify(msg, &ED25519Signature::from_bytes(signature.as_ref()))
.is_ok()
pk.verify(
sig_hash.as_ref(),
&ED25519Signature::from_bytes(signature.as_ref()),
)
.is_ok()
}
}

Expand All @@ -80,9 +83,9 @@ impl PrivateKey {
PublicKey::new(buf)
}

pub fn sign(&self, buf: &[u8]) -> Signature {
pub fn sign_hash(&self, h: &Hash256) -> Signature {
let sk = SigningKey::from_bytes(&self.0[..32].try_into().unwrap());
Signature::new(sk.sign(buf).to_bytes())
Signature::new(sk.sign(h.as_ref()).to_bytes())
}

pub fn as_bytes(&self) -> &[u8] {
Expand Down Expand Up @@ -167,6 +170,12 @@ impl AsRef<[u8; 64]> for Signature {
}
}

impl From<[u8; 64]> for Signature {
fn from(buf: [u8; 64]) -> Self {
Signature(buf)
}
}

impl fmt::Display for Signature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
Expand Down Expand Up @@ -449,7 +458,7 @@ mod tests {
};
let sig = Signature::new(signature.signature.try_into().unwrap());
assert!(
key.public_key().verify(sig_hash.as_ref(), &sig),
key.public_key().verify(&sig_hash, &sig),
"height: {}",
tc.height
);
Expand Down
Loading

0 comments on commit fc39e05

Please sign in to comment.