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

Commit

Permalink
feat: updated code to use latest ACVM
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant committed May 9, 2023
1 parent c19f184 commit 71ad590
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 166 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0"

[dependencies]
#acvm = { version = "0.11.0", features = ["bn254"] }
acvm = { git = "https://github.com/noir-lang/acvm", rev = "d0bb049f2f120d632ea0e55d0d53a2dc8f5cf27e", features=["bn254"] }
acvm = { git = "https://github.com/noir-lang/acvm", rev = "e4bbf564230a19832f1eb3285d59545d3e7c9a9b", features=["bn254"] }

thiserror = "1.0.21"

Expand Down
2 changes: 1 addition & 1 deletion src/acvm_interop/proof_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl ProofSystemCompiler for Barretenberg {
| BlackBoxFunc::EcdsaSecp256k1
| BlackBoxFunc::FixedBaseScalarMul => true,

BlackBoxFunc::AES => false,
BlackBoxFunc::AES => false,
}
}

Expand Down
98 changes: 52 additions & 46 deletions src/acvm_interop/pwg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,29 @@ impl PartialWitnessGenerator for Barretenberg {
fn and(
&self,
initial_witness: &mut BTreeMap<Witness, FieldElement>,
inputs: &[FunctionInput],
outputs: &[Witness],
lhs: &FunctionInput,
rhs: &FunctionInput,
output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
logic::and(initial_witness, inputs, outputs)
logic::and(initial_witness, lhs, rhs, output)
}

fn xor(
&self,
initial_witness: &mut BTreeMap<Witness, FieldElement>,
inputs: &[FunctionInput],
outputs: &[Witness],
lhs: &FunctionInput,
rhs: &FunctionInput,
output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
logic::xor(initial_witness, inputs, outputs)
logic::xor(initial_witness, lhs, rhs, output)
}

fn range(
&self,
initial_witness: &mut BTreeMap<Witness, FieldElement>,
inputs: &[FunctionInput],
input: &FunctionInput,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
range::solve_range_opcode(initial_witness, inputs)
range::solve_range_opcode(initial_witness, input)
}

fn sha256(
Expand All @@ -73,18 +75,17 @@ impl PartialWitnessGenerator for Barretenberg {
fn compute_merkle_root(
&self,
initial_witness: &mut BTreeMap<Witness, FieldElement>,
inputs: &[FunctionInput],
outputs: &[Witness],
leaf: &FunctionInput,
index: &FunctionInput,
hash_path: &[FunctionInput],
output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
let mut inputs_iter = inputs.iter();
let leaf = witness_to_value(initial_witness, leaf.witness)?;

let _leaf = inputs_iter.next().expect("expected a leaf");
let leaf = witness_to_value(initial_witness, _leaf.witness)?;
let index = witness_to_value(initial_witness, index.witness)?;

let _index = inputs_iter.next().expect("expected an index");
let index = witness_to_value(initial_witness, _index.witness)?;

let hash_path: Result<Vec<_>, _> = inputs_iter
let hash_path: Result<Vec<_>, _> = hash_path
.iter()
.map(|input| witness_to_value(initial_witness, input.witness))
.collect();

Expand All @@ -101,29 +102,23 @@ impl PartialWitnessGenerator for Barretenberg {
)
})?;

initial_witness.insert(outputs[0], computed_merkle_root);
initial_witness.insert(*output, computed_merkle_root);
Ok(OpcodeResolution::Solved)
}

fn schnorr_verify(
&self,
initial_witness: &mut BTreeMap<Witness, FieldElement>,
inputs: &[FunctionInput],
outputs: &[Witness],
public_key_x: &FunctionInput,
public_key_y: &FunctionInput,
signature: &[FunctionInput],
message: &[FunctionInput],
output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
// In barretenberg, if the signature fails, then the whole thing fails.

let mut inputs_iter = inputs.iter();

let _pub_key_x = inputs_iter
.next()
.expect("expected `x` component for public key");
let pub_key_x = witness_to_value(initial_witness, _pub_key_x.witness)?.to_be_bytes();

let _pub_key_y = inputs_iter
.next()
.expect("expected `y` component for public key");
let pub_key_y = witness_to_value(initial_witness, _pub_key_y.witness)?.to_be_bytes();
let pub_key_x = witness_to_value(initial_witness, public_key_x.witness)?.to_be_bytes();
let pub_key_y = witness_to_value(initial_witness, public_key_y.witness)?.to_be_bytes();

let pub_key_bytes: Vec<u8> = pub_key_x
.iter()
Expand All @@ -137,9 +132,10 @@ impl PartialWitnessGenerator for Barretenberg {
)
})?;

