Skip to content
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(avm-simulator): keccakf1600 opcode #7545

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions avm-transpiler/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ pub enum AvmOpcode {
MSM,
// Conversions
TORADIXLE,
// Other
SHA256COMPRESSION,
KECCAKF1600,
}

impl AvmOpcode {
Expand Down Expand Up @@ -169,6 +172,9 @@ impl AvmOpcode {
AvmOpcode::MSM => "MSM",
// Conversions
AvmOpcode::TORADIXLE => "TORADIXLE",
// Other
AvmOpcode::SHA256COMPRESSION => "SHA256COMPRESSION",
AvmOpcode::KECCAKF1600 => "KECCAKF1600",
}
}
}
17 changes: 17 additions & 0 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,23 @@ fn handle_black_box_function(avm_instrs: &mut Vec<AvmInstruction>, operation: &B
..Default::default()
});
}
BlackBoxOp::Keccakf1600 { message, output } => {
let message_offset = message.pointer.0;
let message_size_offset = message.size.0;
let dest_offset = output.pointer.0;
assert_eq!(output.size, 25, "Keccakf1600 output size must be 25!");

avm_instrs.push(AvmInstruction {
opcode: AvmOpcode::KECCAKF1600,
indirect: Some(ZEROTH_OPERAND_INDIRECT | FIRST_OPERAND_INDIRECT),
operands: vec![
AvmOperand::U32 { value: dest_offset as u32 },
AvmOperand::U32 { value: message_offset as u32 },
AvmOperand::U32 { value: message_size_offset as u32 },
],
..Default::default()
});
}
BlackBoxOp::ToRadix { input, radix, output } => {
let num_limbs = output.size as u32;
let input_offset = input.0 as u32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ contract AvmTest {
std::hash::keccak256(data, data.len() as u32)
}

#[aztec(public)]
fn keccak_f1600(data: [u64; 25]) -> [u64; 25] {
std::hash::keccak::keccakf1600(data)
}

#[aztec(public)]
fn poseidon2_hash(data: [Field; 10]) -> Field {
std::hash::poseidon2::Poseidon2::hash(data, data.len())
Expand Down
99 changes: 99 additions & 0 deletions yarn-project/foundation/src/crypto/keccak/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { keccakf1600 } from './index.js';

describe('keccakf1600', () => {
it('zero test vector should match', () => {
const input = [...Array(25)].map(() => 0n);

const out = keccakf1600(input);
const asStrings = out?.map(x => x.toString(16).padStart(16, '0'));

expect(asStrings).toEqual([
'f1258f7940e1dde7',
'84d5ccf933c0478a',
'd598261ea65aa9ee',
'bd1547306f80494d',
'8b284e056253d057',
'ff97a42d7f8e6fd4',
'90fee5a0a44647c4',
'8c5bda0cd6192e76',
'ad30a6f71b19059c',
'30935ab7d08ffc64',
'eb5aa93f2317d635',
'a9a6e6260d712103',
'81a57c16dbcf555f',
'43b831cd0347c826',
'01f22f1a11a5569f',
'05e5635a21d9ae61',
'64befef28cc970f2',
'613670957bc46611',
'b87c5a554fd00ecb',
'8c3ee88a1ccf32c8',
'940c7922ae3a2614',
'1841f924a2c509e4',
'16f53526e70465c2',
'75f644e97f30a13b',
'eaf1ff7b5ceca249',
]);
});

it('test vector should match', () => {
const input = [
'f1258f7940e1dde7',
'84d5ccf933c0478a',
'd598261ea65aa9ee',
'bd1547306f80494d',
'8b284e056253d057',
'ff97a42d7f8e6fd4',
'90fee5a0a44647c4',
'8c5bda0cd6192e76',
'ad30a6f71b19059c',
'30935ab7d08ffc64',
'eb5aa93f2317d635',
'a9a6e6260d712103',
'81a57c16dbcf555f',
'43b831cd0347c826',
'01f22f1a11a5569f',
'05e5635a21d9ae61',
'64befef28cc970f2',
'613670957bc46611',
'b87c5a554fd00ecb',
'8c3ee88a1ccf32c8',
'940c7922ae3a2614',
'1841f924a2c509e4',
'16f53526e70465c2',
'75f644e97f30a13b',
'eaf1ff7b5ceca249',
].map(x => BigInt(`0x${x}`));

const out = keccakf1600(input);
const asStrings = out?.map(x => x.toString(16).padStart(16, '0'));

expect(asStrings).toEqual([
'2d5c954df96ecb3c',
'6a332cd07057b56d',
'093d8d1270d76b6c',
'8a20d9b25569d094',
'4f9c4f99e5e7f156',
'f957b9a2da65fb38',
'85773dae1275af0d',
'faf4f247c3d810f7',
'1f1b9ee6f79a8759',
'e4fecc0fee98b425',
'68ce61b6b9ce68a1',
'deea66c4ba8f974f',
'33c43d836eafb1f5',
'e00654042719dbd9',
'7cf8a9f009831265',
'fd5449a6bf174743',
'97ddad33d8994b40',
'48ead5fc5d0be774',
'e3b8c8ee55b7b03c',
'91a0226e649e42e9',
'900e3129e7badd7b',
'202a9ec5faa3cce8',
'5b3402464e1c3db6',
'609f4e62a44c1059',
'20d06cd26a8fbf5c',
]);
});
});
Loading
Loading