Skip to content

Commit

Permalink
Update ec pairing to ouput directly in the register and fix the test
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienFT committed Nov 19, 2024
1 parent 4cf15da commit c8bc192
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 56 deletions.
25 changes: 7 additions & 18 deletions fuel-vm/src/interpreter/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use fuel_crypto::{
use fuel_types::{
Bytes32,
Bytes64,
RegisterId,
Word,
};

Expand Down Expand Up @@ -144,21 +145,14 @@ where

pub(crate) fn ec_pairing(
&mut self,
a: Word,
ra: RegisterId,
b: Word,
c: Word,
d: Word,
) -> SimpleResult<()> {
let owner = self.ownership_registers();
ec_pairing(
self.memory.as_mut(),
owner,
self.registers.pc_mut(),
a,
b,
c,
d,
)
let (SystemRegisters { pc, .. }, mut w) = split_registers(&mut self.registers);
let dest = &mut w[ra.try_into()?];
ec_pairing(self.memory.as_mut(), pc, dest, b, c, d)
}
}

Expand Down Expand Up @@ -432,9 +426,8 @@ pub(crate) fn ec_mul(

pub(crate) fn ec_pairing(
memory: &mut MemoryInstance,
owner: OwnershipRegisters,
pc: RegMut<PC>,
success: Word,
success: &mut u64,
curve_id: Word,
num_elements: Word,
elements_ptr: Word,
Expand Down Expand Up @@ -471,11 +464,7 @@ pub(crate) fn ec_pairing(
)?;
elements.push((a, b));
}
let mut output = [0u8; 32];
if bn::pairing_batch(&elements) == Gt::one() {
output[31] = 1;
}
memory.write_bytes(owner, success, output)?;
*success = (bn::pairing_batch(&elements) == Gt::one()) as u64;
}
_ => {
return Err(crate::error::PanicOrBug::Panic(
Expand Down
45 changes: 10 additions & 35 deletions fuel-vm/src/interpreter/crypto/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,7 @@ fn test_emul_error() -> SimpleResult<()> {
090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b\
12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa",
).unwrap(),
hex::decode(
"0000000000000000000000000000000000000000000000000000000000000001"
).unwrap()
1u64
)]
// From https://github.com/ethereum/tests/blob/develop/GeneralStateTests/stZeroKnowledge/ecpairing_three_point_match_1.json
#[case(
Expand All @@ -544,9 +542,7 @@ fn test_emul_error() -> SimpleResult<()> {
2f997f3dbd66a7afe07fe7862ce239edba9e05c5afff7f8a1259c9733b2dfbb9\
29d1691530ca701b4a106054688728c9972c8512e9789e9567aae23e302ccd75"
).unwrap(),
hex::decode(
"0000000000000000000000000000000000000000000000000000000000000001"
).unwrap()
1u64
)]
// From https://github.com/ethereum/tests/blob/develop/GeneralStateTests/stZeroKnowledge/ecpairing_three_point_fail_1.json
#[case(
Expand All @@ -565,9 +561,7 @@ fn test_emul_error() -> SimpleResult<()> {
00cacf3523caf879d7d05e30549f1e6fdce364cbb8724b0329c6c2a39d4f018e\
0692e55db067300e6e3fe56218fa2f940054e57e7ef92bf7d475a9d8a8502fd2"
).unwrap(),
hex::decode(
"0000000000000000000000000000000000000000000000000000000000000000"
).unwrap()
0u64
)]
// From https://github.com/poanetwork/parity-ethereum/blob/2ea4265b0083c4148571b21e1079c641d5f31dc2/ethcore/benches/builtin.rs#L686
#[case(
Expand Down Expand Up @@ -634,43 +628,31 @@ fn test_emul_error() -> SimpleResult<()> {
275dc4a288d1afb3cbb1ac09187524c7db36395df7be3b99e673b13a075a65ec\
1d9befcd05a5323e6da4d435f3b617cdb3af83285c2df711ef39c01571827f9d"
).unwrap(),
hex::decode(
"0000000000000000000000000000000000000000000000000000000000000001"
).unwrap()
1u64
)]
fn test_epar(#[case] input: Vec<u8>, #[case] expected: Vec<u8>) -> SimpleResult<()> {
fn test_epar(#[case] input: Vec<u8>, #[case] expected: u64) -> SimpleResult<()> {
// Given
let mut memory: MemoryInstance = vec![1u8; MEM_SIZE].try_into().unwrap();
let owner = OwnershipRegisters {
sp: 1000,
ssp: 1000,
hp: 2000,
prev_hp: VM_MAX_RAM - 1,
};
let mut pc = 4;
let points_address = 0;
let result = 2100u64;
let mut result = 0;

// P1(x,y),G2(p1(x,y), p2(x,y))
memory[points_address..points_address + input.len()].copy_from_slice(&input);

// When
ec_pairing(
&mut memory,
owner,
RegMut::new(&mut pc),
result as Word,
&mut result,
0,
2,
points_address as Word,
)?;

// Then
assert_eq!(pc, 8);
assert_eq!(
&memory[result as usize..result.checked_add(32).unwrap() as usize],
&expected
);
assert_eq!(result, expected);
Ok(())
}

Expand All @@ -688,24 +670,17 @@ fn test_epar_error() -> SimpleResult<()> {
)
.unwrap();
let mut memory: MemoryInstance = vec![1u8; MEM_SIZE].try_into().unwrap();
let owner = OwnershipRegisters {
sp: 1000,
ssp: 1000,
hp: 2000,
prev_hp: VM_MAX_RAM - 1,
};
let mut pc = 4;
let points_address = 0;
let result = 2100u64;
let mut result = 0;
// P1(x,y),G2(p1(x,y), p2(x,y))
memory[points_address..points_address + 192].copy_from_slice(&input);

// When
let err = ec_pairing(
&mut memory,
owner,
RegMut::new(&mut pc),
result as Word,
&mut result,
0,
2,
points_address as Word,
Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/executors/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ where
self.gas_costs().epar().map_err(PanicReason::from)?,
len,
)?;
self.ec_pairing(r!(a), r!(b), len, r!(d))?;
self.ec_pairing(a.into(), r!(b), len, r!(d))?;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ fn cant_write_to_reserved_registers(raw_random_instruction: u32) -> TestResult {
// Some opcodes parse the immediate value as a part of the instruction itself,
// and thus fail before the destination register writability check occurs.
Err(Some(PanicReason::InvalidImmediateValue)) => return TestResult::discard(),
// Epar opcode parse the memory read and throw error if incorrect memory
// before changing the register
Err(Some(PanicReason::InvalidEllipticCurvePoint))
if opcode == Opcode::EPAR =>
{
return TestResult::discard();
}
_ => {
return TestResult::error(format!(
"expected ReservedRegisterNotWritable error {:?}",
Expand Down Expand Up @@ -244,8 +251,8 @@ fn writes_to_ra(opcode: Opcode) -> bool {
Opcode::ECAL => true,
Opcode::BSIZ => true,
Opcode::BLDD => false,
Opcode::EADD => true,
Opcode::EMUL => true,
Opcode::EADD => false,
Opcode::EMUL => false,
Opcode::EPAR => true,
}
}
Expand Down

0 comments on commit c8bc192

Please sign in to comment.