This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Exercise the SDK's host-only BM1366 HAL simulator with a captured job.
|
||||
|
||||
use alkane_os::bm1366::{decode_nonce_report, encode_work, REPORT_BYTES};
|
||||
use alkane_os::hal_wire::HalClient;
|
||||
use alkane_os::pow::verify_sha256_report;
|
||||
use alkane_os::HardwareBackend;
|
||||
@@ -14,43 +15,6 @@ const HEADER: [u8; 80] = [
|
||||
0x2c, 0x5b, 0x4c, 0x74, 0xc1, 0x01, 0x2e, 0x6a, 0x8f, 0x06, 0x02, 0x17, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
|
||||
fn crc16(bytes: &[u8]) -> u16 {
|
||||
let mut value = 0xffff_u16;
|
||||
for byte in bytes {
|
||||
value ^= u16::from(*byte) << 8;
|
||||
for _ in 0..8 {
|
||||
value = if value & 0x8000 != 0 {
|
||||
(value << 1) ^ 0x1021
|
||||
} else {
|
||||
value << 1
|
||||
};
|
||||
}
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
fn reverse_words(input: &[u8]) -> [u8; 32] {
|
||||
let mut output = [0_u8; 32];
|
||||
for index in 0..8 {
|
||||
output[index * 4..index * 4 + 4].copy_from_slice(&input[(7 - index) * 4..(8 - index) * 4]);
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
fn captured_job() -> [u8; 88] {
|
||||
let mut frame = [0_u8; 88];
|
||||
frame[..4].copy_from_slice(&[0x55, 0xaa, 0x21, 0x54]);
|
||||
frame[4] = 0x18;
|
||||
frame[10..14].copy_from_slice(&HEADER[72..76]);
|
||||
frame[14..18].copy_from_slice(&HEADER[68..72]);
|
||||
frame[18..50].copy_from_slice(&reverse_words(&HEADER[36..68]));
|
||||
frame[50..82].copy_from_slice(&reverse_words(&HEADER[4..36]));
|
||||
frame[82..86].copy_from_slice(&HEADER[..4]);
|
||||
let crc = crc16(&frame[2..86]);
|
||||
frame[86..88].copy_from_slice(&crc.to_be_bytes());
|
||||
frame
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let path = std::env::args()
|
||||
.nth(1)
|
||||
@@ -59,17 +23,19 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
backend.set_psu_enabled(true)?;
|
||||
backend.set_chain_reset(0, true)?;
|
||||
backend.set_chain_reset(0, false)?;
|
||||
backend.send_frame(0, &captured_job())?;
|
||||
let mut buffer = [0_u8; 11];
|
||||
backend.send_frame(0, &encode_work(&HEADER, 0x18).ok_or("invalid work header")?)?;
|
||||
let mut buffer = [0_u8; REPORT_BYTES];
|
||||
let length = backend.receive_frame(0, &mut buffer)?;
|
||||
if length != 11 || buffer[..2] != [0xaa, 0x55] || buffer[10] & 0x80 == 0 {
|
||||
if length != REPORT_BYTES {
|
||||
return Err("missing BM1366 nonce report".into());
|
||||
}
|
||||
let nonce = u32::from_le_bytes(buffer[2..6].try_into()?);
|
||||
let version_field = u16::from_be_bytes(buffer[8..10].try_into()?);
|
||||
let report = decode_nonce_report(&buffer).map_err(|_| "invalid BM1366 nonce report")?;
|
||||
if report.work_id != 0x18 {
|
||||
return Err("stale BM1366 work ID".into());
|
||||
}
|
||||
let mut target = [0_u8; 32];
|
||||
target[26..28].fill(0xff);
|
||||
let result = verify_sha256_report(&HEADER, nonce, version_field, &target);
|
||||
let result = verify_sha256_report(&HEADER, report.nonce, report.version_field, &target);
|
||||
backend.set_psu_enabled(false)?;
|
||||
if !result.meets_target {
|
||||
return Err("captured nonce does not meet difficulty 1".into());
|
||||
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! BM1366 work and response framing. Nonces only come from a device or replay.
|
||||
|
||||
pub const WORK_BYTES: usize = 88;
|
||||
pub const REPORT_BYTES: usize = 11;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct NonceReport {
|
||||
pub nonce: u32,
|
||||
pub work_id: u8,
|
||||
pub version_field: u16,
|
||||
pub chip_address: u8,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum ReportError {
|
||||
Length,
|
||||
Preamble,
|
||||
RegisterResponse,
|
||||
Checksum,
|
||||
}
|
||||
|
||||
pub fn crc16(bytes: &[u8]) -> u16 {
|
||||
let mut value = 0xffff_u16;
|
||||
for byte in bytes {
|
||||
value ^= u16::from(*byte) << 8;
|
||||
for _ in 0..8 {
|
||||
value = if value & 0x8000 != 0 {
|
||||
(value << 1) ^ 0x1021
|
||||
} else {
|
||||
value << 1
|
||||
};
|
||||
}
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
fn crc5(bytes: &[u8]) -> u8 {
|
||||
let mut value = 0x1f_u8;
|
||||
for byte in bytes {
|
||||
for shift in (0..8).rev() {
|
||||
let feedback = ((value >> 4) ^ (byte >> shift)) & 1;
|
||||
value = ((value << 1) | feedback) & 0x1f;
|
||||
value ^= feedback << 2;
|
||||
}
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
fn reverse_words(input: &[u8]) -> [u8; 32] {
|
||||
let mut output = [0_u8; 32];
|
||||
for index in 0..8 {
|
||||
output[index * 4..index * 4 + 4].copy_from_slice(&input[(7 - index) * 4..(8 - index) * 4]);
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
/// Encode a nonce-zero Bitcoin header for the observed 88-byte SEND_JOB form.
|
||||
/// The low three work-ID bits are reserved by the BM1366 response format.
|
||||
pub fn encode_work(header: &[u8; 80], work_id: u8) -> Option<[u8; WORK_BYTES]> {
|
||||
if header[76..80] != [0; 4] || work_id & 7 != 0 {
|
||||
return None;
|
||||
}
|
||||
let mut frame = [0_u8; WORK_BYTES];
|
||||
frame[..4].copy_from_slice(&[0x55, 0xaa, 0x21, 0x54]);
|
||||
frame[4] = work_id;
|
||||
frame[10..14].copy_from_slice(&header[72..76]);
|
||||
frame[14..18].copy_from_slice(&header[68..72]);
|
||||
frame[18..50].copy_from_slice(&reverse_words(&header[36..68]));
|
||||
frame[50..82].copy_from_slice(&reverse_words(&header[4..36]));
|
||||
frame[82..86].copy_from_slice(&header[..4]);
|
||||
let checksum = crc16(&frame[2..86]);
|
||||
frame[86..88].copy_from_slice(&checksum.to_be_bytes());
|
||||
Some(frame)
|
||||
}
|
||||
|
||||
/// Parse an observed nonce report; register replies must use a separate path.
|
||||
pub fn decode_nonce_report(frame: &[u8]) -> Result<NonceReport, ReportError> {
|
||||
if frame.len() != REPORT_BYTES {
|
||||
return Err(ReportError::Length);
|
||||
}
|
||||
if frame[..2] != [0xaa, 0x55] {
|
||||
return Err(ReportError::Preamble);
|
||||
}
|
||||
if frame[10] & 0x80 == 0 {
|
||||
return Err(ReportError::RegisterResponse);
|
||||
}
|
||||
if crc5(&frame[2..]) != 0 {
|
||||
return Err(ReportError::Checksum);
|
||||
}
|
||||
Ok(NonceReport {
|
||||
nonce: u32::from_le_bytes(frame[2..6].try_into().expect("four-byte nonce")),
|
||||
chip_address: frame[6],
|
||||
work_id: frame[7] & 0xf8,
|
||||
version_field: u16::from_be_bytes(frame[8..10].try_into().expect("version field")),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn crc16_reference_vector() {
|
||||
assert_eq!(crc16(b"123456789"), 0x29b1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_work_and_response() {
|
||||
let header: [u8; 80] = hex_header();
|
||||
let frame = encode_work(&header, 0x18).unwrap();
|
||||
assert_eq!(&frame[..5], &[0x55, 0xaa, 0x21, 0x54, 0x18]);
|
||||
assert_eq!(
|
||||
crc16(&frame[2..86]),
|
||||
u16::from_be_bytes([frame[86], frame[87]])
|
||||
);
|
||||
assert_eq!(&frame[10..14], &header[72..76]);
|
||||
assert_eq!(&frame[82..86], &header[..4]);
|
||||
assert!(encode_work(&header, 0x19).is_none());
|
||||
let mut with_nonce = header;
|
||||
with_nonce[76] = 1;
|
||||
assert!(encode_work(&with_nonce, 0x18).is_none());
|
||||
|
||||
let report = [
|
||||
0xaa, 0x55, 0x38, 0xff, 0xad, 0xed, 0, 0x18, 0x04, 0xb3, 0x94,
|
||||
];
|
||||
let parsed = decode_nonce_report(&report).unwrap();
|
||||
assert_eq!(parsed.nonce, 0xedad_ff38);
|
||||
assert_eq!(parsed.work_id, 0x18);
|
||||
assert_eq!(parsed.version_field, 0x04b3);
|
||||
let mut corrupt = report;
|
||||
corrupt[4] ^= 1;
|
||||
assert_eq!(decode_nonce_report(&corrupt), Err(ReportError::Checksum));
|
||||
}
|
||||
|
||||
fn hex_header() -> [u8; 80] {
|
||||
let input = concat!(
|
||||
"00000020fc3ab8d43021f45eb662389d0f791f657dbf75a5cb93670b57b6c22503",
|
||||
"b4213a43ae1052ebd44443068fe178e2c61ca4994846fd133addb04aab2b382c5",
|
||||
"b4c74c1012e6a8f06021700000000"
|
||||
);
|
||||
let mut header = [0_u8; 80];
|
||||
for (index, bytes) in input.as_bytes().chunks_exact(2).enumerate() {
|
||||
let pair = std::str::from_utf8(bytes).unwrap();
|
||||
header[index] = u8::from_str_radix(pair, 16).unwrap();
|
||||
}
|
||||
header
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
//! An entry in the target matrix is a development obligation, not a claim that
|
||||
//! the pairing already mines. Hardware backends are supplied by SDK images.
|
||||
|
||||
pub mod bm1366;
|
||||
pub mod hal_wire;
|
||||
pub mod pow;
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use alkane_os::bm1366::{decode_nonce_report, encode_work};
|
||||
use alkane_os::pow::verify_sha256_report;
|
||||
use alkane_os::{power_decision, target_matrix};
|
||||
use std::hint::black_box;
|
||||
@@ -46,3 +47,23 @@ fn bench_share_verification() {
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm1366_work_encoding() {
|
||||
let header = [0_u8; 80];
|
||||
measure("bm1366_work_encoding", 20_000, || {
|
||||
black_box(encode_work(black_box(&header), black_box(0x18)));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm1366_report_decode() {
|
||||
let report = [
|
||||
0xaa, 0x55, 0x38, 0xff, 0xad, 0xed, 0, 0x18, 0x04, 0xb3, 0x94,
|
||||
];
|
||||
measure("bm1366_report_decode", 100_000, || {
|
||||
let _ = black_box(decode_nonce_report(black_box(&report)));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user