Skip to content

Commit 9c4ef3b

Browse files
zhiyong1997siq1zhenfeizhang
authored
Public input rebase (#108)
* public input * fmt * clippy * M31+BN w/ public input Signed-off-by: Zhiyong Fang <[email protected]> * update downloading script Signed-off-by: Zhiyong Fang <[email protected]> * correct bn circuit Signed-off-by: Zhiyong Fang <[email protected]> * fix error Signed-off-by: Zhiyong Fang <[email protected]> * fmt Signed-off-by: Zhiyong Fang <[email protected]> * fix Signed-off-by: Zhiyong Fang <[email protected]> * Update expander_circuit.rs Signed-off-by: Zhiyong Fang <[email protected]> * check modulus when loading ecc circuit * correct url addresses for poseidon circuit --------- Signed-off-by: Zhiyong Fang <[email protected]> Co-authored-by: siq1 <[email protected]> Co-authored-by: zhenfei <[email protected]>
1 parent 4633850 commit 9c4ef3b

File tree

21 files changed

+777
-499
lines changed

21 files changed

+777
-499
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rand.workspace = true
2020
sha2.workspace = true
2121
halo2curves.workspace = true
2222
thiserror.workspace = true
23+
ethnum.workspace = true
2324

2425
# for the server
2526
bytes.workspace = true

arith/gf2/src/gf2.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
88

99
use arith::{field_common, FieldSerde, FieldSerdeResult};
1010
use arith::{Field, FieldForECC};
11-
use ark_std::iterable::Iterable;
1211

1312
pub const MOD: u32 = 2;
1413

@@ -36,15 +35,8 @@ impl FieldSerde for GF2 {
3635
}
3736

3837
#[inline(always)]
39-
fn try_deserialize_from_ecc_format<R: Read>(mut reader: R) -> FieldSerdeResult<Self> {
40-
let mut u = [0u8; 32];
41-
reader.read_exact(&mut u)?;
42-
43-
// FIXME:
44-
// assert!(u.iter().skip(1).all(|x| x == 0u8));
45-
assert!(u.iter().skip(4).all(|x| x == 0u8));
46-
47-
Ok(GF2 { v: u[0] % 2 })
38+
fn try_deserialize_from_ecc_format<R: Read>(reader: R) -> FieldSerdeResult<Self> {
39+
Self::deserialize_from(reader)
4840
}
4941
}
5042

arith/mersenne31/src/m31.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,9 @@ impl FieldSerde for M31 {
4747
Ok(M31 { v })
4848
}
4949

50-
#[inline]
51-
fn try_deserialize_from_ecc_format<R: Read>(mut reader: R) -> FieldSerdeResult<Self> {
52-
let mut buf = [0u8; 32];
53-
reader.read_exact(&mut buf)?;
54-
assert!(
55-
buf.iter().skip(4).all(|&x| x == 0),
56-
"non-zero byte found in witness byte"
57-
);
58-
Ok(Self::from(u32::from_le_bytes(buf[..4].try_into().unwrap())))
50+
#[inline(always)]
51+
fn try_deserialize_from_ecc_format<R: Read>(reader: R) -> FieldSerdeResult<Self> {
52+
Self::deserialize_from(reader)
5953
}
6054
}
6155

arith/src/bn254.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,9 @@ impl FieldSerde for Fr {
166166
}
167167
}
168168

169-
#[inline]
170-
fn try_deserialize_from_ecc_format<R: Read>(mut reader: R) -> FieldSerdeResult<Self> {
171-
let mut buffer = [0u8; Self::SERIALIZED_SIZE];
172-
reader.read_exact(&mut buffer)?;
173-
match Fr::from_bytes(&buffer).into_option() {
174-
Some(v) => Ok(v),
175-
None => Err(FieldSerdeError::DeserializeError),
176-
}
169+
#[inline(always)]
170+
fn try_deserialize_from_ecc_format<R: Read>(reader: R) -> FieldSerdeResult<Self> {
171+
Self::deserialize_from(reader)
177172
}
178173
}
179174

arith/src/serde.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,59 @@ pub trait FieldSerde: Sized {
2626
fn try_deserialize_from_ecc_format<R: Read>(reader: R) -> FieldSerdeResult<Self>;
2727
}
2828

