Skip to content

Commit

Permalink
new numeric parser, closes #609, #1161, #1169 (#1171)
Browse files Browse the repository at this point in the history
* new numeric parser, closes #609, #1161, #1169

- merges previous parse_num and parse_uint into a single parse_numeric
- removes bounds check on Num, so that parsing numbers larger than the
  field will modularly wrap
- corrects uint parsing so that we never accidentally parse uints as
  nums
- implements a placeholder syntax for i64 for parsing twos-complement
  numbers (i.e. -1i64 parses to u64::MAX). In future this syntax should
  parse to a new literal type
- add placeholder parsing for u8, u16, u32, u128, i8, i16, i32, i128
  literals so that we correctly error that these have yet to be
  implemented
- adds unit tests to ensure correctness of the above

* clippy

* clippy part II: revenge of the linter

* remove unused byte parsers and associated tests

* comment out printlns
  • Loading branch information
johnchandlerburnham authored Feb 25, 2024
1 parent d91b5e2 commit 7e95ec4
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 156 deletions.
72 changes: 0 additions & 72 deletions src/parser/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,75 +157,3 @@ pub fn parse_litbase_digits<F: LurkField>(
LitBase::Hex => parse_hex_digits()(from),
})
}

pub fn parse_litbase_be_bytes<F: LurkField>(
base: LitBase,
) -> impl Fn(Span<'_>) -> ParseResult<'_, F, Vec<u8>> {
move |from: Span<'_>| {
let (i, o): (Span<'_>, String) = parse_litbase_digits(base)(from)?;
match base_x::decode(base.base_digits(), &o) {
Ok(bytes) => Ok((i, bytes)),
Err(_) => Err(nom::Err::Error(ParseError::new(
i,
ParseErrorKind::InvalidBaseEncoding(base),
))),
}
}
}

pub fn parse_litbase_le_bytes<F: LurkField>(
base: LitBase,
) -> impl Fn(Span<'_>) -> ParseResult<'_, F, Vec<u8>> {
move |from: Span<'_>| {
let (i, bytes) = parse_litbase_be_bytes(base)(from)?;
Ok((i, bytes.into_iter().rev().collect()))
}
}

#[cfg(test)]
pub mod tests {
use halo2curves::bn256::Fr;
use nom::Parser;

use super::*;

fn test_parse<'a, P>(mut p: P, i: &'a str, expected: Option<Vec<u8>>)
where
P: Parser<Span<'a>, Vec<u8>, ParseError<Span<'a>, Fr>>,
{
match (expected, p.parse(Span::<'a>::new(i))) {
(Some(expected), Ok((_i, x))) if x == expected => (),
(Some(expected), Ok((i, x))) => {
println!("input: {i:?}");
println!("expected: {expected:?}");
println!("detected: {x:?}");
unreachable!("unexpected parse result")
}
(Some(..), Err(e)) => {
println!("{e}");
unreachable!("unexpected parse result")
}
(None, Ok((i, x))) => {
println!("input: {i:?}");
println!("expected parse error");
println!("detected: {x:?}");
unreachable!("unexpected parse result")
}
(None, Err(_e)) => (),
}
}

#[test]
fn unit_parse_litbase_bytes_endianesss() {
test_parse(
parse_litbase_le_bytes::<Fr>(LitBase::Hex),
"1234567890",
Some(vec![0x90, 0x78, 0x56, 0x34, 0x12]),
);
test_parse(
parse_litbase_be_bytes::<Fr>(LitBase::Hex),
"1234567890",
Some(vec![0x12, 0x34, 0x56, 0x78, 0x90]),
)
}
}
4 changes: 4 additions & 0 deletions src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum ParseErrorKind<F: LurkField> {
InvalidChar(String),
Nom(ErrorKind),
InterningError(String),
Custom(String),
}

impl<F: LurkField> fmt::Display for ParseErrorKind<F> {
Expand All @@ -27,6 +28,9 @@ impl<F: LurkField> fmt::Display for ParseErrorKind<F> {
Self::ParseIntErr(e) => {
write!(f, "Error parsing number: {e}")
}
Self::Custom(e) => {
write!(f, "Error: {e}")
}
e => write!(f, "internal parser error {e:?}"),
}
}
Expand Down
Loading

1 comment on commit 7e95ec4

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Table of Contents

Overview

This benchmark report shows the Fibonacci GPU benchmark.
NVIDIA L4
Intel(R) Xeon(R) CPU @ 2.20GHz
32 vCPUs
125 GB RAM
Workflow run: https://github.com/lurk-lab/lurk-rs/actions/runs/8035099416

Benchmark Results

LEM Fibonacci Prove - rc = 100

ref=d91b5e27e526632aa56e3b1108012c232726b165 ref=7e95ec4ee868b3f0e52aa3bd0418db87a107b247
num-100 1.46 s (✅ 1.00x) 1.46 s (✅ 1.00x faster)
num-200 2.79 s (✅ 1.00x) 2.79 s (✅ 1.00x slower)

LEM Fibonacci Prove - rc = 600

ref=d91b5e27e526632aa56e3b1108012c232726b165 ref=7e95ec4ee868b3f0e52aa3bd0418db87a107b247
num-100 1.81 s (✅ 1.00x) 1.84 s (✅ 1.01x slower)
num-200 3.02 s (✅ 1.00x) 3.03 s (✅ 1.00x slower)

Made with criterion-table

Please sign in to comment.