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

feat: rand feature #12

Open
wants to merge 101 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
0023bec
downgrade
kevjue Jan 31, 2024
b06a99b
more downgrade
kevjue Jan 31, 2024
5d9e475
commented out usage of rand dependency
kevjue Feb 1, 2024
baa5a51
commenting out more random
kevjue Feb 1, 2024
a1c6512
more changes
kevjue Feb 1, 2024
38329e5
made rand feature for baby-bear
kevjue Feb 1, 2024
7963962
fix
kevjue Feb 1, 2024
b5b1061
made rand feature for field and goldilocks
kevjue Feb 1, 2024
bc37c25
added rand feature to matrix and mersenne
kevjue Feb 1, 2024
b44f9c4
added rand feature to posiedon2
kevjue Feb 1, 2024
0a9df0c
brakedown using rand features
kevjue Feb 1, 2024
9163804
uncommented a line
kevjue Feb 1, 2024
49bd30a
Merge branch 'main' into kevjue/rand_feature
kevjue Feb 1, 2024
db80dc2
added serialization support
kevjue Feb 5, 2024
142ab24
hacks
kevjue Feb 5, 2024
2787000
added some profiling for verify_multi_batches
kevjue Feb 5, 2024
a44cdf5
println
kevjue Feb 5, 2024
4140028
using std for fri
kevjue Feb 5, 2024
f47ab8b
more tracking
kevjue Feb 6, 2024
aef4be0
cycle tracking for hash
kevjue Feb 6, 2024
846cb92
more profiling
kevjue Feb 6, 2024
b532df6
some prints
kevjue Feb 6, 2024
f6e5821
cleaned up some tracking
kevjue Feb 6, 2024
2d26c51
tracking of permutation
kevjue Feb 6, 2024
9e28ff7
mds tracking
kevjue Feb 6, 2024
8b43a05
fixed the tracking of poseidon
kevjue Feb 6, 2024
396d869
fixed tracking
kevjue Feb 6, 2024
0f86dbe
merged main
kevjue Feb 6, 2024
2e18952
added more tracking in compress
kevjue Feb 6, 2024
9769bf2
compress_copy_from_slice
kevjue Feb 6, 2024
a817ea6
revert serialization changes
kevjue Feb 6, 2024
659097c
Revert "added serialization support"
kevjue Feb 6, 2024
717da1b
some cleanup
kevjue Feb 6, 2024
a6d6282
merged main
kevjue Feb 8, 2024
5aed44d
changes for 1.69
kevjue Feb 8, 2024
ce79482
more tracking
kevjue Feb 8, 2024
79ab714
tracking
kevjue Feb 8, 2024
d5b2878
hack
kevjue Feb 8, 2024
a04b853
hopeuflly fixe
kevjue Feb 8, 2024
cb9a0b5
fix
kevjue Feb 8, 2024
924ff2c
fix
kevjue Feb 8, 2024
81f1121
add in hash for poseidon permute_mut
kevjue Feb 8, 2024
22ac86d
added the IN_HASH flag
kevjue Feb 8, 2024
8355b02
poseidon2 external round
kevjue Feb 8, 2024
ba0b258
baby bear
kevjue Feb 8, 2024
4ea44b4
func counts
kevjue Feb 8, 2024
ec1ee75
removed counter
kevjue Feb 8, 2024
06faee3
comment out cycle tracker
kevjue Feb 8, 2024
f234447
removed external round stracker
kevjue Feb 8, 2024
92e532e
unconstrained
kevjue Feb 8, 2024
3a8d172
a print
kevjue Feb 9, 2024
f924861
read_hint_slice
kevjue Feb 9, 2024
8b48b5a
debugging
kevjue Feb 9, 2024
2f7beaa
slice bytes
kevjue Feb 9, 2024
9cd7684
zkvm target
kevjue Feb 9, 2024
093d0f5
fix bug
kevjue Feb 9, 2024
c9760ac
comment out unconstrained
kevjue Feb 9, 2024
ce1d110
unconstrained baby bear try_inverse
kevjue Feb 9, 2024
ea2faee
added babybear add, mul, sub, div unconstrained
kevjue Feb 9, 2024
5207fc5
some fixes
kevjue Feb 9, 2024
5696d0f
some fixes
kevjue Feb 9, 2024
81c6c1c
removed unconstrained
kevjue Feb 9, 2024
164f2da
fix
kevjue Feb 9, 2024
fff0c36
bb inverse as unconstrained
kevjue Feb 9, 2024
067c90f
test
kevjue Feb 9, 2024
7cf593a
test
kevjue Feb 9, 2024
53e250e
fix
kevjue Feb 9, 2024
703ac42
fix
kevjue Feb 9, 2024
55228f1
fix
kevjue Feb 9, 2024
f0bf863
fixes
kevjue Feb 9, 2024
da73d0d
fix
kevjue Feb 9, 2024
2892940
removed unconstrained from inverse
kevjue Feb 9, 2024
d09b100
only unconstrain add, mul ,sub
kevjue Feb 9, 2024
9b78fe7
comment out unconstrained
kevjue Feb 9, 2024
f8f672c
fix
kevjue Feb 9, 2024
380865e
fix
kevjue Feb 9, 2024
1f03be8
uncomment unconstrained
kevjue Feb 9, 2024
11795af
comment out everything
kevjue Feb 9, 2024
8c73158
add
kevjue Feb 9, 2024
eb91e43
fix
kevjue Feb 9, 2024
178eb95
unconstrain add
kevjue Feb 9, 2024
b40a0ca
add constrained
kevjue Feb 9, 2024
7ee3e5f
unconstrained hint_slice
kevjue Feb 9, 2024
24603a8
all constrained
kevjue Feb 9, 2024
68788bc
unconstrain hint_slice
kevjue Feb 9, 2024
d99ff8a
unconstrained add, sub, mul
kevjue Feb 9, 2024
99fb6ba
constrained
kevjue Feb 9, 2024
5d6378c
unconstrained hint_slice
kevjue Feb 9, 2024
0092c67
unconstrained
kevjue Feb 9, 2024
7c68440
uncontraint just the hint_slice
kevjue Feb 9, 2024
5304632
constrain everything
kevjue Feb 9, 2024
e0aa566
unconstrain hint_slice
kevjue Feb 9, 2024
b66094d
unconstrain all
kevjue Feb 9, 2024
8b18f91
all contrained
kevjue Feb 9, 2024
72f789d
uncomment not zkvm
kevjue Feb 9, 2024
80423d9
all is uncontrained
kevjue Feb 9, 2024
8e26482
only hint_slice is unconstrained
kevjue Feb 9, 2024
43005a8
remove cycle-tracker prints
kevjue Feb 10, 2024
b44b84b
unconstain all
kevjue Feb 10, 2024
0b694b6
add constrained
kevjue Feb 10, 2024
46bfd9d
unconstrained hint_slice
kevjue Feb 10, 2024
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
7 changes: 6 additions & 1 deletion baby-bear/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ license = "MIT OR Apache-2.0"