let mut signature = [0u8; 64];
for (i, sig) in signature.iter_mut().enumerate() {
let _sig_i = inputs_iter.next().ok_or_else(|| {
let mut signature = signature.iter();
let mut signature_bytes = [0u8; 64];
for (i, sig) in signature_bytes.iter_mut().enumerate() {
let _sig_i = signature.next().ok_or_else(|| {
OpcodeResolutionError::BlackBoxFunctionFailed(
BlackBoxFunc::SchnorrVerify,
format!("signature should be 64 bytes long, found only {i} bytes"),
Expand All @@ -154,20 +150,20 @@ impl PartialWitnessGenerator for Barretenberg {
})?;
}

let mut message = Vec::new();
for msg in inputs_iter {
let mut message_bytes = Vec::new();
for msg in message.iter() {
let msg_i_field = witness_to_value(initial_witness, msg.witness)?;
let msg_i = *msg_i_field.to_be_bytes().last().ok_or_else(|| {
OpcodeResolutionError::BlackBoxFunctionFailed(
BlackBoxFunc::SchnorrVerify,
"could not get last bytes".into(),
)
})?;
message.push(msg_i);
message_bytes.push(msg_i);
}

let valid_signature = self
.verify_signature(pub_key, signature, &message)
.verify_signature(pub_key, signature_bytes, &message_bytes)
.map_err(|err| {
OpcodeResolutionError::BlackBoxFunctionFailed(
BlackBoxFunc::SchnorrVerify,
Expand All @@ -178,7 +174,7 @@ impl PartialWitnessGenerator for Barretenberg {
dbg!("signature has failed to verify");
}

initial_witness.insert(outputs[0], FieldElement::from(valid_signature));
initial_witness.insert(*output, FieldElement::from(valid_signature));
Ok(OpcodeResolution::Solved)
}

Expand Down Expand Up @@ -206,27 +202,37 @@ impl PartialWitnessGenerator for Barretenberg {
&self,
initial_witness: &mut BTreeMap<Witness, FieldElement>,
inputs: &[FunctionInput],
outputs: &[Witness],
output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
hash::hash_to_field_128_security(initial_witness, inputs, outputs)
hash::hash_to_field_128_security(initial_witness, inputs, output)
}

fn ecdsa_secp256k1(
&self,
initial_witness: &mut BTreeMap<Witness, FieldElement>,
inputs: &[FunctionInput],
outputs: &[Witness],
public_key_x: &[FunctionInput],
public_key_y: &[FunctionInput],
signature: &[FunctionInput],
message: &[FunctionInput],
outputs: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
signature::ecdsa::secp256k1_prehashed(initial_witness, inputs, outputs)
signature::ecdsa::secp256k1_prehashed(
initial_witness,
public_key_x,
public_key_y,
signature,
message,
*outputs,
)
}

fn fixed_base_scalar_mul(
&self,
initial_witness: &mut BTreeMap<Witness, FieldElement>,
inputs: &[FunctionInput],
input: &FunctionInput,
outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError> {
let scalar = witness_to_value(initial_witness, inputs[0].witness)?;
let scalar = witness_to_value(initial_witness, input.witness)?;

let (pub_x, pub_y) = self.fixed_base(scalar).map_err(|err| {
OpcodeResolutionError::BlackBoxFunctionFailed(
Expand Down
Loading

0 comments on commit 71ad590

Please sign in to comment.