ziskos 1.1.0-alpha

Guest runtime and entrypoint for programs targeting the ZisK zkVM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use bytes::{Bytes, BytesMut};
use std::io::{self, Write};
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::thread;
use std::time::Duration;
use zisk_definitions::{CTRL_END, CTRL_START, HINT_INPUT};

pub const DEFAULT_BUFFER_LEN: usize = 1 << 20; // 1 MiB
                                               // TODO: Set MAX_WRITE_LEN based on writer type (file or socket)
pub const MAX_WRITER_LEN: usize = 128 * 1024; // 128KB is the max write size for Unix sockets
pub const WRITE_BUFFER_FLUSH_LEN: usize = 64 * 1024; // Flush writer buffer once it exceeds 64KB
const MAX_INPUT_DATA_CHUNK: usize = 128 * 1024 - 8; // Max input data chunk size is 128KB minus 8 bytes for the header (length)
pub const HEADER_LEN: usize = 8;

pub struct HintBuffer {
    precompiles: Mutex<HintBufferInner>,
    input_data: Mutex<HintBufferInner>,
    not_empty: Condvar,
    closed: Mutex<bool>,
    paused: Mutex<bool>,
    ending: Mutex<bool>,
}

struct HintBufferInner {
    buf: BytesMut,
    commit_pos: usize,
}

pub struct WriteBuffer<'a> {
    hb: &'a HintBuffer,
    g: MutexGuard<'a, HintBufferInner>,
}

pub fn build_hint_buffer() -> Arc<HintBuffer> {
    Arc::new(HintBuffer {
        precompiles: Mutex::new(HintBufferInner {
            buf: BytesMut::with_capacity(DEFAULT_BUFFER_LEN),
            commit_pos: 0,
        }),
        input_data: Mutex::new(HintBufferInner {
            buf: BytesMut::with_capacity(DEFAULT_BUFFER_LEN),
            commit_pos: 0,
        }),
        not_empty: Condvar::new(),
        closed: Mutex::new(true),
        paused: Mutex::new(false),
        ending: Mutex::new(false),
    })
}

impl HintBufferInner {
    #[inline(always)]
    fn write_bytes(&mut self, src: &[u8]) {
        self.buf.extend_from_slice(src);
    }

    #[inline(always)]
    fn commit(&mut self) {
        self.commit_pos = self.buf.len();
    }
}

impl HintBuffer {
    pub fn close(&self) {
        *self.closed.lock().unwrap() = true;
        self.not_empty.notify_all();
    }

    pub fn reset(&self) {
        let mut g = self.precompiles.lock().unwrap();
        g.buf.clear();
        g.commit_pos = 0;
        let mut i = self.input_data.lock().unwrap();
        i.buf.clear();
        i.commit_pos = 0;

        *self.closed.lock().unwrap() = false;
        *self.paused.lock().unwrap() = false;
        *self.ending.lock().unwrap() = false;
        self.not_empty.notify_all();
    }

    pub fn mark_end(&self) {
        *self.ending.lock().unwrap() = true;
        self.not_empty.notify_all();
    }

    #[inline(always)]
    pub fn pause(&self) {
        *self.paused.lock().unwrap() = true;
    }

    #[inline(always)]
    pub fn resume(&self) {
        *self.paused.lock().unwrap() = false;
    }

    #[inline(always)]
    pub fn is_paused(&self) -> bool {
        *self.paused.lock().unwrap()
    }

    #[inline(always)]
    pub fn is_enabled(&self) -> bool {
        let paused = *self.paused.lock().unwrap();
        let closed = *self.closed.lock().unwrap();
        !paused && !closed
    }

    #[inline(always)]
    pub fn begin_hint(&self, hint_id: u32, len: usize, is_result: bool) -> WriteBuffer<'_> {
        let header = ((((if is_result { 0x8000_0000u64 } else { 0 }) | hint_id as u64) << 32)
            | (len as u64))
            .to_le_bytes();

        let mut g = self.precompiles.lock().unwrap();
        g.write_bytes(&header);

