Skip to content
Open
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
2 changes: 0 additions & 2 deletions avm-transpiler/src/procedures/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,8 @@ fn compile_opcode(
Mnemonic::ECADD => {
collector.memory_address_operand()?; // p1 x
collector.memory_address_operand()?; // p1 y
collector.memory_address_operand()?; // p1 is_infinite
collector.memory_address_operand()?; // p2 x
collector.memory_address_operand()?; // p2 y
collector.memory_address_operand()?; // p2 is_infinite
collector.memory_address_operand()?; // result
let collection = collector.finish()?;
result.add_instruction(
Expand Down
19 changes: 7 additions & 12 deletions avm-transpiler/src/procedures/msm.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
pub(crate) const MSM_ASSEMBLY: &str = "
; We are passed three pointers and one usize.
; d0 points to the points. Points are represented by (x: Field, y: Field, is_infinite: bool)
; d0 points to the points. Points are represented by (x: Field, y: Field).
; d1 points to the scalars. Scalars are represented by (lo: Field, hi: Field) both range checked to 128 bits.
; d2 contains the number of points.
; d3 points to the result. The result is a point.
ADD d3, /*the reserved register 'one_usize'*/ $2, d4; Compute the pointer to the result y.
ADD d4, $2, d5; Compute the pointer to the result is_infinite
; Initialize the msm result: point at infinity
SET i3, 0 ff
SET i4, 0 ff
SET i5, 1 u1
; Loop globals
SET d6, 0 u32; Initialize the outer loop variable, ranging from 0 to the number of points
SET d8, 0 ff; Initialize a 0 FF
Expand Down Expand Up @@ -51,35 +49,32 @@ FIND_MSB_BODY: JUMPI i19, FIND_MSB_END; Check if the current bit is one
JUMP FIND_MSB_BODY
; Now we have the pointer of the MSB in d19

; Now store the result of the scalar multiplication in d22, d23, d24
; Now store the result of the scalar multiplication in d22, d23
FIND_MSB_END: MOV i16, d22; x
ADD d16, $2, d25; pointer to y
MOV i25, d23; y
ADD d25, $2, d25; pointer to is_infinite
MOV i25, d24; is_infinite
; Also store the original point in d25, d26, d27
; Also store the original point in d25, d26
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: to avoid a large diff/confusion, rather than shift all the addresses when removing inf I just left gaps (e.g. d24). Happy to remove these gaps though!

MOV d22, d25; x
MOV d23, d26; y
MOV d24, d27; is_infinite

; Now we need to do the inner loop, that will do double then add
; We need to iterate from the pointer of the MSB + 1 to the end pointer (d21)
ADD d19, $2, d19; We start from the pointer of the MSB + 1
INNER_HEAD: LT d19, d21, d28; Check if we are done with the loop
JUMPI d28, INNER_BODY
JUMP INNER_END
INNER_BODY: ECADD d22, d23, d24, d22, d23, d24, /*not indirect, so the result is stored in d22, d23, d24*/ d22; Double the current result.
INNER_BODY: ECADD d22, d23, d22, d23, /*not indirect, so the result is stored in d22, d23*/ d22; Double the current result.
EQ i19, d12, d28; Check if the current bit is zero
JUMPI d28, INNER_INC; If the current bit is zero, continue
ECADD d25, d26, d27, d22, d23, d24, /*not indirect, so the result is stored in d22, d23, d24*/ d22; Add the original point to the result
ECADD d25, d26, d22, d23, /*not indirect, so the result is stored in d22, d23*/ d22; Add the original point to the result
INNER_INC: ADD d19, $2, d19; Increment the pointer
JUMP INNER_HEAD

; After the inner loop we have computed the scalar multiplication. Add it to the msm result
INNER_END: ECADD i3, i4, i5, d22, d23, d24, i3; Add the result to the msm result
INNER_END: ECADD i3, i4, d22, d23, i3; Add the result to the msm result
OUTER_INC: ADD d6, $2, d6; Increment the outer loop variable
JUMP OUTER_HEAD
; After the outer loop we have computed the msm. We can return since we wrote the result in i3, i4, i5
; After the outer loop we have computed the msm. We can return since we wrote the result in i3, i4
OUTER_END: INTERNALRETURN
";

Expand Down
10 changes: 3 additions & 7 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,32 +1280,28 @@ fn handle_black_box_function(
BlackBoxOp::EmbeddedCurveAdd {
input1_x: p1_x_offset,
input1_y: p1_y_offset,
input1_infinite: p1_infinite_offset,
input1_infinite: _,
input2_x: p2_x_offset,
input2_y: p2_y_offset,
input2_infinite: p2_infinite_offset,
input2_infinite: _,
result,
} => avm_instrs.push(AvmInstruction {
opcode: AvmOpcode::ECADD,
// The result (SIXTH operand) is indirect (addressing mode).
// The result (FOURTH operand) is indirect (addressing mode).
addressing_mode: Some(
AddressingModeBuilder::default()
.direct_operand(p1_x_offset)
.direct_operand(p1_y_offset)
.direct_operand(p1_infinite_offset)
.direct_operand(p2_x_offset)
.direct_operand(p2_y_offset)
.direct_operand(p2_infinite_offset)
.indirect_operand(&result.pointer)
.build(),
),
operands: vec![
AvmOperand::U16 { value: p1_x_offset.to_u32() as u16 },
AvmOperand::U16 { value: p1_y_offset.to_u32() as u16 },
AvmOperand::U16 { value: p1_infinite_offset.to_u32() as u16 },
AvmOperand::U16 { value: p2_x_offset.to_u32() as u16 },
AvmOperand::U16 { value: p2_y_offset.to_u32() as u16 },
AvmOperand::U16 { value: p2_infinite_offset.to_u32() as u16 },
AvmOperand::U16 { value: result.pointer.to_u32() as u16 },
],
..Default::default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
#define AVM_POSEIDON2_BASE_L2_GAS 360
#define AVM_SHA256COMPRESSION_BASE_L2_GAS 12288
#define AVM_KECCAKF1600_BASE_L2_GAS 58176
#define AVM_ECADD_BASE_L2_GAS 270
#define AVM_ECADD_BASE_L2_GAS 180
#define AVM_TORADIXBE_BASE_L2_GAS 24
#define AVM_CALLDATACOPY_DYN_L2_GAS 3
#define AVM_RETURNDATACOPY_DYN_L2_GAS 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AvmHardCodedVKAndHash {
using FF = bb::curve::BN254::ScalarField;

// Precomputed VK hash (hash of all commitments below).
static FF vk_hash() { return FF(uint256_t("0x0f0714f53e7fcf7ffb15cfb22b7a1614c65f01742706b0ca20eb80454eaf1e48")); }
static FF vk_hash() { return FF(uint256_t("0x00b6d67db723a570d7686fbcb5f3c4c39945378222f37e86fa9f511af4c036b5")); }

static constexpr std::array<Commitment, NUM_PRECOMPUTED_ENTITIES> get_all()
{
Expand Down Expand Up @@ -71,9 +71,9 @@ class AvmHardCodedVKAndHash {
uint256_t(
"0x090dda25e7d64ab5cabe09fd80fbb731af2a98de7a608157dc10394b4fc022a4")), // precomputed_exec_opcode_dynamic_l2_gas
Commitment(
uint256_t("0x26086b5fb31a24f236f0441d5b922b94ca141e861b9cc640184681c518cd68d3"),
uint256_t("0x1fbccee2ff656d845414c1a520adde56aa3625e29b6fff377044986493023e6d"),
uint256_t(
"0x0bab134bb4e25ff33584c1094847e762ce6573054bae27715d0e4eb2b7278d80")), // precomputed_exec_opcode_opcode_gas
"0x05c88802d3174f1c7b3c9aa1abf4754ebdaf6409d1aaf1dfa3f551da1c10fa93")), // precomputed_exec_opcode_opcode_gas
Commitment(
uint256_t("0x296def9415d1c96b4d8ab91df5f59ad8522a726f98461b1ab5c4d4c5b22471a4"),
uint256_t(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ pub global AVM_DEBUGLOG_BASE_L2_GAS: u32 = 9;
pub global AVM_POSEIDON2_BASE_L2_GAS: u32 = 24 * 15; // SLOW_SIM_MUL = 15
pub global AVM_SHA256COMPRESSION_BASE_L2_GAS: u32 = 12288;
pub global AVM_KECCAKF1600_BASE_L2_GAS: u32 = 58176;
pub global AVM_ECADD_BASE_L2_GAS: u32 = 27 * 10; // SLOW_SIM_MUL = 10
pub global AVM_ECADD_BASE_L2_GAS: u32 = 18 * 10; // SLOW_SIM_MUL = 10
pub global AVM_TORADIXBE_BASE_L2_GAS: u32 = 24;

// Dynamic L2 GAS
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/constants/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ export const AVM_DEBUGLOG_BASE_L2_GAS = 9;
export const AVM_POSEIDON2_BASE_L2_GAS = 360;
export const AVM_SHA256COMPRESSION_BASE_L2_GAS = 12288;
export const AVM_KECCAKF1600_BASE_L2_GAS = 58176;
export const AVM_ECADD_BASE_L2_GAS = 270;
export const AVM_ECADD_BASE_L2_GAS = 180;
export const AVM_TORADIXBE_BASE_L2_GAS = 24;
export const AVM_CALLDATACOPY_DYN_L2_GAS = 3;
export const AVM_RETURNDATACOPY_DYN_L2_GAS = 3;
Expand Down Expand Up @@ -497,6 +497,7 @@ export const GRUMPKIN_ONE_Y = 17631683881184975370165255887551781615748388533673
export const DEFAULT_MAX_DEBUG_LOG_MEMORY_READS = 125000;
export enum DomainSeparator {
NOTE_HASH = 116501019,
PARTIAL_NOTE_COMMITMENT = 568912195,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is just leftover from some previous changes not updating constants - unrelated to this work!

SILOED_NOTE_HASH = 3361878420,
UNIQUE_NOTE_HASH = 226850429,
NOTE_HASH_NONCE = 1721808740,
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/simulator/docs/avm/avm-isa-quick-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ Click on an opcode name to view its detailed documentation.
* **[🔗ECADD](opcodes/ecadd.md)**: Grumpkin elliptic curve addition
* Opcode `0x42`
```javascript
M[dstOffset:dstOffset+3] = grumpkinAdd(
/*point1=*/{x: M[p1XOffset], y: M[p1YOffset], isInfinite: M[p1IsInfiniteOffset]},
/*point2=*/{x: M[p2XOffset], y: M[p2YOffset], isInfinite: M[p2IsInfiniteOffset]}
M[dstOffset:dstOffset+1] = grumpkinAdd(
/*point1=*/{x: M[p1XOffset], y: M[p1YOffset]},
/*point2=*/{x: M[p2XOffset], y: M[p2YOffset]}
)
```
* **[🔗TORADIXBE](opcodes/toradixbe.md)**: Convert to radix (big-endian)
Expand Down
Loading
Loading