29-
impl FieldSerde for u64 {
30-
/// size of the serialized bytes
31-
const SERIALIZED_SIZE: usize = 8;
29+
macro_rules! field_serde_for_integer {
30+
($int_type: ident, $size_in_bytes: expr) => {
31+
impl FieldSerde for $int_type {
32+
/// size of the serialized bytes
33+
const SERIALIZED_SIZE: usize = $size_in_bytes;
34+
35+
/// serialize u64 into bytes
36+
fn serialize_into<W: Write>(&self, mut writer: W) -> FieldSerdeResult<()> {
37+
writer.write_all(&self.to_le_bytes())?;
38+
Ok(())
39+
}
40+
41+
/// deserialize bytes into u64
42+
fn deserialize_from<R: Read>(mut reader: R) -> FieldSerdeResult<Self> {
43+
let mut buffer = [0u8; Self::SERIALIZED_SIZE];
44+
reader.read_exact(&mut buffer)?;
45+
Ok($int_type::from_le_bytes(buffer))
46+
}
47+
48+
fn try_deserialize_from_ecc_format<R: Read>(_reader: R) -> FieldSerdeResult<Self> {
49+
unimplemented!("not implemented")
50+
}
51+
}
52+
};
53+
}
54+
55+
field_serde_for_integer!(u64, 8);
56+
field_serde_for_integer!(usize, 8);
57+
field_serde_for_integer!(u8, 1);
58+
59+
// Consider use const generics after it gets stable
60+
impl FieldSerde for [u64; 4] {
61+
const SERIALIZED_SIZE: usize = 32;
3262

33-
/// serialize u64 into bytes
3463
fn serialize_into<W: Write>(&self, mut writer: W) -> FieldSerdeResult<()> {
35-
writer.write_all(&self.to_le_bytes())?;
64+
for i in self {
65+
writer.write_all(&i.to_le_bytes())?;
66+
}
3667
Ok(())
3768
}
3869

39-
/// deserialize bytes into u64
4070
fn deserialize_from<R: Read>(mut reader: R) -> FieldSerdeResult<Self> {
41-
let mut buffer = [0u8; Self::SERIALIZED_SIZE];
42-
reader.read_exact(&mut buffer)?;
43-
Ok(u64::from_le_bytes(buffer))
71+
let mut ret = [0u64; 4];
72+
let mut buffer = [0u8; u64::SERIALIZED_SIZE];
73+
74+
for r in &mut ret {
75+
reader.read_exact(&mut buffer)?;
76+
*r = u64::from_le_bytes(buffer);
77+
}
78+
Ok(ret)
4479
}
4580

4681
fn try_deserialize_from_ecc_format<R: Read>(_reader: R) -> FieldSerdeResult<Self> {
47-
unimplemented!("not implemented for u64")
82+
unimplemented!()
4883
}
4984
}

benches/gkr_hashes.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
2-
use expander_rs::utils::{KECCAK_M31_CIRCUIT, POSEIDON_CIRCUIT};
2+
use expander_rs::utils::{
3+
KECCAK_BN254_CIRCUIT, KECCAK_BN254_WITNESS, KECCAK_M31_CIRCUIT, KECCAK_M31_WITNESS,
4+
POSEIDON_BN254_CIRCUIT, POSEIDON_M31_CIRCUIT,
5+
};
36
use expander_rs::{
47
BN254ConfigSha2, Circuit, Config, GKRConfig, GKRScheme, M31ExtConfigSha2, MPIConfig, Prover,
58
};
@@ -11,18 +14,33 @@ fn prover_run<C: GKRConfig>(config: &Config<C>, circuit: &mut Circuit<C>) {
1114
prover.prove(circuit);
1215
}
1316

14-
fn benchmark_setup<C: GKRConfig>(scheme: GKRScheme, circuit_file: &str) -> (Config<C>, Circuit<C>) {
17+
fn benchmark_setup<C: GKRConfig>(
18+
scheme: GKRScheme,
19+
circuit_file: &str,
20+
witness_file: Option<&str>,
21+
) -> (Config<C>, Circuit<C>) {
1522
let config = Config::<C>::new(scheme, MPIConfig::new());
1623
let mut circuit = Circuit::<C>::load_circuit(circuit_file);
17-
circuit.set_random_input_for_test();
24+
if witness_file.is_some() {
25+
circuit.load_witness_file(witness_file.unwrap());
26+
} else {
27+
circuit.set_random_input_for_test();
28+
}
1829
(config, circuit)
1930
}
2031

2132
fn criterion_gkr_keccak(c: &mut Criterion) {
22-
let (m31_config, mut m31_circuit) =
23-
benchmark_setup::<M31ExtConfigSha2>(GKRScheme::Vanilla, KECCAK_M31_CIRCUIT);
24-
let (bn254_config, mut bn254_circuit) =
25-
benchmark_setup::<BN254ConfigSha2>(GKRScheme::Vanilla, KECCAK_M31_CIRCUIT);
33+
let (m31_config, mut m31_circuit) = benchmark_setup::<M31ExtConfigSha2>(
34+
GKRScheme::Vanilla,
35+
KECCAK_M31_CIRCUIT,
36+
Some(KECCAK_M31_WITNESS),
37+
);
38+
let (bn254_config, mut bn254_circuit) = benchmark_setup::<BN254ConfigSha2>(
39+
GKRScheme::Vanilla,
40+
KECCAK_BN254_CIRCUIT,
41+
Some(KECCAK_BN254_WITNESS),
42+
);
43+
2644
let num_keccak_m31 = 2 * M31ExtConfigSha2::get_field_pack_size();
2745
let num_keccak_bn254 = 2 * BN254ConfigSha2::get_field_pack_size();
2846

@@ -66,9 +84,9 @@ fn criterion_gkr_keccak(c: &mut Criterion) {
6684

6785
fn criterion_gkr_poseidon(c: &mut Criterion) {
6886
let (m31_config, mut m31_circuit) =
69-
benchmark_setup::<M31ExtConfigSha2>(GKRScheme::GkrSquare, POSEIDON_CIRCUIT);
87+
benchmark_setup::<M31ExtConfigSha2>(GKRScheme::GkrSquare, POSEIDON_M31_CIRCUIT, None);
7088
let (bn254_config, mut bn254_circuit) =
71-
benchmark_setup::<BN254ConfigSha2>(GKRScheme::GkrSquare, POSEIDON_CIRCUIT);
89+
benchmark_setup::<BN254ConfigSha2>(GKRScheme::GkrSquare, POSEIDON_BN254_CIRCUIT, None);
7290

7391
let mut group = c.benchmark_group("single thread proving poseidon by GKR^2");
7492
let num_poseidon_m31 = 120 * M31ExtConfigSha2::get_field_pack_size();

src/circuit.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ pub use ecc_circuit::*;
66

77
mod expander_circuit;
88
pub use expander_circuit::*;
9+
10+
mod witness;
11+
pub use witness::*;
12+
13+
mod serde;
14+
pub use serde::*;

0 commit comments

Comments
 (0)