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

Memory corruption fixes #6697

Merged
merged 2 commits into from
Nov 13, 2020
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
48 changes: 0 additions & 48 deletions src/lib/marlin_plonk_bindings/stubs/src/caml_vector.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,3 @@
use ocaml::ToValue;

pub fn to_array_<T, F: Fn(T) -> ocaml::Value>(v: Vec<T>, f: F) -> ocaml::Array<ocaml::Value> {
ocaml::frame!((array_value) {
let len = v.len();
// Manually allocate an OCaml array of the right size
let mut array = ocaml::Array::alloc(len);
// This is safe because we know that Array::alloc doesn't allocate.
// TODO: Discuss with upstream about better handling arrays so they don't get GC'd out from
// under us.
array_value = array.to_value().clone();
for (i, x) in v.into_iter().enumerate() {
ocaml::frame!((value) {
value = f(x);
unsafe {
array.set_unchecked(i, value);
}
})
}
array
})
}

pub fn ref_to_array<T, F: Fn(&T) -> ocaml::Value>(v: &Vec<T>, f: F) -> ocaml::Array<ocaml::Value> {
ocaml::frame!((array_value) {
let len = v.len();
// Manually allocate an OCaml array of the right size
let mut array = ocaml::Array::alloc(len);
// This is safe because we know that Array::alloc doesn't allocate.
// TODO: Discuss with upstream about better handling arrays so they don't get GC'd out from
// under us.
array_value = array.to_value().clone();
for (i, x) in v.into_iter().enumerate() {
ocaml::frame!((value) {
value = f(x);
unsafe {
array.set_unchecked(i, value);
}
})
}
array
})
}

pub fn to_array<Caml: ocaml::ToValue, T: Into<Caml>>(v: Vec<T>) -> ocaml::Array<ocaml::Value> {
to_array_(v, |x| Into::<Caml>::into(x).to_value())
}

