Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect read_slice impl for ReadAdapter #309

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions air/src/proof/ood_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,11 @@ impl Deserializable for OodFrame {
// ================================================================================================

/// Stores the trace evaluations at `z` and `gz`, where `z` is a random Field element in
/// `current_row` and `next_row`, respectively. If the Air contains a Lagrange kernel auxiliary
/// column, then that column interpolated polynomial will be evaluated at `z`, `gz`, `g^2 z`, ...
/// `g^(2^(v-1)) z`, where `v == log(trace_len)`, and stored in `lagrange_kernel_frame`.
/// `current_row` and `next_row`, respectively.
///
/// If the Air contains a Lagrange kernel auxiliary column, then that column interpolated polynomial
/// will be evaluated at `z`, `gz`, `g^2 z`, ... `g^(2^(v-1)) z`, where `v == log(trace_len)`, and
/// stored in `lagrange_kernel_frame`.
pub struct TraceOodFrame<E: FieldElement> {
current_row: Vec<E>,
next_row: Vec<E>,
Expand Down
8 changes: 5 additions & 3 deletions crypto/src/merkle/concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ pub const MIN_CONCURRENT_LEAVES: usize = 1024;
// PUBLIC FUNCTIONS
// ================================================================================================

/// Builds all internal nodes of the Merkle using all available threads and stores the
/// results in a single vector such that root of the tree is at position 1, nodes immediately
/// under the root is at positions 2 and 3 etc.
/// Builds all internal nodes of the Merkle.
///
/// All available threads are used, and the results are stored in a single vector, such that
/// the root of the tree is at position 1, and nodes immediately under the root are at positions
/// 2 and 3, and so on.
pub fn build_merkle_nodes<H: Hasher>(leaves: &[H::Digest]) -> Vec<H::Digest> {
let n = leaves.len() / 2;

Expand Down
9 changes: 5 additions & 4 deletions examples/src/utils/rescue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ pub const RATE_WIDTH: usize = 4;
/// Two elements (32-bytes) are returned as digest.
const DIGEST_SIZE: usize = 2;

/// The number of rounds is set to 7 to provide 128-bit security level with 40% security margin;
/// computed using algorithm 7 from <https://eprint.iacr.org/2020/1143.pdf>
/// security margin here differs from Rescue Prime specification which suggests 50% security
/// margin (and would require 8 rounds) primarily to make AIR a bit simpler.
/// The number of rounds is set to 7 to provide 128-bit security level with 40% security margin.
///
/// Computed using algorithm 7 from <https://eprint.iacr.org/2020/1143.pdf>, the security margin
/// here differs from Rescue Prime specification which suggests 50% security margin (and would
/// require 8 rounds) primarily to make AIR a bit simpler.
pub const NUM_ROUNDS: usize = 7;

/// Minimum cycle length required to describe Rescue permutation.
Expand Down
2 changes: 2 additions & 0 deletions math/src/field/f64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

//! An implementation of a 64-bit STARK-friendly prime field with modulus $2^{64} - 2^{32} + 1$
//! using Montgomery representation.
//!
//! Our implementation follows <https://eprint.iacr.org/2022/274.pdf> and is constant-time.
//!
//! This field supports very fast modular arithmetic and has a number of other attractive
//! properties, including:
//!
//! * Multiplication of two 32-bit values does not overflow field modulus.
//! * Field arithmetic in this field can be implemented using a few 32-bit addition, subtractions,
//! and shifts.
Expand Down
36 changes: 35 additions & 1 deletion utils/core/src/serde/byte_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,9 @@ impl<'a> ByteReader for ReadAdapter<'a> {
// this will return an error if we hit EOF first
self.buffer_at_least(len)?;

Ok(&self.buffer()[0..len])
let slice = &self.buf[self.pos..(self.pos + len)];
self.pos += len;
Ok(slice)
}

#[inline]
Expand Down Expand Up @@ -740,4 +742,36 @@ mod tests {

assert_eq!(adapter.read_usize(), Ok(VALUE));
}

#[test]
fn read_adapter_for_file() {
use std::fs::File;

use crate::ByteWriter;

let path = std::env::temp_dir().join("read_adapter_for_file.bin");

// Encode some data to a buffer, then write that buffer to a file
{
let mut buf = Vec::<u8>::with_capacity(256);
buf.write_bytes(b"MAGIC\0");
buf.write_bool(true);
buf.write_u32(0xbeef);
buf.write_usize(0xfeed);
buf.write_u16(0x5);

std::fs::write(&path, &buf).unwrap();
}

// Open the file, and try to decode the encoded items
let mut file = File::open(&path).unwrap();
let mut reader = ReadAdapter::new(&mut file);
assert_eq!(reader.peek_u8().unwrap(), b'M');
assert_eq!(reader.read_slice(6).unwrap(), b"MAGIC\0");
assert!(reader.read_bool().unwrap());
assert_eq!(reader.read_u32().unwrap(), 0xbeef);
assert_eq!(reader.read_usize().unwrap(), 0xfeed);
assert_eq!(reader.read_u16().unwrap(), 0x5);
assert!(!reader.has_more_bytes(), "expected there to be no more data in the input");
}
}