        WriteBuffer { hb: self, g }
    }

    #[inline(always)]
    pub fn write_hint_start(&self) {
        let w = self.begin_hint(CTRL_START, 0, false);
        w.commit();
    }

    #[inline(always)]
    pub fn begin_input_data(&self) -> WriteBuffer<'_> {
        WriteBuffer { hb: self, g: self.input_data.lock().unwrap() }
    }

    pub fn drain_to_writer<W, D>(
        &self,
        writer: &mut W,
        mut debug_writer: Option<&mut D>,
        write_flush_threshold: usize,
    ) -> io::Result<()>
    where
        W: Write + ?Sized,
        D: Write + ?Sized,
    {
        fn write_with_retries<W: Write + ?Sized>(
            writer: &mut W,
            buf: &[u8],
            retries: usize,
            base_delay: Duration,
        ) -> io::Result<()> {
            let mut written = 0;
            let mut attempt = 0;
            while written < buf.len() {
                match writer.write(&buf[written..]) {
                    Ok(0) => {
                        return Err(io::Error::new(
                            io::ErrorKind::WriteZero,
                            "write_with_retries: write returned 0 bytes",
                        ));
                    }
                    Ok(n) => {
                        written += n;
                        if attempt > 0 {
                            println!(
                                "write_with_retries: write succeeded after {} attempts",
                                attempt
                            );
                        }
                        attempt = 0;
                    }

                    // Any error: retry with backoff.
                    Err(e) => {
                        if attempt >= retries {
                            return Err(io::Error::new(
                                e.kind(),
                                format!(
                                    "write_with_retries: max retries ({}) reached, kind={:?}, os_error={:?}: {}",
                                    retries,
                                    e.kind(),
                                    e.raw_os_error(),
                                    e
                                ),
                            ));
                        }
                        if attempt == 0 {
                            println!(
                                "write_with_retries: transient error kind={:?}, os_error={:?}: {}, retrying",
                                e.kind(),
                                e.raw_os_error(),
                                e
                            );
                        }
                        let delay = base_delay * (attempt as u32 + 1);
                        thread::sleep(delay);
                        attempt += 1;
                    }
                }
            }
            Ok(())
        }

        // Write hints from the buffer to the writer and optionally to a debug writer
        let mut write_all = |buf: &[u8]| -> io::Result<()> {
            write_with_retries(writer, buf, 10, Duration::from_millis(50))?;

            if let Some(debug_writer) = debug_writer.as_deref_mut() {
                write_with_retries(debug_writer, buf, 10, Duration::from_millis(50))?;
            }

            Ok(())
        };

        fn write_buf<F>(write_all: &mut F, buf: &mut Vec<u8>) -> io::Result<()>
        where
            F: FnMut(&[u8]) -> io::Result<()>,
        {
            if buf.is_empty() {
                return Ok(());
            }

            debug_assert!(buf.len() <= MAX_WRITER_LEN);
            write_all(buf)?;
            buf.clear();

            Ok(())
        }

        fn flush_with_retries<W: Write + ?Sized>(
            writer: &mut W,
            retries: usize,
            base_delay: Duration,
        ) -> io::Result<()> {
            let mut attempt = 0;
            loop {
                match writer.flush() {
                    Ok(()) => {
                        if attempt > 0 {
                            println!("flush_with_retries: succeeded after {} attempts", attempt);
                        }
                        return Ok(());
                    }

                    // Any error: retry with backoff.
                    Err(e) => {
                        if attempt >= retries {
                            return Err(io::Error::new(
                                e.kind(),
                                format!(
                                    "flush_with_retries: max retries ({}) reached, kind={:?}, os_error={:?}: {}",
                                    retries,
                                    e.kind(),
                                    e.raw_os_error(),
                                    e
                                ),
                            ));
                        }
                        if attempt == 0 {
                            println!(
                                "flush_with_retries: transient error kind={:?}, os_error={:?}: {}, retrying",
                                e.kind(),
                                e.raw_os_error(),
                                e
                            );
                        }
                        let delay = base_delay * (attempt as u32 + 1);
                        thread::sleep(delay);
                        attempt += 1;
                    }
                }
            }
        }

        let mut flush_threshold = std::cmp::min(write_flush_threshold, MAX_WRITER_LEN);
        flush_threshold = flush_threshold.max(1);

        let mut buf = Vec::with_capacity(flush_threshold);
        'drain: loop {
            // Get chunk of hints to write from HintBuffer (under lock)
            let chunk: Bytes = loop {
                let mut g = self.precompiles.lock().unwrap();
                let mut i = self.input_data.lock().unwrap();
                let closed = *self.closed.lock().unwrap();

                {
                    let mut ending = self.ending.lock().unwrap();
                    if g.commit_pos == 0 && i.commit_pos == 0 && *ending {
                        *ending = false;
                        *self.closed.lock().unwrap() = true;
                        let header = ((CTRL_END as u64) << 32).to_le_bytes();
                        break Bytes::copy_from_slice(&header);
                    }
                }

                if g.commit_pos == 0 && i.commit_pos == 0 && !closed {
                    drop(i); // Release input_data lock before waiting
                    g = self.not_empty.wait(g).unwrap();
                    continue; // Re-acquire both locks in the next iteration
                }

                if g.commit_pos == 0 && i.commit_pos == 0 && closed {
                    break 'drain;
                }

                break if g.commit_pos > 0 {
                    let n = g.commit_pos;
                    g.commit_pos = 0;
                    g.buf.split_to(n).freeze()
                } else {
                    let n = i.commit_pos.min(MAX_INPUT_DATA_CHUNK);
                    i.commit_pos -= n;
                    let input_chunk = i.buf.split_to(n);
                    let header = (((HINT_INPUT as u64) << 32) | n as u64).to_le_bytes();
                    let mut chunk = BytesMut::with_capacity(HEADER_LEN + n);
                    chunk.extend_from_slice(&header);
                    chunk.unsplit(input_chunk);
                    chunk.freeze()
                };
            };

            // Write hints from chunk without holding the lock
            let mut chunk_pos = 0usize;
            let chunk_len = chunk.len();
            let chunk_base = chunk.as_ptr();

            while chunk_pos < chunk_len {
                let hint_header = unsafe {
                    let header_bytes = core::slice::from_raw_parts(chunk_base.add(chunk_pos), 8);
                    u64::from_le_bytes(header_bytes.try_into().unwrap())
                };

                let hint_data_len = (hint_header & 0xFFFF_FFFF) as usize;
                let pad = (8 - (hint_data_len & 7)) & 7;
                let hint_len = HEADER_LEN + hint_data_len + pad;

                #[cfg(zisk_hints_metrics)]
                {
                    use std::hint;

                    let hint_id = (hint_header >> 32) as u32 & 0x7FFF_FFFF;
                    crate::hints::metrics::inc_hint_count(hint_id, hint_len as u64);
                }

                // If single hint exceeds MAX_WRITER_LEN, write it in chunks directly
                if hint_len > MAX_WRITER_LEN {
                    write_buf(&mut write_all, &mut buf).map_err(|e| {
                        io::Error::new(e.kind(), format!("write_buf before oversized hint: {}", e))
                    })?;

                    let mut hint_pos = 0usize;
                    while hint_pos < hint_len {
                        let chunk_size = std::cmp::min(MAX_WRITER_LEN, hint_len - hint_pos);
                        let hint_bytes: &[u8] = unsafe {
                            core::slice::from_raw_parts(
                                chunk_base.add(chunk_pos + hint_pos),
                                chunk_size,
                            )
                        };

                        write_all(hint_bytes)?;

                        hint_pos += chunk_size;
                    }

                    chunk_pos += hint_len;
                    continue;
                }

                let hint_bytes: &[u8] =
                    unsafe { core::slice::from_raw_parts(chunk_base.add(chunk_pos), hint_len) };

                if buf.len() + hint_len > MAX_WRITER_LEN {
                    write_buf(&mut write_all, &mut buf).map_err(|e| {
                        io::Error::new(
                            e.kind(),
                            format!("write_buf on buffer full before hint: {}", e),
                        )
                    })?;
                }

                buf.extend_from_slice(hint_bytes);

                chunk_pos += hint_len;
            }

            if buf.len() >= flush_threshold {
                write_buf(&mut write_all, &mut buf).map_err(|e| {
                    io::Error::new(e.kind(), format!("write_buf on flush threshold: {}", e))
                })?;
            }
        }

        write_buf(&mut write_all, &mut buf)
            .map_err(|e| io::Error::new(e.kind(), format!("write_buf final drain: {}", e)))?;

        // Flush the writer and debug writer at the end
        flush_with_retries(writer, 10, Duration::from_millis(50))?;
        if let Some(debug_writer) = debug_writer.as_deref_mut() {
            flush_with_retries(debug_writer, 10, Duration::from_millis(50))?;
        }

        Ok(())
    }
}

impl<'a> WriteBuffer<'a> {
    #[inline(always)]
    pub fn write_data_ptr(&mut self, data: *const u8, len: usize) {
        if len == 0 {
            return;
        }
        debug_assert!(!data.is_null(), "write_data_ptr called with null data pointer");
        let payload = unsafe { std::slice::from_raw_parts(data, len) };
        self.g.write_bytes(payload);
    }

    #[inline(always)]
    pub fn write_data_slice(&mut self, payload: &[u8]) {
        if payload.is_empty() {
            return;
        }
        self.g.write_bytes(payload);
    }

    #[inline(always)]
    pub fn commit(mut self) {
        self.g.commit();

        drop(self.g);
        self.hb.not_empty.notify_one();
    }
}