[dependencies]
p3-field = { path = "../field" }
rand = "0.8.5"
rand = { version = "0.8.5", optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"] }
lazy_static = "1.4"
succinct-zkvm = { git="https://github.com/succinctlabs/vm.git", branch="kevjue/recursive_verifier_profiler" }

[dev-dependencies]
p3-field-testing = { path = "../field-testing" }
criterion = "0.5.1"
rand_chacha = "0.3.1"

[features]
rand = ["dep:rand"]

[[bench]]
name = "inverse"
harness = false
Expand Down
4 changes: 4 additions & 0 deletions baby-bear/src/aarch64_neon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use core::mem::transmute;
use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};

use p3_field::{AbstractField, Field, PackedField};

#[cfg(feature = "rand")]
use rand::distributions::{Distribution, Standard};
#[cfg(feature = "rand")]
use rand::Rng;

use crate::BabyBear;
Expand Down Expand Up @@ -540,6 +543,7 @@ impl Sub<PackedBabyBearNeon> for BabyBear {
}
}

#[cfg(feature = "perf")]
impl Distribution<PackedBabyBearNeon> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> PackedBabyBearNeon {
Expand Down
178 changes: 166 additions & 12 deletions baby-bear/src/baby_bear.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
use core::fmt::{self, Debug, Display, Formatter};
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
use std::collections::HashMap;

use p3_field::{
exp_1725656503, exp_u64_by_squaring, AbstractField, Field, PrimeField, PrimeField32,
PrimeField64, TwoAdicField,
};

use lazy_static::lazy_static;
use succinct_zkvm::{io, unconstrained};
use std::sync::Mutex;

#[cfg(feature = "rand")]
use rand::distributions::{Distribution, Standard};
#[cfg(feature = "rand")]
use rand::Rng;
use serde::{Deserialize, Serialize};

lazy_static! {
// pub static ref IN_HASH: Mutex<bool> = Mutex::new(false);
// pub static ref FUNC_COUNTS: Mutex<HashMap<String, u32>> = Mutex::new(HashMap::new());
}

/// The Baby Bear prime
const P: u32 = 0x78000001;

Expand Down Expand Up @@ -76,6 +89,7 @@ impl Debug for BabyBear {
}
}

