Skip to content

Commit

Permalink
ADD: SIGCHECK inst
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyuanyang-uu committed Jan 10, 2024
1 parent 5233d45 commit 53ab7e5
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 11 deletions.
47 changes: 45 additions & 2 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions core/src/vm/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ pub enum ProcessorError {

#[error("Tload flag is invalid: {0}")]
TloadFlagInvalid(u64),

#[error("Pubkey is invalid: {0}")]
PubKeyInvalid(String),

#[error("Signature is invalid: {0}")]
SignatureInvalid(String),

#[error("Message is invalid: {0}")]
MessageInvalid(String),
}
2 changes: 2 additions & 0 deletions executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ rand = "0.8"
bincode = "1"
byteorder = "1.3"
tokio = { version = "1", features = ["full"] }
secp256k1 = { version = "0.28.1", default-features = false, features = ["hashes-std", "std", "recovery"] }
num = "0.4.1"

[dev-dependencies]
9 changes: 2 additions & 7 deletions executor/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ pub fn decode_raw_instruction(
instruction += &reg2_name;
}
}
Opcode::CJMP
| Opcode::TSTORE
| Opcode::SCCALL
| Opcode::SLOAD
| Opcode::SSTORE
| Opcode::SIGCHECK => {
Opcode::CJMP | Opcode::TSTORE | Opcode::SCCALL | Opcode::SLOAD | Opcode::SSTORE => {
instruction += &op_code.to_string();
instruction += " ";
let reg1_name = format!("r{}", reg1);
Expand All @@ -95,7 +90,7 @@ pub fn decode_raw_instruction(
instruction += &reg2_name;
}
}
Opcode::MOV | Opcode::NOT => {
Opcode::MOV | Opcode::NOT | Opcode::SIGCHECK => {
instruction += &op_code.to_string();
instruction += " ";
let reg0_name = format!("r{}", reg0);
Expand Down
38 changes: 38 additions & 0 deletions executor/src/ecdsa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use core::types::merkle_tree::{tree_key_to_u8_arr, u8_arr_to_tree_key};
use core::types::merkle_tree::{TreeKey, TreeValue};
use core::vm::error::ProcessorError;
use num::{BigUint, Num};
use secp256k1::{ecdsa, Message, PublicKey, Secp256k1};
pub fn ecdsa_verify(
x: TreeValue,
y: TreeValue,
r: TreeValue,
s: TreeValue,
msg: TreeValue,
) -> Result<bool, ProcessorError> {
let secp = Secp256k1::new();

let x_arr = tree_key_to_u8_arr(&x);
let y_arr = tree_key_to_u8_arr(&y);

let mut pub_key_bytes = [0u8; 65];
pub_key_bytes[0] = 4;
pub_key_bytes[1..33].copy_from_slice(&x_arr);
pub_key_bytes[33..].copy_from_slice(&y_arr);
let pubkey = PublicKey::from_slice(&pub_key_bytes)
.map_err(|e| ProcessorError::PubKeyInvalid(e.to_string()))?;

let mut signature_bytes = [0u8; 64];
let r_arr = tree_key_to_u8_arr(&r);
let s_arr = tree_key_to_u8_arr(&s);

signature_bytes[..32].copy_from_slice(&r_arr);
signature_bytes[32..].copy_from_slice(&s_arr);
let sig = ecdsa::Signature::from_compact(&signature_bytes)
.map_err(|e| ProcessorError::SignatureInvalid(e.to_string()))?;

let msg_arr = tree_key_to_u8_arr(&msg);
let message =
Message::from_slice(&msg_arr).map_err(|e| ProcessorError::MessageInvalid(e.to_string()))?;
Ok(secp.verify_ecdsa(&message, &sig, &pubkey).is_ok())
}
7 changes: 6 additions & 1 deletion executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use plonky2::field::types::{Field, PrimeField64};
use regex::Regex;
use std::collections::{BTreeMap, HashMap};

use crate::ecdsa::ecdsa_verify;
use crate::load_tx::{init_ctx_addr_info, load_ctx_addr_info};
use crate::tape::TapeTree;
use crate::trace::{gen_memory_table, gen_tape_table};
Expand All @@ -51,6 +52,7 @@ use std::time::Instant;

mod decode;

mod ecdsa;
pub mod load_tx;
pub mod storage;
mod tape;
Expand Down Expand Up @@ -2020,7 +2022,10 @@ impl Process {
memory_op!(self, sig_s_addr + i, data, Opcode::SIGCHECK);
sig_s[i as usize] = data;
}

self.registers[dst_index] = GoldilocksField::from_canonical_u8(ecdsa_verify(
pk_x, pk_y, sig_r, sig_s, msg,
)?
as u8);
self.register_selector.dst = self.registers[dst_index];
}
_ => panic!("not match opcode:{}", opcode),
Expand Down
2 changes: 1 addition & 1 deletion executor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use core::types::merkle_tree::tree_key_default;
use core::types::merkle_tree::{decode_addr, encode_addr};
use core::vm::transaction::init_tx_context_mock;
use log::{debug, LevelFilter};
use num::{BigInt, BigUint, Num};
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::field::types::Field;
use std::collections::HashMap;
Expand Down Expand Up @@ -468,7 +469,6 @@ fn ecdsa_test() {
);
}


#[test]
fn gen_storage_table_test() {
let mut program: Program = Program::default();
Expand Down

0 comments on commit 53ab7e5

Please sign in to comment.