From 91ea65f6af7039095c7a3af7bc1e4ce302a68a8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Rodr=C3=ADguez?= Date: Wed, 31 May 2023 16:28:50 +0200 Subject: [PATCH] feat!: added keccakvar constraints (#213) feat: added keccakvar constraints --- flake.lock | 6 ++--- src/barretenberg_structures.rs | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/flake.lock b/flake.lock index cbf59563..dff81844 100644 --- a/flake.lock +++ b/flake.lock @@ -10,11 +10,11 @@ ] }, "locked": { - "lastModified": 1685019724, - "narHash": "sha256-QWsYyrOda1u0qAQVifybjdibeP6NCWzk4cJ2mtrzA2E=", + "lastModified": 1685045859, + "narHash": "sha256-AdR8z8z/6JZDvyiaMCfHZ+JA5VQw5KhlAeiaytO6sns=", "owner": "AztecProtocol", "repo": "barretenberg", - "rev": "ad282152836f5e3a5c8f34256b29bdea9d16b854", + "rev": "083479164411be9cf9642e755182f059c9415a53", "type": "github" }, "original": { diff --git a/src/barretenberg_structures.rs b/src/barretenberg_structures.rs index 55ca562a..3ffb37ea 100644 --- a/src/barretenberg_structures.rs +++ b/src/barretenberg_structures.rs @@ -337,6 +337,36 @@ impl Keccak256Constraint { } } +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] +pub(crate) struct Keccak256VarConstraint { + pub(crate) inputs: Vec<(i32, i32)>, + pub(crate) result: [i32; 32], + pub(crate) var_message_size: i32, +} + +impl Keccak256VarConstraint { + fn to_bytes(&self) -> Vec { + let mut buffer = Vec::new(); + + let inputs_len = self.inputs.len() as u32; + buffer.extend_from_slice(&inputs_len.to_be_bytes()); + for constraint in self.inputs.iter() { + buffer.extend_from_slice(&constraint.0.to_be_bytes()); + buffer.extend_from_slice(&constraint.1.to_be_bytes()); + } + + let result_len = self.result.len() as u32; + buffer.extend_from_slice(&result_len.to_be_bytes()); + for constraint in self.result.iter() { + buffer.extend_from_slice(&constraint.to_be_bytes()); + } + + buffer.extend_from_slice(&self.var_message_size.to_be_bytes()); + + buffer + } +} + #[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct PedersenConstraint { pub(crate) inputs: Vec, @@ -435,6 +465,7 @@ pub(crate) struct ConstraintSystem { blake2s_constraints: Vec, block_constraints: Vec, keccak_constraints: Vec, + keccak_var_constraints: Vec, pedersen_constraints: Vec, hash_to_field_constraints: Vec, fixed_base_scalar_mul_constraints: Vec, @@ -626,6 +657,13 @@ impl ConstraintSystem { buffer.extend(&constraint.to_bytes()); } + // Serialize each Keccak Var constraint + let keccak_var_len = self.keccak_var_constraints.len() as u32; + buffer.extend_from_slice(&keccak_var_len.to_be_bytes()); + for constraint in self.keccak_var_constraints.iter() { + buffer.extend(&constraint.to_bytes()); + } + // Serialize each Pedersen constraint let pedersen_len = self.pedersen_constraints.len() as u32; buffer.extend_from_slice(&pedersen_len.to_be_bytes()); @@ -749,6 +787,7 @@ impl TryFrom<&Circuit> for ConstraintSystem { let mut blake2s_constraints: Vec = Vec::new(); let mut block_constraints: Vec = Vec::new(); let mut keccak_constraints: Vec = Vec::new(); + let keccak_var_constraints: Vec = Vec::new(); let mut pedersen_constraints: Vec = Vec::new(); let mut compute_merkle_root_constraints: Vec = Vec::new(); let mut schnorr_constraints: Vec = Vec::new(); @@ -1126,6 +1165,7 @@ impl TryFrom<&Circuit> for ConstraintSystem { blake2s_constraints, block_constraints, keccak_constraints, + keccak_var_constraints, hash_to_field_constraints, constraints, fixed_base_scalar_mul_constraints,