#[cfg(feature = "rand")]
impl Distribution<BabyBear> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> BabyBear {
Expand Down Expand Up @@ -199,10 +213,25 @@ impl Field for BabyBear {
return None;
}

// let in_hash = IN_HASH.lock().unwrap();
// if !*in_hash {
// println!("cycle-tracker-start: BabyBear_inv");
// }
// drop(in_hash);
// println!("cycle-tracker-start: BabyBear_inv");


// From Fermat's little theorem, in a prime field `F_p`, the inverse of `a` is `a^(p-2)`.
// Here p-2 = 2013265919 = 1110111111111111111111111111111_2.
// Uses 30 Squares + 7 Multiplications => 37 Operations total.

// let in_hash = IN_HASH.lock().unwrap();
// if !*in_hash {
// println!("cycle-tracker-end: BabyBear_inv");
// }
// drop(in_hash);
// println!("cycle-tracker-end: BabyBear_inv");

let p1 = *self;
let p100000000 = p1.exp_power_of_2(8);
let p100000001 = p100000000 * p1;
Expand Down Expand Up @@ -303,12 +332,51 @@ impl Add for BabyBear {

#[inline]
fn add(self, rhs: Self) -> Self {
let mut sum = self.value + rhs.value;
let (corr_sum, over) = sum.overflowing_sub(P);
if !over {
sum = corr_sum;
// let in_hash = IN_HASH.lock().unwrap();
// if !*in_hash {
// println!("cycle-tracker-start: BabyBear_add");
// }
// let mut func_counts = FUNC_COUNTS.lock().unwrap();
// *func_counts
// .entry("add".to_string())
// .or_insert(0) += 1;
#[cfg(target_os = "zkvm")]
{
// unconstrained!
{
let mut sum = self.value + rhs.value;
let (corr_sum, over) = sum.overflowing_sub(P);
if !over {
sum = corr_sum;
}

unconstrained!
{
io::hint_slice(&sum.to_le_bytes());
}
}

let mut bytes: [u8; 4] = [0; 4];
io::read_hint_slice(&mut bytes);
Self { value: u32::from_le_bytes(bytes) }
}

// if !*in_hash {
// println!("cycle-tracker-end: BabyBear_add");
// }
// drop(in_hash);
// drop(func_counts);

#[cfg(not(target_os = "zkvm"))]
{
let mut sum = self.value + rhs.value;
let (corr_sum, over) = sum.overflowing_sub(P);
if !over {
sum = corr_sum;
}

Self { value: sum }
}
Self { value: sum }
}
}

Expand All @@ -331,10 +399,48 @@ impl Sub for BabyBear {

#[inline]
fn sub(self, rhs: Self) -> Self {
let (mut diff, over) = self.value.overflowing_sub(rhs.value);
let corr = if over { P } else { 0 };
diff = diff.wrapping_add(corr);
BabyBear { value: diff }
// let in_hash = IN_HASH.lock().unwrap();
// if !*in_hash {
// println!("cycle-tracker-start: BabyBear_sub");
// }
// let mut func_counts = FUNC_COUNTS.lock().unwrap();
// *func_counts
// .entry("sub".to_string())
// .or_insert(0) += 1;

#[cfg(target_os = "zkvm")]
{
// unconstrained!
{
let (mut diff, over) = self.value.overflowing_sub(rhs.value);
let corr = if over { P } else { 0 };
diff = diff.wrapping_add(corr);

unconstrained!
{
io::hint_slice(&diff.to_le_bytes());
}
}

let mut bytes: [u8; 4] = [0; 4];
io::read_hint_slice(&mut bytes);
Self {value: u32::from_le_bytes(bytes)}
}

// if !*in_hash {
// println!("cycle-tracker-end: BabyBear_sub");
// }
// drop(in_hash);
// drop(func_counts);

#[cfg(not(target_os = "zkvm"))]
{
let (mut diff, over) = self.value.overflowing_sub(rhs.value);
let corr = if over { P } else { 0 };
diff = diff.wrapping_add(corr);

Self { value: diff }
}
}
}

Expand All @@ -359,9 +465,45 @@ impl Mul for BabyBear {

#[inline]
fn mul(self, rhs: Self) -> Self {
let long_prod = self.value as u64 * rhs.value as u64;
Self {
value: monty_reduce(long_prod),
// let in_hash = IN_HASH.lock().unwrap();
// if !*in_hash {
// println!("cycle-tracker-start: BabyBear_mul");
// }
// let mut func_counts = FUNC_COUNTS.lock().unwrap();
// *func_counts
// .entry("mul".to_string())
// .or_insert(0) += 1;

#[cfg(target_os = "zkvm")]
{
// unconstrained!
{
let long_prod = self.value as u64 * rhs.value as u64;
let value = monty_reduce(long_prod);

unconstrained!
{
io::hint_slice(&value.to_le_bytes());
}
}

let mut bytes: [u8; 4] = [0; 4];
io::read_hint_slice(&mut bytes);
Self {value: u32::from_le_bytes(bytes)}
}
// if !*in_hash {
// println!("cycle-tracker-end: BabyBear_mul");
// }
// drop(in_hash);
// drop(func_counts);

#[cfg(not(target_os = "zkvm"))]
{
let long_prod = self.value as u64 * rhs.value as u64;
let ret = Self {
value: monty_reduce(long_prod),
};
ret
}
}
}
Expand All @@ -386,7 +528,19 @@ impl Div for BabyBear {
#[allow(clippy::suspicious_arithmetic_impl)]
#[inline]
fn div(self, rhs: Self) -> Self {
// let in_hash = IN_HASH.lock().unwrap();
// if !*in_hash {
// println!("cycle-tracker-start: BabyBear_div");
// }
// drop(in_hash);

self * rhs.inverse()

// let in_hash = IN_HASH.lock().unwrap();
// if !*in_hash {
// println!("cycle-tracker-end: BabyBear_div");
// }
// drop(in_hash);
}
}

Expand Down
2 changes: 0 additions & 2 deletions baby-bear/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![no_std]

extern crate alloc;

mod baby_bear;
Expand Down
4 changes: 2 additions & 2 deletions brakedown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ license = "MIT OR Apache-2.0"
p3-code = { path = "../code" }
p3-field = { path = "../field" }
p3-lde = { path = "../lde" }
p3-matrix = { path = "../matrix" }
p3-mersenne-31 = { path = "../mersenne-31" }
p3-matrix = { path = "../matrix", features = ["rand"] }
p3-mersenne-31 = { path = "../mersenne-31", features = ["rand"] }
p3-reed-solomon = { path = "../reed-solomon" }
rand = "0.8.5"
rand_chacha = "0.3.1"
Expand Down
5 changes: 4 additions & 1 deletion field/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ license = "MIT OR Apache-2.0"
p3-util = { path = "../util" }

itertools = "0.12.0"
rand = "0.8.5"
rand = { version = "0.8.5", optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"] }

[features]
rand = ["dep:rand"]
3 changes: 3 additions & 0 deletions field/src/extension/binomial_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use itertools::Itertools;
#[cfg(feature = "rand")]
use rand::distributions::Standard;
#[cfg(feature = "rand")]
use rand::prelude::Distribution;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -518,6 +520,7 @@ where
}
}

#[cfg(feature = "rand")]
impl<F: BinomiallyExtendable<D>, const D: usize> Distribution<BinomialExtensionField<F, D>>
for Standard
where
Expand Down
2 changes: 0 additions & 2 deletions fri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! An implementation of the FRI low-degree test (LDT).

#![no_std]

extern crate alloc;

mod config;
Expand Down
2 changes: 1 addition & 1 deletion fri/src/two_adic_pcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ impl<F: Field, EF: ExtensionField<F>> PowersReducer<F, EF> {
fn new(base: EF, max_width: usize) -> Self {
let powers: Vec<EF> = base
.powers()
.take(max_width.next_multiple_of(F::Packing::WIDTH))
.take(max_width + (max_width % F::Packing::WIDTH))
.collect();

let transposed_packed: Vec<Vec<F::Packing>> = transpose_vec(
Expand Down
2 changes: 1 addition & 1 deletion goldilocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
[dependencies]
p3-field = { path = "../field" }
p3-util = { path = "../util" }
rand = "0.8.5"
rand = { version = "0.8.5", optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"] }

[dev-dependencies]
Expand Down
4 changes: 4 additions & 0 deletions goldilocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use p3_field::{
TwoAdicField,
};
use p3_util::{assume, branch_hint};

#[cfg(feature = "rand")]
use rand::distributions::{Distribution, Standard};
#[cfg(feature = "rand")]
use rand::Rng;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -73,6 +76,7 @@ impl Debug for Goldilocks {
}
}

#[cfg(feature = "rand")]
impl Distribution<Goldilocks> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Goldilocks {
loop {
Expand Down
5 changes: 4 additions & 1 deletion matrix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ license = "MIT OR Apache-2.0"
p3-field = { path = "../field" }
p3-maybe-rayon = { path = "../maybe-rayon" }
p3-util = { path = "../util" }
rand = "0.8.5"
rand = { version = "0.8.5", optional = true }
serde = { version = "1.0", features = ["derive"] }

[features]
rand = ["dep:rand"]

[dev-dependencies]
criterion = "0.5.1"

Expand Down
Loading
Loading