Skip to content

Commit

Permalink
build() method, Default, doctests on *Owned structures
Browse files Browse the repository at this point in the history
  • Loading branch information
naure committed Aug 19, 2020
1 parent 49ed7e8 commit 7a98c2c
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 45 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Version v1.1.4, 2020-08, Rust fixes

Rust:
- Default trait, build() method, doctests for *Owned structures.
- Fix all potential divide-by-zero.


# Version v1.1.3, 2020-07, Rust/C++/libSNARK end-to-end integration

Rust:
Expand Down
6 changes: 3 additions & 3 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ pub mod examples;

pub use reading::Messages;
pub use owned::{
message::MessagesOwned,
circuit::CircuitOwned,
command::CommandOwned,
constraints::ConstraintSystemOwned,
witness::WitnessOwned,
variables::VariablesOwned,
keyvalue::KeyValueOwned,
message::MessagesOwned,
variables::VariablesOwned,
witness::WitnessOwned,
};

// Common definitions.
Expand Down
12 changes: 10 additions & 2 deletions rust/src/owned/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::keyvalue::KeyValueOwned;
use crate::Result;


#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct CircuitOwned {
pub connections: VariablesOwned,

Expand Down Expand Up @@ -97,7 +97,15 @@ impl CircuitOwned {
})
}

/// Write this structure as a Flatbuffers message.
/// Writes this circuit as a Flatbuffers message into the provided buffer.
///
/// # Examples
/// ```
/// let mut buf = Vec::<u8>::new();
/// let circuit = zkinterface::CircuitOwned::default();
/// circuit.write_into(&mut buf).unwrap();
/// assert!(buf.len() > 0);
/// ```
pub fn write_into(&self, writer: &mut impl Write) -> Result<()> {
let mut builder = FlatBufferBuilder::new();
let message = self.build(&mut builder);
Expand Down
12 changes: 10 additions & 2 deletions rust/src/owned/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::zkinterface_generated::zkinterface::{
use crate::Result;


#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct CommandOwned {
pub constraints_generation: bool,
pub witness_generation: bool,
Expand Down Expand Up @@ -49,7 +49,15 @@ impl CommandOwned {
})
}

