From 0c063e0d78b682281795a0924c2ea2aded29f37f Mon Sep 17 00:00:00 2001 From: zkronos73 <94566827+zkronos73@users.noreply.github.com> Date: Thu, 12 Dec 2024 23:48:25 +0100 Subject: [PATCH] add proofvalue to enable/disable input_data memory (#193) --- Cargo.lock | 28 ++++++++++++------------ Cargo.toml | 24 ++++++++++---------- emulator/src/emu.rs | 4 ++-- pil/zisk.pil | 7 ++++-- state-machines/arith/pil/arith_table.pil | 2 +- state-machines/mem/pil/mem.pil | 8 +++++-- state-machines/mem/src/input_data_sm.rs | 9 +++++++- 7 files changed, 48 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd9341db..24c1ea80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1461,7 +1461,7 @@ dependencies = [ [[package]] name = "pil-std-lib" version = "0.1.0" -source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?rev=0.0.16#5c47437feffccb16d95e120e8336ab8a168314e7" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=fix%2Fstarkpil-memcpy-airvalue-proofvalue#a6392a7f20056fa8bff95d60da3b7adabc7c5c74" dependencies = [ "log", "num-bigint", @@ -1479,7 +1479,7 @@ dependencies = [ [[package]] name = "pilout" version = "0.1.0" -source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?rev=0.0.16#5c47437feffccb16d95e120e8336ab8a168314e7" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=fix%2Fstarkpil-memcpy-airvalue-proofvalue#a6392a7f20056fa8bff95d60da3b7adabc7c5c74" dependencies = [ "bytes", "log", @@ -1599,7 +1599,7 @@ dependencies = [ [[package]] name = "proofman" version = "0.1.0" -source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?rev=0.0.16#5c47437feffccb16d95e120e8336ab8a168314e7" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=fix%2Fstarkpil-memcpy-airvalue-proofvalue#a6392a7f20056fa8bff95d60da3b7adabc7c5c74" dependencies = [ "colored", "env_logger", @@ -1620,7 +1620,7 @@ dependencies = [ [[package]] name = "proofman-common" version = "0.1.0" -source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?rev=0.0.16#5c47437feffccb16d95e120e8336ab8a168314e7" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=fix%2Fstarkpil-memcpy-airvalue-proofvalue#a6392a7f20056fa8bff95d60da3b7adabc7c5c74" dependencies = [ "env_logger", "log", @@ -1639,7 +1639,7 @@ dependencies = [ [[package]] name = "proofman-hints" version = "0.1.0" -source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?rev=0.0.16#5c47437feffccb16d95e120e8336ab8a168314e7" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=fix%2Fstarkpil-memcpy-airvalue-proofvalue#a6392a7f20056fa8bff95d60da3b7adabc7c5c74" dependencies = [ "p3-field", "proofman-common", @@ -1649,7 +1649,7 @@ dependencies = [ [[package]] name = "proofman-macros" version = "0.1.0" -source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?rev=0.0.16#5c47437feffccb16d95e120e8336ab8a168314e7" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=fix%2Fstarkpil-memcpy-airvalue-proofvalue#a6392a7f20056fa8bff95d60da3b7adabc7c5c74" dependencies = [ "proc-macro2", "quote", @@ -1659,7 +1659,7 @@ dependencies = [ [[package]] name = "proofman-starks-lib-c" version = "0.1.0" -source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?rev=0.0.16#5c47437feffccb16d95e120e8336ab8a168314e7" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=fix%2Fstarkpil-memcpy-airvalue-proofvalue#a6392a7f20056fa8bff95d60da3b7adabc7c5c74" dependencies = [ "log", ] @@ -1667,7 +1667,7 @@ dependencies = [ [[package]] name = "proofman-util" version = "0.1.0" -source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?rev=0.0.16#5c47437feffccb16d95e120e8336ab8a168314e7" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=fix%2Fstarkpil-memcpy-airvalue-proofvalue#a6392a7f20056fa8bff95d60da3b7adabc7c5c74" dependencies = [ "colored", "sysinfo 0.31.4", @@ -1847,9 +1847,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" dependencies = [ "bitflags 2.6.0", ] @@ -2014,9 +2014,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.19" +version = "0.23.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "934b404430bb06b3fae2cba809eb45a1ab1aecd64491213d7c3301b88393f8d1" +checksum = "5065c3f250cbd332cd894be57c40fa52387247659b14a2d6041d121547903b1b" dependencies = [ "once_cell", "ring", @@ -2316,7 +2316,7 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "stark" version = "0.1.0" -source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?rev=0.0.16#5c47437feffccb16d95e120e8336ab8a168314e7" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=fix%2Fstarkpil-memcpy-airvalue-proofvalue#a6392a7f20056fa8bff95d60da3b7adabc7c5c74" dependencies = [ "log", "p3-field", @@ -2665,7 +2665,7 @@ dependencies = [ [[package]] name = "transcript" version = "0.1.0" -source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?rev=0.0.16#5c47437feffccb16d95e120e8336ab8a168314e7" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=fix%2Fstarkpil-memcpy-airvalue-proofvalue#a6392a7f20056fa8bff95d60da3b7adabc7c5c74" dependencies = [ "proofman-starks-lib-c", ] diff --git a/Cargo.toml b/Cargo.toml index 9f456214..b10f41b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,19 +26,19 @@ opt-level = 3 opt-level = 3 [workspace.dependencies] -proofman-common = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.16" } -proofman-macros = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.16" } -proofman-util = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.16" } -proofman = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.16" } -pil-std-lib = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.16" } -stark = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.16" } +proofman-common = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "fix/starkpil-memcpy-airvalue-proofvalue" } +proofman-macros = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "fix/starkpil-memcpy-airvalue-proofvalue" } +proofman-util = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "fix/starkpil-memcpy-airvalue-proofvalue" } +proofman = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "fix/starkpil-memcpy-airvalue-proofvalue" } +pil-std-lib = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "fix/starkpil-memcpy-airvalue-proofvalue" } +stark = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "fix/starkpil-memcpy-airvalue-proofvalue" } #Local development -# proofman-common = { path = "../pil2-proofman/common" } -# proofman-macros = { path = "../pil2-proofman/macros" } -# proofman-util = { path = "../pil2-proofman/util" } -# proofman = { path = "../pil2-proofman/proofman" } -# pil-std-lib = { path = "../pil2-proofman/pil2-components/lib/std/rs" } -# stark = { path = "../pil2-proofman/provers/stark" } +#proofman-common = { path = "../pil2-proofman/common" } +#proofman-macros = { path = "../pil2-proofman/macros" } +#proofman-util = { path = "../pil2-proofman/util" } +#proofman = { path = "../pil2-proofman/proofman" } +#pil-std-lib = { path = "../pil2-proofman/pil2-components/lib/std/rs" } +#stark = { path = "../pil2-proofman/provers/stark" } p3-field = { git = "https://github.com/Plonky3/Plonky3.git", rev = "c3d754ef77b9fce585b46b972af751fe6e7a9803" } log = "0.4" diff --git a/emulator/src/emu.rs b/emulator/src/emu.rs index 6ec7fa0f..93c6c1d5 100644 --- a/emulator/src/emu.rs +++ b/emulator/src/emu.rs @@ -415,7 +415,7 @@ impl<'a> Emu<'a> { } } - /// Set SP, if specified by the current instruction + // Set SP, if specified by the current instruction // #[cfg(feature = "sp")] // #[inline(always)] // pub fn set_sp(&mut self, instruction: &ZiskInst) { @@ -426,7 +426,7 @@ impl<'a> Emu<'a> { // } // } - /// Set PC, based on current PC, current flag and current instruction + // Set PC, based on current PC, current flag and current instruction #[inline(always)] pub fn set_pc(&mut self, instruction: &ZiskInst) { if instruction.set_pc { diff --git a/pil/zisk.pil b/pil/zisk.pil index bce36ffa..3a4d1111 100644 --- a/pil/zisk.pil +++ b/pil/zisk.pil @@ -11,16 +11,19 @@ require "arith/pil/arith.pil" const int OPERATION_BUS_ID = 5000; +proofval enable_input_data; +enable_input_data * (1 - enable_input_data); + airgroup Zisk { Main(N: 2**21, RC: 2, operation_bus_id: OPERATION_BUS_ID); Rom(N: 2**22); Mem(N: 2**21, RC: 2, base_address: 0xA000_0000); Mem(N: 2**21, RC: 2, base_address: 0x8000_0000, immutable: 1) alias RomData; - Mem(N: 2**21, RC: 2, base_address: 0x9000_0000, free_input_mem: 1) alias InputData; + Mem(N: 2**21, RC: 2, base_address: 0x9000_0000, free_input_mem: 1, enable_flag: enable_input_data) alias InputData; + MemAlign(N: 2**21); MemAlignRom(disable_fixed: 0); - // InputData(N: 2**21, RC: 2); Arith(N: 2**21, operation_bus_id: OPERATION_BUS_ID); ArithTable(); diff --git a/state-machines/arith/pil/arith_table.pil b/state-machines/arith/pil/arith_table.pil index e8bd35d7..53f8b238 100644 --- a/state-machines/arith/pil/arith_table.pil +++ b/state-machines/arith/pil/arith_table.pil @@ -2,7 +2,7 @@ require "std_lookup.pil" const int ARITH_TABLE_ID = 331; -airtemplate ArithTable(int N = 2**7, int generate_table = 1) { +airtemplate ArithTable(int N = 2**7, int generate_table = 0) { // div m32 sa sb primary secondary opcodes na nb np nr sext(c) // ----------------------------------------------------------------------------------- diff --git a/state-machines/mem/pil/mem.pil b/state-machines/mem/pil/mem.pil index f574f264..27052d91 100644 --- a/state-machines/mem/pil/mem.pil +++ b/state-machines/mem/pil/mem.pil @@ -50,7 +50,10 @@ const int MEMORY_MAX_DIFF = 2**24; const int MAX_MEM_STEP_OFFSET = 2; const int MAX_MEM_OPS_PER_MAIN_STEP = (MAX_MEM_STEP_OFFSET + 1) * 2; -airtemplate Mem(const int N = 2**21, const int id = MEMORY_ID, const int RC = 2, const int mem_bytes = 8, const int base_address = 0, const int mem_size = 0x800_0000, int immutable = 0, const int free_input_mem = 0) { +airtemplate Mem(const int N = 2**21, const int id = MEMORY_ID, const int RC = 2, const int mem_bytes = 8, + const int base_address = 0, const int mem_size = 0x800_0000, int immutable = 0, + const int free_input_mem = 0, const expr enable_flag = 1) { + col fixed SEGMENT_L1 = [1,0...]; const expr SEGMENT_LAST = SEGMENT_L1'; @@ -146,7 +149,8 @@ airtemplate Mem(const int N = 2**21, const int id = MEMORY_ID, const int RC = 2, for (int i = 0; i < length(zeros); ++i) { zeros[i] = 0; } - direct_global_update_proves(MEMORY_CONT_ID, [ base_address, 0, internal_base_address, 0, ...zeros]); + + direct_global_update_proves(MEMORY_CONT_ID, [ base_address, 0, internal_base_address, 0, ...zeros], sel: enable_flag); // for security check that first address has correct value, to avoid add huge quantity of instances to "overflow" prime field. range_check(colu: previous_segment_addr - internal_base_address + 1, min: 1, max: MEMORY_MAX_DIFF); diff --git a/state-machines/mem/src/input_data_sm.rs b/state-machines/mem/src/input_data_sm.rs index 220fc33f..2717e1e6 100644 --- a/state-machines/mem/src/input_data_sm.rs +++ b/state-machines/mem/src/input_data_sm.rs @@ -74,10 +74,17 @@ impl InputDataSM { self.std.unregister_predecessor(pctx, None); } } - pub fn prove(&self, inputs: &[MemInput]) { let wcm = self.wcm.clone(); let pctx = wcm.get_pctx(); + + if (inputs.is_empty()) { + pctx.set_proof_value("enable_input_data", F::zero()); + return; + } + + pctx.set_proof_value("enable_input_data", F::one()); + let ectx = wcm.get_ectx(); let sctx = wcm.get_sctx();