Skip to content

Commit

Permalink
feat(precompile): add bn254 precompile
Browse files Browse the repository at this point in the history
Signed-off-by: 0xkanekiken <[email protected]>
  • Loading branch information
0xkanekiken committed Mar 14, 2024
1 parent 5c8b2e3 commit e664670
Show file tree
Hide file tree
Showing 18 changed files with 1,882 additions and 3 deletions.
16 changes: 16 additions & 0 deletions book/writing-programs/precompiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,20 @@ big-endian format. The second half of the input will be overwritten with the dec

```rust,noplayground
pub extern "C" fn syscall_secp256k1_decompress(point: &mut [u8; 64], is_odd: bool);
```

#### Bn254 Add

Adds two Bn254 points. The result is stored in the first point.

```rust,noplayground
pub extern "C" fn syscall_bn254_add(p: *mut u32, q: *mut u32)
```

#### Bn254 Double

Doubles a Bn254 point. The result is stored in the first point.

```rust,noplayground
pub extern "C" fn syscall_bn254_double(p: *mut u32)
```
18 changes: 17 additions & 1 deletion core/src/runtime/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::syscall::{
SyscallEnterUnconstrained, SyscallExitUnconstrained, SyscallHalt, SyscallLWA, SyscallWrite,
};
use crate::utils::ec::edwards::ed25519::{Ed25519, Ed25519Parameters};
use crate::utils::ec::weierstrass::secp256k1::Secp256k1;
use crate::utils::ec::weierstrass::{bn254::Bn254, secp256k1::Secp256k1};
use crate::{runtime::ExecutionRecord, runtime::MemoryReadRecord, runtime::MemoryWriteRecord};

/// A system call is invoked by the the `ecall` instruction with a specific value in register t0.
Expand Down Expand Up @@ -60,6 +60,12 @@ pub enum SyscallCode {
/// Executes the `BLAKE3_COMPRESS_INNER` precompile.
BLAKE3_COMPRESS_INNER = 112,

/// Executes the `BN254_ADD` precompile.
BN254_ADD = 117,

/// Executes the `BN254_MUL` precompile.
BN254_DOUBLE = 118,

WRITE = 999,
}

Expand All @@ -80,6 +86,8 @@ impl SyscallCode {
110 => SyscallCode::ENTER_UNCONSTRAINED,
111 => SyscallCode::EXIT_UNCONSTRAINED,
112 => SyscallCode::BLAKE3_COMPRESS_INNER,
117 => SyscallCode::BN254_ADD,
118 => SyscallCode::BN254_DOUBLE,
999 => SyscallCode::WRITE,
_ => panic!("invalid syscall number: {}", value),
}
Expand Down Expand Up @@ -213,6 +221,14 @@ pub fn default_syscall_map() -> HashMap<SyscallCode, Rc<dyn Syscall>> {
SyscallCode::SECP256K1_DECOMPRESS,
Rc::new(K256DecompressChip::new()),
);
syscall_map.insert(
SyscallCode::BN254_ADD,
Rc::new(WeierstrassAddAssignChip::<Bn254>::new()),
);
syscall_map.insert(
SyscallCode::BN254_DOUBLE,
Rc::new(WeierstrassDoubleAssignChip::<Bn254>::new()),
);
syscall_map.insert(
SyscallCode::BLAKE3_COMPRESS_INNER,
Rc::new(Blake3CompressInnerChip::new()),
Expand Down
9 changes: 8 additions & 1 deletion core/src/syscall/precompiles/weierstrass/weierstrass_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ where
mod tests {
use crate::{
runtime::Program,
utils::{run_test, setup_logger, tests::SECP256K1_ADD_ELF},
utils::{run_test, setup_logger, tests::BN254_ADD_ELF, tests::SECP256K1_ADD_ELF},
};

#[test]
Expand All @@ -356,4 +356,11 @@ mod tests {
let program = Program::from(SECP256K1_ADD_ELF);
run_test(program).unwrap();
}

#[test]
fn test_bn254_add_simple() {
setup_logger();
let program = Program::from(BN254_ADD_ELF);
run_test(program).unwrap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub mod tests {

use crate::{
runtime::Program,
utils::{run_test, setup_logger, tests::SECP256K1_DOUBLE_ELF},
utils::{run_test, setup_logger, tests::BN254_DOUBLE_ELF, tests::SECP256K1_DOUBLE_ELF},
};

#[test]
Expand All @@ -391,4 +391,11 @@ pub mod tests {
let program = Program::from(SECP256K1_DOUBLE_ELF);
run_test(program).unwrap();
}

#[test]
fn test_bn254_double_simple() {
setup_logger();
let program = Program::from(BN254_DOUBLE_ELF);
run_test(program).unwrap();
}
}
6 changes: 6 additions & 0 deletions core/src/utils/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,10 @@ pub mod tests {

pub const SHA2_ELF: &[u8] =
include_bytes!("../../../tests/sha2/elf/riscv32im-succinct-zkvm-elf");

pub const BN254_ADD_ELF: &[u8] =
include_bytes!("../../../tests/bn254-add/elf/riscv32im-succinct-zkvm-elf");

pub const BN254_DOUBLE_ELF: &[u8] =
include_bytes!("../../../tests/bn254-double/elf/riscv32im-succinct-zkvm-elf");
}
Loading

0 comments on commit e664670

Please sign in to comment.