Skip to content

Commit

Permalink
Fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
charlielye committed Jul 10, 2023
1 parent a17867c commit 82340cc
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 30 deletions.

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

Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ serde-big-array = "0.5.1"
flate2 = "1.0"
base64 = "0.13"

acvm = { version = "0.16.0", features = ["bn254"] }
acvm = { version = "0.17.0", features = ["bn254"] }
noirc_abi = { git = "https://github.com/noir-lang/noir.git" }
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl EcdsaConstraint {
buffer
}
}

#[derive(Clone, Hash, Debug, Serialize, Deserialize)]
pub(crate) struct SchnorrConstraint {
pub(crate) message: Vec<i32>,
Expand Down Expand Up @@ -390,6 +391,7 @@ pub struct ConstraintSystem {
sha256_constraints: Vec<Sha256Constraint>,
schnorr_constraints: Vec<SchnorrConstraint>,
ecdsa_secp256k1_constraints: Vec<EcdsaConstraint>,
ecdsa_secp256r1_constraints: Vec<EcdsaConstraint>,
blake2s_constraints: Vec<Blake2sConstraint>,
block_constraints: Vec<BlockConstraint>,
keccak_constraints: Vec<Keccak256Constraint>,
Expand Down Expand Up @@ -454,6 +456,14 @@ impl ConstraintSystem {
self
}

pub(crate) fn ecdsa_secp256r1_constraints(
mut self,
ecdsa_secp256r1_constraints: Vec<EcdsaConstraint>,
) -> Self {
self.ecdsa_secp256r1_constraints = ecdsa_secp256r1_constraints;
self
}

pub(crate) fn blake2s_constraints(
mut self,
blake2s_constraints: Vec<Blake2sConstraint>,
Expand Down Expand Up @@ -555,12 +565,18 @@ impl ConstraintSystem {
}

// Serialize each ECDSA constraint
let ecdsa_len = self.ecdsa_secp256k1_constraints.len() as u32;
buffer.extend_from_slice(&ecdsa_len.to_be_bytes());
let ecdsa_k1_len = self.ecdsa_secp256k1_constraints.len() as u32;
buffer.extend_from_slice(&ecdsa_k1_len.to_be_bytes());
for constraint in self.ecdsa_secp256k1_constraints.iter() {
buffer.extend(&constraint.to_bytes());
}

let ecdsa_r1_len = self.ecdsa_secp256r1_constraints.len() as u32;
buffer.extend_from_slice(&ecdsa_r1_len.to_be_bytes());
for constraint in self.ecdsa_secp256r1_constraints.iter() {
buffer.extend(&constraint.to_bytes());
}

// Serialize each Blake2s constraint
let blake2s_len = self.blake2s_constraints.len() as u32;
buffer.extend_from_slice(&blake2s_len.to_be_bytes());
Expand Down Expand Up @@ -768,6 +784,7 @@ impl TryFrom<&Circuit> for ConstraintSystem {
let mut pedersen_constraints: Vec<PedersenConstraint> = Vec::new();
let mut schnorr_constraints: Vec<SchnorrConstraint> = Vec::new();
let mut ecdsa_secp256k1_constraints: Vec<EcdsaConstraint> = Vec::new();
let mut ecdsa_secp256r1_constraints: Vec<EcdsaConstraint> = Vec::new();
let mut fixed_base_scalar_mul_constraints: Vec<FixedBaseScalarMulConstraint> = Vec::new();
let mut hash_to_field_constraints: Vec<HashToFieldConstraint> = Vec::new();
let mut recursion_constraints: Vec<RecursionConstraint> = Vec::new();
Expand Down Expand Up @@ -1018,6 +1035,65 @@ impl TryFrom<&Circuit> for ConstraintSystem {

ecdsa_secp256k1_constraints.push(constraint);
}
BlackBoxFuncCall::EcdsaSecp256r1 {
public_key_x: public_key_x_inputs,
public_key_y: public_key_y_inputs,
signature: signature_inputs,
hashed_message: hashed_message_inputs,
output,
} => {
// public key x
let mut public_key_x_inputs = public_key_x_inputs.iter();
let mut public_key_x = [0i32; 32];
for (i, pkx) in public_key_x.iter_mut().enumerate() {
let x_byte = public_key_x_inputs
.next()
.ok_or_else(|| format!("Missing rest of `x` component for public key. Tried to get byte {i} but failed"))?;
let x_byte_index = x_byte.witness.witness_index() as i32;
*pkx = x_byte_index;
}

// public key y
let mut public_key_y_inputs = public_key_y_inputs.iter();
let mut public_key_y = [0i32; 32];
for (i, pky) in public_key_y.iter_mut().enumerate() {
let y_byte = public_key_y_inputs
.next()
.ok_or_else(|| format!("Missing rest of `y` component for public key. Tried to get byte {i} but failed"))?;
let y_byte_index = y_byte.witness.witness_index() as i32;
*pky = y_byte_index;
}

// signature
let mut signature_inputs = signature_inputs.iter();
let mut signature = [0i32; 64];
for (i, sig) in signature.iter_mut().enumerate() {
let sig_byte =
signature_inputs.next().ok_or_else(|| format!("Missing rest of signature. Tried to get byte {i} but failed"))?;
let sig_byte_index = sig_byte.witness.witness_index() as i32;
*sig = sig_byte_index;
}

// The rest of the input is the message
let mut hashed_message = Vec::new();
for msg in hashed_message_inputs.iter() {
let msg_byte_index = msg.witness.witness_index() as i32;
hashed_message.push(msg_byte_index);
}

// result
let result = output.witness_index() as i32;

let constraint = EcdsaConstraint {
hashed_message,
signature,
public_key_x,
public_key_y,
result,
};

ecdsa_secp256r1_constraints.push(constraint);
}
BlackBoxFuncCall::FixedBaseScalarMul { input, outputs } => {
let scalar = input.witness.witness_index() as i32;

Expand Down Expand Up @@ -1204,6 +1280,7 @@ impl TryFrom<&Circuit> for ConstraintSystem {
pedersen_constraints,
schnorr_constraints,
ecdsa_secp256k1_constraints,
ecdsa_secp256r1_constraints,
blake2s_constraints,
block_constraints,
keccak_constraints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ pub fn main() {
let circuit_path = Path::new(&path_string);

let circuit_bytes = std::fs::read(&circuit_path).unwrap();

let mut program: PreprocessedProgram =
serde_json::from_slice(&circuit_bytes).expect("could not deserialize program");

program.proving_key = vec![];
program.verification_key = vec![];
let program: PreprocessedProgram =
serde_json::from_slice(&circuit_bytes).expect("could not deserialize program");

write_to_file(&serde_json::to_vec(&program).unwrap(), &circuit_path);
}
Expand Down Expand Up @@ -49,10 +46,7 @@ pub struct PreprocessedProgram {
serialize_with = "serialize_circuit",
deserialize_with = "deserialize_circuit"
)]
pub bytecode: Circuit,

pub proving_key: Vec<u8>,
pub verification_key: Vec<u8>,
pub bytecode: Circuit
}

fn serialize_circuit<S>(circuit: &Circuit, s: S) -> Result<S::Ok, S::Error>
Expand Down
3 changes: 2 additions & 1 deletion circuits/cpp/barretenberg/acir_tests/run_acir_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ set -e
BB=$PWD/${BB:-../cpp/build/bin/bb}
ATBBC=$PWD/acir-to-bberg-circuit/target/release/acir-to-bberg-circuit
CRS_PATH=~/.bb-crs
BRANCH=master

# Pull down the test vectors from the noir repo, if we don't have the folder already.
if [ ! -d acir_tests ]; then
rm -rf noir
git clone --filter=blob:none --no-checkout https://github.com/noir-lang/noir.git
git clone -b $BRANCH --filter=blob:none --no-checkout https://github.com/noir-lang/noir.git
cd noir
git sparse-checkout init --cone
git sparse-checkout set crates/nargo_cli/tests/test_data
Expand Down

0 comments on commit 82340cc

Please sign in to comment.