/// Write this structure as a Flatbuffers message.
/// Writes this command as a Flatbuffers message into the provided buffer.
///
/// # Examples
/// ```
/// let mut buf = Vec::<u8>::new();
/// let command = zkinterface::CommandOwned::default();
/// command.write_into(&mut buf).unwrap();
/// assert!(buf.len() > 0);
/// ```
pub fn write_into(&self, writer: &mut impl Write) -> Result<()> {
let mut builder = FlatBufferBuilder::new();
let message = self.build(&mut builder);
Expand Down
82 changes: 49 additions & 33 deletions rust/src/owned/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::io::Write;
use flatbuffers::FlatBufferBuilder;
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use serde::{Deserialize, Serialize};
use crate::{Result, VariablesOwned};
use crate::zkinterface_generated::zkinterface::{BilinearConstraint, BilinearConstraintArgs, ConstraintSystem, ConstraintSystemArgs, Message, Root, RootArgs};

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct ConstraintSystemOwned {
pub constraints: Vec<BilinearConstraintOwned>,
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct BilinearConstraintOwned {
pub linear_combination_a: VariablesOwned,
pub linear_combination_b: VariablesOwned,
Expand Down Expand Up @@ -80,47 +80,63 @@ impl From<&[((Vec<u64>, Vec<u8>), (Vec<u64>, Vec<u8>), (Vec<u64>, Vec<u8>))]> fo
}
}

impl ConstraintSystemOwned {
/// Writes the constraint system as a Flatbuffers message into the provided buffer.
///
///
/// # Examples
/// ```
/// let mut buf = Vec::<u8>::new();
/// let constraints_owned = zkinterface::ConstraintSystemOwned::from(&[][..]);
/// constraints_owned.write_into(&mut buf).unwrap();
/// ```
pub fn write_into(&self, writer: &mut impl Write) -> Result<()> {
let mut builder = FlatBufferBuilder::new();
let mut constraints_built = vec![];

for bilinear_constraints in &self.constraints {
let lca = bilinear_constraints.linear_combination_a.build(&mut builder);
let lcb = bilinear_constraints.linear_combination_b.build(&mut builder);
let lcc = bilinear_constraints.linear_combination_c.build(&mut builder);
impl BilinearConstraintOwned {
/// Add this structure into a Flatbuffers message builder.
pub fn build<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
&'args self,
builder: &'mut_bldr mut FlatBufferBuilder<'bldr>,
) -> WIPOffset<BilinearConstraint<'bldr>>
{
let lca = self.linear_combination_a.build(builder);
let lcb = self.linear_combination_b.build(builder);
let lcc = self.linear_combination_c.build(builder);

BilinearConstraint::create(builder, &BilinearConstraintArgs {
linear_combination_a: Some(lca),
linear_combination_b: Some(lcb),
linear_combination_c: Some(lcc),
})
}
}

constraints_built.push(BilinearConstraint::create(&mut builder, &BilinearConstraintArgs {
linear_combination_a: Some(lca),
linear_combination_b: Some(lcb),
linear_combination_c: Some(lcc),
}));
}
impl ConstraintSystemOwned {
/// Add this structure into a Flatbuffers message builder.
pub fn build<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
&'args self,
builder: &'mut_bldr mut FlatBufferBuilder<'bldr>,
) -> WIPOffset<Root<'bldr>>
{
let constraints_built: Vec<_> = self.constraints.iter()
.map(|constraint|
constraint.build(builder)
).collect();

let constraints_built = builder.create_vector(&constraints_built);
let r1cs = ConstraintSystem::create(&mut builder, &ConstraintSystemArgs {
let r1cs = ConstraintSystem::create(builder, &ConstraintSystemArgs {
constraints: Some(constraints_built),
info: None,
});

let message = Root::create(&mut builder, &RootArgs {
Root::create(builder, &RootArgs {
message_type: Message::ConstraintSystem,
message: Some(r1cs.as_union_value()),
});
builder.finish_size_prefixed(message, None);
})
}

/// Writes this constraint system as a Flatbuffers message into the provided buffer.
///
/// # Examples
/// ```
/// let mut buf = Vec::<u8>::new();
/// let constraints = zkinterface::ConstraintSystemOwned::from(&[][..]);
/// constraints.write_into(&mut buf).unwrap();
/// assert!(buf.len() > 0);
/// ```
pub fn write_into(&self, writer: &mut impl Write) -> Result<()> {
let mut builder = FlatBufferBuilder::new();
let message = self.build(&mut builder);
builder.finish_size_prefixed(message, None);
writer.write_all(builder.finished_data())?;
Ok(())
}
}

2 changes: 1 addition & 1 deletion rust/src/owned/keyvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::zkinterface_generated::zkinterface::{
KeyValueArgs,
};

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct KeyValueOwned {
pub key: String,
// The value goes into one the following:
Expand Down
2 changes: 1 addition & 1 deletion rust/src/owned/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::circuit::CircuitOwned;
use super::constraints::ConstraintSystemOwned;
use super::witness::WitnessOwned;

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct MessagesOwned {
pub circuits: Vec<CircuitOwned>,
pub constraint_systems: Vec<ConstraintSystemOwned>,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/owned/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::zkinterface_generated::zkinterface::{
};
use crate::reading::{Variable, get_value_size};

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct VariablesOwned {
pub variable_ids: Vec<u64>,
pub values: Option<Vec<u8>>,
Expand Down
12 changes: 10 additions & 2 deletions rust/src/owned/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::variables::VariablesOwned;
use crate::Result;


#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct WitnessOwned {
pub assigned_variables: VariablesOwned,
}
Expand Down Expand Up @@ -45,7 +45,15 @@ impl WitnessOwned {
})
}

/// Write this structure as a Flatbuffers message.
/// Writes this witness as a Flatbuffers message into the provided buffer.
///
/// # Examples
/// ```
/// let mut buf = Vec::<u8>::new();
/// let witness = zkinterface::WitnessOwned::default();
/// witness.write_into(&mut buf).unwrap();
/// assert!(buf.len() > 0);
/// ```
pub fn write_into(&self, writer: &mut impl Write) -> Result<()> {
let mut builder = FlatBufferBuilder::new();
let message = self.build(&mut builder);
Expand Down

0 comments on commit 7a98c2c

Please sign in to comment.