pub fn from_array_<A: ocaml::ToValue + ocaml::FromValue, T, F: Fn(A) -> T>(
array: ocaml::Array<A>,
f: F,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/marlin_plonk_bindings/stubs/src/plonk_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ impl From<CamlPlonkWires> for Wires {
}

#[derive(ocaml::ToValue, ocaml::FromValue)]
pub struct CamlPlonkGate {
pub typ: CamlPlonkGateType, // type of the gate
pub wires: CamlPlonkWires, // gate wires
pub c: ocaml::Array<ocaml::Value>, // constraints vector
pub struct CamlPlonkGate<T> {
pub typ: CamlPlonkGateType, // type of the gate
pub wires: CamlPlonkWires, // gate wires
pub c: T, // constraints vector
}
29 changes: 20 additions & 9 deletions src/lib/marlin_plonk_bindings/stubs/src/tweedle_dee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ unsafe impl ocaml::FromValue for CamlTweedleDeeAffineVector {

unsafe impl ocaml::ToValue for CamlTweedleDeeAffineVector {
fn to_value(self: Self) -> ocaml::Value {
caml_vector::to_array::<CamlTweedleDeeAffine<CamlTweedleFq>, _>(self.0).to_value()
let vec: Vec<CamlTweedleDeeAffine<CamlTweedleFq>> =
self.0.iter().map(|x| (*x).into()).collect();
vec.to_value()
}
}

Expand All @@ -162,13 +164,20 @@ unsafe impl ocaml::FromValue for CamlTweedleDeeAffinePairVector {

unsafe impl ocaml::ToValue for CamlTweedleDeeAffinePairVector {
fn to_value(self: Self) -> ocaml::Value {
caml_vector::to_array_(self.0, |x| {
let (x1, x2) = x;
let x1: CamlTweedleDeeAffine<CamlTweedleFq> = x1.into();
let x2: CamlTweedleDeeAffine<CamlTweedleFq> = x2.into();
ocaml::ToValue::to_value((x1, x2))
})
.to_value()
let vec: Vec<(
CamlTweedleDeeAffine<CamlTweedleFq>,
CamlTweedleDeeAffine<CamlTweedleFq>,
)> = self
.0
.iter()
.map(|x| {
let (x1, x2) = x;
let x1: CamlTweedleDeeAffine<CamlTweedleFq> = (*x1).into();
let x2: CamlTweedleDeeAffine<CamlTweedleFq> = (*x2).into();
(x1, x2)
})
.collect();
vec.to_value()
}
}

Expand Down Expand Up @@ -218,6 +227,8 @@ unsafe impl ocaml::FromValue for CamlTweedleDeePolyCommVector {

unsafe impl ocaml::ToValue for CamlTweedleDeePolyCommVector {
fn to_value(self: Self) -> ocaml::Value {
caml_vector::to_array::<CamlTweedleDeePolyComm<CamlTweedleFq>, _>(self.0).to_value()
let vec: Vec<CamlTweedleDeePolyComm<CamlTweedleFq>> =
self.0.into_iter().map(|x| x.into()).collect();
vec.to_value()
}
}
29 changes: 20 additions & 9 deletions src/lib/marlin_plonk_bindings/stubs/src/tweedle_dum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ unsafe impl ocaml::FromValue for CamlTweedleDumAffineVector {

unsafe impl ocaml::ToValue for CamlTweedleDumAffineVector {
fn to_value(self: Self) -> ocaml::Value {
caml_vector::to_array::<CamlTweedleDumAffine<CamlTweedleFp>, _>(self.0).to_value()
let vec: Vec<CamlTweedleDumAffine<CamlTweedleFp>> =
self.0.iter().map(|x| (*x).into()).collect();
vec.to_value()
}
}

Expand All @@ -162,13 +164,20 @@ unsafe impl ocaml::FromValue for CamlTweedleDumAffinePairVector {

unsafe impl ocaml::ToValue for CamlTweedleDumAffinePairVector {
fn to_value(self: Self) -> ocaml::Value {
caml_vector::to_array_(self.0, |x| {
let (x1, x2) = x;
let x1: CamlTweedleDumAffine<CamlTweedleFp> = x1.into();
let x2: CamlTweedleDumAffine<CamlTweedleFp> = x2.into();
ocaml::ToValue::to_value((x1, x2))
})
.to_value()
let vec: Vec<(
CamlTweedleDumAffine<CamlTweedleFp>,
CamlTweedleDumAffine<CamlTweedleFp>,
)> = self
.0
.iter()
.map(|x| {
let (x1, x2) = x;
let x1: CamlTweedleDumAffine<CamlTweedleFp> = (*x1).into();
let x2: CamlTweedleDumAffine<CamlTweedleFp> = (*x2).into();
(x1, x2)
})
.collect();
vec.to_value()
}
}

Expand Down Expand Up @@ -218,6 +227,8 @@ unsafe impl ocaml::FromValue for CamlTweedleDumPolyCommVector {

unsafe impl ocaml::ToValue for CamlTweedleDumPolyCommVector {
fn to_value(self: Self) -> ocaml::Value {
caml_vector::to_array::<CamlTweedleDumPolyComm<CamlTweedleFp>, _>(self.0).to_value()
let vec: Vec<CamlTweedleDumPolyComm<CamlTweedleFp>> =
self.0.into_iter().map(|x| x.into()).collect();
vec.to_value()
}
}
28 changes: 12 additions & 16 deletions src/lib/marlin_plonk_bindings/stubs/src/tweedle_fp_plonk_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ use std::{
rc::Rc,
};

use crate::caml_vector;
use crate::index_serialization;
use crate::plonk_gate::{CamlPlonkCol, CamlPlonkGate, CamlPlonkWire};
use crate::tweedle_fp::{CamlTweedleFp, CamlTweedleFpPtr};
use crate::tweedle_fp::CamlTweedleFp;
use crate::tweedle_fp_urs::CamlTweedleFpUrs;
use ocaml::{FromValue, ToValue};

pub struct CamlTweedleFpPlonkGateVector(Vec<Gate<Fp>>);
pub type CamlTweedleFpPlonkGateVectorPtr = ocaml::Pointer<CamlTweedleFpPlonkGateVector>;
Expand All @@ -47,30 +45,28 @@ pub fn caml_tweedle_fp_plonk_gate_vector_create() -> CamlTweedleFpPlonkGateVecto
#[ocaml::func]
pub fn caml_tweedle_fp_plonk_gate_vector_add(
mut v: CamlTweedleFpPlonkGateVectorPtr,
gate: CamlPlonkGate,
gate: CamlPlonkGate<Vec<CamlTweedleFp>>,
) {
let c = gate.c.iter().map(|x| x.0).collect();
v.as_mut().0.push(Gate {
typ: gate.typ.into(),
wires: gate.wires.into(),
c: caml_vector::from_array_(gate.c, |x| CamlTweedleFpPtr::from_value(x).as_ref().0),
c,
});
}

#[ocaml::func]
pub fn caml_tweedle_fp_plonk_gate_vector_get(
v: CamlTweedleFpPlonkGateVectorPtr,
i: ocaml::Int,
) -> CamlPlonkGate {
ocaml::frame!((array_value) {
let gate = &(v.as_ref().0)[i as usize];
let c = caml_vector::ref_to_array(&gate.c, |x| CamlTweedleFp(*x).to_value());
array_value = c.to_value().clone();
CamlPlonkGate {
typ: (&gate.typ).into(),
wires: (&gate.wires).into(),
c,
}
})
) -> CamlPlonkGate<Vec<CamlTweedleFp>> {
let gate = &(v.as_ref().0)[i as usize];
let c = gate.c.iter().map(|x| CamlTweedleFp(*x)).collect();
CamlPlonkGate {
typ: (&gate.typ).into(),
wires: (&gate.wires).into(),
c,
}
}

#[ocaml::func]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ unsafe impl ocaml::FromValue for CamlTweedleFpScalarChallengeVec {

unsafe impl ocaml::ToValue for CamlTweedleFpScalarChallengeVec {
fn to_value(self: Self) -> ocaml::Value {
let array =
caml_vector::to_array_(self.0, |x| ocaml::ToValue::to_value(CamlTweedleFp(x.0)));
array.to_value()
let vec: Vec<CamlTweedleFp> = self.0.iter().map(|x| CamlTweedleFp(x.0)).collect();
vec.to_value()
}
}

Expand Down
21 changes: 10 additions & 11 deletions src/lib/marlin_plonk_bindings/stubs/src/tweedle_fp_plonk_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ unsafe impl ocaml::FromValue for CamlTweedleFpVec {

unsafe impl ocaml::ToValue for CamlTweedleFpVec {
fn to_value(self: Self) -> ocaml::Value {
let array = caml_vector::to_array_(self.0, |x| ocaml::ToValue::to_value(CamlTweedleFp(x)));
array.to_value()
let vec: Vec<CamlTweedleFp> = self.0.iter().map(|x| CamlTweedleFp(*x)).collect();
vec.to_value()
}
}

Expand Down Expand Up @@ -105,16 +105,15 @@ unsafe impl ocaml::FromValue for CamlTweedleFpPrevChallenges {

unsafe impl ocaml::ToValue for CamlTweedleFpPrevChallenges {
fn to_value(self: Self) -> ocaml::Value {
let array = caml_vector::to_array_(self.0, |(vec, polycomm)| {
ocaml::frame!((array_value) {
let polycomm: CamlTweedleDeePolyComm<CamlTweedleFq> = polycomm.into();
let array_inner =
caml_vector::to_array_(vec, |x| ocaml::ToValue::to_value(CamlTweedleFp(x)));
array_value = array_inner.to_value().clone();
ocaml::ToValue::to_value((array_inner, polycomm))
let vec: Vec<(Vec<CamlTweedleFp>, CamlTweedleDeePolyComm<CamlTweedleFq>)> = self
.0
.into_iter()
.map(|(vec, polycomm)| {
let vec = vec.into_iter().map(|x| CamlTweedleFp(x)).collect();
(vec, polycomm.into())
})
});
array.to_value()
.collect();
vec.to_value()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,14 @@ pub fn caml_tweedle_fp_plonk_verifier_index_raw_of_parts(
evals: CamlPlonkVerificationEvals<CamlTweedleDeePolyComm<CamlTweedleFqPtr>>,
shifts: CamlPlonkVerificationShifts<CamlTweedleFpPtr>,
) -> CamlTweedleFpPlonkVerifierIndexRaw<'static> {
of_ocaml(max_poly_size, max_quot_size, log_size_of_group, urs, evals, shifts)
of_ocaml(
max_poly_size,
max_quot_size,
log_size_of_group,
urs,
evals,
shifts,
)
}

#[ocaml::func]
Expand Down
24 changes: 11 additions & 13 deletions src/lib/marlin_plonk_bindings/stubs/src/tweedle_fq_plonk_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ use std::{
rc::Rc,
};

use crate::caml_vector;
use crate::index_serialization;
use crate::plonk_gate::{CamlPlonkCol, CamlPlonkGate, CamlPlonkWire};
use crate::tweedle_fq::{CamlTweedleFq, CamlTweedleFqPtr};
use crate::tweedle_fq::CamlTweedleFq;
use crate::tweedle_fq_urs::CamlTweedleFqUrs;
use ocaml::{FromValue, ToValue};

pub struct CamlTweedleFqPlonkGateVector(Vec<Gate<Fq>>);
pub type CamlTweedleFqPlonkGateVectorPtr = ocaml::Pointer<CamlTweedleFqPlonkGateVector>;
Expand All @@ -39,32 +37,32 @@ ocaml::custom!(CamlTweedleFqPlonkGateVector {
finalize: caml_tweedle_fq_plonk_gate_vector_finalize,
});

#[ocaml::func]
pub fn caml_tweedle_fq_plonk_gate_vector_create() -> CamlTweedleFqPlonkGateVector {
CamlTweedleFqPlonkGateVector(Vec::new())
}

#[ocaml::func]
pub fn caml_tweedle_fq_plonk_gate_vector_add(
mut v: CamlTweedleFqPlonkGateVectorPtr,
gate: CamlPlonkGate,
gate: CamlPlonkGate<Vec<CamlTweedleFq>>,
) {
let c = gate.c.iter().map(|x| x.0).collect();
v.as_mut().0.push(Gate {
typ: gate.typ.into(),
wires: gate.wires.into(),
c: caml_vector::from_array_(gate.c, |x| CamlTweedleFqPtr::from_value(x).as_ref().0),
c,
});
}

#[ocaml::func]
pub fn caml_tweedle_fq_plonk_gate_vector_create() -> CamlTweedleFqPlonkGateVector {
CamlTweedleFqPlonkGateVector(Vec::new())
}

#[ocaml::func]
pub fn caml_tweedle_fq_plonk_gate_vector_get(
v: CamlTweedleFqPlonkGateVectorPtr,
i: ocaml::Int,
) -> CamlPlonkGate {
) -> CamlPlonkGate<Vec<CamlTweedleFq>> {
ocaml::frame!((array_value) {
let gate = &(v.as_ref().0)[i as usize];
let c = caml_vector::ref_to_array(&gate.c, |x| CamlTweedleFq(*x).to_value());
array_value = c.to_value().clone();
let c = gate.c.iter().map(|x| CamlTweedleFq(*x)).collect();
CamlPlonkGate {
typ: (&gate.typ).into(),
wires: (&gate.wires).into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ unsafe impl ocaml::FromValue for CamlTweedleFqScalarChallengeVec {

unsafe impl ocaml::ToValue for CamlTweedleFqScalarChallengeVec {
fn to_value(self: Self) -> ocaml::Value {
let array =
caml_vector::to_array_(self.0, |x| ocaml::ToValue::to_value(CamlTweedleFq(x.0)));
array.to_value()
let vec: Vec<CamlTweedleFq> = self.0.iter().map(|x| CamlTweedleFq(x.0)).collect();
vec.to_value()
}
}

Expand Down
21 changes: 10 additions & 11 deletions src/lib/marlin_plonk_bindings/stubs/src/tweedle_fq_plonk_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ unsafe impl ocaml::FromValue for CamlTweedleFqVec {

unsafe impl ocaml::ToValue for CamlTweedleFqVec {
fn to_value(self: Self) -> ocaml::Value {
let array = caml_vector::to_array_(self.0, |x| ocaml::ToValue::to_value(CamlTweedleFq(x)));
array.to_value()
let vec: Vec<CamlTweedleFq> = self.0.iter().map(|x| CamlTweedleFq(*x)).collect();
vec.to_value()
}
}

Expand Down Expand Up @@ -105,16 +105,15 @@ unsafe impl ocaml::FromValue for CamlTweedleFqPrevChallenges {

unsafe impl ocaml::ToValue for CamlTweedleFqPrevChallenges {
fn to_value(self: Self) -> ocaml::Value {
let array = caml_vector::to_array_(self.0, |(vec, polycomm)| {
ocaml::frame!((array_value) {
let polycomm: CamlTweedleDumPolyComm<CamlTweedleFp> = polycomm.into();
let array_inner =
caml_vector::to_array_(vec, |x| ocaml::ToValue::to_value(CamlTweedleFq(x)));
array_value = array_inner.to_value().clone();
ocaml::ToValue::to_value((array_inner, polycomm))
let vec: Vec<(Vec<CamlTweedleFq>, CamlTweedleDumPolyComm<CamlTweedleFp>)> = self
.0
.into_iter()
.map(|(vec, polycomm)| {
let vec = vec.into_iter().map(|x| CamlTweedleFq(x)).collect();
(vec, polycomm.into())
})
});
array.to_value()
.collect();
vec.to_value()
}
}

Expand Down
Loading