mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-07 08:13:11 +00:00
misc
This commit is contained in:
parent
19e2239d3a
commit
ea0e37480d
@ -128,50 +128,6 @@ impl Operation {
|
||||
}
|
||||
}
|
||||
|
||||
fn addmod(x: U256, y: U256, m: U256) -> U256 {
|
||||
if m.is_zero() {
|
||||
return m;
|
||||
}
|
||||
let x = to_biguint(x);
|
||||
let y = to_biguint(y);
|
||||
let m = to_biguint(m);
|
||||
from_biguint((x + y) % m)
|
||||
}
|
||||
|
||||
fn mulmod(x: U256, y: U256, m: U256) -> U256 {
|
||||
if m.is_zero() {
|
||||
return m;
|
||||
}
|
||||
let x = to_biguint(x);
|
||||
let y = to_biguint(y);
|
||||
let m = to_biguint(m);
|
||||
from_biguint(x * y % m)
|
||||
}
|
||||
|
||||
fn submod(x: U256, y: U256, m: U256) -> U256 {
|
||||
if m.is_zero() {
|
||||
return m;
|
||||
}
|
||||
let mut x = to_biguint(x);
|
||||
let y = to_biguint(y);
|
||||
let m = to_biguint(m);
|
||||
while x < y {
|
||||
x += &m;
|
||||
}
|
||||
from_biguint((x - y) % m)
|
||||
}
|
||||
|
||||
fn to_biguint(x: U256) -> BigUint {
|
||||
let mut bytes = [0u8; 32];
|
||||
x.to_little_endian(&mut bytes);
|
||||
BigUint::from_bytes_le(&bytes)
|
||||
}
|
||||
|
||||
fn from_biguint(x: BigUint) -> U256 {
|
||||
let bytes = x.to_bytes_le();
|
||||
U256::from_little_endian(&bytes)
|
||||
}
|
||||
|
||||
fn bn_base_order() -> U256 {
|
||||
U256::from_str("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47").unwrap()
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ use std::mem::{size_of, transmute_copy, ManuallyDrop};
|
||||
|
||||
use ethereum_types::{H160, H256, U256};
|
||||
use itertools::Itertools;
|
||||
use num::BigUint;
|
||||
use plonky2::field::extension::Extendable;
|
||||
use plonky2::field::packed::PackedField;
|
||||
use plonky2::field::polynomial::PolynomialValues;
|
||||
@ -98,3 +99,55 @@ pub(crate) unsafe fn transmute_no_compile_time_size_checks<T, U>(value: T) -> U
|
||||
// Copy the bit pattern. The original value is no longer safe to use.
|
||||
transmute_copy(&value)
|
||||
}
|
||||
|
||||
fn addmod(x: U256, y: U256, m: U256) -> U256 {
|
||||
if m.is_zero() {
|
||||
return m;
|
||||
}
|
||||
let x = u256_to_biguint(x);
|
||||
let y = u256_to_biguint(y);
|
||||
let m = u256_to_biguint(m);
|
||||
biguint_to_u256((x + y) % m)
|
||||
}
|
||||
|
||||
fn mulmod(x: U256, y: U256, m: U256) -> U256 {
|
||||
if m.is_zero() {
|
||||
return m;
|
||||
}
|
||||
let x = u256_to_biguint(x);
|
||||
let y = u256_to_biguint(y);
|
||||
let m = u256_to_biguint(m);
|
||||
biguint_to_u256(x * y % m)
|
||||
}
|
||||
|
||||
fn submod(x: U256, y: U256, m: U256) -> U256 {
|
||||
if m.is_zero() {
|
||||
return m;
|
||||
}
|
||||
let mut x = u256_to_biguint(x);
|
||||
let y = u256_to_biguint(y);
|
||||
let m = u256_to_biguint(m);
|
||||
while x < y {
|
||||
x += &m;
|
||||
}
|
||||
biguint_to_u256((x - y) % m)
|
||||
}
|
||||
|
||||
fn u256_to_biguint(x: U256) -> BigUint {
|
||||
let mut bytes = [0u8; 32];
|
||||
x.to_little_endian(&mut bytes);
|
||||
BigUint::from_bytes_le(&bytes)
|
||||
}
|
||||
|
||||
fn biguint_to_u256(x: BigUint) -> U256 {
|
||||
let bytes = x.to_bytes_le();
|
||||
U256::from_little_endian(&bytes)
|
||||
}
|
||||
|
||||
fn u256_saturating_cast_usize(x: U256) -> usize {
|
||||
if x > usize::MAX.into() {
|
||||
usize::MAX
|
||||
} else {
|
||||
x.as_usize()
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,6 +110,43 @@ pub(crate) fn generate_ternary_arithmetic_op<F: Field>(
|
||||
Ok(registers_state)
|
||||
}
|
||||
|
||||
pub(crate) fn generate_jump<F: Field>(
|
||||
mut registers_state: RegistersState,
|
||||
memory_state: &MemoryState,
|
||||
traces: &mut Traces<F>,
|
||||
mut row: CpuColumnsView<F>,
|
||||
) -> Result<RegistersState, ProgramError> {
|
||||
let [(dst, log_in0)] =
|
||||
stack_pop_with_log_and_fill::<1, _>(&mut registers_state, memory_state, traces, &mut row)?;
|
||||
|
||||
traces.push_memory(log_in0);
|
||||
traces.push_cpu(row);
|
||||
registers_state.program_counter = u256_saturating_cast_usize(dst);
|
||||
// TODO: Set other cols like input0_upper_sum_inv.
|
||||
Ok(registers_state)
|
||||
}
|
||||
|
||||
pub(crate) fn generate_jumpi<F: Field>(
|
||||
mut registers_state: RegistersState,
|
||||
memory_state: &MemoryState,
|
||||
traces: &mut Traces<F>,
|
||||
mut row: CpuColumnsView<F>,
|
||||
) -> Result<RegistersState, ProgramError> {
|
||||
let [(dst, log_in0), (cond, log_in1)] =
|
||||
stack_pop_with_log_and_fill::<2, _>(&mut registers_state, memory_state, traces, &mut row)?;
|
||||
|
||||
traces.push_memory(log_in0);
|
||||
traces.push_memory(log_in1);
|
||||
traces.push_cpu(row);
|
||||
registers_state.program_counter = if cond.is_zero() {
|
||||
registers_state.program_counter + 1
|
||||
} else {
|
||||
u256_saturating_cast_usize(dst)
|
||||
};
|
||||
// TODO: Set other cols like input0_upper_sum_inv.
|
||||
Ok(registers_state)
|
||||
}
|
||||
|
||||
pub(crate) fn generate_push<F: Field>(
|
||||
n: u8,
|
||||
mut registers_state: RegistersState,
|
||||
|
||||
@ -216,8 +216,8 @@ fn perform_op<F: Field>(
|
||||
Operation::KeccakGeneral => todo!(),
|
||||
Operation::ProverInput => todo!(),
|
||||
Operation::Pop => todo!(),
|
||||
Operation::Jump => todo!(),
|
||||
Operation::Jumpi => todo!(),
|
||||
Operation::Jump => generate_jump(registers_state, memory_state, traces, row)?,
|
||||
Operation::Jumpi => generate_jumpi(registers_state, memory_state, traces, row)?,
|
||||
Operation::Pc => todo!(),
|
||||
Operation::Gas => todo!(),
|
||||
Operation::Jumpdest => todo!(),
|
||||
@ -236,6 +236,7 @@ fn perform_op<F: Field>(
|
||||
new_registers_state.program_counter += match op {
|
||||
Operation::Syscall(_) | Operation::ExitKernel => 0,
|
||||
Operation::Push(n) => n as usize + 2,
|
||||
Operation::Jump | Operation::Jumpi => 0,
|
||||
_ => 1,
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user