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

Implement alt_bn128 multiplication #571

Merged
merged 1 commit into from
May 8, 2023
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
25 changes: 24 additions & 1 deletion src/dafny/core/precompiled.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,30 @@ module Precompiled {
const G_BNMUL := 6000

function CallBnMul(data: Array<u8>) : Option<(Array<u8>,nat)> {
Some((data, G_BNMUL))
// Axiom needed for this all to go through.
AltBn128.IsPrimeField();
// Point
var x0 := BNF(Bytes.ReadUint256(data,0) as nat);
var y0 := BNF(Bytes.ReadUint256(data,32) as nat);
// Factor
var n := Bytes.ReadUint256(data,64);
// Sanity check input values are prime fields for BN128
if x0 == None || y0 == None
then
None
else
var p0 := BNP(x0.Unwrap(),y0.Unwrap());
// Sanity check input point is on the BN128 curve
if p0 == None
then
None
else
var p := AltBn128.PointMul(p0.Unwrap(),n);
var p_x := p.0 as u256;
var p_y := p.1 as u256;
var bytes : Array<u8> := U256.ToBytes(p_x) + U256.ToBytes(p_y);
assert |bytes| == 64;
Some((bytes,G_BNMUL))
}

// ========================================================================
Expand Down
12 changes: 12 additions & 0 deletions src/dafny/crypto/alt_bn128.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ module EllipticCurve refines FiniteField {
else
INFINITY
}

// Multiply a point by a given factor on the curve.
function PointMul(p: Point, n: u256) : (r:Point)
requires N > 3 && IsPrime(N)
decreases n
{
if n == 0 then INFINITY
else
var res := PointMul(PointAdd(p,p),n / 2);
if n % 2 == 1 then PointAdd(res,p)
else res
}
}

// The ellptic curve given by y^2 == x^3 + 3.
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/dafnyevm/GeneralStateTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ public class GeneralStateTests {
"StaticcallToPrecompileFromTransaction_Berlin_0_0_0",
"precompsEIP2929_Berlin_0_(43|61|151|169|241|295)_0",
"idPrecomps_Berlin_0_[4-7]_0",
"ecmul_.*",
//"ecmul_.*",
"ecpairing.*",
"pairingTest.*",
"pointMulAdd.*",
//"pointMulAdd.*",
// #455
"MSTORE_Bounds2_Berlin_(0|1)_0_0",
"modexp_Berlin_[0123]_(2|28|29|30|36|37)_0", // int overflow
Expand Down