Skip to content

Commit

Permalink
chore: use Noir implementation of pedersen that uses MSM instead of p…
Browse files Browse the repository at this point in the history
…edersen BBs
  • Loading branch information
dbanks12 committed Sep 25, 2024
1 parent b0d1bab commit a5e5935
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{hash::pedersen_hash, traits::ToField};
use crate::{hash::poseidon2_hash_with_separator, traits::ToField};

pub fn derive_storage_slot_in_map<K>(storage_slot: Field, key: K) -> Field where K: ToField {
pedersen_hash([storage_slot, key.to_field()], 0)
poseidon2_hash_with_separator([storage_slot, key.to_field()], 0)
}

mod test {
Expand Down
11 changes: 3 additions & 8 deletions noir/noir-repo/noir_stdlib/src/hash/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,11 @@ pub fn pedersen_commitment<let N: u32>(input: [Field; N]) -> EmbeddedCurvePoint
}

pub fn pedersen_hash_with_separator<let N: u32>(input: [Field; N], separator: u32) -> Field {
__pedersen_hash_with_separator(input, separator)
pedersen_hash_with_separator_noir(input, separator)
}

fn pedersen_commitment_with_separator<let N: u32>(input: [Field; N], separator: u32) -> EmbeddedCurvePoint {
let value = __pedersen_commitment_with_separator(input, separator);
if (value[0] == 0) & (value[1] == 0) {
EmbeddedCurvePoint { x: 0, y: 0, is_infinite: true }
} else {
EmbeddedCurvePoint { x: value[0], y: value[1], is_infinite: false }
}
pedersen_commitment_with_separator_noir(input, separator)
}

#[no_predicates]
Expand Down Expand Up @@ -78,7 +73,7 @@ fn pedersen_hash_with_separator_noir<let N: u32>(input: [Field; N], separator: u
pub fn pedersen_hash<let N: u32>(input: [Field; N]) -> Field
// docs:end:pedersen_hash
{
__pedersen_hash_with_separator(input, 0)
pedersen_hash_with_separator_noir(input, 0)
}

#[foreign(pedersen_hash)]
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/simulator/src/avm/opcodes/external_calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AvmSimulator } from '../avm_simulator.js';
import { RethrownError } from '../errors.js';
import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
import { Addressing } from './addressing_mode.js';
import { Instruction } from './instruction.js';
import { Instruction, NestedSimulatorCall } from './instruction.js';

abstract class ExternalCall extends Instruction {
// Informs (de)serialization. See Instruction.deserialize.
Expand Down Expand Up @@ -43,7 +43,8 @@ abstract class ExternalCall extends Instruction {
super();
}

public async execute(context: AvmContext) {

public async execute(context: AvmContext, executeNestedContext: NestedSimulatorCall) {
const memory = context.machineState.memory.track(this.type);
const [gasOffset, addrOffset, argsOffset, argsSizeOffset, retOffset, successOffset] = Addressing.fromWire(
this.indirect,
Expand Down Expand Up @@ -87,8 +88,7 @@ abstract class ExternalCall extends Instruction {
FunctionSelector.fromField(functionSelector),
);

const simulator = new AvmSimulator(nestedContext);
const nestedCallResults: AvmContractCallResult = await simulator.execute();
const nestedCallResults: AvmContractCallResult = await executeNestedContext(nestedContext);
const success = !nestedCallResults.reverted;

// TRANSITIONAL: We rethrow here so that the MESSAGE gets propagated.
Expand Down
5 changes: 4 additions & 1 deletion yarn-project/simulator/src/avm/opcodes/instruction.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { strict as assert } from 'assert';

import type { AvmContext } from '../avm_context.js';
import { type AvmContractCallResult } from '../avm_contract_call_result.js';
import { getBaseGasCost, getDynamicGasCost, mulGas, sumGas } from '../avm_gas.js';
import { type MemoryOperations } from '../avm_memory_types.js';
import { type BufferCursor } from '../serialization/buffer_cursor.js';
import { type Serializable } from '../serialization/bytecode_serialization.js';
import { Opcode, type OperandType, deserialize, serializeAs } from '../serialization/instruction_serialization.js';

export type NestedSimulatorCall = (context: AvmContext) => Promise<AvmContractCallResult>;

type InstructionConstructor = {
new (...args: any[]): Instruction;
};
Expand All @@ -21,7 +24,7 @@ export abstract class Instruction {
* This is the main entry point for the instruction.
* @param context - The AvmContext in which the instruction executes.
*/
public abstract execute(context: AvmContext): Promise<void>;
public abstract execute(context: AvmContext, executeNestedContext?: NestedSimulatorCall): Promise<void>;

/**
* Generate a string representation of the instruction including
Expand Down

0 comments on commit a5e5935

Please sign in to comment.