@@ -7,8 +7,8 @@ use alloc::vec::Vec;
77use std:: vec:: Vec ;
88
99use crate :: constants:: {
10- DEFAULT_MAX_INPUT_SIZE , DEFAULT_MAX_OUTPUT_SIZE , DEFAULT_MEMORY_SIZE , DEFAULT_STACK_SIZE ,
11- RAM_START_ADDRESS ,
10+ DEFAULT_MAX_INPUT_SIZE , DEFAULT_MAX_OUTPUT_SIZE , DEFAULT_MAX_TRUSTED_ADVICE_SIZE ,
11+ DEFAULT_MAX_UNTRUSTED_ADVICE_SIZE , DEFAULT_MEMORY_SIZE , DEFAULT_STACK_SIZE , RAM_START_ADDRESS ,
1212} ;
1313
1414#[ allow( clippy:: too_long_first_doc_paragraph) ]
@@ -28,6 +28,8 @@ use crate::constants::{
2828) ]
2929pub struct JoltDevice {
3030 pub inputs : Vec < u8 > ,
31+ pub trusted_advice : Vec < u8 > ,
32+ pub untrusted_advice : Vec < u8 > ,
3133 pub outputs : Vec < u8 > ,
3234 pub panic : bool ,
3335 pub memory_layout : MemoryLayout ,
@@ -37,6 +39,8 @@ impl JoltDevice {
3739 pub fn new ( memory_config : & MemoryConfig ) -> Self {
3840 Self {
3941 inputs : Vec :: new ( ) ,
42+ trusted_advice : Vec :: new ( ) ,
43+ untrusted_advice : Vec :: new ( ) ,
4044 outputs : Vec :: new ( ) ,
4145 panic : false ,
4246 memory_layout : MemoryLayout :: new ( memory_config) ,
@@ -55,6 +59,20 @@ impl JoltDevice {
5559 } else {
5660 self . inputs [ internal_address]
5761 }
62+ } else if self . is_trusted_advice ( address) {
63+ let internal_address = self . convert_trusted_advice_read_address ( address) ;
64+ if self . trusted_advice . len ( ) <= internal_address {
65+ 0
66+ } else {
67+ self . trusted_advice [ internal_address]
68+ }
69+ } else if self . is_untrusted_advice ( address) {
70+ let internal_address = self . convert_untrusted_advice_read_address ( address) ;
71+ if self . untrusted_advice . len ( ) <= internal_address {
72+ 0
73+ } else {
74+ self . untrusted_advice [ internal_address]
75+ }
5876 } else if self . is_output ( address) {
5977 let internal_address = self . convert_write_address ( address) ;
6078 if self . outputs . len ( ) <= internal_address {
@@ -91,6 +109,16 @@ impl JoltDevice {
91109 address >= self . memory_layout . input_start && address < self . memory_layout . input_end
92110 }
93111
112+ pub fn is_trusted_advice ( & self , address : u64 ) -> bool {
113+ address >= self . memory_layout . trusted_advice_start
114+ && address < self . memory_layout . trusted_advice_end
115+ }
116+
117+ pub fn is_untrusted_advice ( & self , address : u64 ) -> bool {
118+ address >= self . memory_layout . untrusted_advice_start
119+ && address < self . memory_layout . untrusted_advice_end
120+ }
121+
94122 pub fn is_output ( & self , address : u64 ) -> bool {
95123 address >= self . memory_layout . output_start && address < self . memory_layout . termination
96124 }
@@ -107,6 +135,14 @@ impl JoltDevice {
107135 ( address - self . memory_layout . input_start ) as usize
108136 }
109137
138+ fn convert_trusted_advice_read_address ( & self , address : u64 ) -> usize {
139+ ( address - self . memory_layout . trusted_advice_start ) as usize
140+ }
141+
142+ fn convert_untrusted_advice_read_address ( & self , address : u64 ) -> usize {
143+ ( address - self . memory_layout . untrusted_advice_start ) as usize
144+ }
145+
110146 fn convert_write_address ( & self , address : u64 ) -> usize {
111147 ( address - self . memory_layout . output_start ) as usize
112148 }
@@ -115,6 +151,8 @@ impl JoltDevice {
115151#[ derive( Debug , Copy , Clone ) ]
116152pub struct MemoryConfig {
117153 pub max_input_size : u64 ,
154+ pub max_trusted_advice_size : u64 ,
155+ pub max_untrusted_advice_size : u64 ,
118156 pub max_output_size : u64 ,
119157 pub stack_size : u64 ,
120158 pub memory_size : u64 ,
@@ -125,6 +163,8 @@ impl Default for MemoryConfig {
125163 fn default ( ) -> Self {
126164 Self {
127165 max_input_size : DEFAULT_MAX_INPUT_SIZE ,
166+ max_trusted_advice_size : DEFAULT_MAX_TRUSTED_ADVICE_SIZE ,
167+ max_untrusted_advice_size : DEFAULT_MAX_UNTRUSTED_ADVICE_SIZE ,
128168 max_output_size : DEFAULT_MAX_OUTPUT_SIZE ,
129169 stack_size : DEFAULT_STACK_SIZE ,
130170 memory_size : DEFAULT_MEMORY_SIZE ,
@@ -139,6 +179,12 @@ impl Default for MemoryConfig {
139179pub struct MemoryLayout {
140180 /// The total size of the elf's sections, including the .text, .data, .rodata, and .bss sections.
141181 pub program_size : u64 ,
182+ pub max_trusted_advice_size : u64 ,
183+ pub trusted_advice_start : u64 ,
184+ pub trusted_advice_end : u64 ,
185+ pub max_untrusted_advice_size : u64 ,
186+ pub untrusted_advice_start : u64 ,
187+ pub untrusted_advice_end : u64 ,
142188 pub max_input_size : u64 ,
143189 pub max_output_size : u64 ,
144190 pub input_start : u64 ,
@@ -163,7 +209,25 @@ impl core::fmt::Debug for MemoryLayout {
163209 f. debug_struct ( "MemoryLayout" )
164210 . field ( "program_size" , & self . program_size )
165211 . field ( "max_input_size" , & self . max_input_size )
212+ . field ( "max_trusted_advice_size" , & self . max_trusted_advice_size )
213+ . field ( "max_untrusted_advice_size" , & self . max_untrusted_advice_size )
166214 . field ( "max_output_size" , & self . max_output_size )
215+ . field (
216+ "trusted_advice_start" ,
217+ & format_args ! ( "{:#X}" , self . trusted_advice_start) ,
218+ )
219+ . field (
220+ "trusted_advice_end" ,
221+ & format_args ! ( "{:#X}" , self . trusted_advice_end) ,
222+ )
223+ . field (
224+ "untrusted_advice_start" ,
225+ & format_args ! ( "{:#X}" , self . untrusted_advice_start) ,
226+ )
227+ . field (
228+ "untrusted_advice_end" ,
229+ & format_args ! ( "{:#X}" , self . untrusted_advice_end) ,
230+ )
167231 . field ( "input_start" , & format_args ! ( "{:#X}" , self . input_start) )
168232 . field ( "input_end" , & format_args ! ( "{:#X}" , self . input_end) )
169233 . field ( "output_start" , & format_args ! ( "{:#X}" , self . output_start) )
@@ -200,6 +264,8 @@ impl MemoryLayout {
200264 }
201265 } // Must be 8-byte aligned
202266
267+ let max_trusted_advice_size = align_up ( config. max_trusted_advice_size , 8 ) ;
268+ let max_untrusted_advice_size = align_up ( config. max_untrusted_advice_size , 8 ) ;
203269 let max_input_size = align_up ( config. max_input_size , 8 ) ;
204270 let max_output_size = align_up ( config. max_output_size , 8 ) ;
205271 let stack_size = align_up ( config. stack_size , 8 ) ;
@@ -208,7 +274,9 @@ impl MemoryLayout {
208274 // Adds 16 to account for panic bit and termination bit
209275 // (they each occupy one full 8-byte word)
210276 let io_region_bytes = max_input_size
211- . checked_add ( max_output_size)
277+ . checked_add ( max_trusted_advice_size)
278+ . and_then ( |s| s. checked_add ( max_untrusted_advice_size) )
279+ . and_then ( |s| s. checked_add ( max_output_size) )
212280 . and_then ( |s| s. checked_add ( 16 ) )
213281 . expect ( "I/O region size overflow" ) ;
214282
@@ -220,9 +288,20 @@ impl MemoryLayout {
220288 let io_bytes = io_region_words
221289 . checked_mul ( 8 )
222290 . expect ( "I/O region byte count overflow" ) ;
223- let input_start = RAM_START_ADDRESS
291+
292+ let trusted_advice_start = RAM_START_ADDRESS
224293 . checked_sub ( io_bytes)
225294 . expect ( "I/O region exceeds RAM_START_ADDRESS" ) ;
295+ let trusted_advice_end = trusted_advice_start
296+ . checked_add ( max_trusted_advice_size)
297+ . expect ( "trusted_advice_end overflow" ) ;
298+
299+ let untrusted_advice_start = trusted_advice_end;
300+ let untrusted_advice_end = untrusted_advice_start
301+ . checked_add ( max_untrusted_advice_size)
302+ . expect ( "untrusted_advice_end overflow" ) ;
303+
304+ let input_start = untrusted_advice_end;
226305 let input_end = input_start
227306 . checked_add ( max_input_size)
228307 . expect ( "input_end overflow" ) ;
@@ -235,6 +314,7 @@ impl MemoryLayout {
235314 let io_end = termination. checked_add ( 8 ) . expect ( "io_end overflow" ) ;
236315
237316 let program_size = config. program_size . unwrap ( ) ;
317+
238318 // stack grows downwards (decreasing addresses) from the bytecode_end + stack_size up to bytecode_end
239319 let stack_end = RAM_START_ADDRESS
240320 . checked_add ( program_size)
@@ -250,8 +330,14 @@ impl MemoryLayout {
250330
251331 Self {
252332 program_size,
333+ max_trusted_advice_size,
334+ max_untrusted_advice_size,
253335 max_input_size,
254336 max_output_size,
337+ trusted_advice_start,
338+ trusted_advice_end,
339+ untrusted_advice_start,
340+ untrusted_advice_end,
255341 input_start,
256342 input_end,
257343 output_start,
0 commit comments