Skip to content

Commit

Permalink
implementing BLAKE3 opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjian committed Mar 19, 2024
1 parent 7694aea commit 8bbdb43
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ paste = "1"
strict_encoding = { version = "2.7.0-beta.1", default-features = false, features = ["float", "derive"] }
strict_types = { version = "2.7.0-beta.2", optional = true }
sha2 = "0.10.8"
blake3 = "1.5.1"
ripemd = "0.1.3"
baid58 = "0.4.4"
secp256k1 = { version = "0.28.2", optional = true, features = ["global-context"] }
Expand Down
3 changes: 3 additions & 0 deletions src/isa/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,7 @@ impl Bytecode for DigestOp {
DigestOp::Ripemd(_, _) => INSTR_RIPEMD,
DigestOp::Sha256(_, _) => INSTR_SHA256,
DigestOp::Sha512(_, _) => INSTR_SHA512,
DigestOp::Blake3(_, _) => INSTR_BLAKE3,

Check warning on line 1302 in src/isa/bytecode.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/bytecode.rs#L1302

Added line #L1302 was not covered by tests
}
}

Expand All @@ -1309,6 +1310,7 @@ impl Bytecode for DigestOp {
match self {
DigestOp::Ripemd(src, dst)
| DigestOp::Sha256(src, dst)
| DigestOp::Blake3(src, dst)

Check warning on line 1313 in src/isa/bytecode.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/bytecode.rs#L1313

Added line #L1313 was not covered by tests
| DigestOp::Sha512(src, dst) => {
writer.write_u4(src)?;
writer.write_u4(dst)?;
Expand All @@ -1328,6 +1330,7 @@ impl Bytecode for DigestOp {
Ok(match instr {
INSTR_RIPEMD => Self::Ripemd(src, dst),
INSTR_SHA256 => Self::Sha256(src, dst),
INSTR_BLAKE3 => Self::Blake3(src, dst),

Check warning on line 1333 in src/isa/bytecode.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/bytecode.rs#L1333

Added line #L1333 was not covered by tests
INSTR_SHA512 => Self::Sha512(src, dst),
x => unreachable!("instruction {:#010b} classified as digest operation", x),
})
Expand Down
8 changes: 8 additions & 0 deletions src/isa/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ impl InstructionSet for DigestOp {
match self {
DigestOp::Ripemd(src, _dst)
| DigestOp::Sha256(src, _dst)
| DigestOp::Blake3(src, _dst)

Check warning on line 1375 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L1375

Added line #L1375 was not covered by tests
| DigestOp::Sha512(src, _dst) => bset![Reg::S(*src)],
}
}
Expand All @@ -1380,6 +1381,7 @@ impl InstructionSet for DigestOp {
match self {
DigestOp::Ripemd(_src, dst) => bset![Reg::new(RegR::R160, *dst)],
DigestOp::Sha256(_src, dst) => bset![Reg::new(RegR::R256, *dst)],
DigestOp::Blake3(_src, dst) => bset![Reg::new(RegR::R256, *dst)],

Check warning on line 1384 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L1384

Added line #L1384 was not covered by tests
DigestOp::Sha512(_src, dst) => bset![Reg::new(RegR::R512, *dst)],
}
}
Expand Down Expand Up @@ -1407,6 +1409,12 @@ impl InstructionSet for DigestOp {
let hash: Option<[u8; 32]> = s.map(|s| sha2::Sha256::digest(s.as_ref()).into());
regs.set_n(RegR::R256, dst, hash);
}
DigestOp::Blake3(src, dst) => {
let s = regs.get_s(*src);
none = s.is_none();
let hash: Option<[u8; 32]> = s.map(|s| blake3::hash(s.as_ref()).into());
regs.set_n(RegR::R256, dst, hash);
}

Check warning on line 1417 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L1412-L1417

Added lines #L1412 - L1417 were not covered by tests
DigestOp::Sha512(src, dst) => {
let s = regs.get_s(*src);
none = s.is_none();
Expand Down
10 changes: 10 additions & 0 deletions src/isa/instr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,16 @@ pub enum DigestOp {
/** Index of string register */ RegS,
/** Index of `r512` register to save result to */ Reg16,
),

/// Computes BLAKE3 hash value
///
/// Sets `st0` to `false` and destination register to `None` if the source register does not
/// contain a value
#[display("Blake3 {0},r256{1}")]
Blake3(
/** Index of string register */ RegS,
/** Index of `r256` register to save result to */ Reg16,
),
}

/// Operations on Secp256k1 elliptic curve
Expand Down
3 changes: 3 additions & 0 deletions src/isa/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,9 @@ macro_rules! instr {
(sha2 s16[$idx1:literal],r256[$idx2:literal]) => {
Instr::Digest(DigestOp::Sha256(RegS::from($idx1), $crate::_reg_idx16!($idx2)))
};
(blake3 s16[$idx1:literal],r256[$idx2:literal]) => {
Instr::Digest(DigestOp::Blake3(RegS::from($idx1), $crate::_reg_idx16!($idx2)))
};
(sha2 s16[$idx1:literal],r512[$idx2:literal]) => {
Instr::Digest(DigestOp::Sha512(RegS::from($idx1), $crate::_reg_idx16!($idx2)))
};
Expand Down
1 change: 1 addition & 0 deletions src/isa/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub const INSTR_RESV_TO: u8 = 0b01_111_111;
pub const INSTR_RIPEMD: u8 = 0b10_000_000;
pub const INSTR_SHA256: u8 = 0b10_000_001;
pub const INSTR_SHA512: u8 = 0b10_000_010;
pub const INSTR_BLAKE3: u8 = 0b10_000_100;

// ### Secp256k1 operations (SECP256K1)

Expand Down

0 comments on commit 8bbdb43

Please sign in to comment.