From f7eff88c22a546f88f7d2373c88cd77ed5089aad Mon Sep 17 00:00:00 2001 From: kevaundray Date: Sun, 29 Oct 2023 16:16:23 +0000 Subject: [PATCH 1/4] add bytes32_to_field method from aztec.nr --- noir_stdlib/src/field.nr | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/noir_stdlib/src/field.nr b/noir_stdlib/src/field.nr index 3959f1ea175..0c6e282f449 100644 --- a/noir_stdlib/src/field.nr +++ b/noir_stdlib/src/field.nr @@ -82,3 +82,20 @@ pub fn modulus_be_bytes() -> [u8] {} #[builtin(modulus_le_bytes)] pub fn modulus_le_bytes() -> [u8] {} + +// Convert a 32 byte array to a field element +pub fn bytes32_to_field(bytes32 : [u8; 32]) -> Field { + // Convert it to a field element + let mut v = 1; + let mut high = 0 as Field; + let mut low = 0 as Field; + + for i in 0..16 { + high = high + (bytes32[15 - i] as Field) * v; + low = low + (bytes32[16 + 15 - i] as Field) * v; + v = v * 256; + } + + // Abuse that a % p + b % p = (a + b) % p and that low < p + low + high * v +} \ No newline at end of file From 1637083229d43002796fee48f0cca91b071e5644 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Sun, 29 Oct 2023 16:17:07 +0000 Subject: [PATCH 2/4] add test to check that we have an equivalent method --- .../tests/execution_success/hash_to_field/src/main.nr | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr b/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr index ffc334179ee..ce01ea15c5f 100644 --- a/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr @@ -1,5 +1,12 @@ use dep::std; fn main(input : Field) -> pub Field { - std::hash::hash_to_field([input]) -} + let expected = std::hash::hash_to_field([input]); + + let input_bytes = input.to_le_bytes(32); + let blake2s = std::hash::blake2s(input_bytes); + let got = dep::std::field::bytes32_to_field(blake2s); + + assert(expected == got); + expected +} \ No newline at end of file From c92c4d4dac4bf4fea809e0dcd9ec9e49b67f1974 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 30 Oct 2023 11:56:25 +0000 Subject: [PATCH 3/4] chore: add `hash_to_field_native` function (#3339) --- noir_stdlib/src/hash.nr | 13 +++++++++++++ .../execution_success/hash_to_field/src/main.nr | 6 +----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/noir_stdlib/src/hash.nr b/noir_stdlib/src/hash.nr index 67e01c36e1c..414a096e904 100644 --- a/noir_stdlib/src/hash.nr +++ b/noir_stdlib/src/hash.nr @@ -22,6 +22,19 @@ pub fn pedersen_hash_with_separator(_input : [Field; N], _separator : u32) -> #[foreign(hash_to_field_128_security)] pub fn hash_to_field(_input : [Field; N]) -> Field {} +pub fn hash_to_field_native(_input : [Field; N]) -> Field { + let mut inputs_as_bytes = []; + + for i in 0..N { + let input_bytes = _input[i].to_le_bytes(32); + for i in 0..32 { + inputs_as_bytes = inputs_as_bytes.push_back(input_bytes[i]); + } + } + + let hashed_input = blake2s(inputs_as_bytes); + crate::field::bytes32_to_field(hashed_input) +} #[foreign(keccak256)] pub fn keccak256(_input : [u8; N], _message_size: u32) -> [u8; 32] {} diff --git a/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr b/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr index ce01ea15c5f..0434b60d52f 100644 --- a/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr @@ -2,11 +2,7 @@ use dep::std; fn main(input : Field) -> pub Field { let expected = std::hash::hash_to_field([input]); - - let input_bytes = input.to_le_bytes(32); - let blake2s = std::hash::blake2s(input_bytes); - let got = dep::std::field::bytes32_to_field(blake2s); - + let got = std::hash::hash_to_field_native([input]); assert(expected == got); expected } \ No newline at end of file From 2fb5e132042cf22433a5fd1afed9dede5a351d27 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 30 Oct 2023 12:00:39 +0000 Subject: [PATCH 4/4] remove the old hash_to_field method --- noir_stdlib/src/hash.nr | 4 +--- .../tests/execution_success/hash_to_field/src/main.nr | 5 +---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/noir_stdlib/src/hash.nr b/noir_stdlib/src/hash.nr index 414a096e904..db9cc78d268 100644 --- a/noir_stdlib/src/hash.nr +++ b/noir_stdlib/src/hash.nr @@ -20,9 +20,7 @@ pub fn pedersen_hash(input : [Field; N]) -> Field { #[foreign(pedersen_hash)] pub fn pedersen_hash_with_separator(_input : [Field; N], _separator : u32) -> Field {} -#[foreign(hash_to_field_128_security)] -pub fn hash_to_field(_input : [Field; N]) -> Field {} -pub fn hash_to_field_native(_input : [Field; N]) -> Field { +pub fn hash_to_field(_input : [Field; N]) -> Field { let mut inputs_as_bytes = []; for i in 0..N { diff --git a/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr b/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr index 0434b60d52f..2b7d59cd8b6 100644 --- a/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/hash_to_field/src/main.nr @@ -1,8 +1,5 @@ use dep::std; fn main(input : Field) -> pub Field { - let expected = std::hash::hash_to_field([input]); - let got = std::hash::hash_to_field_native([input]); - assert(expected == got); - expected + std::hash::hash_to_field([input]) } \ No newline at end of file