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

Commit

Permalink
feat!: added keccakvar constraints (#213)
Browse files Browse the repository at this point in the history
feat: added keccakvar constraints
  • Loading branch information
sirasistant authored May 31, 2023
1 parent 9a233ce commit 91ea65f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
6 changes: 3 additions & 3 deletions flake.lock

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

40 changes: 40 additions & 0 deletions src/barretenberg_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
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<i32>,
Expand Down Expand Up @@ -435,6 +465,7 @@ pub(crate) struct ConstraintSystem {
blake2s_constraints: Vec<Blake2sConstraint>,
block_constraints: Vec<BlockConstraint>,
keccak_constraints: Vec<Keccak256Constraint>,
keccak_var_constraints: Vec<Keccak256VarConstraint>,
pedersen_constraints: Vec<PedersenConstraint>,
hash_to_field_constraints: Vec<HashToFieldConstraint>,
fixed_base_scalar_mul_constraints: Vec<FixedBaseScalarMulConstraint>,
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -749,6 +787,7 @@ impl TryFrom<&Circuit> for ConstraintSystem {
let mut blake2s_constraints: Vec<Blake2sConstraint> = Vec::new();
let mut block_constraints: Vec<BlockConstraint> = Vec::new();
let mut keccak_constraints: Vec<Keccak256Constraint> = Vec::new();
let keccak_var_constraints: Vec<Keccak256VarConstraint> = Vec::new();
let mut pedersen_constraints: Vec<PedersenConstraint> = Vec::new();
let mut compute_merkle_root_constraints: Vec<ComputeMerkleRootConstraint> = Vec::new();
let mut schnorr_constraints: Vec<SchnorrConstraint> = Vec::new();
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 91ea65f

Please sign in to comment.