Skip to content

Commit 5adf586

Browse files
committed
Refactor witness::Witness
- Remove unused methods - Push fields directly into the witness vector, instead of allocating a temporary vector
1 parent 8114923 commit 5adf586

File tree

2 files changed

+30
-59
lines changed

2 files changed

+30
-59
lines changed

ledger/src/proofs/transaction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,14 +3777,14 @@ pub fn compute_witness<C: ProofConstants, F: FieldWitness>(
37773777
w: &Witness<F>,
37783778
) -> [Vec<F>; COLUMNS] {
37793779
if !w.ocaml_aux.is_empty() {
3780-
assert_eq!(w.aux.len(), w.ocaml_aux.len());
3780+
assert_eq!(w.aux().len(), w.ocaml_aux.len());
37813781
};
37823782

37833783
let external_values = |i: usize| {
37843784
if i < C::PRIMARY_LEN {
37853785
w.primary[i]
37863786
} else {
3787-
w.aux[i - C::PRIMARY_LEN]
3787+
w.aux()[i - C::PRIMARY_LEN]
37883788
}
37893789
};
37903790

@@ -4011,7 +4011,7 @@ mod tests_with_wasm {
40114011

40124012
assert_eq!(res, (131085.into(), 65636.into(), 1866.into()));
40134013
assert_eq!(
4014-
witness.aux,
4014+
witness.aux(),
40154015
&[
40164016
0.into(),
40174017
0.into(),

ledger/src/proofs/witness.rs

Lines changed: 27 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use super::{
77

88
#[derive(Debug)]
99
pub struct Witness<F: FieldWitness> {
10-
pub primary: Vec<F>,
11-
pub(super) aux: Vec<F>,
10+
pub(super) primary: Vec<F>,
11+
aux: Vec<F>,
1212
// Following fields are used to compare our witness with OCaml
1313
pub ocaml_aux: Vec<F>,
1414
ocaml_aux_index: usize,
@@ -33,86 +33,57 @@ impl<F: FieldWitness> Witness<F> {
3333
}
3434
}
3535

36-
pub fn push<I: Into<F>>(&mut self, field: I) {
37-
let field = {
38-
let field: F = field.into();
39-
// dbg!(field)
40-
field
41-
};
42-
self.assert_ocaml_aux(&[field]);
43-
self.aux.push(field);
44-
}
45-
46-
pub fn extend<I: Into<F>, V: Iterator<Item = I>>(&mut self, field: V) {
47-
let fields = {
48-
let fields: Vec<F> = field.map(Into::into).collect();
49-
self.assert_ocaml_aux(&fields);
50-
// eprintln!("extend[{}]={:#?}", fields.len(), fields);
51-
fields
52-
};
53-
self.aux.extend(fields)
36+
pub(super) fn aux(&self) -> &[F] {
37+
&self.aux
5438
}
5539

5640
pub fn exists<T>(&mut self, data: T) -> T
5741
where
5842
T: ToFieldElements<F> + Check<F>,
5943
{
60-
// data.to_field_elements(&mut self.aux);
61-
let mut fields = data.to_field_elements_owned();
62-
self.assert_ocaml_aux(&fields);
63-
64-
// eprintln!("index={:?} w{:?}", self.aux.len() + 67, &fields);
65-
if self.ocaml_aux.len() > 0 {
66-
eprintln!(
67-
"index={:?} w{:?}",
68-
self.aux.len() + self.primary.capacity(),
69-
&fields
70-
);
71-
}
72-
self.aux.append(&mut fields);
73-
44+
let data = self.exists_no_check(data);
7445
data.check(self);
7546
data
7647
}
7748

49+
/// Same as `Self::exists`, but do not call `Check::check` on `data`
50+
/// We use this wherever `seal` is used in OCaml, or on `if_` conditions
7851
pub fn exists_no_check<T>(&mut self, data: T) -> T
7952
where
8053
T: ToFieldElements<F>,
8154
{
82-
// data.to_field_elements(&mut self.aux);
83-
let mut fields = data.to_field_elements_owned();
84-
self.assert_ocaml_aux(&fields);
85-
86-
// eprintln!("index={:?} w{:?}", self.aux.len() + 67, &fields);
87-
if self.ocaml_aux.len() > 0 {
88-
eprintln!(
89-
"index={:?} w{:?}",
90-
self.aux.len() + self.primary.capacity(),
91-
&fields
92-
);
93-
}
94-
self.aux.append(&mut fields);
55+
#[cfg(test)]
56+
let start = self.aux.len();
57+
58+
data.to_field_elements(&mut self.aux);
59+
60+
#[cfg(test)]
61+
self.assert_ocaml_aux(start);
9562

9663
data
9764
}
9865

9966
/// Compare our witness with OCaml
100-
fn assert_ocaml_aux(&mut self, new_fields: &[F]) {
67+
#[cfg(test)]
68+
fn assert_ocaml_aux(&mut self, start_offset: usize) {
10169
if self.ocaml_aux.is_empty() {
10270
return;
10371
}
10472

105-
// let len = new_fields.len();
106-
// let before = self.aux.len();
107-
// let ocaml = &self.ocaml_aux[before..before + len];
108-
// eprintln!("w{:?} ocaml{:?} {:?}", new_fields, ocaml, new_fields == ocaml);
109-
73+
let new_fields = &self.aux[start_offset..];
11074
let len = new_fields.len();
111-
let before = self.aux.len();
112-
assert_eq!(before, self.ocaml_aux_index);
113-
assert_eq!(new_fields, &self.ocaml_aux[before..before + len]);
75+
let ocaml_fields = &self.ocaml_aux[start_offset..start_offset + len];
76+
77+
assert_eq!(start_offset, self.ocaml_aux_index);
78+
assert_eq!(new_fields, ocaml_fields);
11479

11580
self.ocaml_aux_index += len;
81+
82+
eprintln!(
83+
"index={:?} w{:?}",
84+
self.aux.len() + self.primary.capacity(),
85+
&self.aux[start_offset..]
86+
);
11687
}
11788

11889
/// Helper

0 commit comments

Comments
 (0)