-
Notifications
You must be signed in to change notification settings - Fork 316
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
feat: constrain note encryption #6432
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a55e9c0
feat: full encryption and decryption of log in ts
LHerskind fbe2f7c
chore: address comments
LHerskind cc6bd87
feat: Update the encrypted note log format
LHerskind 99479dd
feat: constrain encryption
LHerskind 90602c2
feat: kill the app siloed viewing key
LHerskind bd19c82
test: Use +1 to avoid 0 issue that break noir scalar mul
LHerskind 0a6647d
update comments
LHerskind d74840e
add issue pointer
LHerskind 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod header; | ||
mod incoming_body; | ||
mod outgoing_body; | ||
mod payload; |
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
101 changes: 101 additions & 0 deletions
101
noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
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,101 @@ | ||
use dep::protocol_types::{ | ||
address::AztecAddress, grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint, | ||
constants::{GENERATOR_INDEX__IVSK_M, GENERATOR_INDEX__OVSK_M}, hash::poseidon2_hash | ||
}; | ||
|
||
use dep::std::{embedded_curve_ops::{embedded_curve_add, EmbeddedCurvePoint}, field::bytes32_to_field}; | ||
|
||
use crate::oracle::unsafe_rand::unsafe_rand; | ||
|
||
use crate::note::note_interface::NoteInterface; | ||
|
||
use crate::encrypted_logs::{ | ||
header::EncryptedLogHeader, incoming_body::EncryptedLogIncomingBody, | ||
outgoing_body::EncryptedLogOutgoingBody | ||
}; | ||
|
||
pub fn compute_encrypted_note_log<Note, N, NB, M>( | ||
contract_address: AztecAddress, | ||
storage_slot: Field, | ||
ovsk_app: Field, | ||
ovpk: GrumpkinPoint, | ||
ivpk: GrumpkinPoint, | ||
note: Note | ||
) -> [u8; M] where Note: NoteInterface<N, NB> { | ||
// @todo Need to draw randomness from the full domain of Fq not only Fr | ||
let eph_sk: GrumpkinPrivateKey = fr_to_private_key(unsafe_rand()); | ||
let eph_pk = eph_sk.derive_public_key(); | ||
|
||
// @todo This value needs to be populated! | ||
let recipient = AztecAddress::from_field(0); | ||
|
||
let ivpk_app = compute_ivpk_app(ivpk, contract_address); | ||
|
||
let header = EncryptedLogHeader::new(contract_address); | ||
|
||
let incoming_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ivpk); | ||
let outgoing_Header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ovpk); | ||
let incoming_body_ciphertext = EncryptedLogIncomingBody::from_note(note, storage_slot).compute_ciphertext(eph_sk, ivpk_app); | ||
let outgoing_body_ciphertext: [u8; 176] = EncryptedLogOutgoingBody::new(eph_sk, recipient, ivpk_app).compute_ciphertext(fr_to_private_key(ovsk_app), eph_pk); | ||
|
||
let mut encrypted_bytes: [u8; M] = [0; M]; | ||
// @todo We ignore the tags for now | ||
|
||
let eph_pk_bytes = eph_pk.to_be_bytes(); | ||
for i in 0..64 { | ||
encrypted_bytes[64 + i] = eph_pk_bytes[i]; | ||
} | ||
for i in 0..48 { | ||
encrypted_bytes[128 + i] = incoming_header_ciphertext[i]; | ||
encrypted_bytes[176 + i] = outgoing_Header_ciphertext[i]; | ||
} | ||
for i in 0..176 { | ||
encrypted_bytes[224 + i] = outgoing_body_ciphertext[i]; | ||
} | ||
// Then we fill in the rest as the incoming body ciphertext | ||
let size = M - 400; | ||
assert_eq(size, incoming_body_ciphertext.len(), "ciphertext length mismatch"); | ||
for i in 0..size { | ||
encrypted_bytes[400 + i] = incoming_body_ciphertext[i]; | ||
} | ||
|
||
encrypted_bytes | ||
} | ||
|
||
fn fr_to_private_key(r: Field) -> GrumpkinPrivateKey { | ||
let r_bytes = r.to_be_bytes(32); | ||
|
||
let mut high_bytes = [0; 32]; | ||
let mut low_bytes = [0; 32]; | ||
|
||
for i in 0..16 { | ||
high_bytes[16 + i] = r_bytes[i]; | ||
low_bytes[16 + i] = r_bytes[i + 16]; | ||
} | ||
|
||
let low = bytes32_to_field(low_bytes); | ||
let high = bytes32_to_field(high_bytes); | ||
|
||
GrumpkinPrivateKey::new(high, low) | ||
} | ||
|
||
fn compute_ivpk_app(ivpk: GrumpkinPoint, contract_address: AztecAddress) -> GrumpkinPoint { | ||
// It is useless to compute this, it brings no value to derive fully. | ||
// Issue(#6955) | ||
ivpk | ||
|
||
/* | ||
// @todo Just setting infinite to false, but it should be checked. | ||
// for example user could define ivpk = infinity using the registry | ||
assert((ivpk.x != 0) & (ivpk.y != 0), "ivpk is infinite"); | ||
|
||
let i = fr_to_private_key(poseidon2_hash([contract_address.to_field(), ivpk.x, ivpk.y, GENERATOR_INDEX__IVSK_M])); | ||
let I = i.derive_public_key(); | ||
|
||
let embed_I = EmbeddedCurvePoint { x: I.x, y: I.y, is_infinite: false }; | ||
let embed_ivpk = EmbeddedCurvePoint { x: ivpk.x, y: ivpk.y, is_infinite: false }; | ||
|
||
let embed_result = embedded_curve_add(embed_I, embed_ivpk); | ||
|
||
GrumpkinPoint::new(embed_result.x, embed_result.y)*/ | ||
} |
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
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 |
---|---|---|
|
@@ -22,11 +22,17 @@ export function computeAppSecretKey(skM: GrumpkinPrivateKey, app: AztecAddress, | |
} | ||
|
||
export function computeIvpkApp(ivpk: PublicKey, address: AztecAddress) { | ||
return ivpk; | ||
// Computing the siloed key is actually useless because we can derive the master key from it | ||
// Issue(#6955) | ||
const I = Fq.fromBuffer(poseidon2Hash([address.toField(), ivpk.x, ivpk.y, GeneratorIndex.IVSK_M]).toBuffer()); | ||
return curve.add(curve.mul(Grumpkin.generator, I), ivpk); | ||
} | ||
|
||
export function computeIvskApp(ivsk: GrumpkinPrivateKey, address: AztecAddress) { | ||
return ivsk; | ||
// Computing the siloed key is actually useless because we can derive the master key from it | ||
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. Can we link issue here? |
||
// Issue(#6955) | ||
const ivpk = curve.mul(Grumpkin.generator, ivsk); | ||
// Here we are intentionally converting Fr (output of poseidon) to Fq. This is fine even though a distribution of | ||
// P = s * G will not be uniform because 2 * (q - r) / q is small. | ||
|
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
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.
Can we link issue here?