-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: move hash utils to aztec-nr #2583
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use dep::std::hash::{pedersen_with_separator, sha256}; | ||
use crate::constants_gen::{ | ||
GENERATOR_INDEX__SIGNATURE_PAYLOAD, | ||
GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, | ||
}; | ||
|
||
fn sha256_to_field<N>(bytes_to_hash: [u8; N]) -> Field { | ||
let sha256_hashed = sha256(bytes_to_hash); | ||
|
||
// 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 + (sha256_hashed[15 - i] as Field) * v; | ||
low = low + (sha256_hashed[16 + 15 - i] as Field) * v; | ||
v = v * 256; | ||
} | ||
|
||
// Abuse that a % p + b % p = (a + b) % p and that low < p | ||
let hash_in_a_field = low + high * v; | ||
|
||
hash_in_a_field | ||
} | ||
|
||
fn compute_message_hash<N>(args: [Field; N]) -> Field { | ||
// @todo @lherskind We should probably use a separate generator for this, | ||
// to avoid any potential collisions with payloads. | ||
pedersen_with_separator(args, GENERATOR_INDEX__SIGNATURE_PAYLOAD)[0] | ||
} | ||
|
||
fn compute_secret_hash(secret: Field) -> Field { | ||
// TODO(#1205) This is probably not the right index to use | ||
pedersen_with_separator([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ mod auth; | |
mod constants_gen; | ||
mod context; | ||
mod entrypoint; | ||
mod hash; | ||
mod log; | ||
mod messaging; | ||
mod note; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,54 @@ | ||
use dep::std::hash::{pedersen_with_separator, sha256}; | ||
use dep::aztec::constants_gen::{ | ||
GENERATOR_INDEX__SIGNATURE_PAYLOAD, | ||
GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, | ||
}; | ||
use dep::std::hash::pedersen_with_separator; | ||
// docs:start:mint_public_content_hash_nr | ||
use dep::aztec::hash::{sha256_to_field}; | ||
|
||
fn compute_secret_hash(secret: Field) -> Field { | ||
// TODO(#1205) This is probably not the right index to use | ||
pedersen_with_separator([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)[0] | ||
} | ||
|
||
// Computes a content hash of a deposit/mint_private message. | ||
// Computes a content hash of a deposit/mint_public message. | ||
// Refer TokenPortal.sol for reference on L1. | ||
fn get_mint_private_content_hash(amount: Field, secret_hash_for_redeeming_minted_notes: Field, canceller: Field) -> Field { | ||
fn get_mint_public_content_hash(owner_address: Field, amount: Field, canceller: Field) -> Field { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If having the above mentioned, most ofthis could really be discarded which would be quite nice I think. |
||
|
||
let mut hash_bytes: [u8; 100] = [0; 100]; | ||
let amount_bytes = amount.to_be_bytes(32); | ||
let secret_hash_bytes = secret_hash_for_redeeming_minted_notes.to_be_bytes(32); | ||
let recipient_bytes = owner_address.to_be_bytes(32); | ||
let canceller_bytes = canceller.to_be_bytes(32); | ||
|
||
for i in 0..32 { | ||
hash_bytes[i + 4] = amount_bytes[i]; | ||
hash_bytes[i + 36] = secret_hash_bytes[i]; | ||
hash_bytes[i + 36] = recipient_bytes[i]; | ||
hash_bytes[i + 68] = canceller_bytes[i]; | ||
} | ||
|
||
// Function selector: 0x25d46b0f keccak256('mint_private(uint256,bytes32,address)') | ||
hash_bytes[0] = 0x25; | ||
hash_bytes[1] = 0xd4; | ||
hash_bytes[2] = 0x6b; | ||
hash_bytes[3] = 0x0f; | ||
|
||
let content_sha256 = sha256(hash_bytes); | ||
|
||
// // Convert the content_sha256 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 + (content_sha256[15 - i] as Field) * v; | ||
low = low + (content_sha256[16 + 15 - i] as Field) * v; | ||
v = v * 256; | ||
} | ||
// Function selector: 0x63c9440d keccak256('mint_public(uint256,bytes32,address)') | ||
hash_bytes[0] = 0x63; | ||
hash_bytes[1] = 0xc9; | ||
hash_bytes[2] = 0x44; | ||
hash_bytes[3] = 0x0d; | ||
|
||
// Abuse that a % p + b % p = (a + b) % p and that low < p | ||
let content_hash = low + high * v; | ||
let content_hash = sha256_to_field(hash_bytes); | ||
content_hash | ||
} | ||
// docs:end:mint_public_content_hash_nr | ||
|
||
// Computes a content hash of a deposit/mint_public message. | ||
// Computes a content hash of a deposit/mint_private message. | ||
// Refer TokenPortal.sol for reference on L1. | ||
fn get_mint_public_content_hash(owner_address: Field, amount: Field, canceller: Field) -> Field { | ||
fn get_mint_private_content_hash(amount: Field, secret_hash_for_redeeming_minted_notes: Field, canceller: Field) -> Field { | ||
let mut hash_bytes: [u8; 100] = [0; 100]; | ||
let amount_bytes = amount.to_be_bytes(32); | ||
let recipient_bytes = owner_address.to_be_bytes(32); | ||
let secret_hash_bytes = secret_hash_for_redeeming_minted_notes.to_be_bytes(32); | ||
let canceller_bytes = canceller.to_be_bytes(32); | ||
|
||
for i in 0..32 { | ||
hash_bytes[i + 4] = amount_bytes[i]; | ||
hash_bytes[i + 36] = recipient_bytes[i]; | ||
hash_bytes[i + 36] = secret_hash_bytes[i]; | ||
hash_bytes[i + 68] = canceller_bytes[i]; | ||
} | ||
|
||
// Function selector: 0x63c9440d keccak256('mint_public(uint256,bytes32,address)') | ||
hash_bytes[0] = 0x63; | ||
hash_bytes[1] = 0xc9; | ||
hash_bytes[2] = 0x44; | ||
hash_bytes[3] = 0x0d; | ||
|
||
let content_sha256 = sha256(hash_bytes); | ||
|
||
// // Convert the content_sha256 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 + (content_sha256[15 - i] as Field) * v; | ||
low = low + (content_sha256[16 + 15 - i] as Field) * v; | ||
v = v * 256; | ||
} | ||
// Function selector: 0x25d46b0f keccak256('mint_private(uint256,bytes32,address)') | ||
hash_bytes[0] = 0x25; | ||
hash_bytes[1] = 0xd4; | ||
hash_bytes[2] = 0x6b; | ||
hash_bytes[3] = 0x0f; | ||
|
||
// Abuse that a % p + b % p = (a + b) % p and that low < p | ||
let content_hash = low + high * v; | ||
let content_hash = sha256_to_field(hash_bytes); | ||
content_hash | ||
} | ||
|
||
|
@@ -107,20 +74,6 @@ fn get_withdraw_content_hash(recipient: Field, amount: Field, callerOnL1: Field) | |
hash_bytes[i + 36] = recipient_bytes[i]; | ||
hash_bytes[i + 68] = callerOnL1_bytes[i]; | ||
} | ||
let content_sha256 = sha256(hash_bytes); | ||
|
||
// Convert the content_sha256 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 + (content_sha256[15 - i] as Field) * v; | ||
low = low + (content_sha256[16 + 15 - i] as Field) * v; | ||
v = v * 256; | ||
} | ||
|
||
// Abuse that a % p + b % p = (a + b) % p and that low < p | ||
let content = low + high * v; | ||
content | ||
let content_hash = sha256_to_field(hash_bytes); | ||
content_hash | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you are saying same, I'm understanding it as the sha256 and then truncated, but that is not the case, the secretHash is a different hash function.