Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow negative exponents for GroupElement.exp #756

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ergotree-interpreter/src/eval/exponentiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Evaluable for Exponentiate {
mod tests {
use super::*;
use crate::eval::context::Context;
use crate::eval::tests::{eval_out, try_eval_out};
use crate::eval::tests::eval_out;
use crate::sigma_protocol::private_input::DlogProverInput;

use ergo_chain_types::EcPoint;
Expand Down Expand Up @@ -79,13 +79,19 @@ mod tests {
fn eval_exponent_negative() {
let left = force_any_val::<EcPoint>();
let right = BigInt256::from_str_radix("-1", 10).unwrap();

let expected_exp = ergo_chain_types::ec_point::exponentiate(
&left,
&dlog_group::bigint256_to_scalar(right.clone()).unwrap(),
);

let expr: Expr = Exponentiate {
left: Box::new(Expr::Const(left.into())),
right: Box::new(Expr::Const(right.into())),
}
.into();

let ctx = force_any_val::<Context>();
assert!(try_eval_out::<EcPoint>(&expr, &ctx).is_err());
assert_eq!(eval_out::<EcPoint>(&expr, &ctx), expected_exp);
}
}
6 changes: 4 additions & 2 deletions ergotree-ir/src/sigma_protocol/dlog_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ fn biguint_to_bytes(x: &BigUint) -> [u8; 32] {
}

/// Attempts to create Scalar from BigInt256
/// Returns None if not in the range [0, modulus).
pub fn bigint256_to_scalar(bi: BigInt256) -> Option<Scalar> {
// To convert BigInt bi to Scalar calculate (bi mod order)
let order = order();
let mut bi = &**bi % &order;
if Sign::Minus == bi.sign() {
return None;
bi += order;
}
#[allow(clippy::unwrap_used)] // since it's 256-bit BigInt it should always fit into BigUint
let bu = bi.to_biguint().unwrap();
Expand Down
Loading