Skip to content

Commit

Permalink
Fix openssl imports and usage in sha2
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity authored and jack60612 committed Jul 10, 2024
1 parent cc6ce0b commit 64964f8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,19 +830,19 @@ fn test_invalid_node_ptr_type() {
let _ = node.object_type();
}

#[cfg(dbg)]
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn test_node_ptr_overflow() {
NodePtr::new(ObjectType::Bytes, NODE_PTR_IDX_MASK + 1);
NodePtr::new(ObjectType::Bytes, NODE_PTR_IDX_MASK as usize + 1);
}

#[cfg(dbg)]
#[cfg(debug_assertions)]
#[test]
#[should_panic]
fn test_invalid_small_number() {
let mut a = Allocator::new();
a.new_small_number(NODE_PTR_IDX_MASK + 1);
a.new_small_number(NODE_PTR_IDX_MASK + 1).unwrap();
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/more_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::op_utils::{
new_atom_and_cost, nilp, u32_from_u8, MALLOC_COST_PER_BYTE,
};
use crate::reduction::{Reduction, Response};
use crate::sha2::{Digest, Sha256};
use crate::sha2::Sha256;
use chia_bls::G1Element;

const ARITH_BASE_COST: Cost = 99;
Expand Down
6 changes: 3 additions & 3 deletions src/serde/bytes32.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::sha2::{Digest, Sha256};
use crate::sha2::Sha256;

pub type Bytes32 = [u8; 32];

pub fn hash_blob(blob: &[u8]) -> Bytes32 {
let mut sha256 = Sha256::new();
sha256.update(blob);
sha256.finalize().into()
sha256.finalize()
}

pub fn hash_blobs(blobs: &[&[u8]]) -> Bytes32 {
let mut sha256 = Sha256::new();
for blob in blobs.iter() {
sha256.update(blob);
}
sha256.finalize().into()
sha256.finalize()
}
2 changes: 0 additions & 2 deletions src/serde/de_tree.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::io::{Error, Read, Result, Write};

use sha2::Digest;

use crate::sha2::Sha256;

use super::parse_atom::decode_size_with_offset;
Expand Down
6 changes: 3 additions & 3 deletions src/serde/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ pub fn serialized_length_from_bytes_trusted(b: &[u8]) -> io::Result<u64> {
Ok(f.position())
}

use crate::sha2::{Digest, Sha256};
use crate::sha2::Sha256;

fn hash_atom(buf: &[u8]) -> [u8; 32] {
let mut ctx = Sha256::new();
ctx.update([1_u8]);
ctx.update(buf);
ctx.finalize().into()
ctx.finalize()
}

fn hash_pair(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] {
let mut ctx = Sha256::new();
ctx.update([2_u8]);
ctx.update(left);
ctx.update(right);
ctx.finalize().into()
ctx.finalize()
}

#[repr(u8)]
Expand Down
32 changes: 20 additions & 12 deletions src/sha2.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
#[cfg(feature = "openssl")]
use openssl;

#[cfg(not(feature = "openssl"))]
pub use sha2::{Digest, Sha256};
use sha2::Digest;

#[cfg(feature = "openssl")]
#[derive(Clone)]
#[derive(Default, Clone)]
pub struct Sha256 {
ctx: sha::Sha256,
#[cfg(feature = "openssl")]
ctx: openssl::sha::Sha256,

#[cfg(not(feature = "openssl"))]
ctx: sha2::Sha256,
}

#[cfg(feature = "openssl")]
impl Sha256 {
pub fn new() -> Sha256 {
Sha256 {
ctx: openssl::sha::Sha256::new(),
}
pub fn new() -> Self {
Self::default()
}
pub fn update(&mut self, buf: &[u8]) {
self.ctx.update(buf);
pub fn update(&mut self, buf: impl AsRef<[u8]>) {
self.ctx.update(buf.as_ref());
}
pub fn finalize(self) -> [u8; 32] {
self.ctx.finish()
#[cfg(feature = "openssl")]
{
self.ctx.finish()
}
#[cfg(not(feature = "openssl"))]
{
self.ctx.finalize().into()
}
}
}

Expand Down

0 comments on commit 64964f8

Please sign in to comment.