From d23c7352523ffb42f3e8f4229b61f9803ab78a7e Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Thu, 16 Feb 2023 15:04:56 -0500 Subject: [PATCH] fix: Clean up Log Directive hex output (#97) * match up print output from witness elements to evaluate_println during Noir acir gen * remove unnecessary semicolon --- acvm/src/pwg/directives.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/acvm/src/pwg/directives.rs b/acvm/src/pwg/directives.rs index 67819efb3..95f0b7801 100644 --- a/acvm/src/pwg/directives.rs +++ b/acvm/src/pwg/directives.rs @@ -183,7 +183,7 @@ pub fn solve_directives( if witnesses.len() == 1 { let witness = &witnesses[0]; let log_value = witness_to_value(initial_witness, *witness)?; - println!("{}", log_value.to_hex()); + println!("{}", format_field_string(*log_value)); return Ok(()); } @@ -196,11 +196,11 @@ pub fn solve_directives( let mut elements_as_hex = Vec::with_capacity(witnesses.len()); for witness in witnesses { let element = witness_to_value(initial_witness, *witness)?; - elements_as_hex.push(element.to_hex()); + elements_as_hex.push(format_field_string(*element)); } // Join all of the hex strings using a comma - let comma_separated_elements = elements_as_hex.join(","); + let comma_separated_elements = elements_as_hex.join(", "); let output_witnesses_string = "[".to_owned() + &comma_separated_elements + "]"; @@ -228,3 +228,15 @@ fn insert_witness( } Ok(()) } + +/// This trims any leading zeroes. +/// A singular '0' will be prepended as well if the trimmed string has an odd length. +/// A hex string's length needs to be even to decode into bytes, as two digits correspond to +/// one byte. +fn format_field_string(field: FieldElement) -> String { + let mut trimmed_field = field.to_hex().trim_start_matches('0').to_owned(); + if trimmed_field.len() % 2 != 0 { + trimmed_field = "0".to_owned() + &trimmed_field + } + "0x".to_owned() + &trimmed_field +}