From 4f34fbf1b3b736adae959e5e2db5960b67a4af36 Mon Sep 17 00:00:00 2001 From: Mikhail Komarov Date: Mon, 11 Jan 2021 02:47:21 +0300 Subject: [PATCH 1/4] Initial instruction definition implemented --- Cargo.toml | 1 + src/executor/crypto.rs | 37 +++++++++++++++++++++++++++++++++ src/executor/engine/handlers.rs | 1 + 3 files changed, 39 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 8f70eab2..426e3f8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ similar = { features = [ 'bytes' ], optional = true, version = '2.2.0' } ton_block = { git = 'https://github.com/tonlabs/ever-block.git', tag = '1.9.101' } ton_types = { git = 'https://github.com/tonlabs/ever-types.git', tag = '2.0.26' } zstd = { default-features = false, optional = true, version = '0.11' } +crusty3_zk = { git = 'https://github.com/nilfoundation/crusty3-zk.git', branch = '6-extended-vk-encrypted-input-verification' } [features] fift_check = [ ] diff --git a/src/executor/crypto.rs b/src/executor/crypto.rs index 358e88c6..c7c58e9d 100644 --- a/src/executor/crypto.rs +++ b/src/executor/crypto.rs @@ -25,11 +25,20 @@ use crate::{ }, types::{Exception, Status} }; + +use crusty3_zk::create_random_proof; use ed25519::signature::Verifier; use std::borrow::Cow; use ton_block::GlobalCapabilities; +use sha2::Digest; +use ed25519::signature::{Signature, Verifier}; +use std::sync::Arc; use ton_types::{BuilderData, error, GasConsumer, ExceptionCode, UInt256}; +use crusty3_zk::{groth16::{verify_proof, prepare_verifying_key, Parameters}, + bls::{Bls12, Fr} + }; + const PUBLIC_KEY_BITS: usize = PUBLIC_KEY_BYTES * 8; const SIGNATURE_BITS: usize = SIGNATURE_BYTES * 8; const PUBLIC_KEY_BYTES: usize = ed25519_dalek::PUBLIC_KEY_LENGTH; @@ -80,6 +89,34 @@ pub(super) fn execute_sha256u(engine: &mut Engine) -> Status { } } +pub(super) fn execute_vergrth16(engine: &mut Engine) -> Failure { + engine.load_instruction(Instruction::new("VERGRTH16")) + .and_then(|ctx| fetch_stack(ctx, 1)) + .and_then(|ctx| { + let builder = BuilderData::from(ctx.engine.cmd.var(0).as_cell()?); + let cell_proof_data_length = builder.length_in_bits(); + //let data = builder.data(); + let cell_proof = ctx.engine.finalize_cell(builder)?; + let cell_proof_data = cell_proof.data(); + if cell_proof_data_length % 8 == 0 { + + let de_params = Parameters::read(&cell_proof_data[..], true).unwrap(); + + let pvk = prepare_verifying_key::(&de_params.vk); + + //let de_proof = Proof::read(&v[..]).unwrap(); + + //let result = verify_proof(); + let result = true; + ctx.engine.cc.stack.push(boolean!(result)); + Ok(ctx) + } else { + err!(ExceptionCode::CellUnderflow) + } + }) + .err() +} + enum DataForSignature { Hash(BuilderData), Slice(Vec) diff --git a/src/executor/engine/handlers.rs b/src/executor/engine/handlers.rs index 48e786df..638147ab 100644 --- a/src/executor/engine/handlers.rs +++ b/src/executor/engine/handlers.rs @@ -894,6 +894,7 @@ impl Handlers { .set(0x02, execute_sha256u) .set(0x10, execute_chksignu) .set(0x11, execute_chksigns) + .set(0x12, execute_vergrth16) .set(0x40, execute_cdatasizeq) .set(0x41, execute_cdatasize) .set(0x42, execute_sdatasizeq) From a8c3d0733cdc6f3df4dbba04d81aae38181826fd Mon Sep 17 00:00:00 2001 From: nkaskov Date: Mon, 17 May 2021 15:32:41 +0300 Subject: [PATCH 2/4] VERGRTH16 changed to print input byte blob --- src/executor/crypto.rs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/executor/crypto.rs b/src/executor/crypto.rs index c7c58e9d..0d8b7eee 100644 --- a/src/executor/crypto.rs +++ b/src/executor/crypto.rs @@ -33,9 +33,9 @@ use ton_block::GlobalCapabilities; use sha2::Digest; use ed25519::signature::{Signature, Verifier}; use std::sync::Arc; -use ton_types::{BuilderData, error, GasConsumer, ExceptionCode, UInt256}; +use ton_types::{BuilderData, Cell, error, GasConsumer, ExceptionCode, UInt256}; -use crusty3_zk::{groth16::{verify_proof, prepare_verifying_key, Parameters}, +use crusty3_zk::{groth16::{verify_proof, prepare_verifying_key, Parameters, verify_groth16_proof_from_byteblob}, bls::{Bls12, Fr} }; @@ -89,25 +89,38 @@ pub(super) fn execute_sha256u(engine: &mut Engine) -> Status { } } +pub fn obtain_cells_data(cl: Cell) -> Result, Failure> { + let mut byte_blob = Vec::new(); + let mut queue = vec!(cl.clone()); + while let Some(cell) = queue.pop() { + let this_reference_data = cell.data(); + + byte_blob.extend(this_reference_data[0..this_reference_data.len()-1].iter().copied()); + + let count = cell.references_count(); + for i in 0..count { + queue.push(cell.reference(i)?); + } + } + + Ok(byte_blob) +} + pub(super) fn execute_vergrth16(engine: &mut Engine) -> Failure { engine.load_instruction(Instruction::new("VERGRTH16")) .and_then(|ctx| fetch_stack(ctx, 1)) .and_then(|ctx| { let builder = BuilderData::from(ctx.engine.cmd.var(0).as_cell()?); let cell_proof_data_length = builder.length_in_bits(); - //let data = builder.data(); + let cell_proof = ctx.engine.finalize_cell(builder)?; - let cell_proof_data = cell_proof.data(); - if cell_proof_data_length % 8 == 0 { - let de_params = Parameters::read(&cell_proof_data[..], true).unwrap(); + let mut cell_proof_data = obtain_cells_data(cell_proof).unwrap(); - let pvk = prepare_verifying_key::(&de_params.vk); + if cell_proof_data_length % 8 == 0 { - //let de_proof = Proof::read(&v[..]).unwrap(); + let result = verify_groth16_proof_from_byteblob::(&cell_proof_data[..]).unwrap(); - //let result = verify_proof(); - let result = true; ctx.engine.cc.stack.push(boolean!(result)); Ok(ctx) } else { From 7007d35fecfab61b38a6fc47abca95a0ce7ae76a Mon Sep 17 00:00:00 2001 From: Ilias Khairullin Date: Thu, 30 Jun 2022 02:38:41 +0200 Subject: [PATCH 3/4] Groth16 encrypted input mode introduced --- src/executor/crypto.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/executor/crypto.rs b/src/executor/crypto.rs index 0d8b7eee..1f0e2523 100644 --- a/src/executor/crypto.rs +++ b/src/executor/crypto.rs @@ -35,9 +35,9 @@ use ed25519::signature::{Signature, Verifier}; use std::sync::Arc; use ton_types::{BuilderData, Cell, error, GasConsumer, ExceptionCode, UInt256}; -use crusty3_zk::{groth16::{verify_proof, prepare_verifying_key, Parameters, verify_groth16_proof_from_byteblob}, - bls::{Bls12, Fr} - }; +use crusty3_zk::{groth16::{verify_proof, prepare_verifying_key, Parameters, verify_groth16_proof_from_byteblob, verify_encrypted_input_groth16_proof_from_byteblob}, + bls::{Bls12, Fr}, +}; const PUBLIC_KEY_BITS: usize = PUBLIC_KEY_BYTES * 8; const SIGNATURE_BITS: usize = SIGNATURE_BYTES * 8; @@ -115,11 +115,16 @@ pub(super) fn execute_vergrth16(engine: &mut Engine) -> Failure { let cell_proof = ctx.engine.finalize_cell(builder)?; - let mut cell_proof_data = obtain_cells_data(cell_proof).unwrap(); - - if cell_proof_data_length % 8 == 0 { - - let result = verify_groth16_proof_from_byteblob::(&cell_proof_data[..]).unwrap(); + let mut cell_proof_data = obtain_cells_data(cell_proof).unwrap();if cell_proof_data_length % 8 == 0 { + let mut result = false; + if cell_proof_data[0] == 0 { + result = verify_groth16_proof_from_byteblob::(&cell_proof_data[1..]).unwrap(); + } else if cell_proof_data[0] == 1 { + result = verify_encrypted_input_groth16_proof_from_byteblob::(&cell_proof_data[1..]).unwrap(); + } + else { + return err!(ExceptionCode::InvalidOpcode); + } ctx.engine.cc.stack.push(boolean!(result)); Ok(ctx) From 3e1f8f8d2b1654cbc8b339dd612f08e444093e05 Mon Sep 17 00:00:00 2001 From: Ilyar Date: Tue, 12 Sep 2023 21:06:18 +0200 Subject: [PATCH 4/4] Uptodate instruction definition implemented --- src/executor/crypto.rs | 70 ++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/src/executor/crypto.rs b/src/executor/crypto.rs index 1f0e2523..4931d00c 100644 --- a/src/executor/crypto.rs +++ b/src/executor/crypto.rs @@ -26,17 +26,24 @@ use crate::{ types::{Exception, Status} }; -use crusty3_zk::create_random_proof; use ed25519::signature::Verifier; use std::borrow::Cow; use ton_block::GlobalCapabilities; -use sha2::Digest; -use ed25519::signature::{Signature, Verifier}; -use std::sync::Arc; -use ton_types::{BuilderData, Cell, error, GasConsumer, ExceptionCode, UInt256}; - -use crusty3_zk::{groth16::{verify_proof, prepare_verifying_key, Parameters, verify_groth16_proof_from_byteblob, verify_encrypted_input_groth16_proof_from_byteblob}, - bls::{Bls12, Fr}, +use ton_types::{ + BuilderData, + Cell, + error, + GasConsumer, + ExceptionCode, + UInt256, + Result, +}; +use crusty3_zk::{ + groth16::{ + verify_groth16_proof_from_byteblob, + verify_encrypted_input_groth16_proof_from_byteblob, + }, + bls::{Bls12}, }; const PUBLIC_KEY_BITS: usize = PUBLIC_KEY_BYTES * 8; @@ -89,7 +96,7 @@ pub(super) fn execute_sha256u(engine: &mut Engine) -> Status { } } -pub fn obtain_cells_data(cl: Cell) -> Result, Failure> { +pub fn obtain_cells_data(cl: Cell) -> Result> { let mut byte_blob = Vec::new(); let mut queue = vec!(cl.clone()); while let Some(cell) = queue.pop() { @@ -106,33 +113,24 @@ pub fn obtain_cells_data(cl: Cell) -> Result, Failure> { Ok(byte_blob) } -pub(super) fn execute_vergrth16(engine: &mut Engine) -> Failure { - engine.load_instruction(Instruction::new("VERGRTH16")) - .and_then(|ctx| fetch_stack(ctx, 1)) - .and_then(|ctx| { - let builder = BuilderData::from(ctx.engine.cmd.var(0).as_cell()?); - let cell_proof_data_length = builder.length_in_bits(); - - let cell_proof = ctx.engine.finalize_cell(builder)?; - - let mut cell_proof_data = obtain_cells_data(cell_proof).unwrap();if cell_proof_data_length % 8 == 0 { - let mut result = false; - if cell_proof_data[0] == 0 { - result = verify_groth16_proof_from_byteblob::(&cell_proof_data[1..]).unwrap(); - } else if cell_proof_data[0] == 1 { - result = verify_encrypted_input_groth16_proof_from_byteblob::(&cell_proof_data[1..]).unwrap(); - } - else { - return err!(ExceptionCode::InvalidOpcode); - } - - ctx.engine.cc.stack.push(boolean!(result)); - Ok(ctx) - } else { - err!(ExceptionCode::CellUnderflow) - } - }) - .err() +pub(super) fn execute_vergrth16(engine: &mut Engine) -> Status { + engine.load_instruction(Instruction::new("VERGRTH16"))?; + fetch_stack(engine, 1)?; + let builder = engine.cmd.var(0).clone().as_builder_mut()?; + let cell_proof_data_length = builder.length_in_bits(); + let cell_proof = engine.finalize_cell(builder.into())?; + let cell_proof_data = obtain_cells_data(cell_proof).unwrap(); + if cell_proof_data_length % 8 == 0 { + let result = if cell_proof_data[0] == 0 { + verify_groth16_proof_from_byteblob::(&cell_proof_data[1..]).unwrap() + } else { + verify_encrypted_input_groth16_proof_from_byteblob::(&cell_proof_data[1..]).unwrap() + }; + engine.cc.stack.push(boolean!(result)); + return Ok(()) + } else { + err!(ExceptionCode::CellUnderflow) + } } enum DataForSignature {