This commit is contained in:
Dmitry Vagner 2022-12-14 18:57:24 -08:00
commit 397864fb4e
2 changed files with 18 additions and 15 deletions

View File

@ -24,6 +24,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))
@ -381,30 +389,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 bn_base_order_() -> U256 {
U256::from_str("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
.unwrap()
}
fn run_addfp254(&mut self) {
let x = self.pop();
let y = self.pop();
self.push((x + y) % Self::bn_base_order_());
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) % Self::bn_base_order_()).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((Self::bn_base_order_() + x - y) % Self::bn_base_order_());
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

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