Skip to content

Commit

Permalink
Merge pull request #430 from Chia-Network/jn.openssl-chia_rs
Browse files Browse the repository at this point in the history
CHIA-622 Fix openssl feature activation
  • Loading branch information
jack60612 committed Jul 10, 2024
2 parents 1940beb + 64964f8 commit 4460b48
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 28 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ counters = []
# debugging and tracing of programs.
pre-eval = []

# On UNIX-based platforms, you may get a speed boost on `sha256` operations by building
# with OpenSSL.when enabled
openssl = ["dep:openssl"]

[profile.release]
lto = "thin"

Expand Down
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
37 changes: 22 additions & 15 deletions src/sha2.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
#[cfg(not(openssl))]
pub use sha2::{Digest, Sha256};

#[cfg(openssl)]
#[cfg(feature = "openssl")]
use openssl;

#[cfg(openssl)]
#[derive(Clone)]
#[cfg(not(feature = "openssl"))]
use sha2::Digest;

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

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

#[cfg(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 4460b48

Please sign in to comment.