Skip to content

Commit

Permalink
Use try_write_as in synthesizer
Browse files Browse the repository at this point in the history
  • Loading branch information
vicsn committed Jun 6, 2023
1 parent 7f2a6c8 commit d33b4e2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
1 change: 0 additions & 1 deletion algorithms/src/polycommit/sonic_pc/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ impl<E: PairingEngine> FromBytes for CommitterKey<E> {
impl<E: PairingEngine> ToBytes for CommitterKey<E> {
fn write_le<W: Write>(&self, mut writer: W) -> io::Result<()> {
// Serialize `powers`.
// u32::try_from(*degree_bound).map_err(|e| error(e.to_string()))?.write_le(&mut writer)?;
try_write_as::<u32, W>(self.powers_of_beta_g.len(), &mut writer)?;
for power in &self.powers_of_beta_g {
power.write_le(&mut writer)?;
Expand Down
3 changes: 2 additions & 1 deletion synthesizer/coinbase/src/helpers/coinbase_solution/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use super::*;
use snarkvm_utilities::try_write_as;

impl<N: Network> FromBytes for CoinbaseSolution<N> {
/// Reads the coinbase solution from the buffer.
Expand All @@ -34,7 +35,7 @@ impl<N: Network> FromBytes for CoinbaseSolution<N> {
impl<N: Network> ToBytes for CoinbaseSolution<N> {
/// Writes the coinbase solution to the buffer.
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
(u32::try_from(self.partial_solutions.len()).map_err(|e| error(e.to_string()))?).write_le(&mut writer)?;
try_write_as::<u32, W>(self.partial_solutions.len(), &mut writer)?;

for individual_puzzle_solution in &self.partial_solutions {
individual_puzzle_solution.write_le(&mut writer)?;
Expand Down
3 changes: 2 additions & 1 deletion synthesizer/src/block/transaction/deployment/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use super::*;
use snarkvm_utilities::try_write_as;

impl<N: Network> FromBytes for Deployment<N> {
/// Reads the deployment from a buffer.
Expand Down Expand Up @@ -59,7 +60,7 @@ impl<N: Network> ToBytes for Deployment<N> {
// Write the program.
self.program.write_le(&mut writer)?;
// Write the number of entries in the bundle.
(u16::try_from(self.verifying_keys.len()).map_err(|e| error(e.to_string()))?).write_le(&mut writer)?;
try_write_as::<u16, W>(self.verifying_keys.len(), &mut writer)?;
// Write each entry.
for (function_name, (verifying_key, certificate)) in &self.verifying_keys {
// Write the function name.
Expand Down
4 changes: 3 additions & 1 deletion synthesizer/src/block/transaction/execution/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use snarkvm_utilities::try_write_as;

use super::*;

impl<N: Network> FromBytes for Execution<N> {
Expand Down Expand Up @@ -54,7 +56,7 @@ impl<N: Network> ToBytes for Execution<N> {
// Write the version.
0u8.write_le(&mut writer)?;
// Write the number of transitions.
(u8::try_from(self.transitions.len()).map_err(|e| error(e.to_string()))?).write_le(&mut writer)?;
try_write_as::<u8, W>(self.transitions.len(), &mut writer)?;
// Write the transitions.
for transition in self.transitions.values() {
transition.write_le(&mut writer)?;
Expand Down
5 changes: 3 additions & 2 deletions synthesizer/src/block/transactions/confirmed/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use super::*;
use snarkvm_utilities::try_write_as;

impl<N: Network> FromBytes for ConfirmedTransaction<N> {
/// Reads the confirmed transaction from a buffer.
Expand Down Expand Up @@ -82,7 +83,7 @@ impl<N: Network> ToBytes for ConfirmedTransaction<N> {
// Write the transaction.
transaction.write_le(&mut writer)?;
// Write the number of finalize operations.
NumFinalizeSize::try_from(finalize.len()).map_err(|e| error(e.to_string()))?.write_le(&mut writer)?;
try_write_as::<NumFinalizeSize, W>(finalize.len(), &mut writer)?;
// Write the finalize operations.
finalize.iter().try_for_each(|finalize| finalize.write_le(&mut writer))
}
Expand All @@ -94,7 +95,7 @@ impl<N: Network> ToBytes for ConfirmedTransaction<N> {
// Write the transaction.
transaction.write_le(&mut writer)?;
// Write the number of finalize operations.
NumFinalizeSize::try_from(finalize.len()).map_err(|e| error(e.to_string()))?.write_le(&mut writer)?;
try_write_as::<NumFinalizeSize, W>(finalize.len(), &mut writer)?;
// Write the finalize operations.
finalize.iter().try_for_each(|finalize| finalize.write_le(&mut writer))
}
Expand Down
8 changes: 5 additions & 3 deletions synthesizer/src/block/transition/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use snarkvm_utilities::try_write_as;

use super::*;

impl<N: Network> FromBytes for Transition<N> {
Expand Down Expand Up @@ -98,12 +100,12 @@ impl<N: Network> ToBytes for Transition<N> {
self.function_name.write_le(&mut writer)?;

// Write the number of inputs.
(u8::try_from(self.inputs.len()).map_err(|e| error(e.to_string()))?).write_le(&mut writer)?;
try_write_as::<u8, W>(self.inputs.len(), &mut writer)?;
// Write the inputs.
self.inputs.write_le(&mut writer)?;

// Write the number of outputs.
(u8::try_from(self.outputs.len()).map_err(|e| error(e.to_string()))?).write_le(&mut writer)?;
try_write_as::<u8, W>(self.outputs.len(), &mut writer)?;
// Write the outputs.
self.outputs.write_le(&mut writer)?;

Expand All @@ -117,7 +119,7 @@ impl<N: Network> ToBytes for Transition<N> {
// Write the finalize variant.
1u8.write_le(&mut writer)?;
// Write the number of inputs to finalize.
(u8::try_from(finalize.len()).map_err(|e| error(e.to_string()))?).write_le(&mut writer)?;
try_write_as::<u8, W>(finalize.len(), &mut writer)?;
// Write the inputs to finalize.
finalize.write_le(&mut writer)?;
}
Expand Down

0 comments on commit d33b4e2

Please sign in to comment.