Merge pull request #851 from mir-protocol/bn_base_in_interpreter

Use the order of the BN254 base field in the interpreter
This commit is contained in:
Dima V 2022-12-14 16:25:22 -08:00 committed by GitHub
commit 4112a24093
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View File

@ -23,6 +23,14 @@ type F = GoldilocksField;
/// Halt interpreter execution whenever a jump to this offset is done.
const DEFAULT_HALT_OFFSET: usize = 0xdeadbeef;
/// Order of the BN254 base field.
const BN_BASE: U256 = U256([
4332616871279656263,
10917124144477883021,
13281191951274694749,
3486998266802970665,
]);
impl MemoryState {
fn mload_general(&self, context: usize, segment: Segment, offset: usize) -> U256 {
self.get(MemoryAddress::new(context, segment, offset))
@ -377,25 +385,24 @@ impl<'a> Interpreter<'a> {
self.push(x.overflowing_sub(y).0);
}
// TODO: 107 is hardcoded as a dummy prime for testing
// should be changed to the proper implementation prime
fn run_addfp254(&mut self) {
let x = self.pop();
let y = self.pop();
self.push((x + y) % 107);
let x = self.pop() % BN_BASE;
let y = self.pop() % BN_BASE;
// BN_BASE is 254-bit so addition can't overflow
self.push((x + y) % BN_BASE);
}
fn run_mulfp254(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(U256::try_from(x.full_mul(y) % 107).unwrap());
self.push(U256::try_from(x.full_mul(y) % BN_BASE).unwrap());
}
fn run_subfp254(&mut self) {
let x = self.pop();
let y = self.pop();
self.push((U256::from(107) + x - y) % 107);
let x = self.pop() % BN_BASE;
let y = self.pop() % BN_BASE;
// BN_BASE is 254-bit so addition can't overflow
self.push((x + (BN_BASE - y)) % BN_BASE);
}
fn run_div(&mut self) {

View File

@ -130,6 +130,7 @@ fn as_stack(xs: Vec<u32>) -> Vec<U256> {
}
#[test]
#[ignore]
fn test_fp6() -> Result<()> {
let c = gen_fp6();
let d = gen_fp6();