Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
tryfrom all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed May 8, 2023
1 parent 643f32c commit cdb7ed2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 66 deletions.
3 changes: 1 addition & 2 deletions src/acvm_interop/smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ impl SmartContract for Barretenberg {
"acir_proofs_get_solidity_verifier",
vec![&g2_ptr, &vk_ptr, &contract_ptr_ptr.into()],
)?;
let contract_size: usize = contract_size.i32()? as usize;

// We then need to read the pointer at `contract_ptr_ptr` to get the smart contract's location
// and then slice memory again at `contract_ptr_ptr` to get the smart contract string.
let contract_ptr = self.get_pointer(contract_ptr_ptr);

let sc_as_bytes = self.read_memory_variable_length(contract_ptr, contract_size);
let sc_as_bytes = self.read_memory_variable_length(contract_ptr, contract_size.try_into()?);

let verification_key_library: String = sc_as_bytes.iter().map(|b| *b as char).collect();
Ok(format!(
Expand Down
62 changes: 27 additions & 35 deletions src/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ impl Composer for Barretenberg {

self.free(cs_ptr)?;

pow2ceil(circuit_size?.u32()? + NUM_RESERVED_GATES)
let size: u32 = circuit_size?.try_into()?;

pow2ceil(size + NUM_RESERVED_GATES)
}

fn get_exact_circuit_size(&self, constraint_system: &ConstraintSystem) -> Result<u32, Error> {
Expand All @@ -232,9 +234,7 @@ impl Composer for Barretenberg {

self.free(cs_ptr)?;

let size = circuit_size?.u32()?;

Ok(size)
Ok(circuit_size?.try_into()?)
}

fn compute_proving_key(&self, constraint_system: &ConstraintSystem) -> Result<Vec<u8>, Error> {
Expand All @@ -245,18 +245,16 @@ impl Composer for Barretenberg {
// `pk_ptr_ptr` is a pointer to a pointer which holds the proving key.
let pk_ptr_ptr: usize = 0;

let pk_size = self
.call_multiple(
"acir_proofs_init_proving_key",
vec![&cs_ptr, &pk_ptr_ptr.into()],
)?
.i32()?;
let pk_size = self.call_multiple(
"acir_proofs_init_proving_key",
vec![&cs_ptr, &pk_ptr_ptr.into()],
)?;

// We then need to read the pointer at `pk_ptr_ptr` to get the key's location
// and then slice memory again at `pk_ptr` to get the proving key.
let pk_ptr = self.get_pointer(pk_ptr_ptr);

Ok(self.read_memory_variable_length(pk_ptr, pk_size as usize))
Ok(self.read_memory_variable_length(pk_ptr, pk_size.try_into()?))
}

fn compute_verification_key(
Expand All @@ -277,18 +275,16 @@ impl Composer for Barretenberg {
// `vk_ptr_ptr` is a pointer to a pointer which holds the verification key.
let vk_ptr_ptr: usize = 0;

let vk_size = self
.call_multiple(
"acir_proofs_init_verification_key",
vec![&pippenger_ptr, &g2_ptr, &pk_ptr, &vk_ptr_ptr.into()],
)?
.i32()?;
let vk_size = self.call_multiple(
"acir_proofs_init_verification_key",
vec![&pippenger_ptr, &g2_ptr, &pk_ptr, &vk_ptr_ptr.into()],
)?;

// We then need to read the pointer at `vk_ptr_ptr` to get the key's location
// and then slice memory again at `vk_ptr` to get the verification key.
let vk_ptr = self.get_pointer(vk_ptr_ptr);

Ok(self.read_memory_variable_length(vk_ptr, vk_size as usize))
Ok(self.read_memory_variable_length(vk_ptr, vk_size.try_into()?))
}

fn create_proof_with_pk(
Expand All @@ -314,25 +310,23 @@ impl Composer for Barretenberg {
// `proof_ptr_ptr` is a pointer to a pointer which holds the proof data.
let proof_ptr_ptr: usize = 0;

let proof_size = self
.call_multiple(
"acir_proofs_new_proof",
vec![
&pippenger_ptr,
&g2_ptr,
&pk_ptr,
&cs_ptr,
&witness_ptr,
&proof_ptr_ptr.into(),
],
)?
.i32()?;
let proof_size = self.call_multiple(
"acir_proofs_new_proof",
vec![
&pippenger_ptr,
&g2_ptr,
&pk_ptr,
&cs_ptr,
&witness_ptr,
&proof_ptr_ptr.into(),
],
)?;

// We then need to read the pointer at `proof_ptr_ptr` to get the proof's location
// and then slice memory again at `proof_ptr` to get the proof data.
let proof_ptr = self.get_pointer(proof_ptr_ptr);

let result = self.read_memory_variable_length(proof_ptr, proof_size as usize);
let result = self.read_memory_variable_length(proof_ptr, proof_size.try_into()?);

// Barretenberg returns proofs which are prepended with the public inputs.
// This behavior is nonstandard so we strip the public inputs from the proof.
Expand Down Expand Up @@ -370,9 +364,7 @@ impl Composer for Barretenberg {

self.free(proof_ptr)?;

let verified = verified?.bool()?;

Ok(verified)
Ok(verified?.try_into()?)
}
}

Expand Down
75 changes: 50 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pub enum WasmError {
value: i32,
source: std::num::TryFromIntError,
},
#[error("Could not convert value {value} from i32 to usuze")]
InvalidUsize {
value: i32,
source: std::num::TryFromIntError,
},
#[error("Value expected to be 0 or 1 representing a boolean")]
InvalidBool,
#[error("Failed to get pointer")]
Expand Down Expand Up @@ -183,28 +188,6 @@ mod wasm {
#[derive(Debug, Clone)]
pub(super) struct WASMValue(Option<Value>);

impl WASMValue {
pub(super) fn value(self) -> Result<Value, WasmError> {
self.0.ok_or(WasmError::NoValue)
}
pub(super) fn i32(self) -> Result<i32, WasmError> {
self.0
.and_then(|val| val.i32())
.ok_or(WasmError::InvalidI32)
}
pub(super) fn u32(self) -> Result<u32, WasmError> {
let value = self.i32()?;
u32::try_from(value).map_err(|source| WasmError::InvalidU32 { value, source })
}
pub(super) fn bool(self) -> Result<bool, WasmError> {
match self.i32() {
Ok(0) => Ok(false),
Ok(1) => Ok(true),
_ => Err(WasmError::InvalidBool),
}
}
}

impl From<usize> for WASMValue {
fn from(value: usize) -> Self {
WASMValue(Some(Value::I32(value as i32)))
Expand All @@ -223,11 +206,53 @@ mod wasm {
}
}

impl TryFrom<WASMValue> for bool {
type Error = WasmError;

fn try_from(value: WASMValue) -> Result<Self, Self::Error> {
match value.try_into()? {
0 => Ok(false),
1 => Ok(true),
_ => Err(WasmError::InvalidBool),
}
}
}

impl TryFrom<WASMValue> for usize {
type Error = WasmError;

fn try_from(value: WASMValue) -> Result<Self, Self::Error> {
let value: i32 = value.try_into()?;
value
.try_into()
.map_err(|source| WasmError::InvalidUsize { value, source })
}
}

impl TryFrom<WASMValue> for u32 {
type Error = WasmError;

fn try_from(value: WASMValue) -> Result<Self, Self::Error> {
let value = value.try_into()?;
u32::try_from(value).map_err(|source| WasmError::InvalidU32 { value, source })
}
}

impl TryFrom<WASMValue> for i32 {
type Error = WasmError;

fn try_from(value: WASMValue) -> Result<Self, Self::Error> {
value.0.map_or(Err(WasmError::NoValue), |val| {
val.i32().ok_or(WasmError::InvalidI32)
})
}
}

impl TryFrom<WASMValue> for Value {
type Error = WasmError;

fn try_from(x: WASMValue) -> Result<Self, Self::Error> {
x.value()
fn try_from(value: WASMValue) -> Result<Self, Self::Error> {
value.0.ok_or(WasmError::NoValue)
}
}

Expand Down Expand Up @@ -317,7 +342,7 @@ mod wasm {

/// Creates a pointer and allocates the bytes that the pointer references to, to the heap
pub(super) fn allocate(&self, bytes: &[u8]) -> Result<WASMValue, Error> {
let ptr = self.call("bbmalloc", &bytes.len().into())?.i32()?;
let ptr: i32 = self.call("bbmalloc", &bytes.len().into())?.try_into()?;

let i32_bytes = ptr.to_be_bytes();
let u32_bytes = u32::from_be_bytes(i32_bytes);
Expand Down
6 changes: 2 additions & 4 deletions src/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl SchnorrSig for Barretenberg {
self.transfer_to_heap(sig_e, sig_e_ptr);
self.transfer_to_heap(message, message_ptr);

let wasm_value = self.call_multiple(
let verified = self.call_multiple(
"verify_signature",
vec![
&message_ptr.into(),
Expand All @@ -153,9 +153,7 @@ impl SchnorrSig for Barretenberg {

// Note, currently for Barretenberg plonk, if the signature fails
// then the whole circuit fails.
let verified = wasm_value.bool()?;

Ok(verified)
Ok(verified.try_into()?)
}
}

Expand Down

0 comments on commit cdb7ed2

Please sign in to comment.